FileWriter.hh 6.62 KB
/**
 * FileWriter.hh
 *
 *  Created on: 02 oct. 2014
 *      Author: AKKA
 */

#ifndef FILEWRITER_HH_
#define FILEWRITER_HH_

#include "VisitorOfParamData.hh"
#include "DownloadProperties.hh"
#include "Parameter.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "InstrumentMgr.hh"
#include "MissionMgr.hh"
#include "ParameterIndexesTool.hh"

#include <string>

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

//Enumerate that's define type of a data in a file
typedef enum
{
	DT_NONE,
	DT_FLOAT,
	DT_SHORT,
	DT_INT,
	DT_DOUBLE,
	DT_LONGDOUBLE,
	DT_LOGICAL,
	DT_TIME
} FileDataType;

//Info keys for AMDA bloc
#define AMDA_ABOUT           "AMDA_ABOUT"
#define AMDA_VERSION         "AMDA_VERSION"
#define AMDA_ACKNOWLEDGEMENT "AMDA_ACKNOWLEDGEMENT"

//Info keys for request bloc
#define REQUEST_STRUCTURE    "REQUEST_STRUCTURE"
#define REQUEST_TIMEFORMAT   "REQUEST_TIME_FORMAT"
#define REQUEST_TIMERES      "REQUEST_TIME_RESOLUTION"
#define REQUEST_OUTPUTPARAMS "REQUEST_OUTPUT_PARAMS"
#define REQUEST_TTNAME       "REQUEST_TIMETABLE_NAME"

//Info keys for interval bloc
#define INTERVAL_START "INTERVAL_START"
#define INTERVAL_STOP  "INTERVAL_STOP"

/**
 * @class FileWriter
 * @brief Abstract class use to define a file format writer.
 * @details
 */
class FileWriter
{
public:
	/*
	 * @brief Constructor
	 */
	FileWriter(AMDA::Parameters::ParameterManager& pParameterManager);

	/*
	 * @brief Destruction
	 */
	virtual ~FileWriter(void);

	/*
	 * @brief Return extension for the file format
	 */
	virtual std::string getExtension(void) = 0;

	/*
	 * @brief Return File creation. Close the previous opened file if necessary
	 */
	virtual bool createNewFile(std::string filePath) = 0;

	/*
	 * @brief Close the current opened file
	 */
	virtual void closeFile(void) = 0;

	/*
	 * @brief Add an output parameter in the file
	 */
	virtual bool addParameter(ParamProperties* paramProp, AMDA::Common::ParameterIndexComponentList &indexList,
			FileDataType type, bool isFirstParam, int dim1Size = 1, int dim2Size = 1) = 0;

	/*
	 * @brief Function use to know if info must be write in a separate file
	 */
	virtual bool isInfoInSeparateFile(bool separateInfoFile, bool onlyOneInterval, OutputStructure outputStructure)=0;

	/*
	 * @brief Write an error in current opened file
	 */
	virtual bool writeError(std::string msg) = 0;

	/*
	 * @brief Write an AMDA info bloc in current opened file
	 */
	virtual bool writeAMDAInfo(std::string version, std::string createdby, std::string acknowledgement) = 0;

	/*
	 * @brief Write a request info bloc in current opened file
	 */
	virtual bool writeRequestInfo(std::string structure, std::string timeFormat,
			int timeResolution, std::string outputParams, std::string ttName) = 0;

	/*
	 * @brief Write a parameters info bloc in current opened file
	 */
	virtual bool writeParamsInfo(ParamPropertiesList& paramPropertiesList, OutputStructure outputStructure,
			std::string currentParamId = "") = 0;
        	/*
	 * @brief Write a parameters table info bloc in current opened file
	 */
	virtual bool writeTableParamsInfo(std::vector<std::string> tableParams, int level) = 0;

	/*
	 * @brief Write an interval info bloc in current opened file
	 */
	virtual bool writeIntervalInfo(std::string startTime, std::string stopTime) = 0;

