Blame view

src/ParamGetImpl/DDServerInterface/Pusher.hh 10.4 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 {
0dfc4085   Benjamin Renard   Give the possibil...
27
		Pusher (int maxDim1Size=1,int maxDim2Size=1) : _maxDim1Size(maxDim1Size), _maxDim2Size(maxDim2Size), _paramData(NULL), _fillValue(NAN) {}
fbe3c2bb   Benjamin Renard   First commit
28
29
		virtual ~Pusher() {}
		virtual void put(DD_data_t *data) = 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;
		}
0dfc4085   Benjamin Renard   Give the possibil...
37
38
		int _maxDim1Size; /*!< Used for vector and Tab2D */
		int _maxDim2Size; /*!< Used for Tab2D */
fbe3c2bb   Benjamin Renard   First commit
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		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. */
	};
}

//#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
84
	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
85
86
87
88
89
90
91
92
		_paramData = _specParamData = createParamData();
	}

	void put(DD_data_t *data) {
		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
93
94
95
96
97
                            
		

                              if(_dim3CutIndex == -1 && _minSumIndex != -1 && _maxSumIndex != -1){
                                         for (int index = 0; index < data->VarNumber; index++) {
0dfc4085   Benjamin Renard   Give the possibil...
98
99
100
			ElemenType elem = ElemenType(_maxDim1Size,_maxDim2Size);
			elem << NotANumber();
			for (int dim1Index= 0; dim1Index < data->Dimensions[_dim1Num]; ++dim1Index)
fbe3c2bb   Benjamin Renard   First commit
101
			{
0dfc4085   Benjamin Renard   Give the possibil...
102
				for (int dim2Index= 0; dim2Index < data->Dimensions[_dim2Num]; ++dim2Index)
0f321c54   Hacene SI HADJ MOHAND   us is now ok
103
				{   
09ab78e7   Benjamin Renard   Fix fill values i...
104
105
                                        BaseElemenType cumulatedElement;
                                        cumulatedElement << NotANumber() ;
0f321c54   Hacene SI HADJ MOHAND   us is now ok
106
                                                                                         for(int sumIndex=_minSumIndex; sumIndex <= _maxSumIndex; ++sumIndex){
ff482c31   Benjamin Renard   Give the possibil...
107
108
					int dataIndex = 0;
					if (_dim3Num >= 0 && _dim3Num < 3 && data->DimNumber == 3) {
0f321c54   Hacene SI HADJ MOHAND   us is now ok
109
						int curentIndex = (sumIndex >= 0 && sumIndex < data->Dimensions[_dim3Num]) ? sumIndex : 0;
ff482c31   Benjamin Renard   Give the possibil...
110
						if (_dim3Num == 0) {
0f321c54   Hacene SI HADJ MOHAND   us is now ok
111
							dataIndex = curentIndex*data->Dimensions[_dim1Num]*data->Dimensions[_dim2Num] + dim1Index*data->Dimensions[_dim2Num] + dim2Index;
ff482c31   Benjamin Renard   Give the possibil...
112
113
						}
						else if (_dim3Num == 1) {
0f321c54   Hacene SI HADJ MOHAND   us is now ok
114
							dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + curentIndex*data->Dimensions[_dim2Num] + dim2Index;
ff482c31   Benjamin Renard   Give the possibil...
115
116
						}
						else {
9d4b0702   Benjamin Renard   Fix index selecti...
117
							dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + dim2Index*data->Dimensions[_dim3Num] + curentIndex;
ff482c31   Benjamin Renard   Give the possibil...
118
119
120
						}
					}
					else {
0dfc4085   Benjamin Renard   Give the possibil...
121
						dataIndex = dim1Index*data->Dimensions[_dim2Num]+dim2Index;
ff482c31   Benjamin Renard   Give the possibil...
122
					}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
123
					BaseElemenType  baseElem =  lData[index][dataIndex];
fbe3c2bb   Benjamin Renard   First commit
124
125
					if (!isnan(_fillValue))
					{
71249c7c   Benjamin Renard   Fix fillValue det...
126
127
128
						double crt_val = baseElem;
						bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
						if(is_fill_value)
09ab78e7   Benjamin Renard   Fix fill values i...
129
							baseElem << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
130
					}
09ab78e7   Benjamin Renard   Fix fill values i...
131
132
133
134
135
136
137
                                        if (!isNAN(baseElem)) {
                                                if (isNAN(cumulatedElement))
                                                    cumulatedElement << ElemNull();
                                                cumulatedElement += baseElem;
                                        }
                                 }
                                 elem[dim1Index][dim2Index] = cumulatedElement;
0f321c54   Hacene SI HADJ MOHAND   us is now ok
138
139
                                                                                                    }
					
fbe3c2bb   Benjamin Renard   First commit
140
				}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
141
                                                                 _specParamData->getDataList().push_back(elem);
fbe3c2bb   Benjamin Renard   First commit
142
			}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
143
144
145
146
147
                                  
			
		
                              }else{
                                  for (int index = 0; index < data->VarNumber; index++) {
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
148
149
150
151
152
			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)
0f321c54   Hacene SI HADJ MOHAND   us is now ok
153
				{
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
154
155
					int dataIndex = 0;
					if (_dim3Num >= 0 && _dim3Num < 3 && data->DimNumber == 3) {
0f321c54   Hacene SI HADJ MOHAND   us is now ok
156
						int cutIndex = (_dim3CutIndex >= 0 && _dim3CutIndex < data->Dimensions[_dim3Num]) ? _dim3CutIndex : 0;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
157
						if (_dim3Num == 0) {
0f321c54   Hacene SI HADJ MOHAND   us is now ok
158
							dataIndex = cutIndex*data->Dimensions[_dim1Num]*data->Dimensions[_dim2Num] + dim1Index*data->Dimensions[_dim2Num] + dim2Index;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
159
160
						}
						else if (_dim3Num == 1) {
0f321c54   Hacene SI HADJ MOHAND   us is now ok
161
							dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + cutIndex*data->Dimensions[_dim2Num] + dim2Index;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
162
163
						}
						else {
af68db78   Benjamin Renard   Fix index selecti...
164
							dataIndex = dim1Index*data->Dimensions[_dim2Num]*data->Dimensions[_dim3Num] + dim2Index*data->Dimensions[_dim3Num] + cutIndex;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
165
166
167
168
169
						}
					}
					else {
						dataIndex = dim1Index*data->Dimensions[_dim2Num]+dim2Index;
					}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
170
					BaseElemenType baseElem = lData[index][dataIndex];
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
171
172
173
174
175
					if (!isnan(_fillValue))
					{
						double crt_val = baseElem;
						bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
						if(is_fill_value)
09ab78e7   Benjamin Renard   Fix fill values i...
176
							baseElem << NotANumber();
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
177
					}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
178
					elem[dim1Index][dim2Index] = baseElem;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
179
				}
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
180
			}
0f321c54   Hacene SI HADJ MOHAND   us is now ok
181
182
			_specParamData->getDataList().push_back(elem);
		}
