ParameterAxes.cc 6.85 KB
/*
 * ParameterAxes.cc
 *
 *  Created on: Dec 5, 2013
 *      Author: amdadev
 */

#ifndef PARAMETERAXES_CC
#define PARAMETERAXES_CC

#include "ParameterAxes.hh"

#include <iosfwd>

#include <iostream>
#include <sstream>
#include <map>
#include <stdexcept>
#include <utility>
#include <cstring>

namespace plot {
/**
 * add a new series properties. If a series with the same index
 * already exist, it will be overridden.
 * _index field of SerieProperties must have been properly set
 * before calling this method.
 * @param series properties to add.
 */
void ParameterAxes::addYSerieProperties(const SeriesProperties& s_) {
	_ySeriesProperties[s_.getIndex()] = s_;
}

/**
 * adds a new X series. If a series with the same index
 * already exist, it will be overridden.
 * _index field of SerieProperties must have been properly set
 * before calling this method.
 * @param series properties to add, with no drawing settings, just index and xAxis
 */
void ParameterAxes::addXSerieProperties(const XSeriesProperties& s_) {
	_xSeriesProperties[s_.getIndex()] = s_;
}

void ParameterAxes::addSpectroProperties(std::shared_ptr<SpectroProperties> pSPectroProperties){
	_spectroPropertiesPtr = pSPectroProperties;
}

void ParameterAxes::addColorSerieProperties(const ColorSeriesProperties& s_) {
	_colorSeriesProperties.push_back(s_);
}

/**
 * retrieve the series properties for the given 'index' key. (index is the
 * value defined by the 'index' element in xml definition)
 * @param index of the serie.
 * @return the serie.
 * @throw out_of_range exception if not found.
 */
SeriesProperties& ParameterAxes::getYSeriePropertiesAt(AMDA::Common::ParameterIndexComponent index_) {
	// search index :
	auto p = _ySeriesProperties.find(index_);
	if (p == _ySeriesProperties.end()) {
		// index not found try to retrieve default index (-1):
		p = _ySeriesProperties.find(AMDA::Common::ParameterIndexComponent(-1,-1));
	}
	// re-check p :
	if (p != _ySeriesProperties.end()) {
		return p->second;
	} else {
		std::ostringstream errmsg;
		errmsg << "index '" << index_.getDim1Index() << "x" << index_.getDim2Index() << "' not found." << std::endl;
		throw std::out_of_range(errmsg.str());
	}
}

/**
 * Get the list of indexes used for a parameter
 */
std::vector<AMDA::Common::ParameterIndexComponent> ParameterAxes::getParamUsedIndexes(std::string paramId_) {
	std::vector<AMDA::Common::ParameterIndexComponent> keyset;

	if (_spectroPropertiesPtr != nullptr)
	{
		if (_spectroPropertiesPtr->getParamId() == paramId_)
		{
			keyset.push_back(AMDA::Common::ParameterIndexComponent(-1,-1));
			return keyset;
		}

	}

	for (std::map<AMDA::Common::ParameterIndexComponent, SeriesProperties>::iterator it =
			_ySeriesProperties.begin(); it != _ySeriesProperties.end(); ++it) {
			// add index only if serie has same paramId to not store
			// unused values
		if (it->second.getParamId() == paramId_) {
			keyset.push_back(it->first);
		}
	}

	for (std::map<AMDA::Common::ParameterIndexComponent, XSeriesProperties>::iterator it =
			_xSeriesProperties.begin(); it != _xSeriesProperties.end(); ++it) {
			// same remark
		for (auto itXParam : it->second.getXParamIds())
		{
			if (itXParam.second == paramId_)
				keyset.push_back(it->first);
		}
	}

	for (std::vector<ColorSeriesProperties>::iterator it =
			_colorSeriesProperties.begin(); it != _colorSeriesProperties.end(); ++it) {
			// same remark
		for (auto itColorParam : it->getColorParamIds())
		{
			if (itColorParam.second == paramId_)
				keyset.push_back(it->getIndex());
		}
	}

	return keyset;
}

/**
 * Gets all real index for y series.
 */
std::vector<AMDA::Common::ParameterIndexComponent> ParameterAxes::getYSerieIndexList(std::map<std::string, ParameterData> *pParameterValues) {
	std::vector<AMDA::Common::ParameterIndexComponent> indexes;

	std::map<AMDA::Common::ParameterIndexComponent, SeriesProperties>::iterator itAllIndexesSerie = _ySeriesProperties.find(-1);
	if (itAllIndexesSerie != _ySeriesProperties.end()) {
		// contains -1 means all series must be drawn
		indexes.insert(indexes.end(),
				(*pParameterValues)[itAllIndexesSerie->second.getParamId()]._indexes.begin(),
				(*pParameterValues)[itAllIndexesSerie->second.getParamId()]._indexes.end());
		if (indexes.empty()) {
			indexes.push_back(AMDA::Common::ParameterIndexComponent(-1,-1));
		}
	} else {
		for (std::map<AMDA::Common::ParameterIndexComponent, SeriesProperties>::iterator it =
				_ySeriesProperties.begin(); it != _ySeriesProperties.end(); ++it) {
			indexes.push_back(it->first);
		}
	}
	return indexes;
}

/**
 * Get color serie properties by id
 */
 ColorSeriesProperties& ParameterAxes::getColorSeriePropertiesById(int id_) {
	for (auto &prop : _colorSeriesProperties)
		if (prop.getId() == id_)
			return prop;

	std::ostringstream errmsg;
	errmsg << "id '" << id_ << "' not found." << std::endl;
	throw std::out_of_range(errmsg.str());
}

/*
 * Dumps properties for test.
 */
void ParameterAxes::dump(std::ostream& out_, std::string& prefix_) {
	out_ << "[DEFAULT PARAMETER AXES]" << std::endl;
	std::string subPrefix = prefix_ + "default";
	_defaultProperties.dump(out_, subPrefix);

	std::ostringstream os;
	std::map<AMDA::Common::ParameterIndexComponent, SeriesProperties>::iterator it;
	for (it = _ySeriesProperties.begin(); it != _ySeriesProperties.end();
			++it) {
		os.str("");
		os << prefix_ << (it->first.getDim1Index()) << "x" << (it->first.getDim2Index()) << ".";
		subPrefix = os.str();
		it->second.dump(out_, subPrefix);
	}

	std::map<AMDA::Common::ParameterIndexComponent, XSeriesProperties>::iterator xit;
	for (xit = _xSeriesProperties.begin(); xit != _xSeriesProperties.end();
			++xit) {
		os.str("");
		os << prefix_ << (xit->first.getDim1Index()) << "x" << (xit->first.getDim2Index()) << ".";
		subPrefix = os.str();
		xit->second.dump(out_, subPrefix);
	}

	if (_spectroPropertiesPtr != nullptr)
	{
		os.str("");
		os << prefix_ << "spectro" << ".";
		subPrefix = os.str();
		_spectroPropertiesPtr->dump(out_, subPrefix);
	}

	std::vector<ColorSeriesProperties>::iterator colorit;
	for (colorit = _colorSeriesProperties.begin(); colorit != _colorSeriesProperties.end();
			++colorit) {
		os.str("");
		os << prefix_  << ".";
		subPrefix = os.str();
		colorit->dump(out_, subPrefix);
	}
}

std::ostream& operator<<(std::ostream& out_, const ParameterAxes& prop_) {
	out_ << "[PARAMETER AXES]" << std::endl;
	out_ << "{" << std::endl;
	out_ << "  name =" << prop_._originalParamId << std::endl;
	out_ << "  default properties = " << prop_._defaultProperties << std::endl;
	out_ << "  {" << std::endl;
	const std::map<AMDA::Common::ParameterIndexComponent, SeriesProperties>::const_iterator end =
			prop_._ySeriesProperties.end();
	std::map<AMDA::Common::ParameterIndexComponent, SeriesProperties>::const_iterator it;
	for (it = prop_._ySeriesProperties.begin(); it != end; ++it) {
		out_ << it->second << std::endl;
	}
	out_ << "  }" << std::endl;
	out_ << "}" << std::endl;
	return out_;
}

} // end namespace plot

#endif // PARAMETERAXES_CC