Blame view

src/ParamGetImpl/DDServerInterface/Pusher.hh 5.9 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
27
28
29
/*
 * 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 {
		Pusher (int dim1=1,int dim2=1) : _dim1(dim1), _dim2(dim2), _paramData(NULL), _fillValue(NAN) {}
		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;
		}
fbe3c2bb   Benjamin Renard   First commit
37
38
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
		int _dim1; /*!< Used for vector and Tab2D */
		int _dim2; /*!< Used for Tab2D */
		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;

	Pusher(int dim1, int dim2) : Base::Pusher(dim1,dim2) {
		_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++) {
			ElemenType elem = ElemenType(data->Dimensions[0],data->Dimensions[1]);
			for (int dim1Index= 0; dim1Index < data->Dimensions[0]; ++dim1Index)
			{
				for (int dim2Index= 0; dim2Index < data->Dimensions[1]; ++dim2Index)
				{
					BaseElemenType baseElem = lData[index][dim1Index*data->Dimensions[1]+dim2Index];
					if (!isnan(_fillValue))
					{
						if(baseElem == _fillValue)
							baseElem = (BaseElemenType)NAN;
					}
					elem[dim1Index][dim2Index] = baseElem;
				}
			}
			_specParamData->getDataList().push_back(elem);
		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
113
114
115
116
117
118
119
120
121
122
123
	void putNaN() {
		_specParamData->getDataList().resize(1);
		ElemenType nanElem = ElemenType(_dim1,_dim2);
		for(int i = 0; i < _dim1; ++i) {
			for(int j = 0; j < _dim2; ++j) {
				nanElem[i][j] = ((BaseElemenType)NAN);
			}
		}
		_specParamData->getDataList().push_back(nanElem);
	}

fbe3c2bb   Benjamin Renard   First commit
124
125
126
127
128
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
	SpecParamData* createParamData() {
		return new ParamDataSpec< Tab2DData<typename MapType<type>::Type> >(_dim1,_dim2);
	}

};

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

	Pusher(int dim) : Base::Pusher(dim) {
		_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++) {
			ElemenType elem = ElemenType(lData[index],lData[index]+_dim1);
			if(!isnan(_fillValue)) {
				for(auto it = elem.begin(); it != elem.end(); ++it) {
					if(*it == _fillValue) {
						*it = (BaseElemenType)NAN;
					}
				}
			}
			_specParamData->getDataList().push_back(elem);
		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
165
166
167
168
169
170
171
172
173
	void putNaN() {
		_specParamData->getDataList().resize(1);
		ElemenType nanElem = ElemenType();
		for(int i = 0; i < _dim1; ++i) {
			nanElem.push_back((BaseElemenType)NAN);
		}
		_specParamData->getDataList().push_back(nanElem);
	}

fbe3c2bb   Benjamin Renard   First commit
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
	SpecParamData* createParamData() {
		return new ParamDataSpec< std::vector<typename MapType<type>::Type> >(_dim1);
	}

};


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

		if(!isnan(_fillValue)) {
			for (int index = 0; index < data->VarNumber; index++) {
				if(*lData[index] != _fillValue) {
				_specParamData->getDataList().push_back(*lData[index]);
				} else {
					_specParamData->getDataList().push_back((BaseElemenType)NAN);
				}
			}
		}
		else {
			for (int index = 0; index < data->VarNumber; index++) {
				_specParamData->getDataList().push_back(*lData[index]);
			}

		}
	}

a6490f4d   Benjamin Renard   Do not throw an e...
224
225
226
227
228
	void putNaN() {
		_specParamData->getDataList().resize(1);
		_specParamData->getDataList().push_back((BaseElemenType)NAN);
	}

fbe3c2bb   Benjamin Renard   First commit
229
230
231
232
233
234
235
236
	SpecParamData* createParamData() {
		return new SpecParamData();
	}
};

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