Blame view

src/ParamGetImpl/SpeasyProxyInterface/FileReaderCDF.cc 25.6 KB
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
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
/*
 * FileReaderCDF.cc
 *
 *  Created on: Nov 24, 2014
 *  Author: AKKA
 */

#include "FileReaderCDF.hh"

#include "ParameterManager.hh"
#include "SpeasyProxyInterfaceConfig.hh"
#include "TimeUtil.hh"

#include <boost/shared_ptr.hpp>

namespace AMDA {
namespace SpeasyProxyInterface {

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 true;//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)
			{
				if ((varInfo._dataType == CDF_EPOCH) || (varInfo._dataType == CDF_EPOCH16) || (varInfo._dataType == CDF_TIME_TT2000))
				{
					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)
			{
				if ((varInfo._dataType == CDF_EPOCH) || (varInfo._dataType == CDF_EPOCH16) || (varInfo._dataType == CDF_TIME_TT2000))
				{
					result = varInfo._name;
					return result;
				}
			}
		}
	}

	return result;
}

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

	paramType = SpeasyProxyParamType::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 = SpeasyProxyParamType::TYPE_FLOAT;
		break;
	case CDF_REAL8 :
	case CDF_DOUBLE :
	case CDF_EPOCH :
		paramType = SpeasyProxyParamType::TYPE_DOUBLE;
		break;
	case CDF_EPOCH16 :
		paramType = SpeasyProxyParamType::TYPE_EPOCH16;
		break;
	case CDF_BYTE :
	case CDF_INT1 :
	case CDF_UINT1 :
	case CDF_INT2 :
	case CDF_UINT2 :
		paramType = SpeasyProxyParamType::TYPE_SHORT;
		break;
	case CDF_INT4 :
	case CDF_UINT4 :
		paramType = SpeasyProxyParamType::TYPE_INT;
		break;
	case CDF_TIME_TT2000 :
	case CDF_INT8 :
		paramType = SpeasyProxyParamType ::TYPE_TT2000;
		break;
	case CDF_CHAR :
	case CDF_UCHAR :
	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)
{
a0567216   Benjamin Renard   Fix some bugs
198
    if (paramName.empty())
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
199
200
201
202
203
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Empty param name");
		return false;
	}

a0567216   Benjamin Renard   Fix some bugs
204
    if (paramName.size() >= CDF_VAR_NAME_LEN256)
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
205
206
207
208
209
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFVarInfo - Param name too large");
		return false;
	}

3e723802   Benjamin Renard   Improve SpeasyPro...
210
211
212
213
214
    if (_varInfoMap.find(paramName) != _varInfoMap.end()) {
        varInfo = _varInfoMap[paramName];
        return true;
    }

a0567216   Benjamin Renard   Fix some bugs
215
    //reset info structure
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
216
217
218
219
220
221
222
223
	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);

a0567216   Benjamin Renard   Fix some bugs
224
    if (varInfo._num < 0)
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
225
226
227
228
229
	{
		LOG4CXX_INFO(gLogger, "FileReaderCDF::getCDFVarInfo - Cannot find variable");
		return false;
	}

3e723802   Benjamin Renard   Improve SpeasyPro...
230
231
232
233
    long depend1AttNum = CDFattrNum(_cdfid, (char*)"DEPEND_1");
    long depend2AttNum = CDFattrNum(_cdfid, (char*)"DEPEND_2");
    

a0567216   Benjamin Renard   Fix some bugs
234
    //Is it a zVariable?
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
235
236
237
238
239
240
	CDFstatus status = CDFconfirmzVarExistence(_cdfid,varInfo._name);

	bool isZVar = (status == CDF_OK);

	if (isZVar)
	{
a0567216   Benjamin Renard   Fix some bugs
241
    	//get info about a zVariable
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
242
243
244
245
246
247
248
249
250
251
252
253
254
		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);
a0567216   Benjamin Renard   Fix some bugs
255
    
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
256
257
258
259
260
261
262
263
264
265
266
267
268
		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;
		}
3e723802   Benjamin Renard   Improve SpeasyPro...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

        if (depend1AttNum >= 0) {
            long dataType;
            long numElems;
            status = CDFinquireAttrzEntry (_cdfid, depend1AttNum, varInfo._num, &dataType, &numElems);

            if (status == CDF_OK) {
                if (dataType == CDF_CHAR) {
                    status = CDFgetAttrzEntry (_cdfid, depend1AttNum, varInfo._num, varInfo._depend1);
                }
            }
        }

        if (depend2AttNum >= 0) {
            long dataType;
            long numElems;
            status = CDFinquireAttrzEntry (_cdfid, depend2AttNum, varInfo._num, &dataType, &numElems);

            if (status == CDF_OK) {
                if (dataType == CDF_CHAR) {
                    status = CDFgetAttrzEntry (_cdfid, depend2AttNum, varInfo._num, varInfo._depend2);
                }
            }
        }
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
293
294
295
	}
	else
	{
a0567216   Benjamin Renard   Fix some bugs
296
    	//get info about a rVariable
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
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
		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;
		}
