altosui: Remove debug printf from AltosState.java
[fw/altos] / ao-tools / altosui / AltosCSV.java
1 /*
2  * Copyright © 2010 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.lang.*;
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24
25 import altosui.AltosRecord;
26 import altosui.AltosReader;
27
28 public class AltosCSV {
29         File                    name;
30         PrintStream             out;
31         boolean                 header_written;
32         boolean                 seen_boost;
33         int                     boost_tick;
34         LinkedList<AltosRecord> pad_records;
35         AltosState              state;
36
37         static final int ALTOS_CSV_VERSION = 1;
38
39         /* Version 1 format:
40          *
41          * General info
42          *      version number
43          *      serial number
44          *      flight number
45          *      callsign
46          *      time (seconds since boost)
47          *      rssi
48          *
49          * Flight status
50          *      state
51          *      state name
52          *
53          * Basic sensors
54          *      acceleration (m/s²)
55          *      pressure (mBar)
56          *      altitude (m)
57          *      height (m)
58          *      accelerometer speed (m/s)
59          *      barometer speed (m/s)
60          *      temp (°C)
61          *      battery (V)
62          *      drogue (V)
63          *      main (V)
64          *
65          * GPS data
66          *      connected (1/0)
67          *      locked (1/0)
68          *      nsat (used for solution)
69          *      latitude (°)
70          *      longitude (°)
71          *      altitude (m)
72          *      year (e.g. 2010)
73          *      month (1-12)
74          *      day (1-31)
75          *      hour (0-23)
76          *      minute (0-59)
77          *      second (0-59)
78          *      from_pad_dist (m)
79          *      from_pad_dir (deg true)
80          *
81          * GPS Sat data
82          *      hdop
83          *      C/N0 data for all 32 valid SDIDs
84          */
85
86         void write_general_header() {
87                 out.printf("version serial flight call time rssi");
88         }
89
90         void write_general(AltosRecord record) {
91                 out.printf("%s,%d,%d,%s,%8.2f,%4d",
92                            record.version, record.serial, record.flight, record.callsign,
93                            (double) record.time,
94                            record.rssi);
95         }
96
97         void write_flight_header() {
98                 out.printf("state state_name");
99         }
100
101         void write_flight(AltosRecord record) {
102                 out.printf("%d,%8s", record.state, record.state());
103         }
104
105         void write_basic_header() {
106                 out.printf("acceleration pressure altitude height accel_speed baro_speed temperature battery_voltage drogue_voltage main_voltage");
107         }
108
109         void write_basic(AltosRecord record) {
110                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
111                            record.acceleration(),
112                            record.pressure(),
113                            record.altitude(),
114                            record.height(),
115                            record.accel_speed(),
116                            state.baro_speed,
117                            record.temperature(),
118                            record.battery_voltage(),
119                            record.drogue_voltage(),
120                            record.main_voltage());
121         }
122
123         void write_gps_header() {
124                 out.printf("connected locked nsat latitude longitude altitude year month day hour minute second pad_dist pad_dir");
125         }
126
127         void write_gps(AltosRecord record) {
128                 AltosGPS        gps = record.gps;
129                 if (gps == null)
130                         gps = new AltosGPS();
131
132                 AltosGreatCircle from_pad = state.from_pad;
133                 if (from_pad == null)
134                         from_pad = new AltosGreatCircle();
135
136                 out.printf("%2d,%2d,%3d,%12.7f,%12.7f,%6d,%5d,%3d,%3d,%3d,%3d,%3d,%9.0f,%4.0f",
137                            gps.connected?1:0,
138                            gps.locked?1:0,
139                            gps.nsat,
140                            gps.lat,
141                            gps.lon,
142                            gps.alt,
143                            gps.year,
144                            gps.month,
145                            gps.day,
146                            gps.hour,
147                            gps.minute,
148                            gps.second,
149                            from_pad.distance,
150                            from_pad.bearing);
151         }
152
153         void write_header() {
154                 out.printf("# "); write_general_header();
155                 out.printf(" "); write_flight_header();
156                 out.printf(" "); write_basic_header();
157                 out.printf(" "); write_gps_header();
158                 out.printf ("\n");
159         }
160
161         void write_one(AltosRecord record) {
162                 state = new AltosState(record, state);
163                 write_general(record); out.printf(",");
164                 write_flight(record); out.printf(",");
165                 write_basic(record); out.printf(",");
166                 write_gps(record);
167                 out.printf ("\n");
168         }
169
170         void flush_pad() {
171                 while (!pad_records.isEmpty()) {
172                         write_one (pad_records.remove());
173                 }
174         }
175
176         public void write(AltosRecord record) {
177                 if (!header_written) {
178                         write_header();
179                         header_written = true;
180                 }
181                 if (!seen_boost) {
182                         if (record.state >= Altos.ao_flight_boost) {
183                                 seen_boost = true;
184                                 boost_tick = record.tick;
185                                 flush_pad();
186                         }
187                 }
188                 if (seen_boost)
189                         write_one(record);
190                 else
191                         pad_records.add(record);
192         }
193
194         public PrintStream out() {
195                 return out;
196         }
197
198         public void close() {
199                 if (!pad_records.isEmpty()) {
200                         boost_tick = pad_records.element().tick;
201                         flush_pad();
202                 }
203                 out.close();
204         }
205
206         public void write(AltosReader reader) {
207                 AltosRecord     record;
208
209                 reader.write_comments(out());
210                 try {
211                         for (;;) {
212                                 record = reader.read();
213                                 if (record == null)
214                                         break;
215                                 write(record);
216                         }
217                 } catch (IOException ie) {
218                         System.out.printf("IOException\n");
219                 } catch (ParseException pe) {
220                         System.out.printf("ParseException %s\n", pe.getMessage());
221                 }
222         }
223
224         public AltosCSV(File in_name) throws FileNotFoundException {
225                 name = in_name;
226                 out = new PrintStream(name);
227                 pad_records = new LinkedList<AltosRecord>();
228         }
229
230         public AltosCSV(String in_string) throws FileNotFoundException {
231                 this(new File(in_string));
232         }
233 }