Blame view

src/ParamOutputImpl/Download/FileWriterASCIIAbstract.cc 20.4 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
/**
 *
 *  Created on: 03 oct. 2014
 *      Author: AKKA
 */

#include "FileWriterASCIIAbstract.hh"
#include "TimeUtil.hh"
29f304b1   Benjamin Renard   Define Not A Numb...
9
#include "ParamData.hh"
fbe3c2bb   Benjamin Renard   First commit
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

#include <limits>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdlib.h>

namespace AMDA {
namespace ParamOutputImpl {
namespace Download {
namespace FileWriter {

#define ASCII_TIME_ID "AMDA_TIME"

FileWriterASCIIAbstract::FileWriterASCIIAbstract(AMDA::Parameters::ParameterManager& pParameterManager) :
		FileWriter(pParameterManager),
		_outputFilePath(""),
		_writeInfo(true)
{
	//Environment variable used to hide all info blocs
	char *hideHeader;
	if((hideHeader = getenv("HIDE_HEADER_FILE")) != NULL)
		if (strcmp(hideHeader,"true") == 0)
			_writeInfo = false;
}

FileWriterASCIIAbstract::~FileWriterASCIIAbstract(void)
{
}

bool FileWriterASCIIAbstract::createNewFile(std::string filePath)
{
	//close previous opened file
	closeFile();

	//open file stream
	_outputFile.open(filePath);

	if (!_outputFile.is_open())
		return false;

	//write begin file tag
	writeBeginFile();

	//write begin description tag if needed
	if (_writeInfo)
		writeBeginGeneralDescription();

	_outputFilePath = filePath;

	return true;
}

void FileWriterASCIIAbstract::closeFile(void)
{
	if (_outputFile.is_open())
	{
		//write end file tag
		writeEndFile();
		//close file stream
		_outputFile.flush();
		_outputFile.close();
		//re-init writer
		_outputFilePath = "";
		cleanTempFiles();
		_outputParamsList.clear();
	}
}

bool FileWriterASCIIAbstract::addParameter(ParamProperties* paramProp, AMDA::Common::ParameterIndexComponentList &indexList,
		FileDataType type, bool isFirstParam, int dim1Size, int dim2Size)
{
	if (!_outputFile.is_open() || !paramProp)
		return false;

	if (isFirstParam)
	{
		//add time field description
		_fieldInfoMap[ASCII_TIME_ID].name     = "Time";
		_fieldInfoMap[ASCII_TIME_ID].ucd      = "time.epoch";
		_fieldInfoMap[ASCII_TIME_ID].type     = DT_TIME;
		_fieldInfoMap[ASCII_TIME_ID].dimSize1 = 1;
		_fieldInfoMap[ASCII_TIME_ID].dimSize2 = 1;
		_fieldInfoMap[ASCII_TIME_ID].componentsList.clear();
	}

	//get current parameter
	AMDA::Parameters::ParameterSPtr crtParam = _parameterManager.getParameter(paramProp->getOutputId());

	if (crtParam == nullptr)
		return false;

	//get current param info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(crtParam->getInfoId());

	//init field working structure
	_fieldInfoMap[paramProp->getOutputId()].name     = paramInfo->getName();
	_fieldInfoMap[paramProp->getOutputId()].ucd      = paramInfo->getUcd();
	_fieldInfoMap[paramProp->getOutputId()].type     = type;
	_fieldInfoMap[paramProp->getOutputId()].dimSize1 = dim1Size;
	_fieldInfoMap[paramProp->getOutputId()].dimSize2 = dim2Size;
	_fieldInfoMap[paramProp->getOutputId()].componentsList.clear();
	for (auto component : indexList)
		_fieldInfoMap[paramProp->getOutputId()].componentsList.push_back(component);

	//temporary file for this parameter
	std::string paramFileName = _outputFilePath;
	paramFileName += "_";
	paramFileName += paramProp->getOutputId();
	_fieldInfoMap[paramProp->getOutputId()].filePath = paramFileName;
	_fieldInfoMap[paramProp->getOutputId()].file     = new std::fstream();
	_fieldInfoMap[paramProp->getOutputId()].file->open(paramFileName, std::fstream::out);
	if (!_fieldInfoMap[paramProp->getOutputId()].file->is_open())
		return false;

	//ordered list of parameters
	_outputParamsList.push_back(paramProp->getOutputId());

	return true;
}

4f3abe6a   Benjamin Renard   Give the possibil...
131
132
133
134
135
bool FileWriterASCIIAbstract::isInfoInSeparateFile(bool separateInfoFile, bool onlyOneInterval, OutputStructure outputStructure)
{ 	
	if(separateInfoFile) {
         	return true;
	}	
fbe3c2bb   Benjamin Renard   First commit
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
	if (onlyOneInterval)
		return false;
	return ((outputStructure == ONE_FILE_PER_INTERVAL)         ||
			(outputStructure == ONE_FILE_PER_INTERVAL_REFPARAM)||
			(outputStructure == ONE_FILE_PER_PARAMETER_PER_INTERVAL));
}

bool FileWriterASCIIAbstract::writeError(std::string msg)
{
	if (!_outputFile.is_open())
		return false;

	writeErrorInfo(msg);

	if (_writeInfo)
		writeEndGeneralDescription();

	return true;
}

bool FileWriterASCIIAbstract::writeAMDAInfo(std::string version, std::string createdby, std::string acknowledgement)
{
	writeBeginInfoGroup("AMDA INFO",0);
	writeSingleInfo(AMDA_ABOUT, createdby, 0);
	writeSingleInfo(AMDA_VERSION, version, 0);
	writeSingleInfo(AMDA_ACKNOWLEDGEMENT, acknowledgement, 0);
	writeEndInfoGroup(0);
	return true;
}

bool FileWriterASCIIAbstract::writeRequestInfo(std::string structure, std::string timeFormat,
897858c8   Benjamin Renard   Support multi TT ...
167
		int timeResolution, std::string outputParams, std::string ttName)
fbe3c2bb   Benjamin Renard   First commit
168
169
170
171
172
173
174
175
176
177
178
{
	writeBeginInfoGroup("REQUEST INFO",0);
	writeSingleInfo(REQUEST_STRUCTURE, structure, 0);
	writeSingleInfo(REQUEST_TIMEFORMAT, timeFormat, 0);
	if (timeResolution > 0)
	{
		std::stringstream timeRes;
		timeRes << timeResolution;
		writeSingleInfo(REQUEST_TIMERES, timeRes.str(), 0);
	}

897858c8   Benjamin Renard   Support multi TT ...
179
180
	if (!ttName.empty())
		writeSingleInfo(REQUEST_TTNAME, ttName, 0);
fbe3c2bb   Benjamin Renard   First commit
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	writeSingleInfo(REQUEST_OUTPUTPARAMS, outputParams, 0);

	writeEndInfoGroup(0);
	return true;
}

bool FileWriterASCIIAbstract::writeMissionInfo(MissionInfoSPtr missionInfo,
		std::set<std::pair<std::string,ParamInfoSPtr>,FileWriter::cmpParamInfoStruct>& baseParamsInfoList)
{
	std::string missionId;

	writeBeginInfoGroup("",0);
	if (missionInfo == nullptr)
	{
		writeSingleInfo(MISSION_ID, "NONE", 0);
		missionId = "";
	}
	else
	{
		//write info about mission
		std::vector<std::pair<std::string,std::string>> infoMap = missionInfo->getInfoMap();

		for (auto info : infoMap)
			writeSingleInfo(info.first, info.second, 0);

		missionId = missionInfo->getId();
	}

	//build list of instruments to write
	bool writeInstrumentWithoutInfo = false;
093107ce   Benjamin Renard   Fix order of miss...
211
	std::set<std::pair<std::string,InstrumentInfoSPtr>, FileWriter::cmpInstrumentInfoStruct> instrumentInfoList;
fbe3c2bb   Benjamin Renard   First commit
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	for (auto paramInfo : baseParamsInfoList)
	{
		DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(paramInfo.second->getDatasetId());

		if (!dataSetInfo)
			continue;

		InstrumentInfoSPtr instrumentInfo	= InstrumentMgr::getInstance()->getInstrumentInfoFromId(dataSetInfo->getInstrumentId());

		if (!instrumentInfo)
		{
			if (missionId.empty())
				writeInstrumentWithoutInfo = true;
			continue;
		}

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

		if ((!mission && missionId.empty()) ||
			(mission && (mission->getId() == missionId)))
093107ce   Benjamin Renard   Fix order of miss...
232
233
234
235
236
237
238
239
240
241
242
243
244
		{
			bool toAdd = true;
			for (auto existingInstrumentInfo : instrumentInfoList)
			{
				if (existingInstrumentInfo.first.compare(instrumentInfo->getId()) == 0)
				{
					toAdd = false;
					break;
				}
			}
			if (toAdd)
				instrumentInfoList.insert(std::pair<std::string,AMDA::Info::InstrumentInfoSPtr>(instrumentInfo->getId(), instrumentInfo));
		}
fbe3c2bb   Benjamin Renard   First commit
245
246
247
248
249
	}

	//write instruments info
	writeBeginInfoList("INSTRUMENTS", 0);
	for (auto instrumentInfo : instrumentInfoList)
093107ce   Benjamin Renard   Fix order of miss...
250
		writeInstrumentInfo(instrumentInfo.second, baseParamsInfoList);
fbe3c2bb   Benjamin Renard   First commit
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

	if (writeInstrumentWithoutInfo)
		writeInstrumentInfo(InstrumentInfoSPtr(), baseParamsInfoList);
	writeEndInfoList();

	writeEndInfoGroup(0);

	return true;
}

bool FileWriterASCIIAbstract::writeInstrumentInfo(InstrumentInfoSPtr instrumentInfo,
		std::set<std::pair<std::string,ParamInfoSPtr>,FileWriter::cmpParamInfoStruct>& baseParamsInfoList)
{
	std::string instrumentId;

	writeBeginInfoGroup("",1);
	if (instrumentInfo == nullptr)
	{
		writeSingleInfo(INSTRUMENT_ID, "NONE", 1);
		instrumentId = "";
	}
	else
	{
		//write info about instrument
		std::vector<std::pair<std::string,std::string>> infoMap = instrumentInfo->getInfoMap();

		for (auto info : infoMap)
			writeSingleInfo(info.first, info.second, 1);

		instrumentId = instrumentInfo->getId();
	}


	//write list of dataset info to write
	bool writeDatasetWithoutInfo = false;
093107ce   Benjamin Renard   Fix order of miss...
286
	std::set<std::pair<std::string,DataSetInfoSPtr>, FileWriter::cmpDatasetInfoStruct> datasetInfoList;
fbe3c2bb   Benjamin Renard   First commit
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	for (auto paramInfo : baseParamsInfoList)
	{
		DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(paramInfo.second->getDatasetId());

		if (!dataSetInfo)
		{
			if (instrumentId.empty())
				writeDatasetWithoutInfo = true;
			continue;
		}

		InstrumentInfoSPtr instrumentInfo	= InstrumentMgr::getInstance()->getInstrumentInfoFromId(dataSetInfo->getInstrumentId());

		if ((!instrumentInfo && instrumentId.empty()) ||
			(instrumentInfo && (instrumentInfo->getId() == instrumentId)))
093107ce   Benjamin Renard   Fix order of miss...
302
303
304
305
306
307
308
309
310
311
312
313
314
		{
			bool toAdd = true;
			for (auto existingDatasetInfo : datasetInfoList)
			{
				if (existingDatasetInfo.first.compare(dataSetInfo->getId()) == 0)
				{
					toAdd = false;
					break;
				}
			}
			if (toAdd)
				datasetInfoList.insert(std::pair<std::string,AMDA::Info::DataSetInfoSPtr>(dataSetInfo->getId(), dataSetInfo));
		}
fbe3c2bb   Benjamin Renard   First commit
315
316
317
318
319
	}

	//write datasets info
	writeBeginInfoList("DATASETS", 1);
	for (auto datasetInfo : datasetInfoList)
093107ce   Benjamin Renard   Fix order of miss...
320
		writeDatasetInfo(datasetInfo.second, baseParamsInfoList);
fbe3c2bb   Benjamin Renard   First commit
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

	if (writeDatasetWithoutInfo)
		writeDatasetInfo(DataSetInfoSPtr(), baseParamsInfoList);
	writeEndInfoList();

	writeEndInfoGroup(1);

	return true;
}

bool FileWriterASCIIAbstract::writeDatasetInfo(DataSetInfoSPtr datasetInfo,
		std::set<std::pair<std::string,ParamInfoSPtr>,FileWriter::cmpParamInfoStruct>& baseParamsInfoList)
{
	std::string datasetId;

	writeBeginInfoGroup("", 2);
	if (datasetInfo == nullptr)
	{
		writeSingleInfo(DATASET_ID, "NONE", 2);
		datasetId = "";
	}
	else
	{
		//write info about datasetId
		std::vector<std::pair<std::string,std::string>> infoMap = datasetInfo->getInfoMap();

		for (auto info : infoMap)
			writeSingleInfo(info.first, info.second, 2);

		datasetId = datasetInfo->getId();
	}


	//build parameters info to write
	std::set<std::pair<std::string,ParamInfoSPtr>,FileWriter::cmpParamInfoStruct> paramInfoList;
	for (auto paramInfo : baseParamsInfoList)
	{
		DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(paramInfo.second->getDatasetId());

		if ((!dataSetInfo && datasetId.empty()) ||
			(dataSetInfo && (dataSetInfo->getId() == datasetId)))
			paramInfoList.insert(paramInfo);
	}

	//write parameters info
	writeBeginInfoList("PARAMETERS", 2);
	for (auto paramInfo : paramInfoList)
	{
		writeParameterInfo(paramInfo.second, 3);
	}
	writeEndInfoList();

	writeEndInfoGroup(2);

	return true;
}

bool FileWriterASCIIAbstract::writeParameterInfo(ParamInfoSPtr parameterInfo, int level)
{
	if (parameterInfo == nullptr)
		return false;

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

	writeBeginInfoGroup("",level);

	for (auto info : infoMap)
		writeSingleInfo(info.first, info.second, level);

	writeEndInfoGroup(level);

	return true;
}

bool FileWriterASCIIAbstract::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
	if (!derivedParamsInfoList.empty())
	{
		writeBeginInfoGroup("DERIVED PARAMETERS",0);

		writeBeginInfoList("PARAMETERS",0);
		for (auto paramInfo : derivedParamsInfoList)
			writeParameterInfo(paramInfo.second,0);
		writeEndInfoList();

		writeEndInfoGroup(0);
	}