	/*
	 * @brief Write a time data in current opened file
	 */
	virtual bool writeTimeData(std::string paramId, double data, OutputFormatTime timeFormat, bool isFirstParam) = 0;

	/*
	 * @brief Write a float data in current opened file
	 */
	virtual bool writeFloatData(std::string paramId, int varIndex, float data, bool precision) = 0;

	/*
	 * @brief Write a short data in current opened file
	 */
	virtual bool writeShortData(std::string paramId, int varIndex, short data, bool precision) = 0;

	/*
	 * @brief Write a int data in current opened file
	 */
	virtual bool writeIntData(std::string paramId, int varIndex, int data, bool precision) = 0;

	/*
	 * @brief Write a double data in current opened file
	 */
	virtual bool writeDoubleData(std::string paramId, int varIndex, double data, bool precision) = 0;

	/*
	 * @brief Write a long double data in current opened file
	 */
	virtual bool writeLongDoubleData(std::string paramId, int varIndex, long double data, bool precision) = 0;

	/*
	 * @brief Write a logical data in current opened file
	 */
	virtual bool writeLogicalData(std::string paramId, int varIndex, AMDA::Parameters::LogicalData data, bool precision) = 0;

	/*
	 * @brief Flush data in current opened file
	 */
	virtual bool flushData(std::string paramId, bool isFirstParam) = 0;

	/*
	 * @brief Prepare file to receive next data record
	 */
	virtual bool goToNextRecord(std::string paramId, bool isFirstParam) = 0;

	/*
	 * @brief File finalization - Highly dependent of the file format
	 */
	virtual bool finalize(bool isInfoFile = false) = 0;

protected :
	/*
	 * @brief Call back function used to sort ParamInfo list by id
	 */
	struct cmpParamInfoStruct {
	  bool operator() (std::pair<std::string,AMDA::Info::ParamInfoSPtr> const & lhs, std::pair<std::string,AMDA::Info::ParamInfoSPtr> const & rhs) const
	  {
	    return lhs.first > rhs.first;
	  }
	};

	/*
	 * @brief Call back function used to sort DatasetInfo list by id
	*/
	struct cmpDatasetInfoStruct {
		bool operator() (std::pair<std::string,AMDA::Info::DataSetInfoSPtr> const & lhs, std::pair<std::string,AMDA::Info::DataSetInfoSPtr> const & rhs) const
		{
			return lhs.first > rhs.first;
		}
	};

	/*
	 * @brief Call back function used to sort InstrumentInfo list by id
	 */
	struct cmpInstrumentInfoStruct {
		bool operator() (std::pair<std::string,AMDA::Info::InstrumentInfoSPtr> const & lhs, std::pair<std::string,AMDA::Info::InstrumentInfoSPtr> const & rhs) const
		{
			return lhs.first > rhs.first;
		}
	};

	/*
	 * @brief Call back function used to sort MissionInfo list by id
	 */
	struct cmpMissionInfoStruct {
		bool operator() (std::pair<std::string,AMDA::Info::MissionInfoSPtr> const & lhs, std::pair<std::string,AMDA::Info::MissionInfoSPtr> const & rhs) const
		{
			return lhs.first > rhs.first;
		}
	};

	/*
	 * @brief recursive function to sort parameters info in two list: one for base parameters,
	 * and the other one for derived parameters
	 * Parameters info are ordered by id
	 */
	void sortParametersInfoByType(std::string crtParamId,
			std::set<std::pair<std::string,AMDA::Info::ParamInfoSPtr>,cmpParamInfoStruct>& baseParamsInfoList,
			std::set<std::pair<std::string,AMDA::Info::ParamInfoSPtr>,cmpParamInfoStruct>& derivedParamsInfoList);

	AMDA::Parameters::ParameterManager& _parameterManager;
};

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

#endif /* FILEWRITER_HH_ */