Blame view

src/ParamGetImpl/LocalFileInterface/FileReaderCDF.cc 21.4 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
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
/*
 * FileReaderCDF.cc
 *
 *  Created on: Nov 24, 2014
 *  Author: AKKA
 */

#include "FileReaderCDF.hh"

#include "ParameterManager.hh"
#include "LocalFileInterfaceConfig.hh"
#include "TimeUtil.hh"

#include <boost/shared_ptr.hpp>

namespace AMDA {
namespace LocalFileInterface {

FileReaderCDF::FileReaderCDF() : _cdfid(NULL), _workingBuffers(new CDFWorkingBuffers)
{
}

FileReaderCDF::~FileReaderCDF()
{
}

bool FileReaderCDF::open(std::string filePath)
{
	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);

	if (isOpened())
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::open - A file is already opened");
		return false;
	}

	CDFstatus status = CDFopenCDF(filePath.c_str(), &_cdfid);

	return (status == CDF_OK);
}

bool FileReaderCDF::close(void)
{
	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);

	if (isOpened())
	{
		CDFstatus status = CDFcloseCDF(_cdfid);

		if (status == CDF_OK)
			_cdfid = NULL;
	}

	return (_cdfid == NULL);
}

bool FileReaderCDF::isOpened(void)
{
	return (_cdfid != NULL);
}

std::string FileReaderCDF::getTimeParamId(void)
{
	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);

	std::string result = "";

	//search in rVariables
	long numrVars;
	CDFstatus status = CDFgetNumrVars (_cdfid, &numrVars);

	if (status == CDF_OK)
	{
		for (long i = 0; i < numrVars; ++i)
		{
			CDFVarInfo varInfo;
			status = CDFvarInquire(_cdfid, i, varInfo._name, &varInfo._dataType,
					&varInfo._numElts, &varInfo._recVary, varInfo._dimVarys);
			if (status == CDF_OK)
			{
62482147   Benjamin Renard   Add Epoch16 and T...
81
				if ((varInfo._dataType == CDF_EPOCH) || (varInfo._dataType == CDF_EPOCH16) || (varInfo._dataType == CDF_TIME_TT2000))
fbe3c2bb   Benjamin Renard   First commit
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
				{
					result = varInfo._name;
					return result;
				}
			}
		}
	}

	//search in zVariables
	long numzVars;
	status = CDFgetNumzVars (_cdfid, &numzVars);

	if (status == CDF_OK)
	{
		for (long i = 0; i < numzVars; ++i)
		{
			CDFVarInfo varInfo;
			status = CDFinquirezVar(_cdfid, i, varInfo._name, &varInfo._dataType,
					&varInfo._numElts, &varInfo._numDims, varInfo._dimSizes,
					&varInfo._recVary, varInfo._dimVarys);
			if (status == CDF_OK)
			{
62482147   Benjamin Renard   Add Epoch16 and T...
104
				if ((varInfo._dataType == CDF_EPOCH) || (varInfo._dataType == CDF_EPOCH16) || (varInfo._dataType == CDF_TIME_TT2000))
fbe3c2bb   Benjamin Renard   First commit
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
				{
					result = varInfo._name;
					return result;
				}
			}
		}
	}

	return result;
}