	//get list of all missions
093107ce   Benjamin Renard   Fix order of miss...
426
	std::set<std::pair<std::string,MissionInfoSPtr>, FileWriter::cmpMissionInfoStruct> missionInfoList;
fbe3c2bb   Benjamin Renard   First commit
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
	bool atLeastOneParamWithoutMissionInfo = false;
	for (auto paramInfo : baseParamsInfoList)
	{
		DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(paramInfo.second->getDatasetId());

		if (!dataSetInfo)
		{
			atLeastOneParamWithoutMissionInfo = true;
			continue;
		}

		InstrumentInfoSPtr instrumentInfo	= InstrumentMgr::getInstance()->getInstrumentInfoFromId(dataSetInfo->getInstrumentId());

		if (!instrumentInfo)
		{
			atLeastOneParamWithoutMissionInfo = true;
			continue;
		}

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

		if (!mission)
		{
			atLeastOneParamWithoutMissionInfo = true;
			continue;
		}

093107ce   Benjamin Renard   Fix order of miss...
454
455
456
457
458
459
460
461
462
463
464
		bool toAdd = true;
		for (auto existingMissionInfo : missionInfoList)
		{
			if (existingMissionInfo.first.compare(mission->getId()) == 0)
			{
				toAdd = false;
				break;
			}
		}
		if (toAdd)
			missionInfoList.insert(std::pair<std::string,AMDA::Info::MissionInfoSPtr>(mission->getId(), mission));
fbe3c2bb   Benjamin Renard   First commit
465
466
467
468
469
470
471
472
473
474
	}

