Merge branch 'master' of git://git.gag.com/fw/altos
[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_azimuth (deg true)
80          *      from_pad_range (m)
81          *      from_pad_elevation (deg from horizon)
82          *
83          * GPS Sat data
84          *      hdop
85          *      C/N0 data for all 32 valid SDIDs
86          */
87
88         void write_general_header() {
89                 out.printf("version,serial,flight,call,time,rssi");
90         }
91
92         void write_general(AltosRecord record) {
93                 out.printf("%s,%d,%d,%s,%8.2f,%4d",
94                            record.version, record.serial, record.flight, record.callsign,
95                            (double) record.time,
96                            record.rssi);
97         }
98
99         void write_flight_header() {
100                 out.printf("state,state_name");
101         }
102
103         void write_flight(AltosRecord record) {
104                 out.printf("%d,%8s", record.state, record.state());
105         }
106
107         void write_basic_header() {
108                 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
109         }
110
111         void write_basic(AltosRecord record) {
112                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
113                            record.acceleration(),
114                            record.raw_pressure(),
115                            record.raw_altitude(),
116                            record.raw_height(),
117                            record.accel_speed(),
118                            state.baro_speed,
119                            record.temperature(),
120                            record.battery_voltage(),
121                            record.drogue_voltage(),
122                            record.main_voltage());
123         }
124
125         void write_gps_header() {
126                 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el");
127         }
128
129         void write_gps(AltosRecord record) {
130                 AltosGPS        gps = record.gps;
131                 if (gps == null)
132                         gps = new AltosGPS();
133
134                 AltosGreatCircle from_pad = state.from_pad;
135                 if (from_pad == null)
136                         from_pad = new AltosGreatCircle();
137
138                 out.printf("%2d,%2d,%3d,%12.7f,%12.7f,%6d,%5d,%3d,%3d,%3d,%3d,%3d,%9.0f,%9.0f,%4.0f,%4.0f",
139                            gps.connected?1:0,
140                            gps.locked?1:0,
141                            gps.nsat,
142                            gps.lat,
143                            gps.lon,
144                            gps.alt,
145                            gps.year,
146                            gps.month,
147                            gps.day,
148                            gps.hour,
149                            gps.minute,
150                            gps.second,
151                            from_pad.distance,
152                            state.range,
153                            from_pad.bearing,
154                            state.elevation);
155         }
156
157         void write_header() {
158                 out.printf("#"); write_general_header();
159                 out.printf(","); write_flight_header();
160                 out.printf(","); write_basic_header();
161                 out.printf(","); write_gps_header();
162                 out.printf ("\n");
163         }
164
165         void write_one(AltosRecord record) {
166                 state = new AltosState(record, state);
167                 write_general(record); out.printf(",");
168                 write_flight(record); out.printf(",");
169                 write_basic(record); out.printf(",");
170                 write_gps(record);
171                 out.printf ("\n");
172         }
173
174         void flush_pad() {
175                 while (!pad_records.isEmpty()) {
176                         write_one (pad_records.remove());
177                 }
178         }
179
180         public void write(AltosRecord record) {
181                 if (!header_written) {
182                         write_header();
183                         header_written = true;
184                 }
185                 if (!seen_boost) {
186                         if (record.state >= Altos.ao_flight_boost) {
187                                 seen_boost = true;
188                                 boost_tick = record.tick;
189                                 flush_pad();
190                         }
191                 }
192                 if (seen_boost)
193                         write_one(record);
194                 else
195                         pad_records.add(record);
196         }
197
198         public PrintStream out() {
199                 return out;
200         }
201
202         public void close() {
203                 if (!pad_records.isEmpty()) {
204                         boost_tick = pad_records.element().tick;
205                         flush_pad();
206                 }
207                 out.close();
208         }
209
210         public void write(AltosReader reader) {
211                 AltosRecord     record;
212
213                 reader.write_comments(out());
214                 try {
215                         for (;;) {
216                                 record = reader.read();
217                                 if (record == null)
218                                         break;
219                                 write(record);
220                         }
221                 } catch (IOException ie) {
222                         System.out.printf("IOException\n");
223                 } catch (ParseException pe) {
224                         System.out.printf("ParseException %s\n", pe.getMessage());
225                 }
226         }
227
228         public AltosCSV(File in_name) throws FileNotFoundException {
229                 name = in_name;
230                 out = new PrintStream(name);
231                 pad_records = new LinkedList<AltosRecord>();
232         }
233
234         public AltosCSV(String in_string) throws FileNotFoundException {
235                 this(new File(in_string));
236         }
237 }