altos: Need to use 16-bit counts for ao_xmem functions
[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 import org.altusmetrum.AltosLib.*;
31
32 public class AltosFlightStats {
33         double          max_height;
34         double          max_speed;
35         double          max_acceleration;
36         double[]        state_speed = new double[Altos.ao_flight_invalid + 1];
37         double[]        state_baro_speed = new double[Altos.ao_flight_invalid + 1];
38         double[]        state_accel = new double[Altos.ao_flight_invalid + 1];
39         int[]           state_count = new int[Altos.ao_flight_invalid + 1];
40         double[]        state_start = new double[Altos.ao_flight_invalid + 1];
41         double[]        state_end = new double[Altos.ao_flight_invalid + 1];
42         int             serial;
43         int             flight;
44         int             year, month, day;
45         int             hour, minute, second;
46
47         double landed_time(AltosRecordIterable iterable) {
48                 AltosState      state = null;
49                 for (AltosRecord record : iterable) {
50                         state = new AltosState(record, state);
51
52                         if (state.state == Altos.ao_flight_landed)
53                                 break;
54                 }
55
56                 double  landed_height = state.height;
57
58                 state = null;
59
60                 boolean above = true;
61
62                 double  landed_time = -1000;
63
64                 for (AltosRecord record : iterable) {
65                         state = new AltosState(record, state);
66
67                         if (state.height > landed_height + 10) {
68                                 above = true;
69                         } else {
70                                 if (above && state.height < landed_height + 2) {
71                                         above = false;
72                                         landed_time = state.time;
73                                 }
74                         }
75                 }
76                 if (landed_time == -1000)
77                         landed_time = state.time;
78                 return landed_time;
79         }
80
81         double boost_time(AltosRecordIterable iterable) {
82                 double boost_time = -1000;
83
84                 AltosState state = null;
85
86                 for (AltosRecord record : iterable) {
87                         state = new AltosState(record, state);
88                         
89                         if (state.acceleration < 1)
90                                 boost_time = state.time;
91                         if (state.state >= Altos.ao_flight_boost)
92                                 break;
93                 }
94                 if (boost_time == -1000)
95                         boost_time = state.time;
96                 return boost_time;
97         }
98
99
100         public AltosFlightStats(AltosRecordIterable iterable) throws InterruptedException, IOException {
101                 AltosState      state = null;
102                 AltosState      new_state = null;
103                 double          boost_time = boost_time(iterable);
104                 double          end_time = 0;
105                 double          landed_time = landed_time(iterable);
106
107                 year = month = day = -1;
108                 hour = minute = second = -1;
109                 serial = flight = -1;
110                 for (AltosRecord record : iterable) {
111                         if (serial < 0)
112                                 serial = record.serial;
113                         if ((record.seen & AltosRecord.seen_flight) != 0 && flight < 0)
114                                 flight = record.flight;
115                         new_state = new AltosState(record, state);
116                         end_time = new_state.time;
117                         state = new_state;
118                         if (state.time >= boost_time && state.state < Altos.ao_flight_boost)
119                                 state.state = Altos.ao_flight_boost;
120                         if (state.time >= landed_time && state.state < Altos.ao_flight_landed)
121                                 state.state = Altos.ao_flight_landed;
122                         if (0 <= state.state && state.state < Altos.ao_flight_invalid) {
123                                 if (state.state >= Altos.ao_flight_boost) {
124                                         if (state.gps != null && state.gps.locked &&
125                                             year < 0) {
126                                                 year = state.gps.year;
127                                                 month = state.gps.month;
128                                                 day = state.gps.day;
129                                                 hour = state.gps.hour;
130                                                 minute = state.gps.minute;
131                                                 second = state.gps.second;
132                                         }
133                                 }
134                                 state_accel[state.state] += state.acceleration;
135                                 state_speed[state.state] += state.speed;
136                                 state_baro_speed[state.state] += state.baro_speed;
137                                 state_count[state.state]++;
138                                 if (state_start[state.state] == 0.0)
139                                         state_start[state.state] = state.time;
140                                 if (state_end[state.state] < state.time)
141                                         state_end[state.state] = state.time;
142                                 max_height = state.max_height;
143                                 if (state.max_speed != 0)
144                                         max_speed = state.max_speed;
145                                 else
146                                         max_speed = state.max_baro_speed;
147                                 max_acceleration = state.max_acceleration;
148                         }
149                 }
150                 for (int s = Altos.ao_flight_startup; s <= Altos.ao_flight_landed; s++) {
151                         if (state_count[s] > 0) {
152                                 state_speed[s] /= state_count[s];
153                                 state_baro_speed[s] /= state_count[s];
154                                 state_accel[s] /= state_count[s];
155                         }
156                         if (state_start[s] == 0)
157                                 state_start[s] = end_time;
158                         if (state_end[s] == 0)
159                                 state_end[s] = end_time;
160                 }
161         }
162 }