	//write the base parameters tree
	writeBeginInfoGroup("BASE PARAMETERS",0);

	//write missions
	writeBeginInfoList("MISSIONS", 0);
	for (auto missionInfo : missionInfoList)
	{
		//Add mission info
093107ce   Benjamin Renard   Fix order of miss...
475
		writeMissionInfo(missionInfo.second, baseParamsInfoList);
fbe3c2bb   Benjamin Renard   First commit
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
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
	}

	//if at least one parameter without mission, write NO MISSION INFO
	if (atLeastOneParamWithoutMissionInfo)
		writeMissionInfo(MissionInfoSPtr(), baseParamsInfoList);
	writeEndInfoList();

	writeEndInfoGroup(0);

	return true;
}

bool FileWriterASCIIAbstract::writeIntervalInfo(std::string startTime, std::string stopTime)
{
	writeBeginInfoGroup("INTERVAL INFO",0);
	writeSingleInfo(INTERVAL_START, startTime, 0);
	writeSingleInfo(INTERVAL_STOP, stopTime, 0);
	writeEndInfoGroup(0);
	return true;
}

bool FileWriterASCIIAbstract::writeTimeData(std::string paramId, double data, OutputFormatTime timeFormat, bool isFirstParam)
{
	if (!isFirstParam)
		return true; //time already write for this record, no error

	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;

	(*crtFile) << getDataStartTag(true);

	switch (timeFormat)
	{
	case FORMAT_OUTPUT_TIME_ISO:
		TimeUtil::formatTimeDateInIso(data, *crtFile);
		break;
	case FORMAT_OUTPUT_TIME_DD:
		//two fill characters to preserve tests validation
		(*crtFile) << getDataFillCharacter() << getDataFillCharacter();
		TimeUtil::double2DD_TimeDate(data, *crtFile);
		break;
	case FORMAT_OUTPUT_TIME_DOUBLE:
	default:
		//one fill character to preserve tests validation
		(*crtFile) << getDataFillCharacter() << std::scientific << std::setprecision(8) << data;
		break;
	}

	(*crtFile) << getDataStopTag();

	return true;
}

bool FileWriterASCIIAbstract::writeFloatData(std::string paramId, int varIndex, float data, bool precision)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	if (varIndex == 0)
		(*crtFile) << getDataStartTag(false);
	else
		(*crtFile) << getDataValueSeparator();
	crtFile->precision(5);
	(*crtFile) << getDataFillCharacter();
	crtFile->fill(' ');
	if (!precision)
	{
		crtFile->width(7);
		crtFile->setf(std::ios::fixed, std::ios::floatfield);
		crtFile->setf(std::ios::right, std::ios::adjustfield);
		crtFile->precision(3);
	}
9d8ec7d4   Benjamin Renard   Define a specific...
549
550
551
552
	if (isNAN(data))
		(*crtFile) << getNanString();
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
	return true;
}

bool FileWriterASCIIAbstract::writeShortData(std::string paramId, int varIndex, short data, bool precision)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	if (varIndex == 0)
		(*crtFile) << getDataStartTag(false);
	else
		(*crtFile) << getDataValueSeparator();
	(*crtFile) << getDataFillCharacter();
	crtFile->fill(' ');
	if (!precision)
		crtFile->width(7);
29f304b1   Benjamin Renard   Define Not A Numb...
569
	if (isNAN(data))
9d8ec7d4   Benjamin Renard   Define a specific...
570
		(*crtFile) << getNanString();
29f304b1   Benjamin Renard   Define Not A Numb...
571
572
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
	return true;
}

bool FileWriterASCIIAbstract::writeIntData(std::string paramId, int varIndex, int data, bool precision)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	if (varIndex == 0)
		(*crtFile) << getDataStartTag(false);
	else
		(*crtFile) << getDataValueSeparator();
	(*crtFile) << getDataFillCharacter();
	crtFile->fill(' ');
	if (!precision)
		crtFile->width(7);
29f304b1   Benjamin Renard   Define Not A Numb...
589
	if (isNAN(data))
9d8ec7d4   Benjamin Renard   Define a specific...
590
		(*crtFile) << getNanString();
29f304b1   Benjamin Renard   Define Not A Numb...
591
592
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
	return true;
}

