Blame view

src/ParamGetImpl/DDServerInterface/Pusher.hh 12.2 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Pusher.h
 *
 *  Created on: Dec 10, 2012
 *      Author: f.casimir
 */

#ifndef PUSHER_H_
#define PUSHER_H_

#include <vector>
#include "ParamData.hh"


#include "DD.hh"

namespace AMDA {
namespace Parameters {

namespace Base {
	/**
	 * @class Pusher
	 * @details This class is the operation responsible to transform raw data from DDServer into a ParamData
	 * of the corresponding type ( scalar or vector or Tab2D of short or int or float or double or ... type).
	 */
	struct Pusher {
193183ca   Benjamin Renard   Give the possibil...
27
		Pusher (int maxDim1Size=1,int maxDim2Size=1) : _maxDim1Size(maxDim1Size), _maxDim2Size(maxDim2Size), _paramData(NULL), _fillValue(NAN), _timeRestriction(NAN) {}
fbe3c2bb   Benjamin Renard   First commit
28
		virtual ~Pusher() {}
193183ca   Benjamin Renard   Give the possibil...
29
		virtual void put(DD_data_t *data, DD_data_t *timeData) = 0;
a6490f4d   Benjamin Renard   Do not throw an e...
30
		virtual void putNaN() = 0;
fbe3c2bb   Benjamin Renard   First commit
31
32
33
		void setFillValue(double pFillValue) {
			_fillValue = pFillValue;
		}
65c661e8   Benjamin Renard   Table definition ...
34
35
36
		double getFillValue() {
			return _fillValue;
		}
193183ca   Benjamin Renard   Give the possibil...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
		void setTimeRestriction(double timeRestriction) {
			_timeRestriction = timeRestriction;
		}
		double getTimeRestriction() {
			return _timeRestriction;
		}

		double getTimeByIndex(DD_data_t* data, int index) {
			if (index >= data->VarNumber) {
				BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::ex_msg("Try to access to a time outside of the available range"));
			}
			if (data->type == DD_CHAR) {
				return DD_Time2Double((char *) data->Variables[index]);
			}
			else if (data->type == DD_DOUBLE) {
				return *((double*)data->Variables[index]);
			}
			else {
				BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::ex_msg("Unknown time type"));
			}
			return 0.;
		}

0dfc4085   Benjamin Renard   Give the possibil...
60
61
		int _maxDim1Size; /*!< Used for vector and Tab2D */
		int _maxDim2Size; /*!< Used for Tab2D */
fbe3c2bb   Benjamin Renard   First commit
62
63
64
		ParamData* _paramData; /*!< return ParamData to a Process*/
	protected:
		float _fillValue; /*!< When this value is different of NAN, into the raw data, it is replaced by NAN into ParamData. */
193183ca   Benjamin Renard   Give the possibil...
65
		double _timeRestriction;
fbe3c2bb   Benjamin Renard   First commit
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	};
}

//#define DD_SHORT 4
//#define DD_INT     1
//#define DD_FLOAT   2
//#define DD_DOUBLE 3
//#define DD_CHAR   0

typedef enum {
	CT_UNKNOWN,
	CT_SCALAR,
	CT_TAB1D,
	CT_TAB2D
} ContainerType;

template <int type>
struct MapType { typedef void Type; };

template <> struct MapType<4> {	typedef short Type; };
template <> struct MapType<1> {	typedef int Type; };
template <> struct MapType<2> {	typedef float Type; };
template <> struct MapType<3> {	typedef double Type;  };
template <> struct MapType<0> {	typedef char Type; };



template<int type,ContainerType container = CT_SCALAR>
class Pusher;

/**
 * @brief Pusher implementation for the CT_TAB2D.
 */
template<int type>
class Pusher<type,CT_TAB2D> : public Base::Pusher {
public:
	typedef typename MapType<type>::Type BaseElemenType;
	typedef Tab2DData<BaseElemenType> ElemenType;
	typedef ParamDataSpec<ElemenType> SpecParamData;

