Blame view

src/ParamOutputImpl/Plot/ParameterData.cc 4.6 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * 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() :
		_minTime(nan("")), _maxTime(nan("")), _dim1Size(-1), _dim2Size(-1) {
}

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);
	}
}

/**
 * @brief Adds a value by time to parameter
 */
void ParameterData::addValue(double time, double value,
		double sampling, double gapSize, bool &gapDetected, AMDA::Common::ParameterIndexComponent& index) {
	int valueIndex = componentToValuesIndex(index);
	if (valueIndex >= (int)_values.size())
	{
		_values.resize(valueIndex+1);
	}
	// add time if not already in
	if (_dates.empty() || _dates.back() != time) {

		if (!_dates.empty() && ((time - _dates.back()) > gapSize))
		{
			//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);
			_values[valueIndex].addValue(NAN);
			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);
		}
	}
	else if (gapDetected)
		_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() :
		_min(nan("")), _minStrictPos(nan("")), _max(nan("")) {
}

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);

	if (isnan(_min) && !isnan(value)) {
		_min = value;
	} else if (!isnan(_min) && !isnan(value)) {
		_min = std::min(_min, value);
	}

	if (isnan(_max) && !isnan(value)) {
		_max = value;
	} else if (!isnan(value) && !isnan(_max)) {
		_max = std::max(_max, value);
	}

	if (isnan(_minStrictPos) && !isnan(value) && (value > 0))
		_minStrictPos = std::min(_min, value);
}

} /* namespace plot */