Blame view

src/ExternLib/Boxcar/Boxcar.hh 3.03 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
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
/*
 * Boxcar.hh
 *
 *  Created on: Dec 3, 2012
 *      Author: f.casimir
 */

#ifndef Boxcar_HH_
#define Boxcar_HH_

#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"

namespace AMDA {
namespace Parameters {

/**
 * @class Boxcar
 * @brief It is responsible to shift data of any ParamData type.
 * @details This class implement the interface Operation.
 */
template<class TParamData>
class Boxcar : public Operation {
public:
	/**
	 * @brief Constructor.
	 * @details Create the ParamData type of the input ParamData.
	 */
  Boxcar(Process& pProcess, TParamData& paramInput, double second)
	: Operation(pProcess),
	  _paramInput(paramInput),
	  _paramOutput(new TParamData()),
	  _second(second),
	  _nb(0),
	  _lastTime(0.0){
	  _sum << elemNull;
	  _paramDataOutput=_paramOutput;
  }

	/**
	 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
	 */
  void  write(ParamDataIndexInfo &pParamDataIndexInfo) {
    unsigned int index = pParamDataIndexInfo._startIndex;
   //first call
    if(_lastTime == 0.0) {
    	_lastTime = _paramInput.getTime(index);
    	//To create a _sum with the good dimention
    	_sum =  _paramInput.getDataList()[index];
    	_sum << elemNull;
    }
    if(pParamDataIndexInfo._nbDataToProcess > 0) {
    for (; index< pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess; index++) {
    	if(_paramInput.getTime(index) - _lastTime >= _second) {
        	pushData();
          	_lastTime = _paramInput.getTime(index);
    	}
    	_nb++;
    	_sum =_sum + _paramInput.getDataList()[index];
    	_timeMemory.push_back(_paramInput.getTime(index));
    }
    }
    //pParamDataIndexInfo._nbDataToProcess == 0: end of Data
    else {
    	pushData();
    }
  }

  /**
   * @overload Operation::reset(double pStartTime, double pTimeInt)
   * @brief reset static data to process another TimeInterval
   */
  virtual  void  reset() {
	  Operation::reset();
	  _nb = 0;
	  _lastTime = 0.0;
	  _sum << elemNull;
	  _timeMemory.clear();
  }

private:

  /**
   * @brief write time and data in _paramOutput
   */
  inline void pushData() {

		typename TParamData::ElementType lMean = _sum / _nb;
		for(unsigned indexLocal = 0; indexLocal < _nb; ++indexLocal) {
			_paramOutput->pushTime(_timeMemory[indexLocal]);
			_paramOutput->getDataList().push_back(lMean);
		}
  	_sum << elemNull ;
  	_nb = 0;
  	_timeMemory.clear();
  }
	/**
	 * @brief It is the channel of data to shift.
	 */
  TParamData &_paramInput;
	/**
	 * @brief It is the channel of the data shifted.
	 */
  TParamData *_paramOutput;
  /**
   * @brief laps time to shift data.
   */
  double _second;

  /**
   * @brief sum of data to compute mean = _sum / _nb.
   */
  typename TParamData::ElementType _sum;

  /**
   * @brief number of data to compute mean = _sum / _nb.
   */
  unsigned int _nb;

  /**
   * @brief time memory.
   */
  std::vector <double > _timeMemory;

  /**
   * @brief last start time of mean compute .
   */
  double _lastTime;
};

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