FileWriterCDFISTP.cc 8.02 KB
/**
 * FileWriterCDFISTP.cc
 *
 *  Created on: 28 feb. 2022
 *      Author: AKKA
 */

#include "FileWriterCDFISTP.hh"
#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>
#include <boost/cast.hpp>

#define CDF_TIME_VAR "AMDA_TIME"

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

				using namespace AMDA::Info;

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

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

				std::string FileWriterCDFISTP::getExtension(void)
				{
					return "cdf";
				}
				void FileWriterCDFISTP::addInfoInMap(std::string key, std::string value){
					_infoMap.push_back(std::pair<std::string,std::string>(key,value));
				}
				void FileWriterCDFISTP::writeParamVariableAttributes(ParamInfoSPtr paramInfo, long varNum)
				{
					addInfoInMap("CATDESC", paramInfo->getName());
					addInfoInMap("DATA", "NaN");
					addInfoInMap("DISPLAY_TYPE", "time_series");
					addInfoInMap("FIELDNAM", paramInfo->getId());
					addInfoInMap("FILLVAL", std::to_string(paramInfo->getFillValue()));
					addInfoInMap("FORMAT", "ToDo");
					//addInfoInMap("FORM_PTR", "time_series");
					addInfoInMap("LABLAXIS", paramInfo->getShortName());
					addInfoInMap("LABL_PTR_1", "ToDo");
					addInfoInMap("LABL_PTR_2", "ToDo");
					addInfoInMap("LABL_PTR_3", "ToDo");
					addInfoInMap("REPRESENTATION_i", paramInfo->getComponents());
					addInfoInMap("SI_CONVERSION", paramInfo->getSiConversion());
					addInfoInMap("SIZES", "ToDo");	
					addInfoInMap("TENSOR_FRAME", paramInfo->getCoordinatesSystem());			
					addInfoInMap("TENSOR_ORDER", std::to_string(paramInfo->getTensorOrder()));	
					addInfoInMap("UNITS", paramInfo->getUnits());	
					addInfoInMap("VALID_MIN", "-Inf");	
					addInfoInMap("VALID_MAX", "+Inf");
					addInfoInMap("VALUE_TYPE", "ToDo");
					addInfoInMap("VAR_TYPE", "data");
					addInfoInMap("DEPEND_0", CDF_TIME_VAR);

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

				}
				bool FileWriterCDFISTP::writeTableParamsInfo(std::map<int, boost::shared_ptr<ParamTable>> &dependTables, std::string currentParamId)
				{
					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);
											paramList[i] = minMax.min+(minMax.max-minMax.min)/2;
										}
									
										status = CDFputzVarRecordData(_cdfid, tableNum, 0, (void*) paramList);
										free(paramList);

										addAttribute("LABLAXIS", tableLabel, VARIABLE_SCOPE, tableNum);
										addAttribute("UNITS", tableUnits, VARIABLE_SCOPE, tableNum);
										addAttribute("VAR_TYPE", "support_data", VARIABLE_SCOPE, tableNum);

									}
								}
								else{
									//ParameterSPtr p = _parameterManager.getParameter(currentParamId);
									
									int tableSize = 0;
									if(dependTable.first == 0){
										tableSize = _cdfVarMap[currentParamId].dim1Size;
									}
									else{
										tableSize = _cdfVarMap[currentParamId].dim2Size;
									}

									std::string tableLabel = dependTable.second->getName(&_parameterManager);
									std::string tableName =  currentParamId;
									tableName += "_";
									tableName += tableLabel;

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

									long dimSizes[1] = {tableSize};
									long dimVarys[1] = {VARY};
									long recVary= {VARY};
									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);
									// 		paramList[i] = minMax.min+(minMax.max-minMax.min)/2;
									// 	}
									
									// 	status = CDFputzVarRecordData(_cdfid, tableNum, 0, (void*) paramList);
									// 	free(paramList);

									// 	addAttribute("LABLAXIS", tableLabel, VARIABLE_SCOPE, tableNum);
									// 	addAttribute("UNITS", tableUnits, VARIABLE_SCOPE, tableNum);
									// 	addAttribute("VAR_TYPE", "support_data", VARIABLE_SCOPE, tableNum);

									// }
								}
							}
						}

				}
				bool FileWriterCDFISTP::writeParamsInfo(ParamPropertiesList &paramPropertiesList, OutputStructure outputStructure,
													std::string currentParamId)
				{
					//get parameter data and info
					ParameterSPtr p = _parameterManager.getParameter(currentParamId);
					AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(p->getInfoId());

					// Adding variable attributes
					
					writeParamVariableAttributes(paramInfo, _cdfVarMap[currentParamId].varNum);

					if(paramInfo != nullptr){

						// Adding DEPEND_S, only for non-variable case.

						writeTableParamsInfo(paramInfo->getTables(), currentParamId);
					}
					return true;
				
				}

				/*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 */
} /* namespace AMDA */