]> git.gag.com Git - fw/altos/blob - ao-tools/ao-postflight/ao-postflight.c
altos: Add kalman filters for baro-only boards
[fw/altos] / ao-tools / ao-postflight / ao-postflight.c
1 /*
2  * Copyright © 2009 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define _GNU_SOURCE
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include "cc-usb.h"
26 #include "cc.h"
27 #include <plplot/plplot.h>
28
29 static const char *state_names[] = {
30         "startup",
31         "idle",
32         "pad",
33         "boost",
34         "fast",
35         "coast",
36         "drogue",
37         "main",
38         "landed",
39         "invalid"
40 };
41
42 static const char *kml_state_colours[] = {
43         "FF000000",
44         "FF000000",
45         "FF000000",
46         "FF0000FF",
47         "FF4080FF",
48         "FF00FFFF",
49         "FFFF0000",
50         "FF00FF00",
51         "FF000000",
52         "FFFFFFFF"
53 };
54
55 static int plot_colors[3][3] = {
56         { 0, 0x90, 0 }, /* height */
57         { 0xa0, 0, 0 }, /* speed */
58         { 0, 0, 0xc0 }, /* accel */
59 };
60
61 #define PLOT_HEIGHT     0
62 #define PLOT_SPEED      1
63 #define PLOT_ACCEL      2
64
65 static void
66 plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
67                 double min_time, double max_time, int plot_type)
68 {
69         double  *times;
70         double  ymin, ymax;
71         int     ymin_i, ymax_i;
72         int     i;
73         int     start, stop;
74
75         if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
76                 return;
77
78         times = calloc(stop - start + 1, sizeof (double));
79         for (i = start; i <= stop; i++)
80                 times[i-start] = i * d->step / 100.0;
81
82         ymin_i = cc_perioddata_min(d, min_time, max_time);
83         ymax_i = cc_perioddata_max(d, min_time, max_time);
84         ymin = d->data[ymin_i];
85         ymax = d->data[ymax_i];
86         plscol0(1, 0, 0, 0);
87         plscol0(2, plot_colors[plot_type][0],  plot_colors[plot_type][1],  plot_colors[plot_type][2]);
88         plcol0(1);
89         plenv(times[0], times[stop-start],
90               ymin, ymax, 0, 2);
91         pllab("Time", axis_label, plot_label);
92         plcol0(2);
93         plline(stop - start + 1, times, d->data + start);
94         free(times);
95 }
96
97 static void
98 plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
99               double min_time, double max_time, int plot_type)
100 {
101         double  *times;
102         double  *values;
103         double  ymin, ymax;
104         int     ymin_i, ymax_i;
105         int     i;
106         int     start = -1, stop = -1;
107         double  start_time = 0, stop_time = 0;
108         int     num;
109
110         for (i = 0; i < d->num; i++) {
111                 if (start < 0 && d->data[i].time >= min_time) {
112                         start_time = d->data[i].time;
113                         start = i;
114                 }
115                 if (d->data[i].time <= max_time) {
116                         stop_time = d->data[i].time;
117                         stop = i;
118                 }
119         }
120
121         times = calloc(stop - start + 1, sizeof (double));
122         values = calloc(stop - start + 1, sizeof (double));
123
124         ymin_i = cc_timedata_min(d, min_time, max_time);
125         ymax_i = cc_timedata_max(d, min_time, max_time);
126         ymin = d->data[ymin_i].value;
127         ymax = d->data[ymax_i].value;
128         for (i = start; i <= stop; i++) {
129                 times[i-start] = (d->data[i].time - start_time)/100.0;
130                 values[i-start] = d->data[i].value;
131         }
132         plscol0(1, 0, 0, 0);
133         plscol0(2, plot_colors[plot_type][0],  plot_colors[plot_type][1],  plot_colors[plot_type][2]);
134         plcol0(1);
135         plenv(times[0], times[stop-start], ymin, ymax, 0, 2);
136         pllab("Time", axis_label, plot_label);
137         plcol0(2);
138         plline(stop - start + 1, times, values);
139         free(times);
140         free(values);
141 }
142
143 static struct cc_perioddata *
144 merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
145 {
146         int                     i;
147         struct cc_perioddata    *pd;
148         int                     num;
149         double                  start_time, stop_time;
150         double                  t;
151
152         pd = calloc(1, sizeof (struct cc_perioddata));
153         start_time = first->start;
154         stop_time = last->start + last->step * last->num;
155         num = (stop_time - start_time) / first->step;
156         pd->num = num;
157         pd->data = calloc(num, sizeof (double));
158         pd->start = first->start;
159         pd->step = first->step;
160         for (i = 0; i < num; i++) {
161                 t = pd->start + i * pd->step;
162                 if (t <= split_time) {
163                         pd->data[i] = first->data[i];
164                 } else {
165                         int     j;
166
167                         j = (t - last->start) / last->step;
168                         if (j < 0 || j >= last->num)
169                                 pd->data[i] = 0;
170                         else
171                                 pd->data[i] = last->data[j];
172                 }
173         }
174         return pd;
175 }
176
177 static const char kml_header_start[] =
178         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
179         "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
180         "<Document>\n"
181         "  <name>%s</name>\n"
182         "  <description>\n";
183 static const char kml_header_end[] =
184         "  </description>\n"
185         "  <open>0</open>\n";
186
187 static const char kml_style_start[] =
188         "  <Style id=\"ao-flightstate-%s\">\n"
189         "    <LineStyle><color>%s</color><width>4</width></LineStyle>\n"
190         "    <BalloonStyle>\n"
191         "      <text>\n";
192 static const char kml_style_end[] =
193         "      </text>\n"
194         "    </BalloonStyle>\n"
195         "  </Style>\n";
196
197 static const char kml_placemark_start[] =
198         "  <Placemark>\n"
199         "    <name>%s</name>\n"
200         "    <styleUrl>#ao-flightstate-%s</styleUrl>\n"
201         "    <LineString>\n"
202         "      <tessellate>1</tessellate>\n"
203         "      <altitudeMode>absolute</altitudeMode>\n"
204         "      <coordinates>\n";
205
206 static const char kml_coord_fmt[] =
207         "        %12.7f, %12.7f, %12.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
208
209 static const char kml_placemark_end[] =
210         "      </coordinates>\n"
211         "    </LineString>\n"
212         "  </Placemark>\n";
213
214 static const char kml_footer[] =
215         "      </coordinates>\n"
216         "    </LineString>\n"
217         "  </Placemark>\n"
218         "</Document>\n"
219         "</kml>\n";
220
221 static unsigned
222 gps_daytime(struct cc_gpselt *gps)
223 {
224         return ((gps->hour * 60 +
225                  gps->minute) * 60 +
226                 gps->second) * 1000;
227 }
228
229 int
230 daytime_hour(unsigned daytime)
231 {
232         return daytime / 1000 / 60 / 60;
233 }
234
235 int
236 daytime_minute(unsigned daytime)
237 {
238         return (daytime / 1000 / 60) % 60;
239 }
240
241 int
242 daytime_second(unsigned daytime)
243 {
244         return (daytime / 1000) % 60;
245 }
246
247 int
248 daytime_millisecond(unsigned daytime)
249 {
250         return daytime % 1000;
251 }
252
253 static unsigned
254 compute_daytime_ms(double time, struct cc_gpsdata *gps)
255 {
256         int     i;
257         unsigned        gps_start_daytime, gps_stop_daytime;
258
259         if (time <= gps->data[0].time) {
260                 gps_stop_daytime = gps_daytime(&gps->data[0]);
261                 return gps_stop_daytime - (gps->data[0].time - time) * 10;
262         }
263         for (i = 0; i < gps->num - 1; i++)
264                 if (time > gps->data[i].time)
265                         break;
266         gps_start_daytime = gps_daytime(&gps->data[i]);
267         if (i == gps->num - 1) {
268                 return gps_start_daytime + (time - gps->data[i].time) * 10;
269         } else {
270                 unsigned        gps_period_daytime;
271                 double          gps_period_time;
272                 double          time_since_start;
273
274                 gps_stop_daytime = gps_daytime(&gps->data[i + 1]);
275
276                 /* range of gps daytime values */
277                 gps_period_daytime = gps_stop_daytime - gps_start_daytime;
278
279                 /* range of gps time values */
280                 gps_period_time = gps->data[i+1].time - gps->data[i].time;
281
282                 /* sample time after first gps time */
283                 time_since_start = time - gps->data[i].time;
284
285                 return gps_start_daytime +
286                         gps_period_daytime * time_since_start / gps_period_time;
287         }
288 }
289
290 static void
291 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file,
292                FILE *raw_file, char *plot_name, FILE *gps_file, FILE *kml_file)
293 {
294         double  height;
295         double  accel;
296         double  speed;
297         double  avg_speed;
298         double  boost_start, boost_stop;
299         double  min_pres;
300         int     i;
301         int     pres_i, accel_i, speed_i;
302         int     boost_start_set = 0;
303         int     boost_stop_set = 0;
304         enum ao_flight_state    state;
305         double  state_start, state_stop;
306         struct cc_flightcooked *cooked;
307         double  apogee;
308         char    buf[128];
309
310         if (kml_file) {
311                 snprintf(buf, sizeof (buf), "AO Flight#%d S/N: %03d", f->flight, f->serial);
312                 fprintf(kml_file, kml_header_start, buf);
313         }
314
315         fprintf(summary_file,
316                 "Serial:  %9d\n"
317                 "Flight:  %9d\n",
318                 f->serial, f->flight);
319
320         if (f->year) {
321                 snprintf(buf, sizeof (buf),
322                         "Date:   %04d-%02d-%02d\n",
323                         f->year, f->month, f->day);
324                 fprintf(summary_file, "%s", buf);
325                 if (kml_file) fprintf(kml_file, "%s", buf);
326         }
327         if (f->gps.num) {
328                 snprintf(buf, sizeof (buf),
329                         "Time:     %2d:%02d:%02d\n",
330                         f->gps.data[0].hour,
331                         f->gps.data[0].minute,
332                         f->gps.data[0].second);
333                 fprintf(summary_file, "%s", buf);
334                 if (kml_file) fprintf(kml_file, "%s", buf);
335         }
336         boost_start = f->accel.data[0].time;
337         boost_stop = f->accel.data[f->accel.num-1].time;
338         for (i = 0; i < f->state.num; i++) {
339                 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
340                         boost_start = f->state.data[i].time;
341                         boost_start_set = 1;
342                 }
343                 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
344                         boost_stop = f->state.data[i].time;
345                         boost_stop_set = 1;
346                 }
347         }
348
349         pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
350                                  f->pres.data[f->pres.num-1].time);
351         if (pres_i >= 0)
352         {
353                 min_pres = f->pres.data[pres_i].value;
354                 height = cc_barometer_to_altitude(min_pres) -
355                         cc_barometer_to_altitude(f->ground_pres);
356                 apogee = f->pres.data[pres_i].time;
357                 snprintf(buf, sizeof (buf), "Max height: %9.2fm    %9.2fft   %9.2fs\n",
358                         height, height * 100 / 2.54 / 12,
359                         (f->pres.data[pres_i].time - boost_start) / 100.0);
360                 fprintf(summary_file, "%s", buf);
361                 if (kml_file) fprintf(kml_file, "%s", buf);
362         }
363
364         cooked = cc_flight_cook(f);
365         if (cooked) {
366                 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
367                 if (speed_i >= 0) {
368                         speed = cooked->accel_speed.data[speed_i];
369                         snprintf(buf, sizeof (buf), "Max speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
370                                speed, speed * 100 / 2.4 / 12.0,
371                                (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
372                         fprintf(summary_file, "%s", buf);
373                         if (kml_file) fprintf(kml_file, "%s", buf);
374                 }
375         }
376         accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
377         if (accel_i >= 0)
378         {
379                 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
380                                                          f->ground_accel);
381                 snprintf(buf, sizeof (buf), "Max accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
382                         accel, accel /  9.80665,
383                         (f->accel.data[accel_i].time - boost_start) / 100.0);
384                 fprintf(summary_file, "%s", buf);
385                 if (kml_file) fprintf(kml_file, "%s", buf);
386         }
387
388         if (kml_file)
389                 fprintf(kml_file, "%s", kml_header_end);
390
391         for (i = 0; i < f->state.num; i++) {
392                 state = f->state.data[i].value;
393                 state_start = f->state.data[i].time;
394                 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
395                         i++;
396                 if (i < f->state.num - 1)
397                         state_stop = f->state.data[i + 1].time;
398                 else
399                         state_stop = f->accel.data[f->accel.num-1].time;
400                 fprintf(summary_file, "State: %s\n", state_names[state]);
401                 fprintf(summary_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
402                 fprintf(summary_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
403                 if (kml_file) {
404                         fprintf(kml_file, kml_style_start, state_names[state], kml_state_colours[state]);
405                         fprintf(kml_file, "\tState: %s\n", state_names[state]);
406                         fprintf(kml_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
407                         fprintf(kml_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
408                 }
409
410                 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
411                 if (accel_i >= 0)
412                 {
413                         accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
414                                                                  f->ground_accel);
415                         snprintf(buf, sizeof (buf), "\tMax accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
416                                accel, accel / 9.80665,
417                                (f->accel.data[accel_i].time - boost_start) / 100.0);
418                         fprintf(summary_file, "%s", buf);
419                         if (kml_file) fprintf(kml_file, "%s", buf);
420                 }
421
422                 if (cooked) {
423                         if (state < ao_flight_drogue) {
424                                 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
425                                 if (speed_i >= 0)
426                                         speed = cooked->accel_speed.data[speed_i];
427                                 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
428                         } else {
429                                 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
430                                 if (speed_i >= 0)
431                                         speed = cooked->pres_speed.data[speed_i];
432                                 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
433                         }
434                         if (speed_i >= 0)
435                         {
436                                 snprintf(buf, sizeof (buf), "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
437                                        speed, speed * 100 / 2.4 / 12.0,
438                                        (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
439                                 fprintf(summary_file, "%s", buf);
440                                 if (kml_file) fprintf(kml_file, "%s", buf);
441
442                                 snprintf(buf, sizeof (buf), "\tAvg speed:  %9.2fm/s  %9.2fft/s\n",
443                                         avg_speed, avg_speed * 100 / 2.4 / 12.0);
444                                 fprintf(summary_file, "%s", buf);
445                                 if (kml_file) fprintf(kml_file, "%s", buf);
446                         }
447                 }
448                 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
449                 if (pres_i >= 0)
450                 {
451                         min_pres = f->pres.data[pres_i].value;
452                         height = cc_barometer_to_altitude(min_pres) -
453                                 cc_barometer_to_altitude(f->ground_pres);
454                         snprintf(buf, sizeof (buf), "\tMax height: %9.2fm    %9.2fft   %9.2fs\n",
455                                 height, height * 100 / 2.54 / 12,
456                                 (f->pres.data[pres_i].time - boost_start) / 100.0);
457                         fprintf(summary_file, "%s", buf);
458                         if (kml_file) fprintf(kml_file, "%s", buf);
459                 }
460                 if (kml_file) fprintf(kml_file, "%s", kml_style_end);
461         }
462         if (cooked && detail_file) {
463                 double  max_height = 0;
464                 int     i;
465                 double  *times;
466
467                 fprintf(detail_file, "%9s %9s %9s %9s %9s\n",
468                         "time", "height", "speed", "accel", "daytime");
469                 for (i = 0; i < cooked->pres_pos.num; i++) {
470                         double  clock_time = cooked->accel_accel.start + i * cooked->accel_accel.step;
471                         double  time = (clock_time - boost_start) / 100.0;
472                         double  accel = cooked->accel_accel.data[i];
473                         double  pos = cooked->pres_pos.data[i];
474                         double  speed;
475                         unsigned        daytime;
476                         if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
477                                 speed = cooked->accel_speed.data[i];
478                         else
479                                 speed = cooked->pres_speed.data[i];
480                         if (f->gps.num)
481                                 daytime = compute_daytime_ms(clock_time, &f->gps);
482                         else
483                                 daytime = 0;
484                         fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f %02d:%02d:%02d.%03d\n",
485                                 time, pos, speed, accel,
486                                 daytime_hour(daytime),
487                                 daytime_minute(daytime),
488                                 daytime_second(daytime),
489                                 daytime_millisecond(daytime));
490                 }
491         }
492         if (raw_file) {
493                 fprintf(raw_file, "%9s %9s %9s %9s\n",
494                         "time", "height", "accel", "daytime");
495                 for (i = 0; i < cooked->pres.num; i++) {
496                         double time = cooked->pres.data[i].time;
497                         double pres = cooked->pres.data[i].value;
498                         double accel = cooked->accel.data[i].value;
499                         unsigned        daytime;
500                         if (f->gps.num)
501                                 daytime = compute_daytime_ms(time, &f->gps);
502                         else
503                                 daytime = 0;
504                         fprintf(raw_file, "%9.2f %9.2f %9.2f %02d:%02d:%02d.%03d\n",
505                                 time, pres, accel,
506                                 daytime_hour(daytime),
507                                 daytime_minute(daytime),
508                                 daytime_second(daytime),
509                                 daytime_millisecond(daytime));
510                 }
511         }
512         if (gps_file || kml_file) {
513                 int     j = 0, baro_pos;
514                 double  baro_offset;
515                 double  baro = 0.0;
516                 int     state_idx = 0;
517
518                 if (gps_file)
519                         fprintf(gps_file, "%2s %2s %2s %9s %12s %12s %9s %8s %5s\n",
520                                 "hr", "mn", "sc",
521                                 "time", "lat", "lon", "alt", "baro", "nsat");
522                 if (kml_file)
523                         fprintf(kml_file, kml_placemark_start,
524                                 state_names[(int)f->state.data[state_idx].value],
525                                 state_names[(int)f->state.data[state_idx].value]); 
526
527                 if (f->gps.num)
528                         baro_offset = f->gps.data[0].alt;
529                 else
530                         baro_offset = 0;
531                 baro_pos = 0;
532                 for (i = 0; i < f->gps.num; i++) {
533                         int     nsat = 0;
534                         int     k;
535                         while (j < f->gps.numsats - 1) {
536                                 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
537                                     f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
538                                         break;
539                                 j++;
540                         }
541                         if (cooked) {
542                                 while (baro_pos < cooked->pres_pos.num) {
543                                         double  baro_time = cooked->accel_accel.start + baro_pos * cooked->accel_accel.step;
544                                         if (baro_time >= f->gps.data[i].time)
545                                                 break;
546                                         baro_pos++;
547                                 }
548                                 if (baro_pos < cooked->pres_pos.num)
549                                         baro = cooked->pres_pos.data[baro_pos];
550                         }
551                         if (gps_file)
552                                 fprintf(gps_file, "%2d %2d %2d %12.7f %12.7f %12.7f %7.1f %7.1f",
553                                         f->gps.data[i].hour,
554                                         f->gps.data[i].minute,
555                                         f->gps.data[i].second,
556                                         (f->gps.data[i].time - boost_start) / 100.0,
557                                         f->gps.data[i].lat,
558                                         f->gps.data[i].lon,
559                                         f->gps.data[i].alt,
560                                         baro + baro_offset);
561
562                         nsat = 0;
563                         if (f->gps.sats) {
564                                 for (k = 0; k < f->gps.sats[j].nsat; k++) {
565                                         if (f->gps.sats[j].sat[k].svid != 0)
566                                                 nsat++;
567                                 }
568                                 if (gps_file) {
569                                         fprintf(gps_file, " %4d", nsat);
570                                         for (k = 0; k < f->gps.sats[j].nsat; k++) {
571                                                 if (f->gps.sats[j].sat[k].svid != 0) {
572                                                         fprintf (gps_file, " %3d(%4.1f)",
573                                                                  f->gps.sats[j].sat[k].svid,
574                                                                  (double) f->gps.sats[j].sat[k].c_n);
575                                                 }
576                                         }
577                                         fprintf(gps_file, "\n");
578                                 }
579                         }
580
581                         if (kml_file) {
582                                 snprintf(buf, sizeof (buf), kml_coord_fmt,
583                                         f->gps.data[i].lon,
584                                         f->gps.data[i].lat,
585                                         baro + baro_offset,
586                                         f->gps.data[i].alt,
587                                         (f->gps.data[i].time - boost_start) / 100.0,
588                                         nsat);
589                                 fprintf(kml_file, "%s", buf);
590                                 if (state_idx + 1 < f->state.num && f->state.data[state_idx + 1].time <= f->gps.data[i].time) {
591                                         state_idx++;
592                                         if (f->state.data[state_idx - 1].value != f->state.data[state_idx].value) {
593                                                 fprintf(kml_file, "%s", kml_placemark_end);
594                                                 fprintf(kml_file, kml_placemark_start,
595                                                         state_names[(int)f->state.data[state_idx].value],
596                                                         state_names[(int)f->state.data[state_idx].value]); 
597                                                 fprintf(kml_file, "%s", buf);
598                                         }
599                                 }
600                         }
601
602                 }
603                 if (kml_file)
604                         fprintf(kml_file, "%s", kml_footer);
605         }
606         if (cooked && plot_name) {
607                 struct cc_perioddata    *speed;
608                 plsdev("svgcairo");
609                 plsfnam(plot_name);
610 #define PLOT_DPI        96
611                 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
612                 plscolbg(0xff, 0xff, 0xff);
613                 plscol0(1,0,0,0);
614                 plstar(2, 3);
615                 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
616
617                 plot_perioddata(&cooked->pres_pos, "meters", "Height",
618                                 -1e10, 1e10, PLOT_HEIGHT);
619                 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
620                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
621                 plot_perioddata(speed, "meters/second", "Speed",
622                                 -1e10, 1e10, PLOT_SPEED);
623                 plot_perioddata(speed, "meters/second", "Speed to Apogee",
624                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
625                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
626                                 -1e10, 1e10, PLOT_ACCEL);
627 /*              plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
628                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
629                 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
630                                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
631                 free(speed->data);
632                 free(speed);
633                 plend();
634         }
635         if (cooked)
636                 cc_flightcooked_free(cooked);
637 }
638
639 static const struct option options[] = {
640         { .name = "summary", .has_arg = 2, .val = 's' },
641         { .name = "detail", .has_arg = 2, .val = 'd' },
642         { .name = "plot", .has_arg = 2, .val = 'p' },
643         { .name = "raw", .has_arg = 2, .val = 'r' },
644         { .name = "gps", .has_arg = 2, .val = 'g' },
645         { .name = "kml", .has_arg = 2, .val = 'k' },
646         { .name = "all", .has_arg = 0, .val = 'a' },
647         { 0, 0, 0, 0},
648 };
649
650 static void usage(char *program)
651 {
652         fprintf(stderr, "usage: %s\n"
653                 "\t[--all] [-a]\n"
654                 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
655                 "\t[--detail=<detail-file] [-d <detail-file>]\n"
656                 "\t[--raw=<raw-file> -r <raw-file]\n"
657                 "\t[--plot=<plot-file> -p <plot-file>]\n"
658                 "\t[--gps=<gps-file> -g <gps-file>]\n"
659                 "\t[--kml=<kml-file> -k <kml-file>]\n"
660                 "\t{flight-log} ...\n", program);
661         exit(1);
662 }
663
664 char *
665 replace_extension(char *file, char *extension)
666 {
667         char    *slash;
668         char    *dot;
669         char    *new;
670         int     newlen;
671
672         slash = strrchr(file, '/');
673         dot = strrchr(file, '.');
674         if (!dot || (slash && dot < slash))
675                 dot = file + strlen(file);
676         newlen = (dot - file) + strlen (extension) + 1;
677         new = malloc (newlen);
678         strncpy (new, file, dot - file);
679         new[dot-file] = '\0';
680         strcat (new, extension);
681         return new;
682 }
683
684 FILE *
685 open_output(char *outname, char *inname, char *extension)
686 {
687         char    *o;
688         FILE    *out;
689
690         if (outname)
691                 o = outname;
692         else
693                 o = replace_extension(inname, extension);
694         out = fopen(o, "w");
695         if (!out) {
696                 perror (o);
697                 exit(1);
698         }
699         if (o != outname)
700                 free(o);
701         return out;
702 }
703
704 int
705 main (int argc, char **argv)
706 {
707         FILE                    *file;
708         FILE                    *summary_file = NULL;
709         FILE                    *detail_file = NULL;
710         FILE                    *raw_file = NULL;
711         FILE                    *gps_file = NULL;
712         FILE                    *kml_file = NULL;
713         int                     i;
714         int                     ret = 0;
715         struct cc_flightraw     *raw;
716         int                     c;
717         int                     serial;
718         char                    *s;
719         char                    *summary_name = NULL;
720         char                    *detail_name = NULL;
721         char                    *raw_name = NULL;
722         char                    *plot_name = NULL;
723         char                    *gps_name = NULL;
724         char                    *kml_name = NULL;
725         int                     has_summary = 0;
726         int                     has_detail = 0;
727         int                     has_plot = 0;
728         int                     has_raw = 0;
729         int                     has_gps = 0;
730         int                     has_kml = 0;
731         char                    *this_plot_name = NULL;;
732
733         while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:a", options, NULL)) != -1) {
734                 switch (c) {
735                 case 's':
736                         summary_name = optarg;
737                         has_summary = 1;
738                         break;
739                 case 'd':
740                         detail_name = optarg;
741                         has_detail = 1;
742                         break;
743                 case 'p':
744                         plot_name = optarg;
745                         has_plot = 1;
746                         break;
747                 case 'r':
748                         raw_name = optarg;
749                         has_raw = 1;
750                         break;
751                 case 'g':
752                         gps_name = optarg;
753                         has_gps = 1;
754                         break;
755                 case 'k':
756                         kml_name = optarg;
757                         has_kml = 1;
758                         break;
759                 case 'a':
760                         has_summary = has_detail = has_plot = has_raw = has_gps = has_kml = 1;
761                         break;
762                 default:
763                         usage(argv[0]);
764                         break;
765                 }
766         }
767         if (!has_summary)
768                 summary_file = stdout;
769         for (i = optind; i < argc; i++) {
770                 file = fopen(argv[i], "r");
771                 if (!file) {
772                         perror(argv[i]);
773                         ret++;
774                         continue;
775                 }
776                 if (has_summary && !summary_file)
777                         summary_file = open_output(summary_name, argv[i], ".summary");
778                 if (has_detail && !detail_file)
779                         detail_file = open_output(detail_name, argv[i], ".detail");
780                 if (has_plot) {
781                         if (plot_name)
782                                 this_plot_name = plot_name;
783                         else
784                                 this_plot_name = replace_extension(argv[i], ".plot");
785                 }
786                 if (has_raw && !raw_file)
787                         raw_file = open_output(raw_name, argv[i], ".raw");
788                 if (has_gps && !gps_file)
789                         gps_file = open_output(gps_name, argv[i], ".gps");
790                 if (has_kml && !kml_file)
791                         kml_file = open_output(kml_name, argv[i], ".kml");
792                 s = strstr(argv[i], "-serial-");
793                 if (s)
794                         serial = atoi(s + 8);
795                 else
796                         serial = 0;
797                 raw = cc_log_read(file);
798                 if (!raw) {
799                         perror(argv[i]);
800                         ret++;
801                         continue;
802                 }
803                 if (!raw->serial)
804                         raw->serial = serial;
805                 analyse_flight(raw, summary_file, detail_file, raw_file, this_plot_name, gps_file, kml_file);
806                 cc_flightraw_free(raw);
807                 if (has_summary && !summary_name) {
808                         fclose(summary_file); summary_file = NULL;
809                 }
810                 if (has_detail && !detail_name) {
811                         fclose(detail_file); detail_file = NULL;
812                 }
813                 if (this_plot_name && this_plot_name != plot_name) {
814                         free (this_plot_name); this_plot_name = NULL;
815                 }
816                 if (has_raw && !raw_name) {
817                         fclose(raw_file); raw_file = NULL;
818                 }
819                 if (has_gps && !gps_name) {
820                         fclose(gps_file); gps_file = NULL;
821                 }
822                 if (has_kml && !kml_name) {
823                         fclose(kml_file); kml_file = NULL;
824                 }
825         }
826         return ret;
827 }