Blame view

src/ParamOutputImpl/Plot/ParameterData.cc 4.54 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * ParameterData.cc
 *
 *  Created on: 10 déc. 2013
 *      Author: CS
 */

#include "ParameterData.hh"
#include "PlotLogger.hh"
#include <cmath>
#include <iostream>
#include "TimeUtil.hh"

namespace plot {

ParameterData::ParameterData() :
225ac17f   Benjamin Renard   Fix tickmarks plot
17
		_minTime(nan("")), _maxTime(nan("")), _dim1Size(-1), _dim2Size(-1), _paramGapSize(0.) {
fbe3c2bb   Benjamin Renard   First commit
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
}

ParameterData::~ParameterData() {
}

void ParameterData::preAllocate(int nbData)
{
	_dates.reserve(nbData);

	int maxValueIndex = 0;
	for (auto index : _indexes)
	{
		int valueIndex = componentToValuesIndex(index);
		if (valueIndex > maxValueIndex)
			maxValueIndex = valueIndex;
	}
	_values.resize(maxValueIndex+1);

	for (auto index : _indexes)
	{
		int valueIndex = componentToValuesIndex(index);
		_values[valueIndex].preAllocate(nbData);
	}
}

/**
cb9423a7   Benjamin Renard   Fix data gap dete...
44
 * @brief Add a new time record to parameter
fbe3c2bb   Benjamin Renard   First commit
45
 */
cb9423a7   Benjamin Renard   Fix data gap dete...
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
void ParameterData::addTime(double time, double sampling, bool &gapDetected) {
	if (!_dates.empty() && ((time - _dates.back()) > _paramGapSize)) {
		//gap detected => push a NaN value in data list
		if (_dates.back() + sampling > time)
			_dates.push_back((time - _dates.back()) / 2.);
		else
			_dates.push_back(_dates.back() + sampling);
		gapDetected = true;
	}
	else {
		gapDetected = false;
	}
	_dates.push_back(time);

	// calculate min end max time for auto scale
	if (isnan(_minTime)) {
		_minTime = time;
	}
	else {
		_minTime = std::min(time, _minTime);
	}

	if (isnan(_maxTime)) {
		_maxTime = time;
	}
	else {
		_maxTime = std::max(time, _maxTime);
	}
}

/**
 * @brief Add a component value
 */
void ParameterData::addValue(double value, AMDA::Common::ParameterIndexComponent& index, bool gapDetected) {
fbe3c2bb   Benjamin Renard   First commit
80
81
82
83
84
	int valueIndex = componentToValuesIndex(index);
	if (valueIndex >= (int)_values.size())
	{
		_values.resize(valueIndex+1);
	}
fbe3c2bb   Benjamin Renard   First commit
85

cb9423a7   Benjamin Renard   Fix data gap dete...
86
	if (gapDetected)
fbe3c2bb   Benjamin Renard   First commit
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
		_values[valueIndex].addValue(NAN);

	// add relative value
	_values[valueIndex].addValue(value);
}

/**
 * Gets the index of the given time (or just after) in time array,
 * returns -1 if not found
 */
int ParameterData::indexOf(double time_){
	// get index of first time after given time_
	if(isnan(time_)){
		return 0;
	}
	int i = 0;
	for(auto time : _dates){
		if(time >= time_){
			return i;
		}
		++i;
	}
	return -1;
}

/**
 * @brief Computes an interpolated value at a given time
 */
double ParameterData::getInterpolatedValue (double atTime, AMDA::Common::ParameterIndexComponent index) {

	// get index of first time after given time_
	if(isnan(atTime)){
		return nan("");
	}

	int firstSuperiorIndex = 0;
	for(auto time : _dates){
		// No interpolation required
		if (time == atTime){
			return getValues (index, firstSuperiorIndex) [0];
		}
		// Interpolation required
		if (time >= atTime){
			if (firstSuperiorIndex == 0) {
				return nan("");
			}
			else {
				double v1 = getValues (index, firstSuperiorIndex-1) [0];
				double v2 = getValues (index, firstSuperiorIndex) [0];
				double d1 = _dates [firstSuperiorIndex-1];
				double d2 = _dates [firstSuperiorIndex];
				// Return a linear interpolation of the value for the index
				return (v1 + (v2-v1) * (atTime-d1)/(d2-d1));
			}
		}
		++firstSuperiorIndex;
	}
	return nan("");
}


/**
 * Dump properties, for TU
 */
void ParameterData::dump(std::ostream& out_, std::string& prefix_) {
	for(auto index : _indexes){
		out_ << prefix_ << "index " << index.getDim1Index() << "x" << index.getDim2Index() << " => " << _values[componentToValuesIndex(index)].getNumberOfData()
				<< " data" << std::endl;
	}
	for (size_t i = 0; i < _dates.size(); ++i) {
		out_ << prefix_;
		AMDA::TimeUtil::formatTimeDateInIso(_dates[i], out_);
		if(!_indexes.empty()){
			for(auto index : _indexes){
				out_ << prefix_ << getValues(index)[i] << " ";
			}
		}
		else {
			out_ << prefix_ << getValues()[i] << " ";
		}
		out_<< std::endl;
	}
}

 // ------------------ COMPONENTPARAMTERDATA ------------------------ //

ComponentParameterData::ComponentParameterData() :
d57f00dc   Benjamin Renard   Draw NO DATA
174
		_min(nan("")), _minStrictPos(nan("")), _max(nan("")), _noData(true) {
fbe3c2bb   Benjamin Renard   First commit
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
}

ComponentParameterData::~ComponentParameterData() {

}

void ComponentParameterData::preAllocate(int nbData)
{
	_values.reserve(nbData);
}

/**
 * @brief Adds a value and updates min and max values.
 */
void ComponentParameterData::addValue(double value) {
	_values.push_back(value);

f2db3c16   Benjamin Renard   Support variable ...
192
193
	if (!isnan(value))
	{
d57f00dc   Benjamin Renard   Draw NO DATA
194
		_noData = false;
f2db3c16   Benjamin Renard   Support variable ...
195
196
197
198
		if (isnan(_min))
			_min = value;
		else
			_min = std::min(_min, value);
fbe3c2bb   Benjamin Renard   First commit
199

f2db3c16   Benjamin Renard   Support variable ...
200
201
202
203
		if (isnan(_max))
			_max = value;
		else
			_max = std::max(_max, value);
fbe3c2bb   Benjamin Renard   First commit
204

f2db3c16   Benjamin Renard   Support variable ...
205
206
		if (value > 0)
		{
e6975854   Benjamin Renard   Fix a bug with mi...
207

f2db3c16   Benjamin Renard   Support variable ...
208
209
210
			if (isnan(_minStrictPos))
				_minStrictPos = value;
			else
e6975854   Benjamin Renard   Fix a bug with mi...
211
				_minStrictPos = std::min(_minStrictPos, value);
f2db3c16   Benjamin Renard   Support variable ...
212
213
		}
	}
fbe3c2bb   Benjamin Renard   First commit
214
215
216
}

} /* namespace plot */