51bcd6e3f53932755ea7e46e8ea716a2033d84a2
[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 void
189 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file,
190                FILE *raw_file, char *plot_name, FILE *gps_file, FILE *kml_file)
191 {
192         double  height;
193         double  accel;
194         double  speed;
195         double  avg_speed;
196         double  boost_start, boost_stop;
197         double  min_pres;
198         int     i;
199         int     pres_i, accel_i, speed_i;
200         int     boost_start_set = 0;
201         int     boost_stop_set = 0;
202         enum ao_flight_state    state;
203         double  state_start, state_stop;
204         struct cc_flightcooked *cooked;
205         double  apogee;
206
207         fprintf(summary_file,
208                 "Serial:  %9d\n"
209                 "Flight:  %9d\n",
210                 f->serial, f->flight);
211         if (f->year) {
212                 fprintf(summary_file,
213                         "Date:   %04d-%02d-%02d\n",
214                         f->year, f->month, f->day);
215         }
216         if (f->gps.num) {
217                 fprintf(summary_file,
218                         "Time:     %2d:%02d:%02d\n",
219                         f->gps.data[0].hour,
220                         f->gps.data[0].minute,
221                         f->gps.data[0].second);
222         }
223         boost_start = f->accel.data[0].time;
224         boost_stop = f->accel.data[f->accel.num-1].time;
225         for (i = 0; i < f->state.num; i++) {
226                 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
227                         boost_start = f->state.data[i].time;
228                         boost_start_set = 1;
229                 }
230                 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
231                         boost_stop = f->state.data[i].time;
232                         boost_stop_set = 1;
233                 }
234         }
235
236         pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
237                                  f->pres.data[f->pres.num-1].time);
238         if (pres_i >= 0)
239         {
240                 min_pres = f->pres.data[pres_i].value;
241                 height = cc_barometer_to_altitude(min_pres) -
242                         cc_barometer_to_altitude(f->ground_pres);
243                 fprintf(summary_file, "Max height: %9.2fm    %9.2fft   %9.2fs\n",
244                         height, height * 100 / 2.54 / 12,
245                         (f->pres.data[pres_i].time - boost_start) / 100.0);
246                 apogee = f->pres.data[pres_i].time;
247         }
248
249         cooked = cc_flight_cook(f);
250         if (cooked) {
251                 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
252                 if (speed_i >= 0) {
253                         speed = cooked->accel_speed.data[speed_i];
254                         fprintf(summary_file, "Max speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
255                                speed, speed * 100 / 2.4 / 12.0,
256                                (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
257                 }
258         }
259         accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
260         if (accel_i >= 0)
261         {
262                 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
263                                                          f->ground_accel);
264                 fprintf(summary_file, "Max accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
265                         accel, accel /  9.80665,
266                         (f->accel.data[accel_i].time - boost_start) / 100.0);
267         }
268
269         for (i = 0; i < f->state.num; i++) {
270                 state = f->state.data[i].value;
271                 state_start = f->state.data[i].time;
272                 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
273                         i++;
274                 if (i < f->state.num - 1)
275                         state_stop = f->state.data[i + 1].time;
276                 else
277                         state_stop = f->accel.data[f->accel.num-1].time;
278                 fprintf(summary_file, "State: %s\n", state_names[state]);
279                 fprintf(summary_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
280                 fprintf(summary_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
281                 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
282                 if (accel_i >= 0)
283                 {
284                         accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
285                                                                  f->ground_accel);
286                         fprintf(summary_file, "\tMax accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
287                                accel, accel / 9.80665,
288                                (f->accel.data[accel_i].time - boost_start) / 100.0);
289                 }
290
291                 if (cooked) {
292                         if (state < ao_flight_drogue) {
293                                 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
294                                 if (speed_i >= 0)
295                                         speed = cooked->accel_speed.data[speed_i];
296                                 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
297                         } else {
298                                 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
299                                 if (speed_i >= 0)
300                                         speed = cooked->pres_speed.data[speed_i];
301                                 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
302                         }
303                         if (speed_i >= 0)
304                         {
305                                 fprintf(summary_file, "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
306                                        speed, speed * 100 / 2.4 / 12.0,
307                                        (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
308                                 fprintf(summary_file, "\tAvg speed:  %9.2fm/s  %9.2fft/s\n",
309                                         avg_speed, avg_speed * 100 / 2.4 / 12.0);
310                         }
311                 }
312                 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
313                 if (pres_i >= 0)
314                 {
315                         min_pres = f->pres.data[pres_i].value;
316                         height = cc_barometer_to_altitude(min_pres) -
317                                 cc_barometer_to_altitude(f->ground_pres);
318                         fprintf(summary_file, "\tMax height: %9.2fm    %9.2fft   %9.2fs\n",
319                                 height, height * 100 / 2.54 / 12,
320                                 (f->pres.data[pres_i].time - boost_start) / 100.0);
321                 }
322         }
323         if (cooked && detail_file) {
324                 double  max_height = 0;
325                 int     i;
326                 double  *times;
327
328                 fprintf(detail_file, "%9s %9s %9s %9s\n",
329                        "time", "height", "speed", "accel");
330                 for (i = 0; i < cooked->pres_pos.num; i++) {
331                         double  time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
332                         double  accel = cooked->accel_accel.data[i];
333                         double  pos = cooked->pres_pos.data[i];
334                         double  speed;
335                         if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
336                                 speed = cooked->accel_speed.data[i];
337                         else
338                                 speed = cooked->pres_speed.data[i];
339                         fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
340                                time, pos, speed, accel);
341                 }
342         }
343         if (raw_file) {
344                 fprintf(raw_file, "%9s %9s %9s\n",
345                        "time", "height", "accel");
346                 for (i = 0; i < cooked->pres.num; i++) {
347                         double time = cooked->pres.data[i].time;
348                         double pres = cooked->pres.data[i].value;
349                         double accel = cooked->accel.data[i].value;
350                         fprintf(raw_file, "%9.2f %9.2f %9.2f %9.2f\n",
351                                 time, pres, accel);
352                 }
353         }
354         if (gps_file || kml_file) {
355                 int     j = 0, baro_pos;
356                 double  baro_offset;
357                 double  baro = 0.0;
358
359                 if (gps_file)
360                         fprintf(gps_file, "%9s %12s %12s %9s %8s %5s\n",
361                                 "time", "lat", "lon", "alt", "baro", "nsat");
362                 if (kml_file)
363                         fprintf(kml_file, "%s", kml_header);
364                 if (f->gps.num)
365                         baro_offset = f->gps.data[0].alt;
366                 else
367                         baro_offset = 0;
368                 baro_pos = 0;
369                 for (i = 0; i < f->gps.num; i++) {
370                         int     nsat = 0;
371                         int     k;
372                         while (j < f->gps.numsats - 1) {
373                                 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
374                                     f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
375                                         break;
376                                 j++;
377                         }
378                         if (cooked) {
379                                 while (baro_pos < cooked->pres_pos.num) {
380                                         double  baro_time = cooked->accel_accel.start + baro_pos * cooked->accel_accel.step;
381                                         if (baro_time >= f->gps.data[i].time)
382                                                 break;
383                                         baro_pos++;
384                                 }
385                                 if (baro_pos < cooked->pres_pos.num)
386                                         baro = cooked->pres_pos.data[baro_pos];
387                         }
388                         if (gps_file)
389                                 fprintf(gps_file, "%12.7f %12.7f %12.7f %7.1f %7.1f",
390                                         (f->gps.data[i].time - boost_start) / 100.0,
391                                         f->gps.data[i].lat,
392                                         f->gps.data[i].lon,
393                                         f->gps.data[i].alt,
394                                         baro + baro_offset);
395                         if (kml_file) {
396                                 fprintf(kml_file, "%12.7f, %12.7f, %12.7f <!-- alt %12.7f time %12.7f sats %d -->",
397                                         f->gps.data[i].lon,
398                                         f->gps.data[i].lat,
399                                         baro + baro_offset,
400                                         f->gps.data[i].alt,
401                                         (f->gps.data[i].time - boost_start) / 100.0,
402                                         nsat);
403                                 if (i < f->gps.num - 1)
404                                         fprintf(kml_file, ",\n");
405                                 else
406                                         fprintf(kml_file, "\n");
407                         }
408
409                         nsat = 0;
410                         for (k = 0; k < f->gps.sats[j].nsat; k++) {
411                                 if (f->gps.sats[j].sat[k].svid != 0)
412                                         nsat++;
413                         }
414                         if (gps_file) {
415                                 fprintf(gps_file, " %4d", nsat);
416                                 for (k = 0; k < f->gps.sats[j].nsat; k++) {
417                                         if (f->gps.sats[j].sat[k].svid != 0) {
418                                                 fprintf (gps_file, " %3d(%4.1f)",
419                                                          f->gps.sats[j].sat[k].svid,
420                                                          (double) f->gps.sats[j].sat[k].c_n);
421                                         }
422                                 }
423                                 fprintf(gps_file, "\n");
424                         }
425                 }
426                 if (kml_file)
427                         fprintf(kml_file, "%s", kml_footer);
428         }
429         if (cooked && plot_name) {
430                 struct cc_perioddata    *speed;
431                 plsdev("svgcairo");
432                 plsfnam(plot_name);
433 #define PLOT_DPI        96
434                 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
435                 plscolbg(0xff, 0xff, 0xff);
436                 plscol0(1,0,0,0);
437                 plstar(2, 3);
438                 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
439
440                 plot_perioddata(&cooked->pres_pos, "meters", "Height",
441                                 -1e10, 1e10, PLOT_HEIGHT);
442                 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
443                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
444                 plot_perioddata(speed, "meters/second", "Speed",
445                                 -1e10, 1e10, PLOT_SPEED);
446                 plot_perioddata(speed, "meters/second", "Speed to Apogee",
447                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
448                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
449                                 -1e10, 1e10, PLOT_ACCEL);
450 /*              plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
451                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
452                 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
453                                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
454                 free(speed->data);
455                 free(speed);
456                 plend();
457         }
458         if (cooked)
459                 cc_flightcooked_free(cooked);
460 }
461
462 static const struct option options[] = {
463         { .name = "summary", .has_arg = 2, .val = 's' },
464         { .name = "detail", .has_arg = 2, .val = 'd' },
465         { .name = "plot", .has_arg = 2, .val = 'p' },
466         { .name = "raw", .has_arg = 2, .val = 'r' },
467         { .name = "gps", .has_arg = 2, .val = 'g' },
468         { .name = "kml", .has_arg = 2, .val = 'k' },
469         { .name = "all", .has_arg = 0, .val = 'a' },
470         { 0, 0, 0, 0},
471 };
472
473 static void usage(char *program)
474 {
475         fprintf(stderr, "usage: %s\n"
476                 "\t[--all] [-a]\n"
477                 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
478                 "\t[--detail=<detail-file] [-d <detail-file>]\n"
479                 "\t[--raw=<raw-file> -r <raw-file]\n"
480                 "\t[--plot=<plot-file> -p <plot-file>]\n"
481                 "\t[--gps=<gps-file> -g <gps-file>]\n"
482                 "\t[--kml=<kml-file> -k <kml-file>]\n"
483                 "\t{flight-log} ...\n", program);
484         exit(1);
485 }
486
487 char *
488 replace_extension(char *file, char *extension)
489 {
490         char    *slash;
491         char    *dot;
492         char    *new;
493         int     newlen;
494
495         slash = strrchr(file, '/');
496         dot = strrchr(file, '.');
497         if (!dot || (slash && dot < slash))
498                 dot = file + strlen(file);
499         newlen = (dot - file) + strlen (extension) + 1;
500         new = malloc (newlen);
501         strncpy (new, file, dot - file);
502         new[dot-file] = '\0';
503         strcat (new, extension);
504         return new;
505 }
506
507 FILE *
508 open_output(char *outname, char *inname, char *extension)
509 {
510         char    *o;
511         FILE    *out;
512
513         if (outname)
514                 o = outname;
515         else
516                 o = replace_extension(inname, extension);
517         out = fopen(o, "w");
518         if (!out) {
519                 perror (o);
520                 exit(1);
521         }
522         if (o != outname)
523                 free(o);
524         return out;
525 }
526
527 int
528 main (int argc, char **argv)
529 {
530         FILE                    *file;
531         FILE                    *summary_file = NULL;
532         FILE                    *detail_file = NULL;
533         FILE                    *raw_file = NULL;
534         FILE                    *gps_file = NULL;
535         FILE                    *kml_file = NULL;
536         int                     i;
537         int                     ret = 0;
538         struct cc_flightraw     *raw;
539         int                     c;
540         int                     serial;
541         char                    *s;
542         char                    *summary_name = NULL;
543         char                    *detail_name = NULL;
544         char                    *raw_name = NULL;
545         char                    *plot_name = NULL;
546         char                    *gps_name = NULL;
547         char                    *kml_name = NULL;
548         int                     has_summary = 0;
549         int                     has_detail = 0;
550         int                     has_plot = 0;
551         int                     has_raw = 0;
552         int                     has_gps = 0;
553         int                     has_kml = 0;
554         char                    *this_plot_name = NULL;;
555
556         while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:a", options, NULL)) != -1) {
557                 switch (c) {
558                 case 's':
559                         summary_name = optarg;
560                         has_summary = 1;
561                         break;
562                 case 'd':
563                         detail_name = optarg;
564                         has_detail = 1;
565                         break;
566                 case 'p':
567                         plot_name = optarg;
568                         has_plot = 1;
569                         break;
570                 case 'r':
571                         raw_name = optarg;
572                         has_raw = 1;
573                         break;
574                 case 'g':
575                         gps_name = optarg;
576                         has_gps = 1;
577                         break;
578                 case 'k':
579                         kml_name = optarg;
580                         has_kml = 1;
581                         break;
582                 case 'a':
583                         has_summary = has_detail = has_plot = has_raw = has_gps = has_kml = 1;
584                         break;
585                 default:
586                         usage(argv[0]);
587                         break;
588                 }
589         }
590         if (!has_summary)
591                 summary_file = stdout;
592         for (i = optind; i < argc; i++) {
593                 file = fopen(argv[i], "r");
594                 if (!file) {
595                         perror(argv[i]);
596                         ret++;
597                         continue;
598                 }
599                 if (has_summary && !summary_file)
600                         summary_file = open_output(summary_name, argv[i], ".summary");
601                 if (has_detail && !detail_file)
602                         detail_file = open_output(detail_name, argv[i], ".detail");
603                 if (has_plot) {
604                         if (plot_name)
605                                 this_plot_name = plot_name;
606                         else
607                                 this_plot_name = replace_extension(argv[i], ".plot");
608                 }
609                 if (has_raw && !raw_file)
610                         raw_file = open_output(raw_name, argv[i], ".raw");
611                 if (has_gps && !gps_file)
612                         gps_file = open_output(gps_name, argv[i], ".gps");
613                 if (has_kml && !kml_file)
614                         kml_file = open_output(gps_name, argv[i], ".kml");
615                 s = strstr(argv[i], "-serial-");
616                 if (s)
617                         serial = atoi(s + 8);
618                 else
619                         serial = 0;
620                 raw = cc_log_read(file);
621                 if (!raw) {
622                         perror(argv[i]);
623                         ret++;
624                         continue;
625                 }
626                 if (!raw->serial)
627                         raw->serial = serial;
628                 analyse_flight(raw, summary_file, detail_file, raw_file, this_plot_name, gps_file, kml_file);
629                 cc_flightraw_free(raw);
630                 if (has_summary && !summary_name) {
631                         fclose(summary_file); summary_file = NULL;
632                 }
633                 if (has_detail && !detail_name) {
634                         fclose(detail_file); detail_file = NULL;
635                 }
636                 if (this_plot_name && this_plot_name != plot_name) {
637                         free (this_plot_name); this_plot_name = NULL;
638                 }
639                 if (has_raw && !raw_name) {
640                         fclose(raw_file); raw_file = NULL;
641                 }
642                 if (has_gps && !gps_name) {
643                         fclose(gps_file); gps_file = NULL;
644                 }
645                 if (has_kml && !kml_name) {
646                         fclose(kml_file); kml_file = NULL;
647                 }
648         }
649         return ret;
650 }