	SpecParamData* _specParamData;

6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
108
	Pusher(int maxDim1Size, int maxDim2Size, int maxDim3Size, int dim1Num = 0, int dim2Num = 1, int dim3Num = -1, int dim3CutIndex = -1, int minSumIndex=-1, int maxSumIndex=-1) : Base::Pusher(maxDim1Size,maxDim2Size), _maxDim3Size(maxDim3Size), _dim1Num(dim1Num), _dim2Num(dim2Num), _dim3Num(dim3Num), _dim3CutIndex(dim3CutIndex),_minSumIndex(minSumIndex), _maxSumIndex(maxSumIndex) {
fbe3c2bb   Benjamin Renard   First commit
109
110
111
		_paramData = _specParamData = createParamData();
	}

193183ca   Benjamin Renard   Give the possibil...
112
	void put(DD_data_t *data, DD_data_t *timeData) {
fbe3c2bb   Benjamin Renard   First commit
113
114
115
116
		BaseElemenType **lData = reinterpret_cast<BaseElemenType **>(data->Variables);

		//ParamData is created, add data
		_specParamData->getDataList().resize(data->VarNumber);
0f321c54   Hacene SI HADJ MOHAND   us is now ok
117
118
                            
		
193183ca   Benjamin Renard   Give the possibil...
119
120
121
122
		if(_dim3CutIndex == -1 && _minSumIndex != -1 && _maxSumIndex != -1){
			for (int index = 0; index < data->VarNumber; index++) {
				double t = getTimeByIndex(timeData, index);
				_paramData->getTimeList().push_back(t);
0f321c54   Hacene SI HADJ MOHAND   us is now ok
123

193183ca   Benjamin Renard   Give the possibil...
124
125
126
127
				if ((getTimeRestriction() > 0) && !isnan(getTimeRestriction())) {
					if (t >= getTimeRestriction()) {
						putNaN();
						continue;
ff482c31   Benjamin Renard   Give the possibil...
128
					}
193183ca   Benjamin Renard   Give the possibil...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
				}

				ElemenType elem = ElemenType(_maxDim1Size,_maxDim2Size);
				elem << NotANumber();
				for (int dim1Index= 0; dim1Index < data->Dimensions[_dim1Num]; ++dim1Index)
				{
					for (int dim2Index= 0; dim2Index < data->Dimensions[_dim2Num]; ++dim2Index)
					{   
                                        	BaseElemenType cumulatedElement;
                                        	cumulatedElement << NotANumber() ;
						for(int sumIndex=_minSumIndex; sumIndex <= _maxSumIndex; ++sumIndex){
							int dataIndex = 0;
							if (_dim3Num >= 0 && _dim3Num < 3 && data->DimNumber == 3) {
								int curentIndex = (sumIndex >= 0 && sumIndex < data->Dimensions[_dim3Num]) ? sumIndex : 0;
								if (_dim3Num == 0) {
									dataIndex = curentIndex*data->Dimensions[_dim1Num]*data->Dimensions[_dim2Num] + dim1Index*data->Dimensions[_dim2Num] + dim2Index;
								}
								else if (_dim3Num == 1) {
									dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + curentIndex*data->Dimensions[_dim2Num] + dim2Index;
								}
								else {
									dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + dim2Index*data->Dimensions[_dim3Num] + curentIndex;
								}
							}
							else {
								dataIndex = dim1Index*data->Dimensions[_dim2Num]+dim2Index;
							}
							BaseElemenType  baseElem =  lData[index][dataIndex];
							if (!isnan(_fillValue))
							{
								double crt_val = baseElem;
								bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
								if(is_fill_value)
									baseElem << NotANumber();
							}
                                        		if (!isNAN(baseElem)) {
                                                		if (isNAN(cumulatedElement))
                                                    			cumulatedElement << ElemNull();
                                                		cumulatedElement += baseElem;
                                        		}
                                 		}
                                 		elem[dim1Index][dim2Index] = cumulatedElement;
fbe3c2bb   Benjamin Renard   First commit
171
					}
fbe3c2bb   Benjamin Renard   First commit
172
				}
193183ca   Benjamin Renard   Give the possibil...
173
				_specParamData->getDataList().push_back(elem);
fbe3c2bb   Benjamin Renard   First commit
174
			}
193183ca   Benjamin Renard   Give the possibil...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
		}else{
			for (int index = 0; index < data->VarNumber; index++) {
				double t = getTimeByIndex(timeData, index);
				_paramData->getTimeList().push_back(t);

				if ((getTimeRestriction() > 0) && !isnan(getTimeRestriction())) {
					if (getTimeByIndex(data, index) >= getTimeRestriction()) {
						putNaN();
						continue;
					}
				}

				ElemenType elem = ElemenType(_maxDim1Size,_maxDim2Size);
				elem << NotANumber();
				for (int dim1Index= 0; dim1Index < data->Dimensions[_dim1Num]; ++dim1Index)
0f321c54   Hacene SI HADJ MOHAND   us is now ok
190
				{
193183ca   Benjamin Renard   Give the possibil...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
					for (int dim2Index= 0; dim2Index < data->Dimensions[_dim2Num]; ++dim2Index)
					{
						int dataIndex = 0;
						if (_dim3Num >= 0 && _dim3Num < 3 && data->DimNumber == 3) {
							int cutIndex = (_dim3CutIndex >= 0 && _dim3CutIndex < data->Dimensions[_dim3Num]) ? _dim3CutIndex : 0;
							if (_dim3Num == 0) {
								dataIndex = cutIndex*data->Dimensions[_dim1Num]*data->Dimensions[_dim2Num] + dim1Index*data->Dimensions[_dim2Num] + dim2Index;
							}
							else if (_dim3Num == 1) {
								dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + cutIndex*data->Dimensions[_dim2Num] + dim2Index;
							}
							else {
								dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + dim2Index*data->Dimensions[_dim3Num] + cutIndex;
							}
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
205
206
						}
						else {
193183ca   Benjamin Renard   Give the possibil...
207
							dataIndex = dim1Index*data->Dimensions[_dim2Num]+dim2Index;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
208
						}
193183ca   Benjamin Renard   Give the possibil...
209
210
211
212
213
214
215
216
217
						BaseElemenType baseElem = lData[index][dataIndex];
						if (!isnan(_fillValue))
						{
							double crt_val = baseElem;
							bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
							if(is_fill_value)
								baseElem << NotANumber();
						}
						elem[dim1Index][dim2Index] = baseElem;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
218
					}
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
219
				}
193183ca   Benjamin Renard   Give the possibil...
220
				_specParamData->getDataList().push_back(elem);
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
221
			}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
222
		}
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
223
        }
fbe3c2bb   Benjamin Renard   First commit
224

a6490f4d   Benjamin Renard   Do not throw an e...
225
226
	void putNaN() {
		_specParamData->getDataList().resize(1);
0dfc4085   Benjamin Renard   Give the possibil...
227
228
229
		ElemenType nanElem = ElemenType(_maxDim1Size,_maxDim2Size);
		for(int i = 0; i < _maxDim1Size; ++i) {
			for(int j = 0; j < _maxDim2Size; ++j) {
09ab78e7   Benjamin Renard   Fix fill values i...
230
				nanElem[i][j] << NotANumber();
a6490f4d   Benjamin Renard   Do not throw an e...
231
232
233
234
235
			}
		}
		_specParamData->getDataList().push_back(nanElem);
	}

fbe3c2bb   Benjamin Renard   First commit
236
	SpecParamData* createParamData() {
0dfc4085   Benjamin Renard   Give the possibil...
237
		return new ParamDataSpec< Tab2DData<typename MapType<type>::Type> >(_maxDim1Size,_maxDim2Size);
fbe3c2bb   Benjamin Renard   First commit
238
239
	}

ff482c31   Benjamin Renard   Give the possibil...
240
241
private:

0dfc4085   Benjamin Renard   Give the possibil...
242
	int _maxDim3Size;
ff482c31   Benjamin Renard   Give the possibil...
243
244
245
246
247
248
249
250

