altosui: Move number parsing code to Altos general class
[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          *
48          * Flight status
49          *      state
50          *      state name
51          *
52          * Basic sensors
53          *      acceleration (m/s²)
54          *      pressure (mBar)
55          *      altitude (m)
56          *      height (m)
57          *      accelerometer speed (m/s)
58          *      barometer speed (m/s)
59          *      temp (°C)
60          *      battery (V)
61          *      drogue (V)
62          *      main (V)
63          *
64          * GPS data
65          *      connected (1/0)
66          *      locked (1/0)
67          *      nsat (used for solution)
68          *      latitude (°)
69          *      longitude (°)
70          *      altitude (m)
71          *      year (e.g. 2010)
72          *      month (1-12)
73          *      day (1-31)
74          *      hour (0-23)
75          *      minute (0-59)
76          *      second (0-59)
77          *
78          * GPS Sat data
79          *      hdop
80          *      C/N0 data for all 32 valid SDIDs
81          */
82
83         void write_general_header() {
84                 out.printf("version serial flight call time");
85         }
86
87         void write_general(AltosRecord record) {
88                 out.printf("%s,%d,%d,%s,%8.2f",
89                            record.version, record.serial, record.flight, record.callsign,
90                            (double) (record.tick - boost_tick) / 100.0);
91         }
92
93         void write_flight_header() {
94                 out.printf("state state_name");
95         }
96
97         void write_flight(AltosRecord record) {
98                 out.printf("%d,%8s", record.state, record.state());
99         }
100
101         void write_basic_header() {
102                 out.printf("acceleration pressure altitude height accel_speed baro_speed temperature battery_voltage drogue_voltage main_voltage");
103         }
104
105         void write_basic(AltosRecord record) {
106                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
107                            record.acceleration(),
108                            record.pressure(),
109                            record.altitude(),
110                            record.height(),
111                            record.accel_speed(),
112                            state.baro_speed,
113                            record.temperature(),
114                            record.battery_voltage(),
115                            record.drogue_voltage(),
116                            record.main_voltage());
117         }
118
119         void write_gps_header() {
120                 out.printf("connected locked nsat latitude longitude altitude year month day hour minute second");
121         }
122
123         void write_gps(AltosRecord record) {
124                 AltosGPS        gps = record.gps;
125                 if (gps == null)
126                         gps = new AltosGPS();
127
128                 out.printf("%2d,%2d,%3d,%12.7f,%12.7f,%6d,%5d,%3d,%3d,%3d,%3d,%3d",
129                            gps.connected?1:0,
130                            gps.locked?1:0,
131                            gps.nsat,
132                            gps.lat,
133                            gps.lon,
134                            gps.alt,
135                            gps.year,
136                            gps.month,
137                            gps.day,
138                            gps.hour,
139                            gps.minute,
140                            gps.second);
141         }
142
143         void write_header() {
144                 out.printf("# "); write_general_header();
145                 out.printf(" "); write_flight_header();
146                 out.printf(" "); write_basic_header();
147                 out.printf(" "); write_gps_header();
148                 out.printf ("\n");
149         }
150
151         void write_one(AltosRecord record) {
152                 state = new AltosState(record, state);
153                 write_general(record); out.printf(",");
154                 write_flight(record); out.printf(",");
155                 write_basic(record); out.printf(",");
156                 write_gps(record);
157                 out.printf ("\n");
158         }
159
160         void flush_pad() {
161                 while (!pad_records.isEmpty()) {
162                         write_one (pad_records.remove());
163                 }
164         }
165
166         public void write(AltosRecord record) {
167                 if (!header_written) {
168                         write_header();
169                         header_written = true;
170                 }
171                 if (!seen_boost) {
172                         if (record.state >= Altos.ao_flight_boost) {
173                                 seen_boost = true;
174                                 boost_tick = record.tick;
175                                 flush_pad();
176                         }
177                 }
178                 if (seen_boost)
179                         write_one(record);
180                 else
181                         pad_records.add(record);
182         }
183
184         public PrintStream out() {
185                 return out;
186         }
187
188         public void close() {
189                 if (!pad_records.isEmpty()) {
190                         boost_tick = pad_records.element().tick;
191                         flush_pad();
192                 }
193                 out.close();
194         }
195
196         public void write(AltosReader reader) {
197                 AltosRecord     record;
198
199                 reader.write_comments(out());
200                 try {
201                         for (;;) {
202                                 record = reader.read();
203                                 if (record == null)
204                                         break;
205                                 write(record);
206                         }
207                 } catch (IOException ie) {
208                 } catch (ParseException pe) {
209                 }
210         }
211
212         public AltosCSV(File in_name) throws FileNotFoundException {
213                 name = in_name;
214                 out = new PrintStream(name);
215                 pad_records = new LinkedList<AltosRecord>();
216         }
217 }