Blame view

src/ParamOutputImpl/Download/FileWriterCDF.cc 22.1 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
/**
 * FileWriterCDF.cc
 *
 *  Created on: 08 oct. 2014
 *      Author: AKKA
 */

#include "FileWriterCDF.hh"
#include "TimeUtil.hh"

#include "FileWriter.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "InstrumentMgr.hh"
#include "MissionMgr.hh"

#include <sstream>
#include <cstring>
#include <stdlib.h>
9f975da0   Erdogan Furkan   CDF_ISTP working ...
20
21
#include <limits.h>
#include <float.h>
fbe3c2bb   Benjamin Renard   First commit
22
#include <boost/cast.hpp>
c2e1eb0f   Erdogan Furkan   More compliant now
23
#include <boost/filesystem.hpp>
fbe3c2bb   Benjamin Renard   First commit
24

1a6e8df3   Erdogan Furkan   7875 - time inter...
25
namespace AMDA
fbe3c2bb   Benjamin Renard   First commit
26
{
1a6e8df3   Erdogan Furkan   7875 - time inter...
27
	namespace ParamOutputImpl
fbe3c2bb   Benjamin Renard   First commit
28
	{
1a6e8df3   Erdogan Furkan   7875 - time inter...
29
		namespace Download
fbe3c2bb   Benjamin Renard   First commit
30
		{
1a6e8df3   Erdogan Furkan   7875 - time inter...
31
32
			namespace FileWriter
			{
7929a8a9   Benjamin Renard   CDF writer optimi...
33

1a6e8df3   Erdogan Furkan   7875 - time inter...
34
#define CDF_TIME_VAR "AMDA_TIME"
fbe3c2bb   Benjamin Renard   First commit
35

1a6e8df3   Erdogan Furkan   7875 - time inter...
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
				using namespace AMDA::Info;

				FileWriterCDF::FileWriterCDF(AMDA::Parameters::ParameterManager &pParameterManager) : FileWriter(pParameterManager),
																									  _cdfid(NULL)
				{
				}

				FileWriterCDF::~FileWriterCDF(void)
				{
					closeFile();
				}

				std::string FileWriterCDF::getExtension(void)
				{
					return "cdf";
				}

				void FileWriterCDF::closeFile(void)
				{
					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					if (_cdfid != NULL)
						CDFcloseCDF(_cdfid);
					_cdfid = NULL;
					for (auto cdfVar : _cdfVarMap)
					{
						if (cdfVar.second.buffer != NULL)
						{
							free(cdfVar.second.buffer);
							cdfVar.second.buffer = NULL;
						}
1e085406   Furkan   Adding ValidMin/M...
66
67
68
69
70
71
72
73
74
75
						if (cdfVar.second.validMin != NULL)
						{
							free(cdfVar.second.validMin);
							cdfVar.second.validMin = NULL;
						}
						if (cdfVar.second.validMax != NULL)
						{
							free(cdfVar.second.validMax);
							cdfVar.second.validMax = NULL;
						}
1a6e8df3   Erdogan Furkan   7875 - time inter...
76
77
78
79
					}
					_cdfVarMap.clear();
				}

256d4fda   Furkan   The end is close
80
				bool FileWriterCDF::createNewFile(std::string filePath, std::string filePrefix)
1a6e8df3   Erdogan Furkan   7875 - time inter...
81
82
83
84
85
86
87
88
89
90
91
92
				{
					if (_cdfid != NULL)
					{
						closeFile();
					}

					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					CDFstatus status;

					// remove old file if exist
					remove(filePath.c_str());

c2e1eb0f   Erdogan Furkan   More compliant now
93
94
95
96
					// get File Name for Global attributes

					boost::filesystem::path p(filePath);
					_fileName =  p.filename().c_str() ;
256d4fda   Furkan   The end is close
97
					_filePrefix = filePrefix;
c2e1eb0f   Erdogan Furkan   More compliant now
98

1a6e8df3   Erdogan Furkan   7875 - time inter...
99
100
101
102
103
104
105
					// CDFcreateCDF add automatically ".cdf" extension
					status = CDFcreateCDF((char *)filePath.c_str(), &_cdfid);

					return (status == CDF_OK);
				}

				bool FileWriterCDF::addParameter(ParamProperties *paramProp, AMDA::Common::ParameterIndexComponentList &indexList,
5291eadc   Erdogan Furkan   For now - Ca avance
106
												 FileDataType type, bool isFirstParam, int dim1Size, int dim2Size, bool isTableParam)
1a6e8df3   Erdogan Furkan   7875 - time inter...
107
108
109
110
111
				{
					if (isFirstParam)
					{
						if (!createCDFVar(CDF_TIME_VAR, DT_TIME, 1, 1, _cdfVarMap[CDF_TIME_VAR]))
							return false;
c2e1eb0f   Erdogan Furkan   More compliant now
112
						writeTimeInfo();
1a6e8df3   Erdogan Furkan   7875 - time inter...
113
114
					}

5291eadc   Erdogan Furkan   For now - Ca avance
115
					return addCDFParameter(paramProp->getOutputId(), indexList, type, dim1Size, dim2Size, isTableParam);
1a6e8df3   Erdogan Furkan   7875 - time inter...
116
117
118
				}

				bool FileWriterCDF::addCDFParameter(std::string outputId, AMDA::Common::ParameterIndexComponentList &indexList,
5291eadc   Erdogan Furkan   For now - Ca avance
119
													FileDataType type, int dim1Size, int dim2Size, bool isTableParam)
1a6e8df3   Erdogan Furkan   7875 - time inter...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
				{
					std::stringstream paramIdStream;
					paramIdStream << outputId;
					int varDim1Size = dim1Size;
					int varDim2Size = dim2Size;
					if ((type != DT_TIME) && !indexList.empty() && (dim1Size * dim2Size != (int)indexList.size()))
					{
						paramIdStream << "_" << indexList.size();
						varDim1Size = indexList.size();
						varDim2Size = 1;
					}

					std::map<std::string, ParamCDFVar>::iterator it = _cdfVarMap.find(outputId);
					if (it != _cdfVarMap.end())
						return true; // already exist

					return createCDFVar(paramIdStream.str().c_str(), type,
5291eadc   Erdogan Furkan   For now - Ca avance
137
										varDim1Size, varDim2Size, _cdfVarMap[outputId], false, isTableParam);
1a6e8df3   Erdogan Furkan   7875 - time inter...
138
139
140
				}

				bool FileWriterCDF::createCDFVar(const char *name, FileDataType type, long varDim1Size, long varDim2Size,
5291eadc   Erdogan Furkan   For now - Ca avance
141
												 ParamCDFVar &cdfVar, bool isConst,  bool isTableParam)
1a6e8df3   Erdogan Furkan   7875 - time inter...
142
143
144
145
146
147
148
				{
					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					if (_cdfid == NULL)
						return false;

					cdfVar.dim1Size = varDim1Size;
					cdfVar.dim2Size = varDim2Size;
5291eadc   Erdogan Furkan   For now - Ca avance
149
					cdfVar.isTableParam = isTableParam;
1a6e8df3   Erdogan Furkan   7875 - time inter...
150
151
152
153
154

					switch (type)
					{
					case DT_FLOAT:
						cdfVar.type = CDF_FLOAT;
1a6e8df3   Erdogan Furkan   7875 - time inter...
155
156
157
						break;
					case DT_SHORT:
						cdfVar.type = CDF_INT2;
1a6e8df3   Erdogan Furkan   7875 - time inter...
158
159
160
						break;
					case DT_INT:
						cdfVar.type = CDF_INT4;
1a6e8df3   Erdogan Furkan   7875 - time inter...
161
162
163
164
						break;
					case DT_DOUBLE:
					case DT_LONGDOUBLE:
						cdfVar.type = CDF_DOUBLE;
1a6e8df3   Erdogan Furkan   7875 - time inter...
165
166
167
						break;
					case DT_LOGICAL:
						cdfVar.type = CDF_UINT2;
1a6e8df3   Erdogan Furkan   7875 - time inter...
168
169
						break;
					case DT_TIME:
5a693c10   Erdogan Furkan   For now
170
171
172
173
174
175
176
177
178
179
180
181
						switch (_outputFormatTime) {
							
							case OutputFormatTime::FORMAT_OUTPUT_TIME_EPOCH16:
								cdfVar.type = CDF_EPOCH16;
								break;
							case OutputFormatTime::FORMAT_OUTPUT_TIME_TT2000:
								cdfVar.type = CDF_TIME_TT2000;
								break;
							case OutputFormatTime::FORMAT_OUTPUT_TIME_EPOCH:
							default:
								cdfVar.type = CDF_EPOCH;						
						}
993426a6   Erdogan Furkan   TIME2000 included
182
						break;
1a6e8df3   Erdogan Furkan   7875 - time inter...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
					case DT_NONE:
						cdfVar.type = CDF_CHAR;
						break;
					default:
						return false;
					}

					cdfVar.recNum = 0;

					long numDims;
					long *dimSizes = NULL;
					long *dimVarys = NULL;

					if (varDim2Size > 1)
					{
						numDims = 2L;
						dimSizes = new long[2];
						dimSizes[0] = varDim1Size;
						dimSizes[1] = varDim2Size;
						dimVarys = new long[2];
						dimVarys[0] = VARY;
						dimVarys[1] = VARY;
					}
					else
5a693c10   Erdogan Furkan   For now
207
					{	
1a6e8df3   Erdogan Furkan   7875 - time inter...
208
209
210
211
212
213
						numDims = (varDim1Size > 1) ? 1L : 0L;
						dimSizes = new long[1];
						dimSizes[0] = varDim1Size;
						dimVarys = new long[1];
						dimVarys[0] = VARY;
					}
5a693c10   Erdogan Furkan   For now
214
					
1a6e8df3   Erdogan Furkan   7875 - time inter...
215
216
					long recVary = {VARY};
					long numElts = 1;
5a693c10   Erdogan Furkan   For now
217
218
					if (isConst)
						recVary= {NOVARY};
1a6e8df3   Erdogan Furkan   7875 - time inter...
219
220
221
222
223
224
225

					CDFstatus status = CDFgetDataTypeSize(cdfVar.type, &cdfVar.numBytes);
					if (status != CDF_OK)
					{
						return false;
					}
					cdfVar.buffer = malloc(varDim1Size * varDim2Size * cdfVar.numBytes * NB_RECORDS_BUFFER);
1e085406   Furkan   Adding ValidMin/M...
226
227
					cdfVar.validMin = malloc(cdfVar.numBytes);
					cdfVar.validMax = malloc(cdfVar.numBytes);
1a6e8df3   Erdogan Furkan   7875 - time inter...
228

1e085406   Furkan   Adding ValidMin/M...
229
230
231
232
233
234
235
					switch (cdfVar.type){
						case CDF_FLOAT:
						{
							float validMin = -std::numeric_limits<float>::max();
							float validMax = std::numeric_limits<float>::max();
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
236
							cdfVar.format="F11.4";
1e085406   Furkan   Adding ValidMin/M...
237
238
239
240
241
242
243
244
						}
							break;
						case CDF_INT2:
						{
							short validMin = -std::numeric_limits<short>::max();
							short validMax = std::numeric_limits<short>::max();
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
245
							cdfVar.format="I";
1e085406   Furkan   Adding ValidMin/M...
246
247
248
249
250
251
252
253
						}
							break;
						case CDF_INT4:
						{
							int validMin = -std::numeric_limits<int>::max();
							int validMax = std::numeric_limits<int>::max();
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
254
							cdfVar.format="I";
1e085406   Furkan   Adding ValidMin/M...
255
256
257
258
259
260
261
262
						}
							break; 
						case CDF_DOUBLE:
						{
							double validMin = -std::numeric_limits<double>::max();
							double validMax = std::numeric_limits<double>::max();
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
263
							cdfVar.format="F14.3";
1e085406   Furkan   Adding ValidMin/M...
264
265
266
267
268
269
270
271
						}
							break; 
						case CDF_UINT2:
						{
							unsigned short validMin = 0;
							unsigned short validMax = std::numeric_limits<unsigned short>::max();
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
272
							cdfVar.format="I";
1e085406   Furkan   Adding ValidMin/M...
273
274
275
276
277
278
279
280
281
282
						}
							break; 
						case CDF_EPOCH16: 
						{
							double validMin[2] = {0,0};
							double validMax[2] = {0,0};
							computeEPOCH16(1970,1,1,0,0,0,0,0,0,0,validMin);
							computeEPOCH16(2050,12,31,23,59,59,999,999,999,999,validMax);
							memcpy(cdfVar.validMin,validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
283
							cdfVar.format="I";
1e085406   Furkan   Adding ValidMin/M...
284
285
286
287
288
289
290
291
						}
							break; 
						case CDF_TIME_TT2000:
						{
							long long validMin = computeTT2000(1970,1,1,TT2000END);
							long long validMax = computeTT2000(2050,12,31,TT2000END);
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
292
							cdfVar.format="I";
1e085406   Furkan   Adding ValidMin/M...
293
294
295
296
297
298
299
300
						}
							break; 
						case CDF_EPOCH:
						{
							double validMin = computeEPOCH(1970,1,1,0,0,0,0);
							double validMax = computeEPOCH(2050,12,31,23,59,59,999);
							memcpy(cdfVar.validMin,&validMin,cdfVar.numBytes);
							memcpy(cdfVar.validMax,&validMax,cdfVar.numBytes);
256d4fda   Furkan   The end is close
301
							cdfVar.format="I";
1e085406   Furkan   Adding ValidMin/M...
302
303
304
						}
							break;
					}
1a6e8df3   Erdogan Furkan   7875 - time inter...
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
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
475
476
477
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
					status = CDFcreatezVar(_cdfid, name, cdfVar.type, numElts, numDims,
										   dimSizes, recVary, dimVarys, &cdfVar.varNum);

					delete[] dimSizes;
					delete[] dimVarys;

					return (status == CDF_OK);
				}

				bool FileWriterCDF::isInfoInSeparateFile(bool /* separateInfoFile */, bool /* onlyOneInterval */, OutputStructure /* outputStructure */)
				{
					return false;
				}

				bool FileWriterCDF::writeError(std::string msg)
				{
					addAttribute("AMDA_ERROR", msg, GLOBAL_SCOPE);
					return true;
				}

				bool FileWriterCDF::writeAMDAInfo(std::string version, std::string createdby, std::string acknowledgement)
				{
					addAttribute(AMDA_ABOUT, createdby, GLOBAL_SCOPE);
					addAttribute(AMDA_VERSION, version, GLOBAL_SCOPE);
					addAttribute(AMDA_ACKNOWLEDGEMENT, acknowledgement, GLOBAL_SCOPE);
					return true;
				}

				bool FileWriterCDF::writeRequestInfo(std::string structure, std::string /* timeFormat */,
													 int timeResolution, std::string outputParams, std::string ttName)
				{
					addAttribute(REQUEST_STRUCTURE, structure, GLOBAL_SCOPE);
					addAttribute(REQUEST_TIMEFORMAT, "EPOCH", GLOBAL_SCOPE);
					if (timeResolution > 0)
					{
						std::stringstream timeRes;
						timeRes << timeResolution;
						addAttribute(REQUEST_TIMERES, timeRes.str(), GLOBAL_SCOPE);
					}
					addAttribute(REQUEST_OUTPUTPARAMS, outputParams, GLOBAL_SCOPE);
					if (!ttName.empty())
						addAttribute(REQUEST_TTNAME, ttName, GLOBAL_SCOPE);
					return true;
				}

				bool FileWriterCDF::writeMissionInfo(MissionInfoSPtr missionInfo, long varNum)
				{
					if (missionInfo == nullptr)
						return false;

					// write info about mission
					std::vector<std::pair<std::string, std::string>> infoMap = missionInfo->getInfoMap();

					for (auto info : infoMap)
						addAttribute(info.first, info.second, VARIABLE_SCOPE, varNum);
					return true;
				}

				bool FileWriterCDF::writeInstrumentInfo(InstrumentInfoSPtr instrumentInfo, long varNum)
				{
					if (instrumentInfo == nullptr)
						return false;

					// write info about instrument
					std::vector<std::pair<std::string, std::string>> infoMap = instrumentInfo->getInfoMap();

					for (auto info : infoMap)
						addAttribute(info.first, info.second, VARIABLE_SCOPE, varNum);

					MissionInfoSPtr mission = MissionMgr::getInstance()->getMissionInfoFromId(instrumentInfo->getMissionId());
					writeMissionInfo(mission, varNum);

					return true;
				}

				bool FileWriterCDF::writeDatasetInfo(DataSetInfoSPtr datasetInfo, long varNum)
				{
					if (datasetInfo == nullptr)
						return false;

					// write info about datasetId
					std::vector<std::pair<std::string, std::string>> infoMap = datasetInfo->getInfoMap();

					for (auto info : infoMap)
						addAttribute(info.first, info.second, VARIABLE_SCOPE, varNum);

					InstrumentInfoSPtr instrumentInfo = InstrumentMgr::getInstance()->getInstrumentInfoFromId(datasetInfo->getInstrumentId());
					writeInstrumentInfo(instrumentInfo, varNum);

					return true;
				}

				bool FileWriterCDF::writeParameterInfo(ParamInfoSPtr parameterInfo, long varNum)
				{
					if (parameterInfo == nullptr)
						return false;

					std::vector<std::pair<std::string, std::string>> infoMap = parameterInfo->getInfoMap(&_parameterManager);

					for (auto info : infoMap)
						addAttribute(info.first, info.second, VARIABLE_SCOPE, varNum);

					DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(parameterInfo->getDatasetId());
					writeDatasetInfo(dataSetInfo, varNum);

					return true;
				}
				bool FileWriterCDF::writeTableParamsInfo(std::vector<std::string> tableParams, int level)
				{
					if (tableParams.empty())
						return false;
					for (auto tableId : tableParams)
					{
						// get current param info
						AMDA::Info::ParamInfoSPtr tableInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(tableId);
						std::vector<std::pair<std::string, std::string>> tableInfoMap = tableInfo->getTableInfoMap(&_parameterManager);
						for (auto info : tableInfoMap)
							addAttribute(info.first, info.second, VARIABLE_SCOPE, level);
						DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(tableInfo->getDatasetId());
						writeDatasetInfo(dataSetInfo, level);
					}
					return true;
				}

				bool FileWriterCDF::writeParamsInfo(ParamPropertiesList &paramPropertiesList, OutputStructure outputStructure,
													std::string currentParamId)
				{
					std::set<std::pair<std::string, ParamInfoSPtr>, FileWriter::cmpParamInfoStruct> baseParamsInfoList;
					std::set<std::pair<std::string, ParamInfoSPtr>, FileWriter::cmpParamInfoStruct> derivedParamsInfoList;

					// sort parameters info
					for (auto paramProperties : paramPropertiesList)
					{
						if ((outputStructure == ONE_FILE_PER_PARAMETER_PER_INTERVAL) &&
							(currentParamId != paramProperties->getOutputId()))
							// add info only for current parameter
							continue;
						sortParametersInfoByType(paramProperties->getOutputId(),
												 baseParamsInfoList, derivedParamsInfoList);
					}

					// write derived parameters info
					for (auto paramInfo : derivedParamsInfoList)
					{
						std::map<std::string, ParamCDFVar>::iterator it = _cdfVarMap.find(paramInfo.first);
						if (it == _cdfVarMap.end())
						{
							createCDFVar(paramInfo.first.c_str(), DT_NONE, 1, 1,
										 _cdfVarMap[paramInfo.first]);
							addAttribute("AMDA_INFO", "Derived parameter description. No data output.", VARIABLE_SCOPE, _cdfVarMap[paramInfo.first].varNum);
						}

						writeParameterInfo(paramInfo.second, _cdfVarMap[paramInfo.first].varNum);
					}

					// write base parameters info
					for (auto paramInfo : baseParamsInfoList)
					{
						std::map<std::string, ParamCDFVar>::iterator it = _cdfVarMap.find(paramInfo.first);
						if (it == _cdfVarMap.end())
						{
							createCDFVar(paramInfo.first.c_str(), DT_NONE, 1, 1,
										 _cdfVarMap[paramInfo.first]);
							addAttribute("AMDA_INFO", "Base parameter description. No data output.", VARIABLE_SCOPE, _cdfVarMap[paramInfo.first].varNum);
						}
						writeParameterInfo(paramInfo.second, _cdfVarMap[paramInfo.first].varNum);
					}

					return true;
				}

				bool FileWriterCDF::writeIntervalInfo(std::string startTime, std::string stopTime, bool isSeveralIntervals)
				{

					if (!isSeveralIntervals)
					{
						addAttribute(INTERVAL_START, startTime, GLOBAL_SCOPE);
						addAttribute(INTERVAL_STOP, stopTime, GLOBAL_SCOPE);
						return true;
					}
					else
					{
						addAttribute(INTERVAL_MIN_START, startTime, GLOBAL_SCOPE);
						addAttribute(INTERVAL_MAX_STOP, stopTime, GLOBAL_SCOPE);
						return true;
					}
				}

				bool FileWriterCDF::addAttribute(std::string attName, std::string attVal,
												 long scope, long varNum)
				{
					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					if (_cdfid == NULL)
						return false;

					CDFstatus status;

					long attNum = CDFgetAttrNum(_cdfid, (char *)attName.c_str());

					if (attNum < 0)
					{
						status = CDFcreateAttr(_cdfid, (char *)attName.c_str(), scope, &attNum);

						if (status != CDF_OK)
							return false;
					}

					if (scope == GLOBAL_SCOPE)
						status = CDFputAttrgEntry(_cdfid, attNum, 0, CDF_CHAR, attVal.size(), (void *)attVal.c_str());
					else
						status = CDFputAttrzEntry(_cdfid, attNum, varNum, CDF_CHAR, attVal.size(), (void *)attVal.c_str());

					return (status == CDF_OK);
				}

c2e1eb0f   Erdogan Furkan   More compliant now
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
				bool FileWriterCDF::addAttribute(std::string attName, double attVal,
												 long scope, long varNum)
				{
					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					if (_cdfid == NULL)
						return false;

					CDFstatus status;

					long attNum = CDFgetAttrNum(_cdfid, (char *)attName.c_str());

					if (attNum < 0)
					{
						status = CDFcreateAttr(_cdfid, (char *)attName.c_str(), scope, &attNum);

						if (status != CDF_OK)
							return false;
					}

					if (scope == GLOBAL_SCOPE)
						status = CDFputAttrgEntry(_cdfid, attNum, 0, CDF_DOUBLE, 1, (void *)&attVal);
					else
						status = CDFputAttrzEntry(_cdfid, attNum, varNum, CDF_DOUBLE, 1, (void *)&attVal);

					return (status == CDF_OK);
				}

1e085406   Furkan   Adding ValidMin/M...
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
				bool FileWriterCDF::addAttribute(std::string attName, void* attBuffer,
												 long scope, long type, long varNum)
				{
					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					if (_cdfid == NULL)
						return false;

					CDFstatus status;

					long attNum = CDFgetAttrNum(_cdfid, (char *)attName.c_str());

					if (attNum < 0)
					{
						status = CDFcreateAttr(_cdfid, (char *)attName.c_str(), scope, &attNum);

						if (status != CDF_OK)
							return false;
					}

					if (scope == GLOBAL_SCOPE)
						status = CDFputAttrgEntry(_cdfid, attNum, 0, type, 1, attBuffer);
					else
						status = CDFputAttrzEntry(_cdfid, attNum, varNum, type, 1, attBuffer);

					return (status == CDF_OK);
				}

c2e1eb0f   Erdogan Furkan   More compliant now
574

1a6e8df3   Erdogan Furkan   7875 - time inter...
575
576
577
578
579
580
581
582
				bool FileWriterCDF::writeTimeData(std::string /* paramId */, double data, OutputFormatTime /* timeFormat */, bool isFirstParam)
				{
					if (_cdfid == NULL)
						return false;

					if (!isFirstParam)
						return true;

5a693c10   Erdogan Furkan   For now
583
584
585
586
					switch (_outputFormatTime) {
							
						case OutputFormatTime::FORMAT_OUTPUT_TIME_EPOCH16:
						{
5a693c10   Erdogan Furkan   For now
587
							double epochTime;
45291948   Benjamin Renard   Fix time conversi...
588
							UnixTimetoEPOCH16(&data, &epochTime, 1);
5a693c10   Erdogan Furkan   For now
589
590
591
592
							return writeOneData(CDF_TIME_VAR, 0, &epochTime);
						}
						case OutputFormatTime::FORMAT_OUTPUT_TIME_TT2000:
						{
45291948   Benjamin Renard   Fix time conversi...
593
594
							long long epochTime = 0;
							UnixTimetoTT2000(&data, &epochTime, 1);
5a693c10   Erdogan Furkan   For now
595
596
597
598
599
600
							return writeOneData(CDF_TIME_VAR, 0, &epochTime);
						}
						case OutputFormatTime::FORMAT_OUTPUT_TIME_EPOCH:
						default:
						{
							double epochTime;
45291948   Benjamin Renard   Fix time conversi...
601
							UnixTimetoEPOCH(&data, &epochTime, 1);
5a693c10   Erdogan Furkan   For now
602
603
604
							return writeOneData(CDF_TIME_VAR, 0, &epochTime);
						}					
					}
1a6e8df3   Erdogan Furkan   7875 - time inter...
605
				}
c2e1eb0f   Erdogan Furkan   More compliant now
606
				void FileWriterCDF::writeTimeInfo() {}
1a6e8df3   Erdogan Furkan   7875 - time inter...
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
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

				bool FileWriterCDF::writeOneData(std::string paramId, int varIndex, void *data)
				{
					if (_cdfid == NULL)
						return false;

					if (_cdfVarMap[paramId].varNum < 0)
						return false;

					void *pos = _cdfVarMap[paramId].buffer;
					pos = static_cast<char *>(pos) + _cdfVarMap[paramId].bufferNbRec * _cdfVarMap[paramId].dim1Size * _cdfVarMap[paramId].dim2Size * _cdfVarMap[paramId].numBytes + varIndex * _cdfVarMap[paramId].numBytes;

					memcpy(pos, data, _cdfVarMap[paramId].numBytes);

					return true;
				}

				bool FileWriterCDF::flushData(std::string paramId, bool isFirstParam)
				{
					boost::mutex::scoped_lock scoped_lock(ParameterManager::mutexCDFLib);
					if (_cdfid == NULL)
						return false;

					if (_cdfVarMap[paramId].varNum < 0)
						return false;

					CDFstatus status;
					if (isFirstParam)
					{
						if (_cdfVarMap[CDF_TIME_VAR].varNum < 0)
							return false;

						if (_cdfVarMap[CDF_TIME_VAR].bufferNbRec > 0)
						{
							status = CDFputzVarRangeRecordsByVarID(_cdfid, _cdfVarMap[CDF_TIME_VAR].varNum, _cdfVarMap[CDF_TIME_VAR].lastPushRec,
																   _cdfVarMap[CDF_TIME_VAR].lastPushRec + _cdfVarMap[CDF_TIME_VAR].bufferNbRec - 1, _cdfVarMap[CDF_TIME_VAR].buffer);
							if (status != CDF_OK)
							{
								return false;
							}

							_cdfVarMap[CDF_TIME_VAR].lastPushRec += (_cdfVarMap[CDF_TIME_VAR].bufferNbRec);
							_cdfVarMap[CDF_TIME_VAR].bufferNbRec = 0;
						}
					}

					if (_cdfVarMap[paramId].bufferNbRec > 0)
					{
						status = CDFputzVarRangeRecordsByVarID(_cdfid, _cdfVarMap[paramId].varNum, _cdfVarMap[paramId].lastPushRec,
															   _cdfVarMap[paramId].lastPushRec + _cdfVarMap[paramId].bufferNbRec - 1, _cdfVarMap[paramId].buffer);

						if (status != CDF_OK)
						{
							return false;
						}

						_cdfVarMap[paramId].lastPushRec += (_cdfVarMap[paramId].bufferNbRec);
						_cdfVarMap[paramId].bufferNbRec = 0;
					}

					return true;
				}

				bool FileWriterCDF::writeFloatData(std::string paramId, int varIndex, float data, bool /* precision */)
				{
					return writeOneData(paramId, varIndex, &data);
				}

				bool FileWriterCDF::writeShortData(std::string paramId, int varIndex, short data, bool /* precision */)
				{
					return writeOneData(paramId, varIndex, &data);
				}

				bool FileWriterCDF::writeIntData(std::string paramId, int varIndex, int data, bool /* precision */)
				{
					return writeOneData(paramId, varIndex, &data);
				}

				bool FileWriterCDF::writeDoubleData(std::string paramId, int varIndex, double data, bool /* precision */)
				{
					return writeOneData(paramId, varIndex, &data);
				}

				bool FileWriterCDF::writeLongDoubleData(std::string paramId, int varIndex, long double data, bool /* precision */)
				{
					// convert long double to double
					double doubleData = boost::numeric_cast<double>(data);

					return writeOneData(paramId, varIndex, &doubleData);
				}

				bool FileWriterCDF::writeLogicalData(std::string paramId, int varIndex, AMDA::Parameters::LogicalData data, bool /* precision */)
				{
					// convert LogicalData to 2-byte unsigned integer
					unsigned short uShortData;
					switch (data)
					{
					case AMDA::Parameters::LogicalData::False:
						uShortData = 0;
						break;
					case AMDA::Parameters::LogicalData::True:
						uShortData = 1;
						break;
					case AMDA::Parameters::LogicalData::NaN:
						uShortData = 2;
						break;
					default:
						return false;
					}

					return writeOneData(paramId, varIndex, &uShortData);
				}

				bool FileWriterCDF::goToNextRecord(std::string paramId, bool isFirstParam)
				{
					++_cdfVarMap[paramId].recNum;
					++_cdfVarMap[paramId].bufferNbRec;

					if (isFirstParam)
					{
						++_cdfVarMap[CDF_TIME_VAR].recNum;
						++_cdfVarMap[CDF_TIME_VAR].bufferNbRec;
					}

					if (_cdfVarMap[paramId].bufferNbRec >= NB_RECORDS_BUFFER - 1)
					{
						return flushData(paramId, isFirstParam);
					}

					return true;
				}

				bool FileWriterCDF::finalize(bool /* isInfoFile */)
				{
					// nothing to do
					return true;
				}

			} /* namespace FileWriter */
		}	  /* namespace Download */
	}		  /* namespace ParamOutputImpl */
fbe3c2bb   Benjamin Renard   First commit
748
} /* namespace AMDA */