EpochAxisDecorator.cc 4.95 KB
/*
 * EpochAxisDecorator.cc
 *
 *  Created on: Jan 16, 2013
 *      Author: AKKA
 */

#include "EpochAxisDecorator.hh"
#include "plplot/plplot.h"

#include "plplot/plstream.h"

#include "PlotLogger.hh"
#include "EpochAxis.hh"
#include "EpochPlot.hh"

#include "DefaultTimeAxisDecorator.hh"

namespace plot {

#define EPOCH_NORMALIZED_UNITS "normalized"
#define EPOCH_SECONDS_UNITS    "seconds"
#define EPOCH_MINUTES_UNITS    "minutes"
#define EPOCH_HOURS_UNITS      "hours"
#define EPOCH_DAYS_UNITS       "days"

#define IS_EPOCH_SECONDS(duration) (duration <= 5 * TimeAxis::SECONDS_IN_MINUTES)
#define IS_EPOCH_MINUTES(duration) (duration <= 5 * TimeAxis::SECONDS_IN_HOUR)
#define IS_EPOCH_HOURS(duration) (duration <= 5 * TimeAxis::SECONDS_IN_DAY)


void EpochAxisDecorator::installLabelGenerator(PanelPlotOutput* /*pplot_*/, TimeAxis* axis_)
{

	axis_->_labelGenerator->_data = axis_;

	if (axis_->_showTickMark == true)
	{
		axis_->_labelGenerator->_function = generateEpochTimeLabel;
	} else {
		axis_->_labelGenerator->_function = generateNoTimeLabel;
	}

	//set units
	EpochAxis* epochAxis = static_cast<EpochAxis*>(axis_);
	if (epochAxis->isNormalized())
		epochAxis->setAxisUnits(EPOCH_NORMALIZED_UNITS);
	else
	{
		double duration = epochAxis->getRange().getMax() - epochAxis->getRange().getMin();
		if (IS_EPOCH_SECONDS(duration))
			epochAxis->setAxisUnits(EPOCH_SECONDS_UNITS);
		else if (IS_EPOCH_MINUTES(duration))
			epochAxis->setAxisUnits(EPOCH_MINUTES_UNITS);
		else if (IS_EPOCH_HOURS(duration))
			epochAxis->setAxisUnits(EPOCH_HOURS_UNITS);
		else
			epochAxis->setAxisUnits(EPOCH_DAYS_UNITS);
	}
}

/***************************** Format EPOCH TIME *******************************/
void getEpochTimeChar(double startTime, double stopTime,
		double crtTime, char *label, int length)
{
	if (length <= 0)
		return;

	memset(label,0,length*sizeof(char));

	if (length == 1)
		return;

	if (crtTime == 0)
	{
		snprintf(label, length, "%s", "T#d0#u");
		return;
	}

	double interval = abs(stopTime - startTime);

	//set sign
	if (crtTime > 0)
		label[0] = '+';
	else
		label[0] = '-';

	if (length == 2)
		return;

	double crtDuration = abs(crtTime);

	if (IS_EPOCH_SECONDS(interval))
	{
		//show seconds
		snprintf(&label[1], length-1, "%d", (int)crtDuration);
	}
	else if (IS_EPOCH_MINUTES(interval))
	{
		//show minutes
		double nbMin = crtDuration/TimeAxis::SECONDS_IN_MINUTES;
		if (nbMin - (int)nbMin == 0)
			snprintf(&label[1], length-1, "%d", (int)nbMin);
		else
			snprintf(label, length, "%.1f", nbMin);
		return;
	}
	else if (IS_EPOCH_HOURS(interval))
	{
		//show hours
		double nbHou = crtDuration/TimeAxis::SECONDS_IN_HOUR;
		if (nbHou - (int)nbHou == 0)
			snprintf(&label[1], length-1, "%d", (int)nbHou);
		else
			snprintf(label, length, "%.1f", nbHou);
		return;
	}

	//else
	//show days

	double nbDay = crtDuration/TimeAxis::SECONDS_IN_DAY;
	if (nbDay - (int)nbDay == 0)
		snprintf(&label[1], length-1, "%d", (int)nbDay);
	else
		snprintf(label, length, "%.1f", nbDay);

	//BRE - Old version
	/*if (interval <= TimeAxis::SECONDS_IN_MINUTES)
	{
		//show seconds
		snprintf(&label[1], length-1, " %02d", crtDuration);
		return;
	}
	else if (interval < TimeAxis::SECONDS_IN_HOUR)
	{
		//show minutes and seconds
		int nbMin = crtDuration/TimeAxis::SECONDS_IN_MINUTES;
		int nbSec = crtDuration - nbMin*TimeAxis::SECONDS_IN_MINUTES;
		snprintf(&label[1], length-1, " %02d:%02d", nbMin, nbSec);
		return;
	}
	else if (interval < TimeAxis::SECONDS_IN_DAY)
	{
		//show hours, minutes and seconds
		int nbHou = crtDuration/TimeAxis::SECONDS_IN_HOUR;
		int nbMin = (crtDuration - nbHou*TimeAxis::SECONDS_IN_HOUR) / TimeAxis::SECONDS_IN_MINUTES;
		int nbSec = crtDuration - nbHou*TimeAxis::SECONDS_IN_HOUR - nbMin*TimeAxis::SECONDS_IN_MINUTES;
		snprintf(&label[1], length-1, " %02d:%02d:%02d", nbHou, nbMin, nbSec);
		return;
	}

	//else
	//show days, hour min and sec
	int nbDay = crtDuration/TimeAxis::SECONDS_IN_DAY;
	int nbHou = (crtDuration - nbDay * TimeAxis::SECONDS_IN_DAY) / TimeAxis::SECONDS_IN_HOUR;
	int nbMin = (crtDuration - nbDay*TimeAxis::SECONDS_IN_DAY - nbHou*TimeAxis::SECONDS_IN_HOUR) / TimeAxis::SECONDS_IN_MINUTES;
	int nbSec = crtDuration - nbDay*TimeAxis::SECONDS_IN_DAY - nbHou*TimeAxis::SECONDS_IN_HOUR - nbMin*TimeAxis::SECONDS_IN_MINUTES;
	if (nbDay > 0)
		snprintf(&label[1], length-1, " %d/%02d:%02d:%02d", nbDay, nbHou, nbMin, nbSec);
	else
		snprintf(&label[1], length-1, " %02d:%02d:%02d", nbHou, nbMin, nbSec);*/
}

/***************************** EXTERNAL METHODS ********************************/

void generateEpochTimeLabel(PLINT axis, PLFLT value, char *label, PLINT length,
		PLPointer data) {
	EpochAxis* epochAxis = static_cast<EpochAxis*>(data);
	if (axis == PL_X_AXIS)
	{
		if (!epochAxis->isNormalized())
			getEpochTimeChar(epochAxis->getRange().getMin(), epochAxis->getRange().getMax(),
					value, label, length);
		else
		{
			if (value == 0)
			{
				snprintf(label, length, "%s", "T#d0#u");
			}
			else
			{
				snprintf(label, length, "%2.1f", value);
			}
		}
	}
}


} /* namespace plot */