FileWriterASCIIVOTable.cc 5.96 KB
/**
 * FileWriterASCIIVOTable.cc
 *  Created on: 21 oct. 2014
 *      Author: AKKA
 */

#include "FileWriterASCIIVOTable.hh"
#include "TimeUtil.hh"

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

FileWriterASCIIVOTable::FileWriterASCIIVOTable(AMDA::Parameters::ParameterManager& pParameterManager) :
		FileWriterASCIIAbstract(pParameterManager)
{
}

FileWriterASCIIVOTable::~FileWriterASCIIVOTable(void)
{
	if (_outputFile.is_open())
		writeEndFile();
}

bool FileWriterASCIIVOTable::isInfoInSeparateFile(bool /* separateInfoFile */, bool /* onlyOneInterval */, OutputStructure /* outputStructure */)
{
	return false;
}

std::string FileWriterASCIIVOTable::getExtension(void)
{
	return "vot";
}

std::string FileWriterASCIIVOTable::getDataFillCharacter(void)
{
	return "";
}

std::string FileWriterASCIIVOTable::getNanString(void)
{
	return "NaN";
}

void FileWriterASCIIVOTable::writeBeginFile(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "<?xml version=\"1.0\"?>" << std::endl;
	_outputFile << "<VOTABLE version=\"1.3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.ivoa.net/xml/VOTable/v1.3\" xmlns:stc=\"http://www.ivoa.net/xml/STC/v1.30\">" << std::endl;
	_outputFile << "  <RESOURCE name=\"CDPP/AMDA Resource\">" << std::endl;
	_outputFile << "    <TABLE name=\"CDPP/AMDA Table\">" << std::endl;

}

void FileWriterASCIIVOTable::writeEndFile(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "    </TABLE>" << std::endl;
	_outputFile << "  </RESOURCE>" << std::endl;
	_outputFile << "</VOTABLE>" << std::endl;
}

void FileWriterASCIIVOTable::writeBeginGeneralDescription(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "      <DESCRIPTION>" << std::endl;
}

void FileWriterASCIIVOTable::writeEndGeneralDescription(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "      </DESCRIPTION>" << std::endl;
}

void FileWriterASCIIVOTable::writeErrorInfo(std::string msg)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << msg;
}

void FileWriterASCIIVOTable::writeBeginInfoGroup(std::string title, int level)
{
	if (!_outputFile.is_open())
		return;
	if (title.empty() || level > 0)
		_outputFile << std::endl;
	else
		_outputFile << "* " << title << " :" << std::endl;
}

void FileWriterASCIIVOTable::writeEndInfoGroup(int level)
{
	if (!_outputFile.is_open())
		return;
	if (level == 0)
		_outputFile << std::endl;
}

void FileWriterASCIIVOTable::writeSingleInfo(std::string key, std::string value, int level)
{
	if (!_outputFile.is_open())
		return;
	std::string prefix;
	for (int i = 0; i < level; ++i)
		prefix += "  ";
	_outputFile << prefix << key << " : " << value << std::endl;
}

void FileWriterASCIIVOTable::writeBeginInfoList(std::string /*title*/, int /*level*/)
{
	//nothing to do
}

void FileWriterASCIIVOTable::writeEndInfoList(void)
{
	//nothing to do
}

void FileWriterASCIIVOTable::writeBeginFieldsDescription(void)
{
	//nothing to do
}

void FileWriterASCIIVOTable::writeEndFieldsDescription(void)
{
	//nothing to do
}

void FileWriterASCIIVOTable::writeFieldDescription(std::string paramId)
{
	if (!_outputFile.is_open())
		return;

	_outputFile << "      <FIELD name=\"";
	_outputFile << _fieldInfoMap[paramId].name << "\" ID=\"";
	_outputFile << paramId << "\" ucd=\"";
	_outputFile << _fieldInfoMap[paramId].ucd << "\" datatype=\"";

	switch (_fieldInfoMap[paramId].type)
	{
	case DT_FLOAT :
		_outputFile << "float\" utype=\"\"";
		break;
	case DT_SHORT :
	case DT_LOGICAL :
		_outputFile << "short\" utype=\"\"";
		break;
	case DT_INT :
		_outputFile << "int\" utype=\"\"";
		break;
	case DT_DOUBLE :
	case DT_LONGDOUBLE :
		_outputFile << "double\" utype=\"\"";
		break;
	case DT_TIME :
		_outputFile << "char\" xtype=\"dateTime\" utype=\"\"";
		break;
	default:
		_outputFile << "\"";
	}

	_outputFile << " arraysize=\"";
	if ((_fieldInfoMap[paramId].type != DT_TIME) &&
		!_fieldInfoMap[paramId].componentsList.empty() &&
		(_fieldInfoMap[paramId].dimSize1 * _fieldInfoMap[paramId].dimSize2 != (int)_fieldInfoMap[paramId].componentsList.size()))
	{
		//some components are not write
		_outputFile << _fieldInfoMap[paramId].componentsList.size();
	}
	else if (_fieldInfoMap[paramId].type == DT_TIME)
		_outputFile << "*";
	else
	{
		//all components are write
		_outputFile <<  _fieldInfoMap[paramId].dimSize1;
		if (_fieldInfoMap[paramId].dimSize2 > 1)
		{
			_outputFile <<  "x";
			_outputFile <<  _fieldInfoMap[paramId].dimSize2;
		}
	}
	_outputFile << "\"";

	_outputFile << "/>" << std::endl;
}

void FileWriterASCIIVOTable::writeBeginData(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "      <DATA>" << std::endl;
	_outputFile << "        <TABLEDATA>" << std::endl;
}

void FileWriterASCIIVOTable::writeEndData(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "        </TABLEDATA>" << std::endl;
	_outputFile << "      </DATA>" << std::endl;
}

void FileWriterASCIIVOTable::writeDataRecord(std::string record)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "          <TR>" << std::endl;
	_outputFile << record << std::endl;
	_outputFile << "          </TR>" << std::endl;
}

std::string FileWriterASCIIVOTable::getDataStartTag(bool isTimeData)
{
	if (isTimeData)
		return "            <TD>";
	return "<TD>";
}

std::string FileWriterASCIIVOTable::getDataStopTag(void)
{
	return "</TD>";
}

std::string FileWriterASCIIVOTable::getDataValueSeparator(void)
{
	return " ";
}

bool FileWriterASCIIVOTable::writeTimeData(std::string paramId, double data, OutputFormatTime /*timeFormat*/, bool isFirstParam)
{
	//force ISO format for time field
	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);

	TimeUtil::formatTimeDateInIso(data, *crtFile);

	(*crtFile) << "Z";

	(*crtFile) << getDataStopTag();

	return true;
}

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