Blame view

src/ExternLib/ttcat_to_param/TTCatToParam.hh 2.54 KB
40f1fe40   Benjamin Renard   Define a simple p...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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 {

ab842a73   Benjamin Renard   Optimize ttcat_to...
22
23
24
25
bool compareInterval(TimeTableCatalog::TimeInterval i1, TimeTableCatalog::TimeInterval i2) {
	return (i1._startTime < i2._startTime);
}

40f1fe40   Benjamin Renard   Define a simple p...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * @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;
ab842a73   Benjamin Renard   Optimize ttcat_to...
42
43
	std::sort(_intervals.begin(), _intervals.end(), compareInterval);
	_crtIt = _intervals.begin();
40f1fe40   Benjamin Renard   Define a simple p...
44
45
46
47
48
49
50
51
52
53
  }

  /**
   * @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);
ab842a73   Benjamin Renard   Optimize ttcat_to...
54
55
		double value = getValue(crtTime);
		if (!isNAN(value)) {
d321080e   Benjamin Renard   Fix last interval...
56
			_paramOutput->getDataList().push_back(value);
ab842a73   Benjamin Renard   Optimize ttcat_to...
57
58
			_paramOutput->pushTime(crtTime);
		}
40f1fe40   Benjamin Renard   Define a simple p...
59
60
61
62
	}
  }

  double getValue(double time) {
ab842a73   Benjamin Renard   Optimize ttcat_to...
63
64
65
66
67
68
	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) {
40f1fe40   Benjamin Renard   Define a simple p...
69
			continue;
ab842a73   Benjamin Renard   Optimize ttcat_to...
70
		}
40f1fe40   Benjamin Renard   Define a simple p...
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
		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;
ab842a73   Benjamin Renard   Optimize ttcat_to...
97
  std::vector<TimeTableCatalog::TimeInterval>::iterator _crtIt;
40f1fe40   Benjamin Renard   Define a simple p...
98
99
100
101
102
};

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