19471e9fa650847b8af2d5406e3036f8db19f4be
[fw/altos] / altosui / AltosFlightStats.java
1 /*
2  * Copyright © 2011 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30
31 public class AltosFlightStats {
32         double          max_height;
33         double          max_speed;
34         double          max_acceleration;
35         double[]        state_speed = new double[Altos.ao_flight_invalid + 1];
36         double[]        state_baro_speed = new double[Altos.ao_flight_invalid + 1];
37         double[]        state_accel = new double[Altos.ao_flight_invalid + 1];
38         int[]           state_count = new int[Altos.ao_flight_invalid + 1];
39         double[]        state_start = new double[Altos.ao_flight_invalid + 1];
40         double[]        state_end = new double[Altos.ao_flight_invalid + 1];
41
42         public AltosFlightStats(AltosFlightReader reader) throws InterruptedException, IOException {
43                 AltosState      state = null;
44                 AltosState      new_state = null;
45                 double          boost_time = -1;
46                 double          start_time;
47
48                 for (;;) {
49                         try {
50                                 AltosRecord     record = reader.read();
51                                 if (record == null)
52                                         break;
53                                 new_state = new AltosState(record, state);
54                                 if (state == null) {
55                                         start_time = new_state.time;
56                                 }
57                                 state = new_state;
58                                 if (0 <= state.state && state.state < Altos.ao_flight_invalid) {
59                                         if (boost_time == -1 && state.state >= Altos.ao_flight_boost)
60                                                 boost_time = state.time;
61                                         state_accel[state.state] += state.acceleration;
62                                         state_speed[state.state] += state.speed;
63                                         state_baro_speed[state.state] += state.baro_speed;
64                                         state_count[state.state]++;
65                                         if (state_start[state.state] == 0.0)
66                                                 state_start[state.state] = state.time;
67                                         if (state_end[state.state] < state.time)
68                                                 state_end[state.state] = state.time;
69                                         max_height = state.max_height;
70                                         if (state.max_speed != 0)
71                                                 max_speed = state.max_speed;
72                                         else
73                                                 max_speed = state.max_baro_speed;
74                                         max_acceleration = state.max_acceleration;
75                                 }
76                         } catch (ParseException pp) {
77                                 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
78                         } catch (AltosCRCException ce) {
79
80                         }
81                 }
82                 for (int s = Altos.ao_flight_startup; s <= Altos.ao_flight_landed; s++) {
83                         if (state_count[s] > 0) {
84                                 state_speed[s] /= state_count[s];
85                                 state_baro_speed[s] /= state_count[s];
86                                 state_accel[s] /= state_count[s];
87                         }
88                 }
89         }
90
91         public AltosFlightStats(AltosRecordIterable iterable, File file) throws InterruptedException, IOException {
92                 this(new AltosReplayReader(iterable.iterator(), file));
93         }
94
95         public AltosFlightStats(AltosRecordIterable iterable) throws InterruptedException, IOException {
96                 this(iterable, new File(""));
97         }
98 }