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