3e723802   Benjamin Renard   Improve SpeasyPro...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

        if (depend1AttNum >= 0) {
            long dataType;
            long numElems;
            status = CDFinquireAttrrEntry (_cdfid, depend1AttNum, varInfo._num, &dataType, &numElems);

            if (status == CDF_OK) {
                if (dataType == CDF_CHAR) {
                    status = CDFgetAttrrEntry (_cdfid, depend1AttNum, varInfo._num, varInfo._depend1);
                }
            }
        }

        if (depend2AttNum >= 0) {
            long dataType;
            long numElems;
            status = CDFinquireAttrrEntry (_cdfid, depend2AttNum, varInfo._num, &dataType, &numElems);

            if (status == CDF_OK) {
                if (dataType == CDF_CHAR) {
                    status = CDFgetAttrrEntry (_cdfid, depend2AttNum, varInfo._num, varInfo._depend2);
                }
            }
        }
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
352
	}
3e723802   Benjamin Renard   Improve SpeasyPro...
353
354
355

    _varInfoMap[paramName] = varInfo;

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
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
	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;
	}

	if ((dataType != CDF_EPOCH) && (dataType != CDF_EPOCH16) && (dataType != CDF_TIME_TT2000))
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getRecordIndex - Not a time var " << timeId);
		return -1;
	}

	//search nearest record
	for (long i = 0; i < nbRec; ++i)
	{
		double crtTime;
		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;
		}
		if (timeEPOCH <= crtTime)
			return i;
	}

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

3e723802   Benjamin Renard   Improve SpeasyPro...
429
FileReaderStatus FileReaderCDF::getParamPacketData(std::string& timeId, std::string& paramId, int crtIndex, int dependNumber,
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
		SpeasyProxyParamDataPacket *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 SpeasyProxyParamType::TYPE_FLOAT:
			packetValueSize = sizeof(float);
			break;
		case SpeasyProxyParamType::TYPE_DOUBLE:
			packetValueSize = sizeof(double);
			break;
		case SpeasyProxyParamType::TYPE_SHORT:
			packetValueSize = sizeof(short);
			break;
		case SpeasyProxyParamType::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 (!timeId.empty()) {
c62ae28e   Furkan   For now
464
		if (!getCDFData(timeId, crtIndex, CDFWorkingBuffers::BufferType::BUFFER_TIME, dataTimeType, numTimeBytes, nbTimeRec, dimTimeSize))
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Cannot get time var data for " << timeId);
			return FRS_ERROR;
		}

		if ((dataTimeType != CDF_EPOCH) && (dataTimeType != CDF_EPOCH16) && (dataTimeType != CDF_TIME_TT2000))
		{
			LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Bad time format " << timeId);
			return FRS_ERROR;
		}
	}
	else {
		nbTimeRec = 1;
	}

a0567216   Benjamin Renard   Fix some bugs
480
481
482
483
    if (nbTimeRec == 0) {
        return FRS_EOF;
    }

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
484
	//get data
3e723802   Benjamin Renard   Improve SpeasyPro...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    std::string paramToGet = paramId;
    if (dependNumber > 0) {
        paramToGet = "";
        CDFVarInfo varInfo;
        if (getCDFVarInfo(paramId, varInfo)){
            if (dependNumber == 1) {
                paramToGet = std::string(varInfo._depend1);
            }
            else if (dependNumber == 2) {
                paramToGet = std::string(varInfo._depend2);
            }
        }
    }

    if (paramToGet.empty()) {
        LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Cannot determine param to get");
        return FRS_ERROR;
    }

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
504
505
506
507
	long nbRec;
	long dimSize;
	long dataType;
	long numBytes;
3e723802   Benjamin Renard   Improve SpeasyPro...
508
	if (!getCDFData(paramToGet, crtIndex, CDFWorkingBuffers::BufferType::BUFFER_DATA, dataType, numBytes, nbRec, dimSize))
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
509
510
511
512
513
514
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getParamPacketData - Cannot get var data for " << paramId);
		return FRS_ERROR;
	}

	//check dimension integrity
