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          *      link quality
49          *
50          * Flight status
51          *      state
52          *      state name
53          *
54          * Basic sensors
55          *      acceleration (m/s²)
56          *      pressure (mBar)
57          *      altitude (m)
58          *      height (m)
59          *      accelerometer speed (m/s)
60          *      barometer speed (m/s)
61          *      temp (°C)
62          *      battery (V)
63          *      drogue (V)
64          *      main (V)
65          *
66          * GPS data
67          *      connected (1/0)
68          *      locked (1/0)
69          *      nsat (used for solution)
70          *      latitude (°)
71          *      longitude (°)
72          *      altitude (m)
73          *      year (e.g. 2010)
74          *      month (1-12)
75          *      day (1-31)
76          *      hour (0-23)
77          *      minute (0-59)
78          *      second (0-59)
79          *      from_pad_dist (m)
80          *      from_pad_azimuth (deg true)
81          *      from_pad_range (m)
82          *      from_pad_elevation (deg from horizon)
83          *      hdop
84          *
85          * GPS Sat data
86          *      C/N0 data for all 32 valid SDIDs
87          */
88
89         void write_general_header() {
90                 out.printf("version,serial,flight,call,time,rssi,lqi");
91         }
92
93         void write_general(AltosRecord record) {
94                 out.printf("%s, %d, %d, %s, %8.2f, %4d, %3d",
95                            record.version, record.serial, record.flight, record.callsign,
96                            (double) record.time,
97                            record.rssi,
98                            record.status & 0x7f);
99         }
100
101         void write_flight_header() {
102                 out.printf("state,state_name");
103         }
104
105         void write_flight(AltosRecord record) {
106                 out.printf("%d,%8s", record.state, record.state());
107         }
108
109         void write_basic_header() {
110                 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
111         }
112
113         void write_basic(AltosRecord record) {
114                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
115                            record.acceleration(),
116                            record.raw_pressure(),
117                            record.raw_altitude(),
118                            record.raw_height(),
119                            record.accel_speed(),
120                            state.baro_speed,
121                            record.temperature(),
122                            record.battery_voltage(),
123                            record.drogue_voltage(),
124                            record.main_voltage());
125         }
126
127         void write_gps_header() {
128                 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
129         }
130
131         void write_gps(AltosRecord record) {
132                 AltosGPS        gps = record.gps;
133                 if (gps == null)
134                         gps = new AltosGPS();
135
136                 AltosGreatCircle from_pad = state.from_pad;
137                 if (from_pad == null)
138                         from_pad = new AltosGreatCircle();
139
140                 out.printf("%2d,%2d,%3d,%12.7f,%12.7f,%6d,%5d,%3d,%3d,%3d,%3d,%3d,%9.0f,%9.0f,%4.0f,%4.0f,%6.1f",
141                            gps.connected?1:0,
142                            gps.locked?1:0,
143                            gps.nsat,
144                            gps.lat,
145                            gps.lon,
146                            gps.alt,
147                            gps.year,
148                            gps.month,
149                            gps.day,
150                            gps.hour,
151                            gps.minute,
152                            gps.second,
153                            from_pad.distance,
154                            state.range,
155                            from_pad.bearing,
156                            state.elevation,
157                            gps.hdop);
158         }
159
160         void write_gps_sat_header() {
161                 for(int i = 1; i <= 32; i++) {
162                         out.printf("sat%02d", i);
163                         if (i != 32)
164                                 out.printf(",");
165                 }
166         }
167
168         void write_gps_sat(AltosRecord record) {
169                 AltosGPS        gps = record.gps;
170                 for(int i = 1; i <= 32; i++) {
171                         int     c_n0 = 0;
172                         if (gps != null && gps.cc_gps_sat != null) {
173                                 for(int j = 0; j < gps.cc_gps_sat.length; j++)
174                                         if (gps.cc_gps_sat[j].svid == i) {
175                                                 c_n0 = gps.cc_gps_sat[j].c_n0;
176                                                 break;
177                                         }
178                         }
179                         out.printf ("%3d", c_n0);
180                         if (i != 32)
181                                 out.printf(",");
182                 }
183         }
184
185         void write_header() {
186                 out.printf("#"); write_general_header();
187                 out.printf(","); write_flight_header();
188                 out.printf(","); write_basic_header();
189                 out.printf(","); write_gps_header();
190                 out.printf(","); write_gps_sat_header();
191                 out.printf ("\n");
192         }
193
194         void write_one(AltosRecord record) {
195                 state = new AltosState(record, state);
196                 write_general(record); out.printf(",");
197                 write_flight(record); out.printf(",");
198                 write_basic(record); out.printf(",");
199                 write_gps(record); out.printf(",");
200                 write_gps_sat(record);
201                 out.printf ("\n");
202         }
203
204         void flush_pad() {
205                 while (!pad_records.isEmpty()) {
206                         write_one (pad_records.remove());
207                 }
208         }
209
210         public void write(AltosRecord record) {
211                 if (!header_written) {
212                         write_header();
213                         header_written = true;
214                 }
215                 if (!seen_boost) {
216                         if (record.state >= Altos.ao_flight_boost) {
217                                 seen_boost = true;
218                                 boost_tick = record.tick;
219                                 flush_pad();
220                         }
221                 }
222                 if (seen_boost)
223                         write_one(record);
224                 else
225                         pad_records.add(record);
226         }
227
228         public PrintStream out() {
229                 return out;
230         }
231
232         public void close() {
233                 if (!pad_records.isEmpty()) {
234                         boost_tick = pad_records.element().tick;
235                         flush_pad();
236                 }
237                 out.close();
238         }
239
240         public void write(AltosRecordIterable iterable) {
241                 iterable.write_comments(out());
242                 for (AltosRecord r : iterable)
243                         write(r);
244         }
245
246         public AltosCSV(File in_name) throws FileNotFoundException {
247                 name = in_name;
248                 out = new PrintStream(name);
249                 pad_records = new LinkedList<AltosRecord>();
250         }
251
252         public AltosCSV(String in_string) throws FileNotFoundException {
253                 this(new File(in_string));
254         }
255 }