bool FileReaderCDF::getParamInfo(std::string& paramId, LocalParamType& paramType, int& dim1Size, int& dim2Size)
{
	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);

	paramType = LocalParamType::TYPE_UNKNOWN;
	dim1Size = 0;
	dim2Size = 0;

	//get CDF var info
	CDFVarInfo varInfo;
	if (!getCDFVarInfo(paramId, varInfo))
		return false;

	//get ParamType in relation to the CDF var type
	switch (varInfo._dataType)
	{
	case CDF_REAL4 :
	case CDF_FLOAT :
		paramType = LocalParamType::TYPE_FLOAT;
		break;
	case CDF_REAL8 :
	case CDF_DOUBLE :
	case CDF_EPOCH :
		paramType = LocalParamType::TYPE_DOUBLE;
		break;
62482147   Benjamin Renard   Add Epoch16 and T...
141
142
143
	case CDF_EPOCH16 :
		paramType = LocalParamType::TYPE_EPOCH16;
		break;
fbe3c2bb   Benjamin Renard   First commit
144
145
146
147
148
149
150
151
152
153
154
	case CDF_BYTE :
	case CDF_INT1 :
	case CDF_UINT1 :
	case CDF_INT2 :
	case CDF_UINT2 :
		paramType = LocalParamType::TYPE_SHORT;
		break;
	case CDF_INT4 :
	case CDF_UINT4 :
		paramType = LocalParamType::TYPE_INT;
		break;
62482147   Benjamin Renard   Add Epoch16 and T...
155
156
157
158
	case CDF_TIME_TT2000 :
	case CDF_INT8 :
		paramType = LocalParamType::TYPE_TT2000;
		break;
fbe3c2bb   Benjamin Renard   First commit
159
160
	case CDF_CHAR :
	case CDF_UCHAR :
fbe3c2bb   Benjamin Renard   First commit
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
	default :
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamInfo - Unknown data type");
		return false;
	}

	//set param size
	if (varInfo._numDims > 2)
	{
		dim1Size = 0;
		dim2Size = 0;
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamInfo - Unknown dimension");
		return false;
	}
	else if (varInfo._numDims == 2)
	{
		//Dimensions for a Tab2D
		dim1Size = varInfo._dimSizes[0];
		dim2Size = varInfo._dimSizes[1];
	}
	else if (varInfo._numDims == 1)
	{
		//Dimensions for a Vector
		dim1Size = varInfo._dimSizes[0];
		dim2Size = 1;
	}
	else
	{
		//Dimensions for a Scalar
		dim1Size = 1;
		dim2Size = 1;
	}

	return true;
}

bool FileReaderCDF::getCDFVarInfo(std::string paramName, CDFVarInfo& varInfo)
{
	if (paramName.empty())
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Empty param name");
		return false;
	}

	if (paramName.size() >= CDF_VAR_NAME_LEN256)
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Param name too large");
		return false;
	}

	//reset info structure
	memset(&varInfo,0,sizeof(CDFVarInfo));

	//confirm variable existence
	memcpy(varInfo._name,paramName.c_str(),paramName.size()*sizeof(char));
	varInfo._name[paramName.size()] = '\0';

	varInfo._num = CDFgetVarNum(_cdfid,varInfo._name);

	if (varInfo._num < 0)
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Cannot find variable");
		return false;
	}

	//Is it a zVariable?
	CDFstatus status = CDFconfirmzVarExistence(_cdfid,varInfo._name);

	bool isZVar = (status == CDF_OK);

	if (isZVar)
	{
		//get info about a zVariable
		varInfo._type = CDFVT_Z;

		status = CDFinquirezVar(_cdfid, varInfo._num, varInfo._name, &varInfo._dataType,
				&varInfo._numElts, &varInfo._numDims, varInfo._dimSizes, &varInfo._recVary,
				varInfo._dimVarys);

		if (status != CDF_OK)
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Error to call CDFinquirezVar");
			return false;
		}

		status = CDFgetzVarMaxWrittenRecNum(_cdfid, varInfo._num, &varInfo._maxRecNum);

		if (status != CDF_OK)
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Error to call CDFgetzVarMaxWrittenRecNum");
			return false;
		}

		status = CDFgetDataTypeSize(varInfo._dataType,&varInfo._numBytes);

		if (status != CDF_OK)
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Error to call CDFgetDataTypeSize");
			return false;
		}
	}
	else
	{
		//get info about a rVariable
		varInfo._type = CDFVT_R;

		status = CDFvarInquire(_cdfid, varInfo._num, varInfo._name,
				&varInfo._dataType, &varInfo._numElts, &varInfo._recVary, varInfo._dimVarys);

		if (status != CDF_OK)
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Error to call CDFvarInquire");
			return false;
		}

		long encoding;
		long majority;
		long numVars;
		long numAttrs;
		status = CDFinquire (_cdfid, &varInfo._numDims, varInfo._dimSizes, &encoding, &majority,
				&varInfo._maxRecNum, &numVars, &numAttrs);

		if (status != CDF_OK)
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Error to call CDFinquire");
			return false;
		}

		status = CDFgetDataTypeSize(varInfo._dataType,&varInfo._numBytes);

		if (status != CDF_OK)
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Error to call CDFgetDataTypeSize");
			return false;
		}
	}

	return true;
}

int FileReaderCDF::getRecordIndex(std::string& timeId, double time)
{
	LOG4CXX_DEBUG(gLogger, "FileReaderCDF::getRecordIndex");

	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);

	//get corresponding epoch time
	double timeEPOCH = timeStampToEPOCHTime(time);

	//get all time data
	long nbRec;
	long dimSize;
	long dataType;
	long numBytes;

	if (!getCDFData(timeId, 0, CDFWorkingBuffers::BufferType::BUFFER_TIME,
			dataType, numBytes, nbRec, dimSize))
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Cannot get time var data");
		return -1;
	}

