Blame view

src/ParamGetImpl/LocalFileInterface/ParamGetLocalFile.cc 7.7 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
/*
 * ParamGetLocalFile.cc
 *
 *  Created on: 21 nov. 2014
 *      Author: AKKA
 */

#include "LocalFileInterfaceConfig.hh"
#include "ParamGetLocalFile.hh"
#include "VirtualInstrumentManager.hh"

#include <stdlib.h>

#include "Parameter.hh"
#include "ParamData.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "DicError.hh"
#include "TimeUtil.hh"
#include "Helper.hh"

namespace AMDA {

namespace LocalFileInterface {

ParamGetLocalFile::ParamGetLocalFile(Parameter &parameter) :
		ParamGet_CRTP<ParamGetLocalFile>(parameter),
		_paramId(""), _paramType(LocalParamType::TYPE_UNKNOWN), _paramDim1Size(0),
f5c53312   Benjamin Renard   Get calib info in...
29
		_paramDim2Size(0), _paramMinSampling(0.), _paramMaxSampling(0.), _timeFormat(LocalTimeFormat::DOUBLE), _fillValue(NAN),
fbe3c2bb   Benjamin Renard   First commit
30
31
32
33
34
35
36
37
38
39
40
41
42
		_viId(""), _timeStamp(0), _pusher(NULL)
{

}

ParamGetLocalFile::ParamGetLocalFile(const ParamGetLocalFile &pParamGetLocalFile, Parameter &parameter) :
		ParamGet_CRTP<ParamGetLocalFile>(pParamGetLocalFile, parameter),
		_paramId(pParamGetLocalFile._paramId),
		_paramType(pParamGetLocalFile._paramType),
		_paramDim1Size(pParamGetLocalFile._paramDim1Size),
		_paramDim2Size(pParamGetLocalFile._paramDim2Size),
		_paramMinSampling(pParamGetLocalFile._paramMinSampling),
		_paramMaxSampling(pParamGetLocalFile._paramMaxSampling),
f5c53312   Benjamin Renard   Get calib info in...
43
44
		_timeFormat(pParamGetLocalFile._timeFormat),
		_infoRequestList(pParamGetLocalFile._infoRequestList),
fbe3c2bb   Benjamin Renard   First commit
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
		_fillValue(pParamGetLocalFile._fillValue),
		 _viId(pParamGetLocalFile._viId), _timeStamp(pParamGetLocalFile._timeStamp),
		 _pusher(pParamGetLocalFile._pusher)
{

}

ParamGetLocalFile::~ParamGetLocalFile()
{
	//delete the pusher if needed
	if (_pusher != NULL)
		delete _pusher;
}

TimeStamp ParamGetLocalFile::init()
{
	LOG4CXX_DEBUG(gLogger, "ParamGetLocalFile::init");
	//get Virtual Instrument
	_vi = VirtualInstrumentManager::getInstance()->getVirtualInstrument(
			_viId);

	//create file reader
f5c53312   Benjamin Renard   Get calib info in...
67
	if (!_vi->createFileReader(_timeFormat))
fbe3c2bb   Benjamin Renard   First commit
68
69
70
71
72
73
74
75
76
77
	{
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_LOCALFILE_READER) << AMDA::ex_msg(std::string("Cannot create corresponding file reader")));
	}

	//get time param id
	if (!_vi->updateTimeId())
	{
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_LOCALFILE_TIMEPARAM) << AMDA::ex_msg(std::string("Cannot detect time param Id")));
	}

f5c53312   Benjamin Renard   Get calib info in...
78
79
80
	//get calib info
	getCalibInfo();

fbe3c2bb   Benjamin Renard   First commit
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
	//create pusher
	LocalParamType paramType = _paramType;
	int paramDim1Size = _paramDim1Size;
	int paramDim2Size = _paramDim2Size;
	_pusher = _vi->createPusher(_paramId,paramType,paramDim1Size,paramDim2Size);

	if (_pusher == NULL)
	{
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_TYPE_DATA_UNKNOWN) << AMDA::ex_msg(std::string("Cannot create param data")));
	}

	//set link to the param data
	_paramData = ParamDataSPtr(_pusher->getParamData());

	_paramData->setMinSampling(_paramMinSampling);

	// Get ParamFlow instance only if there is at least one TimeInterval to process
	// and if delta is not equal to 0.
	if (_timeIntervalList->size() != 0 && !(_timeIntervalList->size() == 1 && (_timeIntervalList->front()._stopTime - _timeIntervalList->front()._startTime) == 0) )
	{
		_paramFlow = _vi->getParamFlow(_paramId, _timeIntervalList,paramType,paramDim1Size,paramDim2Size);
	} else if (_timeIntervalList->size() == 0) {
		LOG4CXX_WARN(gLogger, "ParamGetLocalFile::init => List of time interval is empty");
	} else {
		// Nothing to do
	}

	if (_timeStamp == 0 && _signatureTrigger != "") {
		// _signatureTrigger must be a name of xml parameter file
		_timeStamp = AMDA::Helpers::Helper::dateOfFile(
				_signatureTrigger.c_str());
	}
2f05d991   RENARD Benjamin   Generate param in...
113
114
115
        
        setGlobalStart(_vi->getGlobalStartTime());
        setGlobalStop(_vi->getGlobalStopTime());
fbe3c2bb   Benjamin Renard   First commit
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

	return _timeStamp;
}


