Blame view

src/ParamOutputImpl/Plot/TimeAxis.cc 4.48 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
/*
 * TimeAxis.cc
 *
 *  Created on: 27 nov. 2013
 *      Author: CS
 */

#include "TimeAxis.hh"
#include <iostream>
#include <map>

namespace plot {

const double TimeAxis::SECONDS_IN_MINUTES = 60.0;
const double TimeAxis::SECONDS_IN_HOUR = 60.0 * SECONDS_IN_MINUTES;
const double TimeAxis::SECONDS_IN_DAY = 24.0 * SECONDS_IN_HOUR;

/**
 * @overload Axis::getAutoMajorTickSpace
 */
double TimeAxis::getAutoMajorTickSpace(double min, double max) {
	return TimeAxis::computeAutoMajorTickSpace (min, max);
}

/**
 * @overload Axis::getAutoMajorTickSpace
 */
double TimeAxis::computeAutoMajorTickSpace(double min, double max) {
	double interval = abs(max - min);
	double basicInterval;
	double factor;
	if (interval < 3 * TimeAxis::SECONDS_IN_MINUTES) {
		// second
		factor = 1;
		basicInterval = interval;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_HOUR) {
		// minutes
		factor = SECONDS_IN_MINUTES;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY) {
		// days
		factor = SECONDS_IN_HOUR;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 7) {
		// weeks
		factor = SECONDS_IN_DAY;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 365.25) {
		// years
		factor = SECONDS_IN_DAY * 7;
		basicInterval = interval / factor;
	} else {
		// a lot of years
		factor = SECONDS_IN_DAY * 365.25;
		basicInterval = interval / factor;
	}

	double t1 = log10(basicInterval);
	double np = floor(t1);
	t1 = t1 - np;

	double t2;
	if (t1 > 0.7781512503) {
		t2 = 2.0;
	} else if (t1 > 0.4771212549) {
		t2 = 1.0;
	} else if (t1 > 0.1760912591) {
		t2 = 5.0;
		np = np - 1;
	} else {
		t2 = 2.0;
		np = np - 1;
	}

	return t2 * pow(10.0, (double) np) * factor;
}

/**
 * @overload Axis::getAutoMinorTickNumber
 */
double TimeAxis::getAutoMinorTickNumber(double min, double max) {
	double interval = abs(max - min);
	double basicInterval;
	double factor;
	if (interval < 3 * TimeAxis::SECONDS_IN_MINUTES) {
		// second
		factor = 1;
		basicInterval = interval;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_HOUR) {
		// minutes
		factor = SECONDS_IN_MINUTES;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY) {
		// days
		factor = SECONDS_IN_HOUR;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 7) {
		// weeks
		factor = SECONDS_IN_DAY;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 365.25) {
		// years
		factor = SECONDS_IN_DAY * 7;
		basicInterval = interval / factor;
	} else {
		// a lot of years
		factor = SECONDS_IN_DAY * 365.25;
		basicInterval = interval / factor;
	}

	double t1 = log10(basicInterval);
	double np = floor(t1);
	t1 = t1 - np;

	double ns;
	if (t1 > 0.7781512503) {
		ns = 4;
	} else if (t1 > 0.4771212549) {
		ns = 5;
	} else if (t1 > 0.1760912591) {
		ns = 5;
		np = np - 1;
	} else {
		ns = 4;
	}

	return ns;
}

std::string getPlTimeFormat(std::string userFormat, double startTime,
		double stopTime, double numberOfMajorTicks) {
	double interval = abs(stopTime - startTime);
	// interval is less than a minute, show seconds
	if (interval <= TimeAxis::SECONDS_IN_MINUTES) {
		return "%S%3%";
	} // interval is less than an hour, show minutes
	else if (interval < TimeAxis::SECONDS_IN_HOUR) {
		/*if (!std::isnan(numberOfMajorTicks)
				&& interval / TimeAxis::SECONDS_IN_MINUTES
						< numberOfMajorTicks) {
			return "%M:%S%3%";
		}*/
		return "%M:%S";

	} // interval is less than an day, show hours
	else if (interval < TimeAxis::SECONDS_IN_DAY) {
		/*if (!std::isnan(numberOfMajorTicks)
				&& interval / TimeAxis::SECONDS_IN_HOUR < numberOfMajorTicks) {

			return "%H:%M:%S";
		}*/
		return "%H:%M";
	} // interval is more than a day, show days
	else {
		if (userFormat == "ddd/yy") {
			if (!std::isnan(numberOfMajorTicks)
					&& interval / TimeAxis::SECONDS_IN_DAY
							< numberOfMajorTicks) {
				return "%j/%H:%M";
			}
			return "%j/%y";
		} else {
			if (!std::isnan(numberOfMajorTicks)
					&& interval / TimeAxis::SECONDS_IN_DAY
							< numberOfMajorTicks) {
				return "%d/%H:%M";
			}
			return "%d/%m/%y";
		}
	}
}

std::string getPlStartTimeFormat(std::string userFormat, double startTime, double stopTime) {
	double interval = stopTime - startTime;
	std::string dateStr;

	if (userFormat == "ddd/yy") {
		dateStr = "%b %d (%j) %Y";
	} else {
		dateStr = "%b %d %Y";
	}

	if (interval < TimeAxis::SECONDS_IN_HOUR) {
		dateStr += " %H:%M";
	}
	return dateStr;
}

} /* namespace plot */