Pusher.hh 12.2 KB
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 30 31 32 33 34 35 36 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 113 114 115 116 117 118 119 120 121 122 123 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 165 166 167 168 169 170 171 172 173 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/*
 * 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 maxDim1Size=1,int maxDim2Size=1) : _maxDim1Size(maxDim1Size), _maxDim2Size(maxDim2Size), _paramData(NULL), _fillValue(NAN), _timeRestriction(NAN) {}
		virtual ~Pusher() {}
		virtual void put(DD_data_t *data, DD_data_t *timeData) = 0;
		virtual void putNaN() = 0;
		void setFillValue(double pFillValue) {
			_fillValue = pFillValue;
		}
		double getFillValue() {
			return _fillValue;
		}
		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.;
		}

		int _maxDim1Size; /*!< Used for vector and Tab2D */
		int _maxDim2Size; /*!< 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. */
		double _timeRestriction;
	};
}

//#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 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) {
		_paramData = _specParamData = createParamData();
	}

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

		//ParamData is created, add data
		_specParamData->getDataList().resize(data->VarNumber);
                            
		
		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);

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

				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;
					}
				}
				_specParamData->getDataList().push_back(elem);
			}
		}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)
				{
					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;
							}
						}
						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();
						}
						elem[dim1Index][dim2Index] = baseElem;
					}
				}
				_specParamData->getDataList().push_back(elem);
			}
		}
        }

	void putNaN() {
		_specParamData->getDataList().resize(1);
		ElemenType nanElem = ElemenType(_maxDim1Size,_maxDim2Size);
		for(int i = 0; i < _maxDim1Size; ++i) {
			for(int j = 0; j < _maxDim2Size; ++j) {
				nanElem[i][j] << NotANumber();
			}
		}
		_specParamData->getDataList().push_back(nanElem);
	}

	SpecParamData* createParamData() {
		return new ParamDataSpec< Tab2DData<typename MapType<type>::Type> >(_maxDim1Size,_maxDim2Size);
	}

private:

	int _maxDim3Size;

	int _dim1Num;

	int _dim2Num;

	int _dim3Num;

	int _dim3CutIndex;
        
                   int _minSumIndex;
    
                    int _maxSumIndex;
};

/**
 * @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 maxDimSize) : Base::Pusher(maxDimSize) {
		_paramData = _specParamData = createParamData();
	}

	void put(DD_data_t *data, DD_data_t *timeData) {
		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++) {
			double t = getTimeByIndex(timeData, index);
			_paramData->getTimeList().push_back(t);

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

			ElemenType elem = ElemenType();
			elem.resize(_maxDim1Size);
			elem << NotANumber();
			std::copy (lData[index], lData[index] + data->Dimensions[0], elem.begin());
			if(!isnan(_fillValue)) {
				for(auto it = elem.begin(); it != elem.end(); ++it) {
					double crt_val = *it;
					bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
					if(is_fill_value) {
						*it << NotANumber();
					}
				}
			}
			_specParamData->getDataList().push_back(elem);
		}
	}

	void putNaN() {
		_specParamData->getDataList().resize(1);
		ElemenType nanElem = ElemenType();
		nanElem.resize(_maxDim1Size);
		nanElem << NotANumber();
		_specParamData->getDataList().push_back(nanElem);
	}

	SpecParamData* createParamData() {
		return new ParamDataSpec< std::vector<typename MapType<type>::Type> >(_maxDim1Size);
	}

};


/**
 * @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, DD_data_t *timeData) {
		BaseElemenType **lData = reinterpret_cast<BaseElemenType **>(data->Variables);

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

                BaseElemenType nanElem;
                nanElem << NotANumber();

		if(!isnan(_fillValue)) {
                        BaseElemenType nanElem;
                        nanElem << NotANumber();
			for (int index = 0; index < data->VarNumber; index++) {
				double t = getTimeByIndex(timeData, index);
				_paramData->getTimeList().push_back(t);

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

				double crt_val = *lData[index];
				bool is_fill_value = isApproximatelyEqual(crt_val, _fillValue);
				if(!is_fill_value) {
					_specParamData->getDataList().push_back(*lData[index]);
				} else {
					_specParamData->getDataList().push_back(nanElem);
				}
			}
		}
		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 (t >= getTimeRestriction()) {
						putNaN();
						continue;
					}
				}

				_specParamData->getDataList().push_back(*lData[index]);
			}

		}
	}

	void putNaN() {
                BaseElemenType nanElem;
                nanElem << NotANumber();
		_specParamData->getDataList().resize(1);
		_specParamData->getDataList().push_back(nanElem);
	}

	SpecParamData* createParamData() {
		return new SpecParamData();
	}
};

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