Blame view

src/ParamOutputImpl/Download/FileWriterCDFISTP.cc 13.4 KB
9e1bbe22   Benjamin Renard   Skeleton to imple...
1
2
3
4
5
6
7
8
/**
 * FileWriterCDFISTP.cc
 *
 *  Created on: 28 feb. 2022
 *      Author: AKKA
 */

#include "FileWriterCDFISTP.hh"
993426a6   Erdogan Furkan   TIME2000 included
9
#include "FileWriterCDF.hh"
9e1bbe22   Benjamin Renard   Skeleton to imple...
10
11
12
13
14
15
16
17
18
19
20
21
22
#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>
#include <boost/cast.hpp>

993426a6   Erdogan Furkan   TIME2000 included
23
24
#define CDF_TIME_VAR "AMDA_TIME"

9e1bbe22   Benjamin Renard   Skeleton to imple...
25
namespace AMDA {
993426a6   Erdogan Furkan   TIME2000 included
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
	namespace ParamOutputImpl {
		namespace Download {
			namespace FileWriter {

				using namespace AMDA::Info;

				FileWriterCDFISTP::FileWriterCDFISTP(AMDA::Parameters::ParameterManager& pParameterManager) :
					FileWriterCDF(pParameterManager)
				{
				}

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

5a693c10   Erdogan Furkan   For now
42
43
44
				void FileWriterCDFISTP::addInfoInMap(std::string key, std::string value){
					_infoMap.push_back(std::pair<std::string,std::string>(key,value));
				}
c2e1eb0f   Erdogan Furkan   More compliant now
45
46
47

				bool FileWriterCDFISTP::writeAMDAInfo(std::string version, std::string createdby, std::string acknowledgement)
				{
256d4fda   Furkan   The end is close
48
49
					addAttribute("Data_type", "AMDA>Generated by Automated Multi Dataset Analysis tool", GLOBAL_SCOPE);
					addAttribute("Data_version", "01", GLOBAL_SCOPE);
c2e1eb0f   Erdogan Furkan   More compliant now
50
					addAttribute("Acknowledgement", acknowledgement, GLOBAL_SCOPE);
256d4fda   Furkan   The end is close
51
52
53
54
55
56
57
58
					addAttribute("Software_version", version, GLOBAL_SCOPE);
					addAttribute("PI_affiliation", "CDPP", GLOBAL_SCOPE);
					addAttribute("PI_name", "AMDA Team", GLOBAL_SCOPE);
					addAttribute("Project", "AMDA>Automated Multi Dataset Analysis", GLOBAL_SCOPE);
					addAttribute("Source_name", "N/A", GLOBAL_SCOPE);
					addAttribute("TEXT", "N/A", GLOBAL_SCOPE);
					addAttribute("Generated_by", createdby, GLOBAL_SCOPE);
					addAttribute("HTTP_LINK", "http://amda.cdpp.eu/", GLOBAL_SCOPE);
c2e1eb0f   Erdogan Furkan   More compliant now
59
60
61
62
63
64
65
66
					return true;
				}

				bool FileWriterCDFISTP::writeRequestInfo(std::string structure, std::string /* timeFormat */,
													 int timeResolution, std::string outputParams, std::string ttName)
				{

					addAttribute("Descriptor", outputParams, GLOBAL_SCOPE);
256d4fda   Furkan   The end is close
67
					addAttribute("Instrument_type", "Plasma and Solar Wind", GLOBAL_SCOPE);
c2e1eb0f   Erdogan Furkan   More compliant now
68
					addAttribute("Logical_file_id", _fileName, GLOBAL_SCOPE);
256d4fda   Furkan   The end is close
69
70
71
					addAttribute("Logical_source", _filePrefix, GLOBAL_SCOPE);
					addAttribute("Logical_source_description", _filePrefix, GLOBAL_SCOPE);
					addAttribute("Mission_group", "N/A", GLOBAL_SCOPE);
c2e1eb0f   Erdogan Furkan   More compliant now
72
					addAttribute(REQUEST_STRUCTURE, structure, GLOBAL_SCOPE);
256d4fda   Furkan   The end is close
73
74
75
76
77
78
79
					if (timeResolution > 0)
					{
						std::string resolution = std::to_string(timeResolution)+" sec";
						addAttribute("Time_resolution", resolution, GLOBAL_SCOPE);
					}
					if (!ttName.empty())
						addAttribute(REQUEST_TTNAME,ttName, GLOBAL_SCOPE);
c2e1eb0f   Erdogan Furkan   More compliant now
80
81
82
83
84
85
86
					return true;
				}

