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