62482147   Benjamin Renard   Add Epoch16 and T...
322
	if ((dataType != CDF_EPOCH) && (dataType != CDF_EPOCH16) && (dataType != CDF_TIME_TT2000))
fbe3c2bb   Benjamin Renard   First commit
323
	{
62482147   Benjamin Renard   Add Epoch16 and T...
324
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Not a time var " << timeId);
fbe3c2bb   Benjamin Renard   First commit
325
326
327
328
329
330
331
		return -1;
	}

	//search nearest record
	for (long i = 0; i < nbRec; ++i)
	{
		double crtTime;
62482147   Benjamin Renard   Add Epoch16 and T...
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
		switch (dataType) {
			case CDF_EPOCH :
				if (!extractDouble(CDFWorkingBuffers::BufferType::BUFFER_TIME, i, 0, dimSize,dataType,numBytes,crtTime))
				{
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Error to extract time data");
					return -1;
				}
				break;
			case CDF_EPOCH16 :
				double crtEpoch16Time[2];
				if (!extractEpoch16(CDFWorkingBuffers::BufferType::BUFFER_TIME, i, 0, dimSize,dataType,numBytes,crtEpoch16Time))
				{
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Error to extract time data");
					return -1;
				}
				crtTime = epoch16ToEpochTime(crtEpoch16Time);
				break;
			case CDF_TIME_TT2000 :
				long long crtTT2000Time;
				if (!extractTT2000(CDFWorkingBuffers::BufferType::BUFFER_TIME, i, 0, dimSize,dataType,numBytes,crtTT2000Time))
				{
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Error to extract time data");
					return -1;
				}
				crtTime = tt2000ToEpochTime(crtTT2000Time);
				break;
			default:
				LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Error to extract time data");
				return -1;
fbe3c2bb   Benjamin Renard   First commit
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
		}
		if (timeEPOCH <= crtTime)
			return i;
	}

	LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Cannot get record index");
	return -1;
}

FileReaderStatus FileReaderCDF::getParamPacketData(std::string& timeId, std::string& paramId,
		int recordIndex, double stopTime, LocalParamDataPacket *packet)
{
	LOG4CXX_DEBUG(gLogger, "FileReaderCDF::getParamPacketData");

	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);


	//get the size of one value of a data in the packet
	int packetValueSize;
	switch (packet->getType())
	{
		case LocalParamType::TYPE_FLOAT:
			packetValueSize = sizeof(float);
			break;
		case LocalParamType::TYPE_DOUBLE:
			packetValueSize = sizeof(double);
			break;
		case LocalParamType::TYPE_SHORT:
			packetValueSize = sizeof(short);
			break;
		case LocalParamType::TYPE_INT:
			packetValueSize = sizeof(int);
			break;
		default:
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - ParamType not implemented for CDF format");
			return FRS_ERROR;
	}

	//get time data
	long nbTimeRec;
	long dimTimeSize;
	long dataTimeType;
	long numTimeBytes;
	if (!getCDFData(timeId, recordIndex, CDFWorkingBuffers::BufferType::BUFFER_TIME, dataTimeType, numTimeBytes, nbTimeRec, dimTimeSize))
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Cannot get time var data for " << timeId);
		return FRS_ERROR;
	}

62482147   Benjamin Renard   Add Epoch16 and T...
410
	if ((dataTimeType != CDF_EPOCH) && (dataTimeType != CDF_EPOCH16) && (dataTimeType != CDF_TIME_TT2000))