				void FileWriterCDFISTP::writeTimeInfo()
				{
					int varNum = _cdfVarMap[CDF_TIME_VAR].varNum;
					
256d4fda   Furkan   The end is close
87
					addAttribute("CATDESC", "Epoch", VARIABLE_SCOPE, varNum);
c2e1eb0f   Erdogan Furkan   More compliant now
88
					addAttribute("FIELDNAM", "Time", VARIABLE_SCOPE, varNum);
256d4fda   Furkan   The end is close
89
					addAttribute("FORMAT", _cdfVarMap[CDF_TIME_VAR].format, VARIABLE_SCOPE, varNum);
c2e1eb0f   Erdogan Furkan   More compliant now
90
					addAttribute("VAR_TYPE", "support_data", VARIABLE_SCOPE, varNum);
1e085406   Furkan   Adding ValidMin/M...
91
92

					if(_cdfVarMap[CDF_TIME_VAR].type == CDF_TIME_TT2000){
256d4fda   Furkan   The end is close
93
						addAttribute("FILLVAL", "9999-12-31:23:59:59.999999999", VARIABLE_SCOPE, varNum);
1e085406   Furkan   Adding ValidMin/M...
94
95
						addAttribute("TIME_BASE", "J2000", VARIABLE_SCOPE, varNum);
						addAttribute("SI_CONVERSION", "1.0E-9>Second", VARIABLE_SCOPE, varNum);
256d4fda   Furkan   The end is close
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
						//addAttribute("UNITS", "ns", VARIABLE_SCOPE, varNum);
					}
					else{
						addAttribute("TIME_BASE", "UTC", VARIABLE_SCOPE, varNum);
						if (_cdfVarMap[CDF_TIME_VAR].type == CDF_EPOCH)
						{
							addAttribute("FILLVAL", "9999-12-31:23:59:59.999", VARIABLE_SCOPE, varNum);
							addAttribute("SI_CONVERSION", "1.0E-3>Second", VARIABLE_SCOPE, varNum);
							//addAttribute("UNITS", "ms", VARIABLE_SCOPE, varNum);
						}
						else
						{
							addAttribute("FILLVAL", "9999-12-31:23:59:59.999999999999", VARIABLE_SCOPE, varNum);
							addAttribute("SI_CONVERSION", "1.0E-12>Second", VARIABLE_SCOPE, varNum);
							//addAttribute("UNITS", "ps", VARIABLE_SCOPE, varNum);
						}
1e085406   Furkan   Adding ValidMin/M...
112
					}
1e085406   Furkan   Adding ValidMin/M...
113
114
115

					addAttribute("VALIDMIN", _cdfVarMap[CDF_TIME_VAR].validMin, VARIABLE_SCOPE, _cdfVarMap[CDF_TIME_VAR].type, varNum);
					addAttribute("VALIDMAX", _cdfVarMap[CDF_TIME_VAR].validMax, VARIABLE_SCOPE, _cdfVarMap[CDF_TIME_VAR].type, varNum);
c2e1eb0f   Erdogan Furkan   More compliant now
116
				}
5291eadc   Erdogan Furkan   For now - Ca avance
117
				void FileWriterCDFISTP::writeParamVariableAttributes(ParamInfoSPtr paramInfo, std::string paramId)
5a693c10   Erdogan Furkan   For now
118
				{
5291eadc   Erdogan Furkan   For now - Ca avance
119
					int varNum = _cdfVarMap[paramId].varNum;
5a693c10   Erdogan Furkan   For now
120
					addInfoInMap("CATDESC", paramInfo->getName());
256d4fda   Furkan   The end is close
121
					addInfoInMap("DATA", "N/A");
9f975da0   Erdogan Furkan   CDF_ISTP working ...
122
					if(paramInfo->getTables().size() > 0)
5291eadc   Erdogan Furkan   For now - Ca avance
123
						addInfoInMap("DISPLAY_TYPE", "spectrogram");
9f975da0   Erdogan Furkan   CDF_ISTP working ...
124
					else
5291eadc   Erdogan Furkan   For now - Ca avance
125
						addInfoInMap("DISPLAY_TYPE", "time_series");
256d4fda   Furkan   The end is close
126

9f975da0   Erdogan Furkan   CDF_ISTP working ...
127
					addInfoInMap("FIELDNAM", paramInfo->getShortName());
c2e1eb0f   Erdogan Furkan   More compliant now
128
					addAttribute("FILLVAL", paramInfo->getFillValue(), VARIABLE_SCOPE, varNum);
256d4fda   Furkan   The end is close
129
					addInfoInMap("FORMAT", _cdfVarMap[paramId].format);
79523545   Benjamin Renard   Add LABL_PTR_1 in...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

					std::vector<std::string> components; 
					if ((paramInfo->getTables().size() == 0) && (_cdfVarMap[paramId].dim1Size > 1) && (_cdfVarMap[paramId].dim2Size <= 1)) {
						for (int i = 0; i < _cdfVarMap[paramId].dim1Size; ++i) {
								AMDA::Common::ParameterIndexComponent paramIndex(i, -1);
								std::string component = paramInfo->getComponents(paramIndex);
								if (component.empty()) {
									component = paramInfo->getShortName();
									component += "[";
									component += std::to_string(i);
									component += "]";
								}
								components.push_back(component);
						}
					}
					else {
						addInfoInMap("LABLAXIS", paramInfo->getShortName());
					}
9f975da0   Erdogan Furkan   CDF_ISTP working ...
148
					//addInfoInMap("REPRESENTATION_i", paramInfo->getComponents());
5a693c10   Erdogan Furkan   For now
149
					addInfoInMap("SI_CONVERSION", paramInfo->getSiConversion());
9f975da0   Erdogan Furkan   CDF_ISTP working ...
150
					//addInfoInMap("SIZES", "ToDo");	
5a693c10   Erdogan Furkan   For now
151
152
153
					addInfoInMap("TENSOR_FRAME", paramInfo->getCoordinatesSystem());			
					addInfoInMap("TENSOR_ORDER", std::to_string(paramInfo->getTensorOrder()));	
					addInfoInMap("UNITS", paramInfo->getUnits());	
1e085406   Furkan   Adding ValidMin/M...
154
155
156
					
					addAttribute("VALIDMIN", _cdfVarMap[paramId].validMin, VARIABLE_SCOPE, _cdfVarMap[paramId].type, varNum);
					addAttribute("VALIDMAX", _cdfVarMap[paramId].validMax, VARIABLE_SCOPE, _cdfVarMap[paramId].type, varNum);
9f975da0   Erdogan Furkan   CDF_ISTP working ...
157
158
159
160
161
162
					//addInfoInMap("VALUE_TYPE", "ToDo");
					if (_cdfVarMap[paramId].isTableParam)
						addInfoInMap("VAR_TYPE", "support_data");
					else
						addInfoInMap("VAR_TYPE", "data");

256d4fda   Furkan   The end is close
163
164
					addInfoInMap("VAR_NOTES", paramInfo->getProcessInfo());
					addInfoInMap("DEPEND_0", CDF_TIME_VAR);	
5a693c10   Erdogan Furkan   For now
165
166

					for (auto info : _infoMap)
79523545   Benjamin Renard   Add LABL_PTR_1 in...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
						addAttribute(info.first, info.second, VARIABLE_SCOPE, varNum);

					if (!components.empty()) {
						std::string label_variable = paramId;
						label_variable += "_";
						label_variable += "label";
						addAttribute("LABL_PTR_1", label_variable, VARIABLE_SCOPE, varNum);
						long dimSizes[1] = {(long)components.size()};
						long dimVarys[1] = {VARY};
						long recVary= {NOVARY};
						long labelNum = 0;
						// id,varName,dataType,numElements,numDims,dimSizes,recVary,dimVarys,varNum
						int maxCompSize = 0;
						for (int i = 0; i < (int)components.size(); ++i) {
							maxCompSize = std::max((int)components[i].size(), maxCompSize);
						}
38b5d6f8   Benjamin Renard   Fix
183
						CDFstatus status =  CDFcreatezVar(FileWriterCDF::_cdfid, label_variable.c_str(), CDF_CHAR, maxCompSize, 1L,
79523545   Benjamin Renard   Add LABL_PTR_1 in...
184
185
186
187
188
189
190
191
192
193
194
195
196
												dimSizes, recVary, dimVarys, &labelNum);
						std::string labelName = paramInfo->getName();
						labelName += " - labels";
						addAttribute("CATDESC", labelName, VARIABLE_SCOPE, labelNum);
						addAttribute("DATA", "N/A", VARIABLE_SCOPE, labelNum);
						addAttribute("FIELDNAM", labelName,VARIABLE_SCOPE, labelNum);
						addAttribute("FILLVAL", "nan", VARIABLE_SCOPE, labelNum);
						std::string labelFormat = "A";
						labelFormat += std::to_string(maxCompSize);
						addAttribute("FORMAT", labelFormat, VARIABLE_SCOPE, labelNum);
						addAttribute("VAR_TYPE", "metadata", VARIABLE_SCOPE, labelNum);
						if (status == CDF_OK)
						{
38b5d6f8   Benjamin Renard   Fix
197
							void *buffer = malloc((maxCompSize)*sizeof(char));
79523545   Benjamin Renard   Add LABL_PTR_1 in...
198
199
							long indices[1]; 
							for (int i = 0; i < (int)components.size(); ++i) {
38b5d6f8   Benjamin Renard   Fix
200
								memset(buffer, 0, maxCompSize*sizeof(char));
79523545   Benjamin Renard   Add LABL_PTR_1 in...
201
202
203
204
205
206
207
208
								if (components[i].size() > 0)
									memcpy(buffer, components[i].c_str(), components[i].size()*sizeof(char));
								indices[0] = i;
								status = CDFputzVarData(FileWriterCDF::_cdfid, labelNum, 0, indices, buffer);
							}
							free(buffer); 
						}
					}
5a693c10   Erdogan Furkan   For now
209
210
				}
				bool FileWriterCDFISTP::writeTableParamsInfo(std::map<int, boost::shared_ptr<ParamTable>> &dependTables, std::string currentParamId)
993426a6   Erdogan Furkan   TIME2000 included
211
				{
5a693c10   Erdogan Furkan   For now
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
					for (auto dependTable : dependTables)
						{
							if(dependTable.second != nullptr)
							{
								if (!dependTable.second->isVariable(&_parameterManager))
								{
									std::string tableLabel = dependTable.second->getName(&_parameterManager);
									std::string tableName =  currentParamId;
									tableName += "_";
									tableName += tableLabel;

									int tableSize = dependTable.second->getSize(&_parameterManager);
									std::string tableUnits = dependTable.second->getUnits(&_parameterManager);

									long dimSizes[1] = {tableSize};
									long dimVarys[1] = {VARY};
									long recVary= {NOVARY};
									long tableNum = 0;

									std::string dependName = "DEPEND_";
									dependName += std::to_string(dependTable.first +1);
									addAttribute(dependName, tableName, VARIABLE_SCOPE, _cdfVarMap[currentParamId].varNum);

									// Creation of the new parameter.
									

									CDFstatus status =  CDFcreatezVar(FileWriterCDF::_cdfid, tableName.c_str(), CDF_DOUBLE, 1, 1L,
											dimSizes, recVary, dimVarys, &tableNum);	
									if (status == CDF_OK)
									{
										double* paramList = (double*)malloc(tableSize*sizeof(double));
										for (int i = 0; i <tableSize; ++i ){
											AMDA::Info::t_TableBound minMax =  dependTable.second->getBound(&_parameterManager, i);
5631534a   Erdogan Furkan   Las modifications
245
											paramList[i] = (minMax.max+minMax.min)/2.;
26dc55e4   Erdogan Furkan   For now
246
										}
5a693c10   Erdogan Furkan   For now
247
									
26dc55e4   Erdogan Furkan   For now
248
249
										status = CDFputzVarRecordData(_cdfid, tableNum, 0, (void*) paramList);
										free(paramList);
5a693c10   Erdogan Furkan   For now
250

256d4fda   Furkan   The end is close
251
252
253
254
255
										addAttribute("CATDESC", tableLabel, VARIABLE_SCOPE, tableNum);
										addAttribute("DATA", "N/A", VARIABLE_SCOPE, tableNum);
										addAttribute("FIELDNAM", tableLabel,VARIABLE_SCOPE, tableNum);
										addAttribute("FILLVAL", "nan", VARIABLE_SCOPE, tableNum);
										addAttribute("FORMAT", "F14.3", VARIABLE_SCOPE, tableNum);
26dc55e4   Erdogan Furkan   For now
256
257
										addAttribute("LABLAXIS", tableLabel, VARIABLE_SCOPE, tableNum);
										addAttribute("UNITS", tableUnits, VARIABLE_SCOPE, tableNum);
256d4fda   Furkan   The end is close
258
259
260
261
262
263

										double validMin = -std::numeric_limits<double>::max();
										double validMax = std::numeric_limits<double>::max();
										
										addAttribute("VALIDMIN", (void*)&validMin, VARIABLE_SCOPE, CDF_DOUBLE, tableNum);
										addAttribute("VALIDMAX", (void*)&validMax, VARIABLE_SCOPE, CDF_DOUBLE, tableNum);
26dc55e4   Erdogan Furkan   For now
264
										addAttribute("VAR_TYPE", "support_data", VARIABLE_SCOPE, tableNum);
5a693c10   Erdogan Furkan   For now
265
266
267

									}
								}
26dc55e4   Erdogan Furkan   For now
268
								else{
5291eadc   Erdogan Furkan   For now - Ca avance
269
									AMDA::Parameters::ParameterSPtr paramTable = dependTable.second->createRelatedParameter(&_parameterManager);
26dc55e4   Erdogan Furkan   For now
270
271
272

									std::string dependName = "DEPEND_";
									dependName += std::to_string(dependTable.first +1);
5291eadc   Erdogan Furkan   For now - Ca avance
273
									addAttribute(dependName, paramTable->getId(), VARIABLE_SCOPE, _cdfVarMap[currentParamId].varNum);
26dc55e4   Erdogan Furkan   For now
274
								}
5a693c10   Erdogan Furkan   For now
275
276
							}
						}
9f975da0   Erdogan Furkan   CDF_ISTP working ...
277
					return true;
5a693c10   Erdogan Furkan   For now
278
279
280
281
				}
				bool FileWriterCDFISTP::writeParamsInfo(ParamPropertiesList &paramPropertiesList, OutputStructure outputStructure,
													std::string currentParamId)
				{
993426a6   Erdogan Furkan   TIME2000 included
282

5291eadc   Erdogan Furkan   For now - Ca avance
283
284
285
286
287
288
289
290
291
292
					for (auto paramProperties : paramPropertiesList)
					{
						if ((outputStructure == ONE_FILE_PER_PARAMETER_PER_INTERVAL) &&
							(currentParamId != paramProperties->getOutputId()))
							// add info only for current parameter
							continue;
						ParameterSPtr p = _parameterManager.getParameter(paramProperties->getOutputId());
						AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(p->getInfoId());
						writeParamVariableAttributes(paramInfo, paramProperties->getOutputId());
						if(paramInfo != nullptr){
993426a6   Erdogan Furkan   TIME2000 included
293

5291eadc   Erdogan Furkan   For now - Ca avance
294
							// Adding DEPEND_S, only for non-variable case.
993426a6   Erdogan Furkan   TIME2000 included
295

5291eadc   Erdogan Furkan   For now - Ca avance
296
297
							writeTableParamsInfo(paramInfo->getTables(), paramProperties->getOutputId());
						}
5a693c10   Erdogan Furkan   For now
298
					}
5a693c10   Erdogan Furkan   For now
299
300
					return true;
				
993426a6   Erdogan Furkan   TIME2000 included
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
328
329
330
331
				}

				/*bool FileWriterCDFISTP::writeMissionInfo(MissionInfoSPtr missionInfo, long varNum)
				{
					if (missionInfo == nullptr) {
						std::cout <<  std::endl << std::endl << std::endl <<  "FER - HAHAHAHA" << std::endl << std::endl << std::endl<< std::endl;
						addAttribute("Source_name", "unk>Unknown mission", GLOBAL_SCOPE);
						return false;
					}

					std::vector<std::pair<std::string,std::string>> infoMap = missionInfo->getInfoMap();
					
					for (auto info : infoMap)
						std::cout <<  std::endl << std::endl << std::endl  << "FER - " << &info << std::cout <<  std::endl << std::endl << std::endl << std::endl;
					//for (auto info : infoMap)
					//	if(info.first == "MISSION_NAME")
					//		addAttribute(info.first + ">" + info["MISSION_DESCRIPTION"]);

						//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;
						//                                        }
					return true; 
				}*/
			} /* namespace FileWriter */
		} /* namespace Download */
	} /* namespace ParamOutputImpl */
9e1bbe22   Benjamin Renard   Skeleton to imple...
332
} /* namespace AMDA */