VirtualInstrument.cc
12 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* VirtualInstrument.cc
*
* Created on: Nov 21, 2014
* Author: AKKA
*/
#include "DicError.hh"
#include "LocalFileInterfaceConfig.hh"
#include "VirtualInstrument.hh"
#include "VirtualInstrumentManager.hh"
#include "VirtualInstrumentInterval.hh"
#include "LocalParamData.hh"
#include "FileReaderCDF.hh"
#include "FileReaderASCII.hh"
#include "FileReaderVOTable.hh"
#include "FileReaderNetCDF.hh"
#include <boost/filesystem/path.hpp>
#include <boost/filesystem.hpp>
namespace AMDA {
namespace LocalFileInterface {
VirtualInstrument::VirtualInstrument(VirtualInstrumentManager& pVirtualInstrumentManager, const std::string& VIId) :
_virtualInstrumentManager(pVirtualInstrumentManager),
_VIId(VIId), _globalStartTime(0.), _globalStopTime(0.),
_filesFormat(VIFileFormat::FORMAT_UNKNOWN), _timeId(""), _fileReaderPtr(NULL)
{
}
VirtualInstrument::~VirtualInstrument()
{
//delete the file reader if needed
if (_fileReaderPtr != NULL)
delete _fileReaderPtr;
}
void VirtualInstrument::addFileDefinition(const char* pName, double pStart,double pStop)
{
//push back a file in the files list
LocalFileDefinition newDef;
newDef._name = pName;
newDef._start = pStart;
newDef._stop = pStop;
_files.push_back(newDef);
}
bool VirtualInstrument::createFileReader(LocalTimeFormat timeFormat)
{
if (_fileReaderPtr != NULL)
//file reader already created
return true;
switch (_filesFormat)
{
case VIFileFormat::FORMAT_CDF :
//CDF file format
_fileReaderPtr = new FileReaderCDF();
LOG4CXX_INFO(gLogger, "VirtualInstrument::createFileReader - CDF Reader created");
break;
case VIFileFormat::FORMAT_ASCII :
//ASCII file format
_fileReaderPtr = new FileReaderASCII();
reinterpret_cast<FileReaderASCII*>(_fileReaderPtr)->setTimeFormat(timeFormat);
LOG4CXX_INFO(gLogger, "VirtualInstrument::createFileReader - ASCII Reader created");
break;
case VIFileFormat::FORMAT_VOT :
//VOTable file format
_fileReaderPtr = new FileReaderVOTable();
LOG4CXX_INFO(gLogger, "VirtualInstrument::createFileReader - VOTable Reader created");
break;
case VIFileFormat::FORMAT_NETCDF :
//netCDF file format
_fileReaderPtr = new FileReaderNetCDF();
LOG4CXX_INFO(gLogger, "VirtualInstrument::createFileReader - netCDF Reader created");
break;
default:
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createFileReader - Reader not implemented");
}
return (_fileReaderPtr != NULL);
}
std::string VirtualInstrument::getFileFullPath(int fileIndex)
{
//build file full path from file index
if ((fileIndex >= (int)_files.size()) || (fileIndex < 0))
return "";
std::string filePath = _virtualInstrumentManager.getBasePath()+"/";
filePath += _files[fileIndex]._name;
boost::filesystem::path p(filePath);
boost::filesystem::path full_p = boost::filesystem::complete(p); // complete == absolute
std::string res = full_p.string();
return res;
}
PusherBase* VirtualInstrument::createPusher(std::string& paramId,
LocalParamType ¶mType, int& dim1Size, int& dim2Size)
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::createPusher " << paramId);
PusherBase* lPusher = NULL;
//Check reader
if (_fileReaderPtr == NULL)
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - No reader defined");
return NULL;
}
//Check list of files
if (_files.empty())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - No file define for the VirtualInstrument " << _VIId);
return NULL;
}
//use the first file to get the param type
std::string filePath = getFileFullPath(0);
//open the file
if (!_fileReaderPtr->open(filePath))
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Cannot open file " << filePath);
return NULL;
}
//get param type and size
bool success = _fileReaderPtr->getParamInfo(paramId, paramType, dim1Size, dim2Size);
//close the file
if (!_fileReaderPtr->close())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Cannot close file " << filePath);
}
if (!success)
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Cannot get param info for " << paramId);
return NULL;
}
if (paramType == LocalParamType::TYPE_UNKNOWN)
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Unknown data type");
return NULL;
}
//create the pusher
if ((dim1Size > 1) && (dim2Size > 1))
{
//Tab2D
switch (paramType)
{
case TYPE_FLOAT :
lPusher = new Pusher<TYPE_FLOAT,LocalContainerType::CONTAINER_MATRIX>(dim1Size,dim2Size);
break;
case TYPE_DOUBLE :
lPusher = new Pusher<TYPE_DOUBLE,LocalContainerType::CONTAINER_MATRIX>(dim1Size,dim2Size);
break;
case TYPE_SHORT :
lPusher = new Pusher<TYPE_SHORT,LocalContainerType::CONTAINER_MATRIX>(dim1Size,dim2Size);
break;
case TYPE_INT :
lPusher = new Pusher<TYPE_INT,LocalContainerType::CONTAINER_MATRIX>(dim1Size,dim2Size);
break;
default:
lPusher = NULL;
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Data type not implemented");
}
}
else if ((dim1Size > 1) || (dim2Size > 1))
{
//vector
int dimSize = (dim1Size > 1) ? dim1Size : dim2Size;
switch (paramType)
{
case TYPE_FLOAT :
lPusher = new Pusher<TYPE_FLOAT,LocalContainerType::CONTAINER_VECTOR>(dimSize);
break;
case TYPE_DOUBLE :
lPusher = new Pusher<TYPE_DOUBLE,LocalContainerType::CONTAINER_VECTOR>(dimSize);
break;
case TYPE_SHORT :
lPusher = new Pusher<TYPE_SHORT,LocalContainerType::CONTAINER_VECTOR>(dimSize);
break;
case TYPE_INT :
lPusher = new Pusher<TYPE_INT,LocalContainerType::CONTAINER_VECTOR>(dimSize);
break;
default:
lPusher = NULL;
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Data type not implemented");
}
}
else if (dim1Size == 1)
{
//scalar
switch (paramType)
{
case TYPE_FLOAT :
lPusher = new Pusher<TYPE_FLOAT,LocalContainerType::CONTAINER_SCALAR>();
break;
case TYPE_DOUBLE :
lPusher = new Pusher<TYPE_DOUBLE,LocalContainerType::CONTAINER_SCALAR>();
break;
case TYPE_SHORT :
lPusher = new Pusher<TYPE_SHORT,LocalContainerType::CONTAINER_SCALAR>();
break;
case TYPE_INT :
lPusher = new Pusher<TYPE_INT,LocalContainerType::CONTAINER_SCALAR>();
break;
default:
lPusher = NULL;
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Data type not implemented");
}
}
else {
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Incorrect param size");
return NULL;
}
return lPusher;
}
bool VirtualInstrument::updateTimeId()
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::updateTimeId");
//Check reader
if (_fileReaderPtr == NULL)
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::updateTimeId - No reader defined");
return false;
}
//Check list of files
if (_files.empty())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::updateTimeId - No file define for the VirtualInstrument " << _VIId);
return false;
}
//use the first file to get the time param id
std::string filePath = getFileFullPath(0);
//open the file
if (!_fileReaderPtr->open(filePath))
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::updateTimeId - Cannot open file " << filePath);
return false;
}
_timeId = _fileReaderPtr->getTimeParamId();
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::updateTimeId - _timeId: " << _timeId);
//close the file
if (!_fileReaderPtr->close())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::updateTimeId - Cannot close file " << filePath);
}
return (_timeId != "");
}
ParamFlowSPtr VirtualInstrument::getParamFlow(const std::string& pParamId, TimeIntervalListSPtr pTimeIntervalList,
LocalParamType paramType, int dim1Size, int dim2Size)
{
// Define function to compare two TimeIntervalListSptr.
// This used to identify if the requested TimeIntervalList already exist in _intervalList attribute or not.
auto lIt = std::find_if( _intervalList.begin(), _intervalList.end(), [&](const IntervalList::value_type& val) -> bool {
if (val.first->size() != pTimeIntervalList->size()) {
return false;
} else {
TimeIntervalList::const_iterator itA = val.first->begin();
TimeIntervalList::const_iterator itB = pTimeIntervalList->begin();
while (itA != val.first->end()) {
if ( (itA->_startTime != itB->_startTime) || (itA->_stopTime != itB->_stopTime) ) {
return false;
} else {
++itA;
++itB;
}
}
return true;
}
});
// TimeIntervalListSPtr not found
if (lIt == _intervalList.end()) {
std::pair<TimeIntervalListSPtr, VirtualIntrumentIntervalSPtr> value(pTimeIntervalList, VirtualIntrumentIntervalSPtr(new VirtualInstrumentInterval(*this, pTimeIntervalList.get())));
std::pair<IntervalList::iterator,bool> lIter = _intervalList.insert(value);
lIt = lIter.first;
} else {
// Nothing to do.
}
return lIt->second->getParamFlow(pParamId, paramType, dim1Size, dim2Size);
}
bool VirtualInstrument::getDataPosition(double& startTime, double& stopTime, int& fileIndex, int& recordIndex)
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::getDataPosition");
if (startTime > _globalStopTime || stopTime < _globalStartTime)
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::getDataPosition - Time out of the global interval definition");
return false;
}
//Check if time is include in a file
fileIndex = 0;
for (auto fileInfo : _files)
{
if ((fileInfo._start <= startTime) && (fileInfo._stop >= startTime))
break;
++fileIndex;
}
if (fileIndex == (int)_files.size())
{
//change time to the nearest file start time
fileIndex = 0;
for (auto fileInfo : _files)
{
if (fileInfo._start > startTime)
{
startTime = fileInfo._start;
recordIndex = 0;
return true;
}
++fileIndex;
}
}
//search time position in file
std::string filePath = getFileFullPath(fileIndex);
//open the file
if (!_fileReaderPtr->open(filePath))
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getDataPosition - Cannot open file " << filePath);
return false;
}
recordIndex = _fileReaderPtr->getRecordIndex(_timeId, startTime);
//close the file
if (!_fileReaderPtr->close())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::createPusher - Cannot close file " << filePath);
}
return (recordIndex >= 0);
}
FileReaderStatus VirtualInstrument::getParamPacketData(std::string& paramId, int fileIndex, int recordIndex,
double stopTime, LocalParamDataPacket *packet)
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::getParamPacketData");
if (fileIndex >= (int)_files.size())
//no more file available
return FRS_FINISH;
//get file full path
std::string filePath = getFileFullPath(fileIndex);
if (filePath == "")
return FRS_ERROR;
//open the file
if (!_fileReaderPtr->open(filePath))
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getParamData - Cannot open file " << filePath);
return FRS_ERROR;
}
FileReaderStatus status = _fileReaderPtr->getParamPacketData(_timeId, paramId, recordIndex,
stopTime, packet);
//close the file
if ((status != FRS_MORE) && !_fileReaderPtr->close())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getParamData - Cannot close file " << filePath);
}
return status;
}
InfoValuesSPtr& VirtualInstrument::getFileInfo(const char* pInfoName)
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrument::getFileInfo " << pInfoName);
auto lIt = _infoMap.find(pInfoName);
if (lIt == _infoMap.end()) {
_infoMap[pInfoName].reset(new std::vector<double>);
//Check reader
if (_fileReaderPtr == NULL)
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getFileInfo - No reader defined");
return _infoMap[pInfoName];
}
//Check list of files
if (_files.empty())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getFileInfo - No file define for the VirtualInstrument " << _VIId);
return _infoMap[pInfoName];
}
//use the first file to get the param type
std::string filePath = getFileFullPath(0);
//open the file
if (!_fileReaderPtr->open(filePath))
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getFileInfo - Cannot open file " << filePath);
return _infoMap[pInfoName];
}
_fileReaderPtr->getInfo(pInfoName,*_infoMap[pInfoName].get());
if (!_fileReaderPtr->close())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrument::getParamData - Cannot close file " << filePath);
}
return _infoMap[pInfoName];
}
return lIt->second;
}
} /* namespace LocalFileInterface */
} /* namespace AMDA */