fbe3c2bb   Benjamin Renard   First commit
183
	}
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
184
        }
fbe3c2bb   Benjamin Renard   First commit
185

a6490f4d   Benjamin Renard   Do not throw an e...
186
187
	void putNaN() {
		_specParamData->getDataList().resize(1);
0dfc4085   Benjamin Renard   Give the possibil...
188
189
190
		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...
191
				nanElem[i][j] << NotANumber();
a6490f4d   Benjamin Renard   Do not throw an e...
192
193
194
195
196
			}
		}
		_specParamData->getDataList().push_back(nanElem);
	}

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

ff482c31   Benjamin Renard   Give the possibil...
201
202
private:

0dfc4085   Benjamin Renard   Give the possibil...
203
	int _maxDim3Size;
ff482c31   Benjamin Renard   Give the possibil...
204
205
206
207
208
209
210
211

	int _dim1Num;

	int _dim2Num;

	int _dim3Num;

	int _dim3CutIndex;
6edc9ff8   Hacene SI HADJ MOHAND   rm_6463
212
213
214
215
        
                   int _minSumIndex;
    
                    int _maxSumIndex;
fbe3c2bb   Benjamin Renard   First commit
216
217
218
219
220
221
222
223
224
225
226
227
228
229
};

/**
 * @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...
230
	Pusher(int maxDimSize) : Base::Pusher(maxDimSize) {
fbe3c2bb   Benjamin Renard   First commit
231
232
233
234
235
236
237
238
239
240
		_paramData = _specParamData = createParamData();
	}

	void put(DD_data_t *data) {
		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++) {
0dfc4085   Benjamin Renard   Give the possibil...
241
242
243
244
			ElemenType elem = ElemenType();
			elem.resize(_maxDim1Size);
			elem << NotANumber();
			std::copy (lData[index], lData[index] + data->Dimensions[0], elem.begin());
fbe3c2bb   Benjamin Renard   First commit
245
246
			if(!isnan(_fillValue)) {
				for(auto it = elem.begin(); it != elem.end(); ++it) {
71249c7c   Benjamin Renard   Fix fillValue det...
247
248
249
					double crt_val = *it;
					bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
					if(is_fill_value) {
09ab78e7   Benjamin Renard   Fix fill values i...
250
						*it << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
251
252
253
254
255
256
257
					}
				}
			}
			_specParamData->getDataList().push_back(elem);
		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
258
259
260
	void putNaN() {
		_specParamData->getDataList().resize(1);
		ElemenType nanElem = ElemenType();
0dfc4085   Benjamin Renard   Give the possibil...
261
262
		nanElem.resize(_maxDim1Size);
		nanElem << NotANumber();
a6490f4d   Benjamin Renard   Do not throw an e...
263
264
265
		_specParamData->getDataList().push_back(nanElem);
	}

fbe3c2bb   Benjamin Renard   First commit
266
	SpecParamData* createParamData() {
0dfc4085   Benjamin Renard   Give the possibil...
267
		return new ParamDataSpec< std::vector<typename MapType<type>::Type> >(_maxDim1Size);
fbe3c2bb   Benjamin Renard   First commit
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
	}

};


/**
 * @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();
	}

	void put(DD_data_t *data) {
		BaseElemenType **lData = reinterpret_cast<BaseElemenType **>(data->Variables);

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

09ab78e7   Benjamin Renard   Fix fill values i...
299
300
301
                BaseElemenType nanElem;
                nanElem << NotANumber();

fbe3c2bb   Benjamin Renard   First commit
302
		if(!isnan(_fillValue)) {
09ab78e7   Benjamin Renard   Fix fill values i...
303
304
                        BaseElemenType nanElem;
                        nanElem << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
305
			for (int index = 0; index < data->VarNumber; index++) {
71249c7c   Benjamin Renard   Fix fillValue det...
306
307
308
309
				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
310
				} else {
09ab78e7   Benjamin Renard   Fix fill values i...
311
					_specParamData->getDataList().push_back(nanElem);
fbe3c2bb   Benjamin Renard   First commit
312
313
314
315
316
317
318
319
320
321
322
				}
			}
		}
		else {
			for (int index = 0; index < data->VarNumber; index++) {
				_specParamData->getDataList().push_back(*lData[index]);
			}

		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
323
	void putNaN() {
09ab78e7   Benjamin Renard   Fix fill values i...
324
325
                BaseElemenType nanElem;
                nanElem << NotANumber();
a6490f4d   Benjamin Renard   Do not throw an e...
326
		_specParamData->getDataList().resize(1);
09ab78e7   Benjamin Renard   Fix fill values i...
327
		_specParamData->getDataList().push_back(nanElem);
a6490f4d   Benjamin Renard   Do not throw an e...
328
329
	}

fbe3c2bb   Benjamin Renard   First commit
330
331
332
333
334
335
336
337
	SpecParamData* createParamData() {
		return new SpecParamData();
	}
};

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