Blame view

src/ParamOutputImpl/Plot/Epoch/EpochAxisDecorator.cc 4.95 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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 */