TTCatToParam.hh
2.55 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
/*
* TTCatToParam.hh
*
* Created on: Jan 31, 2019
* Author: AKKA
*/
#ifndef TTCatToParam_HH_
#define TTCatToParam_HH_
#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"
#include "Catalog.hh"
#include <boost/lexical_cast.hpp>
namespace AMDA {
namespace Parameters {
bool compareInterval(TimeTableCatalog::TimeInterval i1, TimeTableCatalog::TimeInterval i2) {
return (i1._startTime < i2._startTime);
}
/**
* @class TTCatToParam
* @brief Convert a time table or a catalog to a parameter.
*/
class TTCatToParam : public Operation {
public:
/**
* @brief Constructor.
*/
TTCatToParam(Process& pProcess, ParamData& paramInput, const std::vector<TimeTableCatalog::TimeInterval>& intervals, std::string paramName = "", int paramComp = 0)
: Operation(pProcess),
_paramInput(paramInput),
_paramOutput(new ParamDataScalaireDouble()),
_intervals(intervals), _paramName(paramName), _paramComp(paramComp) {
_paramDataOutput=_paramOutput;
std::sort(_intervals.begin(), _intervals.end(), compareInterval);
_crtIt = _intervals.begin();
}
/**
* @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
*/
void write(ParamDataIndexInfo &pParamDataIndexInfo) {
//Store data in a working list
for (unsigned int index = pParamDataIndexInfo._startIndex;
index< pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess; index++) {
double crtTime = _paramInput.getTime(index);
double value = getValue(crtTime);
if (!isNAN(value)) {
_paramOutput->getDataList().push_back(getValue(crtTime));
_paramOutput->pushTime(crtTime);
}
}
}
double getValue(double time) {
for(std::vector<TimeTableCatalog::TimeInterval>::iterator it = _crtIt; it != _intervals.end(); ++it) {
_crtIt = it;
if (time < it->_startTime) {
return NAN;
}
else if (time > it->_stopTime) {
continue;
}
if (_paramName.empty()) {
return 1.;
}
else {
std::vector<std::string> data = it->getParameterData(_paramName);
if (_paramComp >= (int)data.size()) {
return NAN;
}
std::string value = data[_paramComp];
try {
return boost::lexical_cast<double>(value);
} catch (const boost::bad_lexical_cast &e) {
return NAN;
}
}
}
return NAN;
}
ParamData &_paramInput;
ParamDataScalaireDouble *_paramOutput;
std::vector<TimeTableCatalog::TimeInterval> _intervals;
std::string _paramName;
int _paramComp;
std::vector<TimeTableCatalog::TimeInterval>::iterator _crtIt;
};
} /* namespace Parameters */
} /* namespace AMDA */
#endif /* TTCatToParam */