unsigned int ParamGetLocalFile::write()
{
	unsigned int result = 0;

	if (_paramFlow.get() == nullptr)
	{
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::ex_msg("Param flow not yet initialized for parameter '" + _paramId));
	} else {
		// Nothing to do
	}

	//get packet and push it in ParamData
	LOG4CXX_DEBUG(gLogger, "ParamGetLocalFile::write " << _paramId);
	LocalParamDataPacket* lPacket = _paramFlow->get(_paramId);
	if (lPacket) {
		_pusher->setFillValue(_fillValue);
		do {
d85350c3   Benjamin Renard   Add no data supor...
138
139
140
141
142
143
144
145
146
147
148
			if (lPacket->isNoData()) {
				LOG4CXX_DEBUG(gLogger, "ParamGetLocalFile::write => no data packet");
				_paramData->getTimeList().push_back(lPacket->getStartTime());
				_pusher->putNaN();
				_paramData->getTimeList().push_back(lPacket->getStopTime());
				_pusher->putNaN();
				result += 2;
			}
			else {
				result += _pusher->put(lPacket);
			}
fbe3c2bb   Benjamin Renard   First commit
149
			delete lPacket;
c6a67968   Benjamin Renard   Fix some violatio...
150
151
			lPacket = _paramFlow->tryGet(_paramId);
		} while(lPacket);
fbe3c2bb   Benjamin Renard   First commit
152
153
154
155
	}

	// Push up the information if all time interval was processed.
	_paramData->getIndexInfo()._timeIntToProcessChanged = _paramFlow->isTimeIntToProcessChanged(_paramId);
fa4b7852   Benjamin Renard   Fix bug around ti...
156
	_paramData->getIndexInfo()._noMoreTimeInt =  _paramFlow->isNoMoreTimeInt(_paramId);
fbe3c2bb   Benjamin Renard   First commit
157
158
159
160
161
162
163
164
165
166
167
168
169

	// if time interval changed store index which delimit the end of the time interval.
	if (_paramData->getIndexInfo()._timeIntToProcessChanged || (!_paramData->getIndexInfo()._timeIntToProcessChanged && result == 0)) {
		unsigned int lEndTimeIntIndex = _paramData->getIndexInfo()._nbDataToProcess;
		_paramData->getIndexInfo()._endTimeIntIndexList.push_back(lEndTimeIntIndex);
	}
	else {
		// Nothing to do.
	}

	return result;
}

f5c53312   Benjamin Renard   Get calib info in...
170
171
172
173
174
175
176
177
178
179
180
void ParamGetLocalFile::getCalibInfo() {
	for (InfoRequestList::iterator lIt = _infoRequestList.begin(); lIt != _infoRequestList.end(); ++lIt)
	{
		auto lInfoName = *lIt;
		if (_parameter.getInfoList().find(lInfoName) == _parameter.getInfoList().end()) {
			LOG4CXX_INFO( gLogger, "ParamGetLocalFile:getCalibInfo( " << lInfoName << "')");
			_parameter.setInfoValues(*lIt, _vi->getFileInfo(lIt->c_str()));
		}
	}
}

fbe3c2bb   Benjamin Renard   First commit
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
/*
 * @overload DataWriter::getMinSampling
 */
double ParamGetLocalFile::getMinSampling()
{
	return getParamMinSampling();
}

void ParamGetLocalFile::updateInfo(Parameter & parameter)
{
	LOG4CXX_DEBUG(gLogger, "ParamGetLocalFile::updateInfo - " << parameter.getId());
	if (parameter.getInfoId().empty())
		parameter.setInfoId(parameter.getId());

	//Param info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(parameter.getInfoId(),true);

	if (paramInfo == nullptr)
		return;

	//if a fill value defined => save it
	if (!isnan(paramInfo->getFillValue()))
		_fillValue = paramInfo->getFillValue();

	//fill value automatically replace by nan in the Pusher
	paramInfo->setFillValue(NAN);

	//Add parameter info id as parameter name if no exist
	if (paramInfo->getName().empty())
		paramInfo->setName(_paramId);

	//Add parameter info id as parameter short name if no exist
	if (paramInfo->getShortName().empty())
		paramInfo->setShortName(_paramId);

	std::string datasetId = paramInfo->getDatasetId();
	if (datasetId.empty())
		//get vi ID as dataset id if no dataset info defined
		datasetId = _vi->getVIId();

	//link dataset info to param info
	paramInfo->setDatasetId(datasetId);

	//Dataset info
	AMDA::Info::DataSetInfoSPtr datasetInfo = AMDA::Info::DataSetMgr::getInstance()->getDataSetInfoFromId(datasetId,true);

	if (datasetInfo == nullptr)
		return;

	//Add dataset id as dataset name if no exist
	if (datasetInfo->getName().empty())
		datasetInfo->setName(_vi->getVIId());

	//Set sampling values
	datasetInfo->setMinSampling((int)_paramMinSampling);
	datasetInfo->setMaxSampling((int)_paramMaxSampling);

	//Set global start time
	std::stringstream isoTime;
	TimeUtil::formatTimeDateInIso(_vi->getGlobalStartTime(), isoTime);
	datasetInfo->setGlobalStart(isoTime.str());

	//Set global stop time
	isoTime.str("");
	TimeUtil::formatTimeDateInIso(_vi->getGlobalStopTime(), isoTime);
	datasetInfo->setGlobalStop(isoTime.str());

	//Set source
	datasetInfo->setSource("CDPP/AMDA Local base");
}

} /* namespace LocalFileInterface */
} /* namespace AMDA */