Don't crash if --plot isn't passed on ao-postflight command line
[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) {
355                 int     j = 0;
356                 fprintf(gps_file, "%9s %12s %12s %12s\n",
357                         "time", "lat", "lon", "alt");
358                 for (i = 0; i < f->gps.num; i++) {
359                         int     nsat = 0;
360                         int     k;
361                         while (j < f->gps.numsats - 1) {
362                                 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
363                                     f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
364                                         break;
365                                 j++;
366                         }
367                         fprintf(gps_file, "%12.7f %12.7f %12.7f %12.7f",
368                                 (f->gps.data[i].time - boost_start) / 100.0,
369                                 f->gps.data[i].lat,
370                                 f->gps.data[i].lon,
371                                 f->gps.data[i].alt);
372                         nsat = 0;
373                         for (k = 0; k < f->gps.sats[j].nsat; k++) {
374                                 fprintf (gps_file, " %12.7f", (double) f->gps.sats[j].sat[k].c_n);
375                                 if (f->gps.sats[j].sat[k].svid != 0)
376                                         nsat++;
377                         }
378                         fprintf(gps_file, " %d\n", nsat);
379                 }
380         }
381         if (kml_file) {
382                 int     j = 0;
383
384                 fprintf(kml_file, "%s", kml_header);
385                 for (i = 0; i < f->gps.num; i++) {
386                         int     nsat = 0;
387                         int     k;
388                         while (j < f->gps.numsats - 1) {
389                                 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
390                                     f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
391                                         break;
392                                 j++;
393                         }
394                         nsat = 0;
395                         if (j < f->gps.numsats) {
396                                 for (k = 0; k < f->gps.sats[j].nsat; k++)
397                                         if (f->gps.sats[j].sat[k].svid != 0)
398                                                 nsat++;
399                         }
400
401                         fprintf(kml_file, "%12.7f, %12.7f, %12.7f <!-- time %12.7f sats %d -->",
402                                 f->gps.data[i].lon,
403                                 f->gps.data[i].lat,
404                                 f->gps.data[i].alt,
405                                 (f->gps.data[i].time - boost_start) / 100.0,
406                                 nsat);
407                         if (i < f->gps.num - 1)
408                                 fprintf(kml_file, ",\n");
409                         else
410                                 fprintf(kml_file, "\n");
411                 }
412                 fprintf(kml_file, "%s", kml_footer);
413         }
414         if (cooked && plot_name) {
415                 struct cc_perioddata    *speed;
416                 plsdev("svgcairo");
417                 plsfnam(plot_name);
418 #define PLOT_DPI        96
419                 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
420                 plscolbg(0xff, 0xff, 0xff);
421                 plscol0(1,0,0,0);
422                 plstar(2, 3);
423                 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
424
425                 plot_perioddata(&cooked->pres_pos, "meters", "Height",
426                                 -1e10, 1e10, PLOT_HEIGHT);
427                 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
428                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
429                 plot_perioddata(speed, "meters/second", "Speed",
430                                 -1e10, 1e10, PLOT_SPEED);
431                 plot_perioddata(speed, "meters/second", "Speed to Apogee",
432                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
433                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
434                                 -1e10, 1e10, PLOT_ACCEL);
435 /*              plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
436                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
437                 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
438                                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
439                 free(speed->data);
440                 free(speed);
441                 plend();
442         }
443         if (cooked)
444                 cc_flightcooked_free(cooked);
445 }
446
447 static const struct option options[] = {
448         { .name = "summary", .has_arg = 2, .val = 's' },
449         { .name = "detail", .has_arg = 2, .val = 'd' },
450         { .name = "plot", .has_arg = 2, .val = 'p' },
451         { .name = "raw", .has_arg = 2, .val = 'r' },
452         { .name = "gps", .has_arg = 2, .val = 'g' },
453         { .name = "kml", .has_arg = 2, .val = 'k' },
454         { .name = "all", .has_arg = 0, .val = 'a' },
455         { 0, 0, 0, 0},
456 };
457
458 static void usage(char *program)
459 {
460         fprintf(stderr, "usage: %s\n"
461                 "\t[--all] [-a]\n"
462                 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
463                 "\t[--detail=<detail-file] [-d <detail-file>]\n"
464                 "\t[--raw=<raw-file> -r <raw-file]\n"
465                 "\t[--plot=<plot-file> -p <plot-file>]\n"
466                 "\t[--gps=<gps-file> -g <gps-file>]\n"
467                 "\t[--kml=<kml-file> -k <kml-file>]\n"
468                 "\t{flight-log} ...\n", program);
469         exit(1);
470 }
471
472 char *
473 replace_extension(char *file, char *extension)
474 {
475         char    *slash;
476         char    *dot;
477         char    *new;
478         int     newlen;
479
480         slash = strrchr(file, '/');
481         dot = strrchr(file, '.');
482         if (!dot || (slash && dot < slash))
483                 dot = file + strlen(file);
484         newlen = (dot - file) + strlen (extension) + 1;
485         new = malloc (newlen);
486         strncpy (new, file, dot - file);
487         new[dot-file] = '\0';
488         strcat (new, extension);
489         return new;
490 }
491
492 FILE *
493 open_output(char *outname, char *inname, char *extension)
494 {
495         char    *o;
496         FILE    *out;
497
498         if (outname)
499                 o = outname;
500         else
501                 o = replace_extension(inname, extension);
502         out = fopen(o, "w");
503         if (!out) {
504                 perror (o);
505                 exit(1);
506         }
507         if (o != outname)
508                 free(o);
509         return out;
510 }
511
512 int
513 main (int argc, char **argv)
514 {
515         FILE                    *file;
516         FILE                    *summary_file = NULL;
517         FILE                    *detail_file = NULL;
518         FILE                    *raw_file = NULL;
519         FILE                    *gps_file = NULL;
520         FILE                    *kml_file = NULL;
521         int                     i;
522         int                     ret = 0;
523         struct cc_flightraw     *raw;
524         int                     c;
525         int                     serial;
526         char                    *s;
527         char                    *summary_name = NULL;
528         char                    *detail_name = NULL;
529         char                    *raw_name = NULL;
530         char                    *plot_name = NULL;
531         char                    *gps_name = NULL;
532         char                    *kml_name = NULL;
533         int                     has_summary = 0;
534         int                     has_detail = 0;
535         int                     has_plot = 0;
536         int                     has_raw = 0;
537         int                     has_gps = 0;
538         int                     has_kml = 0;
539         char                    *this_plot_name = NULL;;
540
541         while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:a", options, NULL)) != -1) {
542                 switch (c) {
543                 case 's':
544                         summary_name = optarg;
545                         has_summary = 1;
546                         break;
547                 case 'd':
548                         detail_name = optarg;
549                         has_detail = 1;
550                         break;
551                 case 'p':
552                         plot_name = optarg;
553                         has_plot = 1;
554                         break;
555                 case 'r':
556                         raw_name = optarg;
557                         has_raw = 1;
558                         break;
559                 case 'g':
560                         gps_name = optarg;
561                         has_gps = 1;
562                         break;
563                 case 'k':
564                         kml_name = optarg;
565                         has_kml = 1;
566                         break;
567                 case 'a':
568                         has_summary = has_detail = has_plot = has_raw = has_gps = has_kml = 1;
569                         break;
570                 default:
571                         usage(argv[0]);
572                         break;
573                 }
574         }
575         if (!has_summary)
576                 summary_file = stdout;
577         for (i = optind; i < argc; i++) {
578                 file = fopen(argv[i], "r");
579                 if (!file) {
580                         perror(argv[i]);
581                         ret++;
582                         continue;
583                 }
584                 if (has_summary && !summary_file)
585                         summary_file = open_output(summary_name, argv[i], ".summary");
586                 if (has_detail && !detail_file)
587                         detail_file = open_output(detail_name, argv[i], ".detail");
588                 if (has_plot) {
589                         if (plot_name)
590                                 this_plot_name = plot_name;
591                         else
592                                 this_plot_name = replace_extension(argv[i], ".plot");
593                 }
594                 if (has_raw && !raw_file)
595                         raw_file = open_output(raw_name, argv[i], ".raw");
596                 if (has_gps && !gps_file)
597                         gps_file = open_output(gps_name, argv[i], ".gps");
598                 if (has_kml && !kml_file)
599                         kml_file = open_output(gps_name, argv[i], ".kml");
600                 s = strstr(argv[i], "-serial-");
601                 if (s)
602                         serial = atoi(s + 8);
603                 else
604                         serial = 0;
605                 raw = cc_log_read(file);
606                 if (!raw) {
607                         perror(argv[i]);
608                         ret++;
609                         continue;
610                 }
611                 if (!raw->serial)
612                         raw->serial = serial;
613                 analyse_flight(raw, summary_file, detail_file, raw_file, this_plot_name, gps_file, kml_file);
614                 cc_flightraw_free(raw);
615                 if (has_summary && !summary_name) {
616                         fclose(summary_file); summary_file = NULL;
617                 }
618                 if (has_detail && !detail_name) {
619                         fclose(detail_file); detail_file = NULL;
620                 }
621                 if (this_plot_name && this_plot_name != plot_name) {
622                         free (this_plot_name); this_plot_name = NULL;
623                 }
624                 if (has_raw && !raw_name) {
625                         fclose(raw_file); raw_file = NULL;
626                 }
627                 if (has_gps && !gps_name) {
628                         fclose(gps_file); gps_file = NULL;
629                 }
630                 if (has_kml && !kml_name) {
631                         fclose(kml_file); kml_file = NULL;
632                 }
633         }
634         return ret;
635 }