fbe3c2bb   Benjamin Renard   First commit
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
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Bad time format " << timeId);
		return FRS_ERROR;
	}

	//get data
	long nbRec;
	long dimSize;
	long dataType;
	long numBytes;
	if (!getCDFData(paramId, recordIndex, CDFWorkingBuffers::BufferType::BUFFER_DATA, dataType, numBytes, nbRec, dimSize))
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Cannot get var data for " << paramId);
		return FRS_ERROR;
	}

	//check dimension integrity
	if (dimSize != packet->getDimsSize())
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Incompatibility between dim size of the packet and the CDF var");
		return FRS_ERROR;
	}

	//alloc buffer for one record of the packet
	_workingBuffers->reallocBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC,
			packetValueSize*packet->getDimsSize());

	for (int i = 0; i < nbTimeRec; ++i)
	{
		//time
62482147   Benjamin Renard   Add Epoch16 and T...
441
		double crtTime;
fbe3c2bb   Benjamin Renard   First commit
442
		double crtTimeEPOCH;
62482147   Benjamin Renard   Add Epoch16 and T...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
		switch (dataTimeType) {
			case CDF_EPOCH :
				if (!extractDouble(CDFWorkingBuffers::BufferType::BUFFER_TIME, i, 0,
						dimTimeSize,dataTimeType,numTimeBytes,crtTimeEPOCH))
				{
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract time data");
					return FRS_ERROR;
				}
				break;
			case CDF_EPOCH16 :
				double crtTimeEPOCH16[2];
				if (!extractEpoch16(CDFWorkingBuffers::BufferType::BUFFER_TIME, i, 0,
						dimTimeSize,dataTimeType,numTimeBytes,crtTimeEPOCH16))
				{
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract time data");
					return FRS_ERROR;
				}
				crtTimeEPOCH = epoch16ToEpochTime(crtTimeEPOCH16);
				break;
			case CDF_TIME_TT2000 :
				long long crtTimeTT2000;
				if (!extractTT2000(CDFWorkingBuffers::BufferType::BUFFER_TIME, i, 0,
						dimTimeSize,dataTimeType,numTimeBytes,crtTimeTT2000))
				{
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract time data");
					return FRS_ERROR;
				}
				crtTimeEPOCH = tt2000ToEpochTime(crtTimeTT2000);
				break;
			default:
				LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract time data");
				return FRS_ERROR;
fbe3c2bb   Benjamin Renard   First commit
475
476
		}

62482147   Benjamin Renard   Add Epoch16 and T...
477
		crtTime = epochTimeToTimeStamp(crtTimeEPOCH);
fbe3c2bb   Benjamin Renard   First commit
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

		//check if stop time is reached
		if (crtTime > stopTime)
			return FRS_FINISH;

		bool packetFull;

		if (i >= nbRec)
		{
			//add record data
			if (!packet->addData(crtTime,
					_workingBuffers->getBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC),
					packetFull))
			{
				if (packetFull)
					return FRS_MORE;
				LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to add data in packet");
				return FRS_ERROR;
			}
			continue;
		}

		for (int j = 0; j < dimSize; ++j)
		{
			switch (packet->getType())
			{
				case LocalParamType::TYPE_FLOAT:
				{
					//extract and copy value in the buffer
					float f;
					if (!extractFloat(CDFWorkingBuffers::BufferType::BUFFER_DATA, i, j,
							dimSize, dataType, numBytes, f))
					{
						LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract a data");
						return FRS_ERROR;
					}
					void* p = (void*)((intptr_t)_workingBuffers->getBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC) + j*packetValueSize);
					memcpy(p,&f,sizeof(float));
				}
				break;
				case LocalParamType::TYPE_DOUBLE:
				{
					//extract and copy value in the buffer
					double d;
					if (!extractDouble(CDFWorkingBuffers::BufferType::BUFFER_DATA, i, j,
							dimSize, dataType, numBytes, d))
					{
						LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract a data");
						return FRS_ERROR;
					}
					void* p = (void*)((intptr_t)_workingBuffers->getBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC) + j*packetValueSize);
					memcpy(p,&d,sizeof(double));
				}
				break;
				case LocalParamType::TYPE_SHORT:
				{
					//extract and copy value in the buffer
					short s;
					if (!extractShort(CDFWorkingBuffers::BufferType::BUFFER_DATA, i, j,
							dimSize, dataType, numBytes, s))
					{
						LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract a data");
						return FRS_ERROR;
					}
					void* p = (void*)((intptr_t)_workingBuffers->getBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC) + j*packetValueSize);
					memcpy(p,&s,sizeof(short));
				}
				break;
				case LocalParamType::TYPE_INT:
				{
					//extract and copy value in the buffer
					int ii;
					if (!extractInt(CDFWorkingBuffers::BufferType::BUFFER_DATA, i, j,
							dimSize, dataType, numBytes, ii))
					{
						LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to extract a data");
						return FRS_ERROR;
					}
					void* p = (void*)((intptr_t)_workingBuffers->getBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC) + j*packetValueSize);
					memcpy(p,&ii,sizeof(int));
				}
				break;
				default:
					LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - ParamType not implemented for CDF format");
					return FRS_ERROR;
			}
		}

		//add data record in the packet
		if (!packet->addData(crtTime,
				_workingBuffers->getBuffer(CDFWorkingBuffers::BufferType::BUFFER_PACKETREC),
				packetFull))
		{
			if (packetFull)
				return FRS_MORE;
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Error to add data in packet");
			return FRS_ERROR;
		}
	}

	return FRS_EOF;
}

