Blame view

src/ParamOutputImpl/Download/FileWriterASCIIAbstract.cc 21.8 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

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

ffb71385   Benjamin Renard   Limit filename si...
17
18
19
#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>

fbe3c2bb   Benjamin Renard   First commit
20
21
22
23
24
namespace AMDA {
namespace ParamOutputImpl {
namespace Download {
namespace FileWriter {

fbe3c2bb   Benjamin Renard   First commit
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
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
59034873   Hacene SI HADJ MOHAND   8675 ok
89
90
91
92
93
94
		_fieldInfoMap[_outputFormatTime].name     = "Time";
		_fieldInfoMap[_outputFormatTime].ucd      = "time.epoch";
		_fieldInfoMap[_outputFormatTime].type     = DT_TIME;
		_fieldInfoMap[_outputFormatTime].dimSize1 = 1;
		_fieldInfoMap[_outputFormatTime].dimSize2 = 1;
		_fieldInfoMap[_outputFormatTime].componentsList.clear();
fbe3c2bb   Benjamin Renard   First commit
95
96
97
98
99
100
101
102
103
104
105
106
107
108
	}

	//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();
f534aaeb   Benjamin Renard   Add unit for fiel...
109
	_fieldInfoMap[paramProp->getOutputId()].unit     = paramInfo->getUnits();
fbe3c2bb   Benjamin Renard   First commit
110
111
112
113
114
115
116
117
	_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
ffb71385   Benjamin Renard   Limit filename si...
118
119
120
121
122
123
	boost::filesystem::path p{_outputFilePath};
	std::string paramFileName = p.parent_path().string();
	paramFileName += boost::filesystem::path::preferred_separator;
	boost::hash<std::string> string_hash;
	paramFileName += std::to_string(string_hash(p.filename().string()));
	//_outputFilePath;
fbe3c2bb   Benjamin Renard   First commit
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	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...
138
139
140
141
142
bool FileWriterASCIIAbstract::isInfoInSeparateFile(bool separateInfoFile, bool onlyOneInterval, OutputStructure outputStructure)
{ 	
	if(separateInfoFile) {
         	return true;
	}	
fbe3c2bb   Benjamin Renard   First commit
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
	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 ...
174
		int timeResolution, std::string outputParams, std::string ttName)
fbe3c2bb   Benjamin Renard   First commit
175
176
177
178
179
180
181
182
183
184
185
{
	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 ...
186
187
	if (!ttName.empty())
		writeSingleInfo(REQUEST_TTNAME, ttName, 0);
fbe3c2bb   Benjamin Renard   First commit
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
	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...
218
	std::set<std::pair<std::string,InstrumentInfoSPtr>, FileWriter::cmpInstrumentInfoStruct> instrumentInfoList;
fbe3c2bb   Benjamin Renard   First commit
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
	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...
239
240
241
242
243
244
245
246
247
248
249
250
251
		{
			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
252
253
254
255
256
	}

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

	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...
293
	std::set<std::pair<std::string,DataSetInfoSPtr>, FileWriter::cmpDatasetInfoStruct> datasetInfoList;
fbe3c2bb   Benjamin Renard   First commit
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	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...
309
310
311
312
313
314
315
316
317
318
319
320
321
		{
			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
322
323
324
325
326
	}

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

	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;
}
65414a1c   Hacene SI HADJ MOHAND   working for mav
401
402
403
404
405
406
407
408
409
410
411
412
bool FileWriterASCIIAbstract::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)
		writeSingleInfo(info.first, info.second, level);
    }
	return true;
}
fbe3c2bb   Benjamin Renard   First commit
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

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...
445
	std::set<std::pair<std::string,MissionInfoSPtr>, FileWriter::cmpMissionInfoStruct> missionInfoList;
fbe3c2bb   Benjamin Renard   First commit
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
	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...
473
474
475
476
477
478
479
480
481
482
483
		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
484
485
486
487
488
489
490
491
492
493
	}

	//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...
494
		writeMissionInfo(missionInfo.second, baseParamsInfoList);
fbe3c2bb   Benjamin Renard   First commit
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
	}

	//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;
5b387233   Vincent Cephirins   RM7926 - Evol - C...
537
538
539
540
        case FORMAT_OUTPUT_TIME_SPACES:
                TimeUtil:: formatTimeDateWithSpaces(data, *crtFile);
                break;
        case FORMAT_OUTPUT_TIME_MS:
31dbec88   Benjamin Renard   date spaces ok
541
		//one fill character to preserve tests validation
1a57f44a   Hacene SI HADJ MOHAND   setting precision
542
		(*crtFile) << getDataFillCharacter() << std::scientific << std::setprecision(12) << data;
5b387233   Vincent Cephirins   RM7926 - Evol - C...
543
                break;
fbe3c2bb   Benjamin Renard   First commit
544
545
546
	case FORMAT_OUTPUT_TIME_DOUBLE:
	default:
		//one fill character to preserve tests validation
1a57f44a   Hacene SI HADJ MOHAND   setting precision
547
		(*crtFile) << getDataFillCharacter() << std::scientific << std::setprecision(9) <<(double)(int) data;
fbe3c2bb   Benjamin Renard   First commit
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
		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);
	}
8bed959f   Benjamin Renard   Trim data value i...
575
	else if (!trimData()) {
954b4c5b   Benjamin Renard   Fix floating fiel...
576
577
		crtFile->width(12);
	}
9d8ec7d4   Benjamin Renard   Define a specific...
578
579
580
581
	if (isNAN(data))
		(*crtFile) << getNanString();
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
	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...
598
	if (isNAN(data))
9d8ec7d4   Benjamin Renard   Define a specific...
599
		(*crtFile) << getNanString();
29f304b1   Benjamin Renard   Define Not A Numb...
600
601
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
	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...
618
	if (isNAN(data))
9d8ec7d4   Benjamin Renard   Define a specific...
619
		(*crtFile) << getNanString();
29f304b1   Benjamin Renard   Define Not A Numb...
620
621
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
	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...
644
645
646
647
648
649
650
651
652
653
		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
654
	}
fbe3c2bb   Benjamin Renard   First commit
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
	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...
677
678
679
680
	if (isNAN(data))
		(*crtFile) << getNanString();
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
	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...
697
	if (isNAN(data))
9d8ec7d4   Benjamin Renard   Define a specific...
698
		(*crtFile) << getNanString();
29f304b1   Benjamin Renard   Define Not A Numb...
699
700
	else
		(*crtFile) << data;
fbe3c2bb   Benjamin Renard   First commit
701
702
703
704
	return true;
}


7929a8a9   Benjamin Renard   CDF writer optimi...
705
bool FileWriterASCIIAbstract::goToNextRecord(std::string paramId, bool /*isFirstParam*/)
fbe3c2bb   Benjamin Renard   First commit
706
707
708
709
710
711
712
713
714
{
	std::fstream *crtFile = _fieldInfoMap[paramId].file;
	if (crtFile == NULL)
		return false;
	(*crtFile) << getDataStopTag() << std::endl;

	return true;
}

7929a8a9   Benjamin Renard   CDF writer optimi...
715
716
717
718
719
bool FileWriterASCIIAbstract::flushData(std::string /*paramId*/, bool /*isFirstParam*/)
{
	return true;
}

fbe3c2bb   Benjamin Renard   First commit
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
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();
59034873   Hacene SI HADJ MOHAND   8675 ok
759
		writeFieldDescription(_outputFormatTime);
fbe3c2bb   Benjamin Renard   First commit
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
		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 */