	int _dim1Num;

	int _dim2Num;

	int _dim3Num;

	int _dim3CutIndex;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
251
252
253
254
        
                   int _minSumIndex;
    
                    int _maxSumIndex;
fbe3c2bb   Benjamin Renard   First commit
255
256
257
258
259
260
261
262
263
264
265
266
267
268
};

/**
 * @brief Pusher implementation for the CT_TAB1D.
 */
template<int type>
class Pusher<type,CT_TAB1D> : public Base::Pusher {
public:
	typedef typename MapType<type>::Type BaseElemenType;
	typedef std::vector<BaseElemenType> ElemenType;
	typedef ParamDataSpec<ElemenType> SpecParamData;

	SpecParamData* _specParamData;

0dfc4085   Benjamin Renard   Give the possibil...
269
	Pusher(int maxDimSize) : Base::Pusher(maxDimSize) {
fbe3c2bb   Benjamin Renard   First commit
270
271
272
		_paramData = _specParamData = createParamData();
	}

193183ca   Benjamin Renard   Give the possibil...
273
	void put(DD_data_t *data, DD_data_t *timeData) {
fbe3c2bb   Benjamin Renard   First commit
274
275
276
277
278
279
		BaseElemenType **lData = reinterpret_cast<BaseElemenType **>(data->Variables);

		//ParamData is created, add data
		_specParamData->getDataList().resize(data->VarNumber);

		for (int index = 0; index < data->VarNumber; index++) {
193183ca   Benjamin Renard   Give the possibil...
280
281
282
283
284
285
286
287
288
289
			double t = getTimeByIndex(timeData, index);
			_paramData->getTimeList().push_back(t);

			if ((getTimeRestriction() > 0) && !isnan(getTimeRestriction())) {
				if (t >= getTimeRestriction()) {
					putNaN();
					continue;
				}
			}

0dfc4085   Benjamin Renard   Give the possibil...
290
291
292
293
			ElemenType elem = ElemenType();
			elem.resize(_maxDim1Size);
			elem << NotANumber();
			std::copy (lData[index], lData[index] + data->Dimensions[0], elem.begin());
fbe3c2bb   Benjamin Renard   First commit
294
295
			if(!isnan(_fillValue)) {
				for(auto it = elem.begin(); it != elem.end(); ++it) {
71249c7c   Benjamin Renard   Fix fillValue det...
296
297
298
					double crt_val = *it;
					bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
					if(is_fill_value) {
09ab78e7   Benjamin Renard   Fix fill values i...
299
						*it << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
300
301
302
303
304
305
306
					}
				}
			}
			_specParamData->getDataList().push_back(elem);
		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
307
308
309
	void putNaN() {
		_specParamData->getDataList().resize(1);
		ElemenType nanElem = ElemenType();
0dfc4085   Benjamin Renard   Give the possibil...
310
311
		nanElem.resize(_maxDim1Size);
		nanElem << NotANumber();
a6490f4d   Benjamin Renard   Do not throw an e...
312
313
314
		_specParamData->getDataList().push_back(nanElem);
	}

fbe3c2bb   Benjamin Renard   First commit
315
	SpecParamData* createParamData() {
0dfc4085   Benjamin Renard   Give the possibil...
316
		return new ParamDataSpec< std::vector<typename MapType<type>::Type> >(_maxDim1Size);
fbe3c2bb   Benjamin Renard   First commit
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	}

};


/**
 * @brief Pusher implementation for the scalar.
 */
template<int type>
class Pusher<type,CT_SCALAR> : public Base::Pusher {
public:
	typedef typename MapType<type>::Type BaseElemenType;
	typedef BaseElemenType ElemenType;
	typedef ParamDataSpec<ElemenType> SpecParamData;

	SpecParamData* _specParamData;

	/**
	 * @brief Constructor
	 * @details Create the real ParamData.
	 */
	Pusher() : Base::Pusher() {
		_paramData = _specParamData = createParamData();
	}

193183ca   Benjamin Renard   Give the possibil...
342
	void put(DD_data_t *data, DD_data_t *timeData) {
fbe3c2bb   Benjamin Renard   First commit
343
344
345
346
347
		BaseElemenType **lData = reinterpret_cast<BaseElemenType **>(data->Variables);

		//ParamData is created, add data
		_specParamData->getDataList().resize(data->VarNumber);

09ab78e7   Benjamin Renard   Fix fill values i...
348
349
350
                BaseElemenType nanElem;
                nanElem << NotANumber();

fbe3c2bb   Benjamin Renard   First commit
351
		if(!isnan(_fillValue)) {
09ab78e7   Benjamin Renard   Fix fill values i...
352
353
                        BaseElemenType nanElem;
                        nanElem << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
354
			for (int index = 0; index < data->VarNumber; index++) {
193183ca   Benjamin Renard   Give the possibil...
355
356
357
358
359
360
361
362
363
364
				double t = getTimeByIndex(timeData, index);
				_paramData->getTimeList().push_back(t);

				if ((getTimeRestriction() > 0) && !isnan(getTimeRestriction())) {
					if (t >= getTimeRestriction()) {
						putNaN();
						continue;
					}
				}

71249c7c   Benjamin Renard   Fix fillValue det...
365
366
367
368
				double crt_val = *lData[index];
				bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
				if(!is_fill_value) {
					_specParamData->getDataList().push_back(*lData[index]);
fbe3c2bb   Benjamin Renard   First commit
369
				} else {
09ab78e7   Benjamin Renard   Fix fill values i...
370
					_specParamData->getDataList().push_back(nanElem);
fbe3c2bb   Benjamin Renard   First commit
371
372
373
374
375
				}
			}
		}
		else {
			for (int index = 0; index < data->VarNumber; index++) {
193183ca   Benjamin Renard   Give the possibil...
376
377
378
379
380
381
382
383
384
385
				double t = getTimeByIndex(timeData, index);
				_paramData->getTimeList().push_back(t);

				if ((getTimeRestriction() > 0) && !isnan(getTimeRestriction())) {
					if (t >= getTimeRestriction()) {
						putNaN();
						continue;
					}
				}

fbe3c2bb   Benjamin Renard   First commit
386
387
388
389
390
391
				_specParamData->getDataList().push_back(*lData[index]);
			}

		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
392
	void putNaN() {
09ab78e7   Benjamin Renard   Fix fill values i...
393
394
                BaseElemenType nanElem;
                nanElem << NotANumber();
a6490f4d   Benjamin Renard   Do not throw an e...
395
		_specParamData->getDataList().resize(1);
09ab78e7   Benjamin Renard   Fix fill values i...
396
		_specParamData->getDataList().push_back(nanElem);
a6490f4d   Benjamin Renard   Do not throw an e...
397
398
	}

fbe3c2bb   Benjamin Renard   First commit
399
400
401
402
403
404
405
406
	SpecParamData* createParamData() {
		return new SpecParamData();
	}
};

} /* namespace Parameters */
} /* namespace AMDA */
#endif /* PUSHER_H_ */