double FileReaderCDF::timeStampToEPOCHTime(double timeStamp)
{
	std::stringstream isoTime;
	TimeUtil::formatTimeDateInIso(timeStamp, isoTime);

	std::string timeString = isoTime.str();

	char timeChar[EPOCH4_STRING_LEN+1];
	memset(timeChar,0,(EPOCH4_STRING_LEN+1)*sizeof(char));

	if ((timeString.size() > 0) && (timeString.size() <= EPOCH4_STRING_LEN))
			memcpy(timeChar,timeString.c_str(),timeString.size()*sizeof(char));

	return parseEPOCH4(timeChar);
}

double FileReaderCDF::epochTimeToTimeStamp(double epochTime)
{
	char isoTime[EPOCH4_STRING_LEN+1];
	encodeEPOCH4(epochTime,isoTime);
	return TimeUtil::readTimeInIso(isoTime);
}

62482147   Benjamin Renard   Add Epoch16 and T...
604
605
606
607
608
609
610
611
612
613
614
615
double FileReaderCDF::epoch16ToEpochTime(double epoch16Time[])
{
	long year, month, day, hour, minute, second, msec, microsec, nanosec, picosec;
	EPOCH16breakdown(epoch16Time, &year, &month, &day, &hour, &minute, &second, &msec, &microsec, &nanosec, &picosec);
	return computeEPOCH(year, month, day, hour, minute, second, msec);
}

double FileReaderCDF::tt2000ToEpochTime(long long tt2000Time)
{
	return CDF_TT2000_to_UTC_EPOCH(tt2000Time);
}

fbe3c2bb   Benjamin Renard   First commit
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
bool FileReaderCDF::getCDFData(std::string& varId, long startRec,
		CDFWorkingBuffers::BufferType bufferType,
		long& dataType, long& numBytes, long& nbRec, long& dimSize)
{
	nbRec = 0;
	dimSize = 0;

	CDFVarInfo varInfo;
	if (!getCDFVarInfo(varId, varInfo))
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFData - Cannot get param info " << varId);
		return false;
	}

	dataType = varInfo._dataType;
	numBytes = varInfo._numBytes;

	if (varInfo._numElts > 1)
	{
		//only possible if CDF_CHAR
		//not implemented (CDF_CHAR variable is not a valid variable for the ParamGet)
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFData - _numElts >= 1! " << varId);
		return false;
	}

	if (varInfo._maxRecNum <= 0)
		//no record in file
		return true;

	nbRec = varInfo._maxRecNum - startRec + 1;

	dimSize = 1;
	if (varInfo._numDims > 0)
		for (int i = 0; i < varInfo._numDims; ++i)
			dimSize *= varInfo._dimSizes[i];

	_workingBuffers->reallocBuffer(bufferType, nbRec*varInfo._numBytes*dimSize);

	CDFstatus status = CDFgetVarRangeRecordsByVarName(_cdfid, varInfo._name, startRec,
		varInfo._maxRecNum, _workingBuffers->getBuffer(bufferType));

	if (status != CDF_OK)
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFData - Error to get data for " << varId);
		return false;
	}

	return true;
}

bool FileReaderCDF::extractShort(CDFWorkingBuffers::BufferType bufferType, long recIndex, long dimIndex, long dimSize,
		long dataType, long numBytes, short& value)
{
	void *p = (void*)((intptr_t)_workingBuffers->getBuffer(bufferType) + (recIndex*dimSize + dimIndex)*numBytes);

	switch(dataType)
	{
		case CDF_BYTE :
		case CDF_INT1 :
		{
			char* c = (char*)p;
			value = (short)(*c);
			break;
		}
		case CDF_UINT1 :
		{
			unsigned char* uc = (unsigned char*)p;
			value = (short)(*uc);
			break;
		}
		case CDF_INT2 :
		{
			short* s = (short *)p;
			value = (short)(*s);
			break;
		}
		case CDF_UINT2 :
		{
			unsigned short* us = (unsigned short*)p;
			value = (short)(*us);
			break;
		}
		default:
			return false;
	}
	return true;
}

