<commit_before>/*
 * writer.cpp
 *
 *  Created on: Nov 26, 2012
 *      Author: partio
 */

#include "writer.h"
#include "plugin_factory.h"
#include "logger_factory.h"
#include <fstream>
#include <boost/filesystem/operations.hpp>
#include <boost/lexical_cast.hpp>

#define HIMAN_AUXILIARY_INCLUDE

#include "grib.h"
#include "querydata.h"
#include "neons.h"

#undef HIMAN_AUXILIARY_INCLUDE

using namespace himan::plugin;

writer::writer()
{
	itsLogger = std::unique_ptr<logger> (logger_factory::Instance()->GetLog("writer"));
}

bool writer::ToFile(std::shared_ptr<info> theInfo,
                    const std::string& theOutputFile,
                    HPFileType theFileType,
                    bool theActiveOnly)
{

	bool ret = false;

	boost::filesystem::path p(theOutputFile);
	std::string theCorrectOutputFile = p.stem().string();

	switch (theFileType)
	{

		case kGRIB:
		case kGRIB1:
		case kGRIB2:
			{

				theCorrectOutputFile += ".grib";

				std::shared_ptr<grib> theGribWriter = std::dynamic_pointer_cast<grib> (plugin_factory::Instance()->Plugin("grib"));

				ret = theGribWriter->ToFile(theInfo, theCorrectOutputFile, theFileType, theActiveOnly);

				break;
			}
		case kQueryData:
			{
				theCorrectOutputFile += ".fqd";

				std::shared_ptr<querydata> theWriter = std::dynamic_pointer_cast<querydata> (plugin_factory::Instance()->Plugin("querydata"));

				if (!theWriter)
				{
					throw std::runtime_error("Unable to load QueryData plugin");
				}

				ret = theWriter->ToFile(theInfo, theCorrectOutputFile, theActiveOnly);

				break;
			}
		case kNetCDF:
			break;

			// Must have this or compiler complains
		default:
			throw std::runtime_error(ClassName() + ": Invalid file type: " + boost::lexical_cast<std::string> (theFileType));
			break;

	}

	if (ret)
	{
		itsLogger->Info("Wrote file '" + theCorrectOutputFile + "'");
	}

	return ret;
}<commit_msg>Initial support for writing metadata to neons<commit_after>/*
 * writer.cpp
 *
 *  Created on: Nov 26, 2012
 *      Author: partio
 */

#include "writer.h"
#include "plugin_factory.h"
#include "logger_factory.h"
#include <fstream>
//#include <boost/filesystem/operations.hpp>
#include <boost/lexical_cast.hpp>

#define HIMAN_AUXILIARY_INCLUDE

#include "grib.h"
#include "querydata.h"
#include "neons.h"
#include "util.h"

#undef HIMAN_AUXILIARY_INCLUDE

using namespace himan::plugin;

writer::writer()
{
	itsLogger = std::unique_ptr<logger> (logger_factory::Instance()->GetLog("writer"));
}

bool writer::ToFile(std::shared_ptr<info> theInfo,
                    HPFileType theFileType,
                    bool theActiveOnly,
                    const std::string& theOutputFile)
{

	bool ret = false;

	std::string correctFileName = theOutputFile;

	if (correctFileName.empty())
	{
		std::shared_ptr<util> u = std::dynamic_pointer_cast<util> (plugin_factory::Instance()->Plugin("util"));


		correctFileName = u->MakeNeonsFileName(theInfo);
	}

	switch (theFileType)
	{

		case kGRIB:
		case kGRIB1:
		case kGRIB2:
			{

				std::shared_ptr<grib> theGribWriter = std::dynamic_pointer_cast<grib> (plugin_factory::Instance()->Plugin("grib"));

				correctFileName += ".grib";

				ret = theGribWriter->ToFile(theInfo, correctFileName, theFileType, theActiveOnly);

				break;
			}
		case kQueryData:
			{
				std::shared_ptr<querydata> theWriter = std::dynamic_pointer_cast<querydata> (plugin_factory::Instance()->Plugin("querydata"));

				correctFileName += ".fqd";

				ret = theWriter->ToFile(theInfo, correctFileName, theActiveOnly);

				break;
			}
		case kNetCDF:
			break;

			// Must have this or compiler complains
		default:
			throw std::runtime_error(ClassName() + ": Invalid file type: " + boost::lexical_cast<std::string> (theFileType));
			break;

	}

	if (ret && theActiveOnly)
	{
		std::shared_ptr<neons> n = std::dynamic_pointer_cast<neons> (plugin_factory::Instance()->Plugin("neons"));

		// Save file information to neons

		ret = n->Save(theInfo);

		if (ret)
		{
			itsLogger->Info("Wrote file '" + correctFileName + "'");
		}
		else
		{
			itsLogger->Warning("Saving file information to neons failed");
			unlink(correctFileName.c_str());
		}

	}

	return ret;
}
{'fraction_non_alphanumeric': 0.14094280928451783, 'fraction_numerical': 0.00382866714525006, 'mean_word_length': 4.091352009744214, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1662107', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1375, 'n_words': 272}