bool FileWriterASCIIAbstract::writeDoubleData(std::string paramId, int varIndex, double data, bool precision)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	if (varIndex == 0)
		(*crtFile) << getDataStartTag(false);
	else
		(*crtFile) << getDataValueSeparator();
	//five fill characters to preserve tests validation
	(*crtFile) << getDataFillCharacter() << getDataFillCharacter() << getDataFillCharacter() <<
			getDataFillCharacter() << getDataFillCharacter();
	crtFile->fill(' ');
	if (!precision)
	{
		crtFile->width(10);
		crtFile->setf(std::ios::right, std::ios::adjustfield);
		crtFile->precision(3);
		//crtFile->precision(std::numeric_limits<double>::digits10 + 1);
9d8ec7d4   Benjamin Renard   Define a specific...
615
616
617
618
619
620
621
622
623
624
		if (isNAN(data))
			(*crtFile) << std::fixed << getNanString();
		else
			(*crtFile) << std::fixed << data;
	}
	else {
		if (isNAN(data))
			(*crtFile) << getNanString();
		else
			(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
625
	}
fbe3c2bb   Benjamin Renard   First commit
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
	return true;
}

bool FileWriterASCIIAbstract::writeLongDoubleData(std::string paramId, int varIndex, long double data, bool precision)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	if (varIndex == 0)
		(*crtFile) << getDataStartTag(false);
	else
		(*crtFile) << getDataValueSeparator();
	//five fill characters to preserve tests validation
	(*crtFile) << getDataFillCharacter() << getDataFillCharacter() << getDataFillCharacter() <<
			getDataFillCharacter() << getDataFillCharacter();
	crtFile->fill(' ');
	if (!precision)
	{
		crtFile->width(10);
		crtFile->setf(std::ios::right, std::ios::adjustfield);
		crtFile->precision(3);
	}