bool FileReaderCDF::extractInt(CDFWorkingBuffers::BufferType bufferType, long recIndex, long dimIndex, long dimSize,
		long dataType, long numBytes, int& value)
{
	//try to extract a short
	short s;
	if (extractShort(bufferType, recIndex, dimIndex, dimSize,
			dataType, numBytes, s))
	{
		//cast short to int
		value = (int)s;
		return true;
	}

	void *p = (void*)((intptr_t)_workingBuffers->getBuffer(bufferType) + (recIndex*dimSize + dimIndex)*numBytes);
	switch (dataType)
	{
	case CDF_INT4 :
	{
		int* i = (int*)p;
		value = (int)(*i);
		break;
	}
	case CDF_UINT4 :
	{
		unsigned int* ui = (unsigned int*)(p);
		value = (int)(*ui);
		break;
	}
	default:
		return false;
	}

	return true;
}

bool FileReaderCDF::extractFloat(CDFWorkingBuffers::BufferType bufferType, long recIndex, long dimIndex, long dimSize,
		long dataType, long numBytes, float& value)
{
	//try to extract an int
	int i;
	if (extractInt(bufferType, recIndex, dimIndex, dimSize,
			dataType, numBytes, i))
	{
		//cast int to float
		value = (float)i;
		return true;
	}

	void *p = (void*)((intptr_t)_workingBuffers->getBuffer(bufferType) + (recIndex*dimSize + dimIndex)*numBytes);
	switch (dataType)
	{
	case CDF_REAL4 :
	case CDF_FLOAT :
	{
		float* f = (float*)p;
		value = (float)(*f);
		break;
	}
	default :
		return false;
	}

	return true;
}

bool FileReaderCDF::extractDouble(CDFWorkingBuffers::BufferType bufferType, long recIndex, long dimIndex, long dimSize,
		long dataType, long numBytes, double& value)
{
	//try to extract an float
	float f;
	if (extractFloat(bufferType, recIndex, dimIndex, dimSize,
		dataType, numBytes, f))
	{
		//cast float to double
		value = (double)f;
		return true;
	}

	void *p = (void*)((intptr_t)_workingBuffers->getBuffer(bufferType) + (recIndex*dimSize + dimIndex)*numBytes);

	switch(dataType)
	{
	case CDF_REAL8 :
	case CDF_DOUBLE :
	case CDF_EPOCH :
	{
		double* d = (double*)p;
		value = (double)(*d);
		break;
	}
	default :
		return false;
	}

	return true;
}

62482147   Benjamin Renard   Add Epoch16 and T...
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
bool FileReaderCDF::extractEpoch16(CDFWorkingBuffers::BufferType bufferType, long recIndex, long dimIndex, long dimSize,
		long dataType, long numBytes, double value[])
{
	void *p = (void*)((intptr_t)_workingBuffers->getBuffer(bufferType) + (recIndex*dimSize + dimIndex)*numBytes);

	switch(dataType)
	{
	case CDF_EPOCH16 :
	{
		std::memcpy(value, p, 2*sizeof(double));
		return true;
	}
	default :
		return false;
	}

	return true;
}

bool FileReaderCDF::extractTT2000(CDFWorkingBuffers::BufferType bufferType, long recIndex, long dimIndex, long dimSize,
		long dataType, long numBytes, long long& value)
{
	void *p = (void*)((intptr_t)_workingBuffers->getBuffer(bufferType) + (recIndex*dimSize + dimIndex)*numBytes);

	switch(dataType)
	{
	case CDF_TIME_TT2000 :
	{
		long long* ll = (long long*)p;
		value = (long long)(*ll);
		break;
	}
	default :
		return false;
	}

	return true;
}

f5c53312   Benjamin Renard   Get calib info in...
840
841
842
/*
 * @brief Get an information
 */
d6c7a344   RENARD Benjamin   Fix some warning ...
843
bool FileReaderCDF::getInfo(const char* /*pInfoName*/, std::vector<double>& /*res*/)
f5c53312   Benjamin Renard   Get calib info in...
844
845
846
847
848
849
850
851
852
853
{
	LOG4CXX_DEBUG(gLogger, "FileReaderCDF::getInfo");

	boost::mutex::scoped_lock scoped_lock(AMDA::Parameters::ParameterManager::mutexCDFLib);

	//ToDo

	return false;
}

fbe3c2bb   Benjamin Renard   First commit
854
855
} /* LocalFileInterface */
} /* AMDA */