TTCatToParam.hh 2.54 KB
/*
 * 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(value);
			_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 */