a0567216   Benjamin Renard   Fix some bugs
515
    if (dimSize != packet->getDimsSize())
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
516
	{
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
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
604
605
606
607
608
609
610
611
612
613
614
615
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
		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)
	{
		double crtTime = 0;
		if (!timeId.empty()) {
			//time
			double crtTimeEPOCH;
			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;
			}

			crtTime = epochTimeToTimeStamp(crtTimeEPOCH);
		}

		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 SpeasyProxyParamType::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 SpeasyProxyParamType::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 SpeasyProxyParamType::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 SpeasyProxyParamType::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);
}

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);
}

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;
a0567216   Benjamin Renard   Fix some bugs
708
    if (!getCDFVarInfo(varId, varInfo))
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
709
710
711
712
713
714
715
716
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFData - Cannot get param info " << varId);
		return false;
	}

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

a0567216   Benjamin Renard   Fix some bugs
717
    if (varInfo._numElts > 1)
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
718
719
720
721
722
723
724
	{
		//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;
	}

a0567216   Benjamin Renard   Fix some bugs
725
    if ((varInfo._maxRecNum < 0) && (varInfo._recVary != 0))
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
726
727
728
		//no record in file
		return true;

a0567216   Benjamin Renard   Fix some bugs
729
    nbRec = varInfo._maxRecNum - startRec + 1;
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
730
731
732
733
734
735

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

a0567216   Benjamin Renard   Fix some bugs
736
    _workingBuffers->reallocBuffer(bufferType, nbRec*varInfo._numBytes*dimSize);
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
737
738
739
740

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

a0567216   Benjamin Renard   Fix some bugs
741
    if (status != CDF_OK)
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
742
743
744
745
746
	{
		LOG4CXX_ERROR(gLogger, "FileReaderCDF::getCDFData - Error to get data for " << varId);
		return false;
	}

a0567216   Benjamin Renard   Fix some bugs
747
    return true;
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
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
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
}

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;
a0567216   Benjamin Renard   Fix some bugs
828
    if (extractInt(bufferType, recIndex, dimIndex, dimSize,
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
			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;
}

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;
}

/*
 * @brief Get an information
 */
bool FileReaderCDF::getInfo(const char* pInfoName, std::vector<double>& res)
{
	LOG4CXX_DEBUG(gLogger, "FileReaderCDF::getInfo");

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

	//get CDF var info
	CDFVarInfo varInfo;
a0567216   Benjamin Renard   Fix some bugs
935
936
    if (getCDFVarInfo(pInfoName, varInfo)) {
    	scoped_lock.unlock();
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
		std::string emptyTimeStr = "";
		std::string paramName = pInfoName;
		SpeasyProxyParamType paramType;
		int dim1Size, dim2Size;
		getParamInfo(paramName, paramType, dim1Size, dim2Size);

		SpeasyProxyParamDataPacket packet;

		SpeasyProxyContainerType containerType = CONTAINER_SCALAR;
		if ((dim1Size > 1) && (dim2Size <= 1))
			containerType = CONTAINER_VECTOR;
		else if ((dim1Size > 1) && (dim2Size > 1))
			containerType = CONTAINER_MATRIX;

		packet.init(containerType,paramType,dim1Size,dim2Size);
3e723802   Benjamin Renard   Improve SpeasyPro...
952
		getParamPacketData(emptyTimeStr, paramName, 0, 0, &packet);
a0567216   Benjamin Renard   Fix some bugs
953
        if (packet.getNbData() > 0) {
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
			for (int i = 0; i < dim1Size; ++i) {
				for (int j = 0; j < dim2Size; ++j) {
					switch (paramType)
					{
						case TYPE_FLOAT :
						{
							float f = 0.;
							packet.getDataValue(&f, 0, i, j);
							res.push_back(f);
							break;
						}
						case TYPE_DOUBLE :
						{
							double d = 0;;
							packet.getDataValue(&d, 0, i, j);
							res.push_back(d);
							break;
						}
						case TYPE_SHORT :
						{
							short s = 0;
							packet.getDataValue(&s, 0, i, j);
							res.push_back(s);
							break;
						}
						case TYPE_INT :
						{
							int d = 0;
							packet.getDataValue(&d, 0, i, j);
							res.push_back(d);
							break;
						}
						default:
							return false;
					}
				}
			}
			packet.free();
			return true;
		}
	}
a0567216   Benjamin Renard   Fix some bugs
995
    
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
996
997
998
999
1000
	return false;
}

} /* SpeasyProxyInterface */
} /* AMDA */