altoslib: Get KML export working again
[fw/altos] / altoslib / AltosTimeSeries.java
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 package org.altusmetrum.altoslib_11;
16
17 import java.util.*;
18
19 public class AltosTimeSeries implements Iterable<AltosTimeValue> {
20         public String                   label;
21         public AltosUnits               units;
22         ArrayList<AltosTimeValue>       values;
23
24         public void add(AltosTimeValue tv) {
25                 values.add(tv);
26         }
27
28         public void add(double time, double value) {
29                 add(new AltosTimeValue(time, value));
30         }
31
32         public AltosTimeValue get(int i) {
33                 return values.get(i);
34         }
35
36         private double lerp(AltosTimeValue v0, AltosTimeValue v1, double t) {
37                 /* degenerate case */
38                 if (v0.time == v1.time)
39                         return (v0.value + v1.value) / 2;
40
41                 return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / (v1.time - v0.time);
42         }
43
44         private int after_index(double time) {
45                 int     lo = 0;
46                 int     hi = values.size() - 1;
47
48                 while (lo <= hi) {
49                         int mid = (lo + hi) / 2;
50
51                         if (values.get(mid).time < time)
52                                 lo = mid + 1;
53                         else
54                                 hi = mid - 1;
55                 }
56                 return lo;
57         }
58
59         /* Compute a value for an arbitrary time */
60         public double value(double time) {
61                 int after = after_index(time);
62                 double ret;
63
64                 if (after == 0)
65                         ret = values.get(0).value;
66                 else if (after == values.size())
67                         ret = values.get(after - 1).value;
68                 else {
69                         AltosTimeValue b = values.get(after-1);
70                         AltosTimeValue a = values.get(after);
71                         ret = lerp(b, a, time);
72                 }
73                 return ret;
74         }
75
76         /* Find the value just before an arbitrary time */
77         public double value_before(double time) {
78                 int after = after_index(time);
79
80                 if (after == 0)
81                         return values.get(0).value;
82                 return values.get(after-1).value;
83         }
84
85         /* Find the value just after an arbitrary time */
86         public double value_after(double time) {
87                 int after = after_index(time);
88
89                 if (after == values.size())
90                         return values.get(after-1).value;
91                 return values.get(after).value;
92         }
93
94         public double time_of(double value) {
95                 double  last = AltosLib.MISSING;
96                 for (AltosTimeValue v : values) {
97                         if (v.value >= value)
98                                 return v.time;
99                         last = v.time;
100                 }
101                 return last;
102         }
103
104         public int size() {
105                 return values.size();
106         }
107
108         public Iterator<AltosTimeValue> iterator() {
109                 return values.iterator();
110         }
111
112         public double max() {
113                 double max = AltosLib.MISSING;
114                 for (AltosTimeValue tv : values) {
115                         if (max == AltosLib.MISSING || tv.value > max)
116                                 max = tv.value;
117                 }
118                 return max;
119         }
120
121         public double max(double start_time, double end_time) {
122                 double max = AltosLib.MISSING;
123                 for (AltosTimeValue tv : values) {
124                         if (start_time <= tv.time && tv.time <= end_time)
125                                 if (max == AltosLib.MISSING || tv.value > max)
126                                         max = tv.value;
127                 }
128                 return max;
129         }
130
131         public double min() {
132                 double min = AltosLib.MISSING;
133                 for (AltosTimeValue tv : values) {
134                         if (min == AltosLib.MISSING || tv.value < min)
135                                 min = tv.value;
136                 }
137                 return min;
138         }
139
140         public double min(double start_time, double end_time) {
141                 double min = AltosLib.MISSING;
142                 for (AltosTimeValue tv : values) {
143                         if (start_time <= tv.time && tv.time <= end_time)
144                                 if (min == AltosLib.MISSING || tv.value < min)
145                                         min = tv.value;
146                 }
147                 return min;
148         }
149
150         public double average() {
151                 double total = 0;
152                 int count = 0;
153                 for (AltosTimeValue tv : values) {
154                         total += tv.value;
155                         count++;
156                 }
157                 if (count == 0)
158                         return AltosLib.MISSING;
159                 return total / count;
160         }
161
162         public double average(double start_time, double end_time) {
163                 double total = 0;
164                 int count = 0;
165                 for (AltosTimeValue tv : values) {
166                         if (start_time <= tv.time && tv.time <= end_time) {
167                                 total += tv.value;
168                                 count++;
169                         }
170                 }
171                 if (count == 0)
172                         return AltosLib.MISSING;
173                 return total / count;
174         }
175
176         public AltosTimeSeries integrate(AltosTimeSeries integral) {
177                 double  value = 0.0;
178                 double  pvalue = 0.0;
179                 double  time = 0.0;
180                 boolean start = true;
181
182                 for (AltosTimeValue v : values) {
183                         if (start) {
184                                 value = 0.0;
185                                 start = false;
186                         } else {
187                                 value += (pvalue + v.value) / 2.0 * (v.time - time);
188                         }
189                         pvalue = v.value;
190                         time = v.time;
191                         integral.add(time, value);
192
193                 }
194                 return integral;
195         }
196
197         public AltosTimeSeries differentiate(AltosTimeSeries diff) {
198                 double value = 0.0;
199                 double time = 0.0;
200                 boolean start = true;
201
202                 for (AltosTimeValue v: values) {
203                         if (start) {
204                                 value = v.value;
205                                 time = v.time;
206                                 start = false;
207                         } else {
208                                 double  dx = v.time - time;
209                                 double  dy = v.value - value;
210
211                                 if (dx != 0)
212                                         diff.add(time, dy/dx);
213
214                                 time = v.time;
215                                 value = v.value;
216                         }
217                 }
218                 return diff;
219         }
220
221         private int find_left(int i, double dt) {
222                 int j;
223                 double t = values.get(i).time - dt;
224                 for (j = i; j >= 0; j--)        {
225                         if (values.get(j).time < t)
226                                 break;
227                 }
228                 return j + 1;
229
230         }
231
232         private int find_right(int i, double dt) {
233                 int j;
234                 double t = values.get(i).time + dt;
235                 for (j = i; j < values.size(); j++) {
236                         if (values.get(j).time > t)
237                                 break;
238                 }
239                 return j - 1;
240
241         }
242
243         private double filter_coeff(double dist, double width) {
244                 double ratio = dist / (width / 2);
245
246                 return Math.cos(ratio * Math.PI / 2);
247         }
248
249         public AltosTimeSeries filter(AltosTimeSeries f, double width) {
250                 double  half_width = width/2;
251                 for (int i = 0; i < values.size(); i++) {
252                         double  center_time = values.get(i).time;
253                         double  left_time = center_time - half_width;
254                         double  right_time = center_time + half_width;
255                         double  total_coeff = 0.0;
256                         double  total_value = 0.0;
257
258                         int     left = find_left(i, half_width);
259                         int     right = find_right(i, half_width);
260
261                         for (int j = left; j <= right; j++) {
262                                 double  j_time = values.get(j).time;
263
264                                 if (left_time <= j_time && j_time <= right_time) {
265                                         double  j_left = j == left ? left_time : values.get(j-1).time;
266                                         double  j_right = j == right ? right_time : values.get(j+1).time;
267                                         double  interval = (j_right - j_left) / 2.0;
268                                         double  coeff = filter_coeff(j_time - center_time, width) * interval;
269
270                                         total_coeff += coeff;
271                                         total_value += coeff * values.get(j).value;
272                                 }
273                         }
274                         if (total_coeff != 0.0)
275                                 f.add(center_time, total_value / total_coeff);
276                 }
277                 return f;
278         }
279
280         public AltosTimeSeries(String label, AltosUnits units) {
281                 this.label = label;
282                 this.units = units;
283                 this.values = new ArrayList<AltosTimeValue>();
284         }
285 }