9d8ec7d4   Benjamin Renard   Define a specific...
648
649
650
651
	if (isNAN(data))
		(*crtFile) << getNanString();
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
	return true;
}

bool FileWriterASCIIAbstract::writeLogicalData(std::string paramId, int varIndex, AMDA::Parameters::LogicalData data, bool precision)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	if (varIndex == 0)
		(*crtFile) << getDataStartTag(false);
	else
		(*crtFile) << getDataValueSeparator();
	(*crtFile) << getDataFillCharacter();
	(*crtFile).fill(' ');
	if (!precision)
		crtFile->width(6);
29f304b1   Benjamin Renard   Define Not A Numb...
668
	if (isNAN(data))
9d8ec7d4   Benjamin Renard   Define a specific...
669
		(*crtFile) << getNanString();
29f304b1   Benjamin Renard   Define Not A Numb...
670
671
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
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
	return true;
}


bool FileWriterASCIIAbstract::goToNextRecord(std::string paramId)
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	(*crtFile) << getDataStopTag() << std::endl;

	return true;
}

bool FileWriterASCIIAbstract::finalize(bool isInfoFile)
{
	if (!_outputFile.is_open())
	{
		cleanTempFiles();
		return false;
	}

	if (isInfoFile && _writeInfo)
	{
		writeEndGeneralDescription();
		return true;
	}

	if (_fieldInfoMap.empty())
	{
		cleanTempFiles();
		return false;
	}

	//set position to the beginning of all parameters files
	for (auto paramId : _outputParamsList)
	{
		if (!_fieldInfoMap[paramId].file->is_open())
		{
			cleanTempFiles();
			return false;
		}
		_fieldInfoMap[paramId].file->flush();
		_fieldInfoMap[paramId].file->close();
		//reopen the file in read mode
		_fieldInfoMap[paramId].file->open(_fieldInfoMap[paramId].filePath, std::fstream::in);
	}

	if (_writeInfo)
	{
		writeEndGeneralDescription();

		writeBeginFieldsDescription();
		writeFieldDescription(ASCII_TIME_ID);
		for (auto paramId : _outputParamsList)
			writeFieldDescription(paramId);
		writeEndFieldsDescription();
	}

	//Merge result and push it in output file
	writeBeginData();
	std::string loadedLine;
	bool finished = false;
	do {
		std::stringstream resultLine;
		for (auto paramId : _outputParamsList)
		{
			std::getline((*_fieldInfoMap[paramId].file), loadedLine);
			if (finished && !(*_fieldInfoMap[paramId].file).eof())
			{
				//files seems to not have the same number of lines!!
				cleanTempFiles();
				return false;
			}
			finished = (*_fieldInfoMap[paramId].file).eof();
			resultLine << loadedLine;
		}
		if (!resultLine.str().empty())
			writeDataRecord(resultLine.str());
	} while (!finished);
	cleanTempFiles();
	writeEndData();

	return true;
}

void FileWriterASCIIAbstract::cleanTempFiles()
{
	for (auto fieldInfo : _fieldInfoMap)
	{
		if (fieldInfo.second.file != NULL)
		{
			if (fieldInfo.second.file->is_open())
			{
				fieldInfo.second.file->flush();
				fieldInfo.second.file->close();
			}
			delete fieldInfo.second.file;
			fieldInfo.second.file = NULL;
		}
	}

	for (auto fieldInfo : _fieldInfoMap)
		remove(fieldInfo.second.filePath.c_str());

	_fieldInfoMap.clear();
	_outputParamsList.clear();
}

} /* namespace FileWriter */
} /* namespace Download */
} /* namespace ParamOutputImpl */
} /* namespace AMDA */