Make ao-postflight create filenames using input filenames.
[fw/altos] / ao-tools / ao-postflight / ao-postflight.c
index 6683c67c9f3a0624b4b2bd2435ca9ce65c34c76e..7cedaa5b79ecb0cd36b42e5001ebfbc06de808b4 100644 (file)
@@ -24,8 +24,7 @@
 #include <getopt.h>
 #include "cc-usb.h"
 #include "cc.h"
-
-#define NUM_BLOCK      512
+#include <plplot/plplot.h>
 
 static const char *state_names[] = {
        "startup",
@@ -40,12 +39,160 @@ static const char *state_names[] = {
        "invalid"
 };
 
-void
-analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
+static int plot_colors[3][3] = {
+       { 0, 0x90, 0 }, /* height */
+       { 0xa0, 0, 0 }, /* speed */
+       { 0, 0, 0xc0 }, /* accel */
+};
+
+#define PLOT_HEIGHT    0
+#define PLOT_SPEED     1
+#define PLOT_ACCEL     2
+
+static void
+plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
+               double min_time, double max_time, int plot_type)
+{
+       double  *times;
+       double  ymin, ymax;
+       int     ymin_i, ymax_i;
+       int     i;
+       int     start, stop;
+
+       if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
+               return;
+
+       times = calloc(stop - start + 1, sizeof (double));
+       for (i = start; i <= stop; i++)
+               times[i-start] = i * d->step / 100.0;
+
+       ymin_i = cc_perioddata_min(d, min_time, max_time);
+       ymax_i = cc_perioddata_max(d, min_time, max_time);
+       ymin = d->data[ymin_i];
+       ymax = d->data[ymax_i];
+       plscol0(1, 0, 0, 0);
+       plscol0(2, plot_colors[plot_type][0],  plot_colors[plot_type][1],  plot_colors[plot_type][2]);
+       plcol0(1);
+       plenv(times[0], times[stop-start],
+             ymin, ymax, 0, 2);
+       pllab("Time", axis_label, plot_label);
+       plcol0(2);
+       plline(stop - start + 1, times, d->data + start);
+       free(times);
+}
+
+static void
+plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
+             double min_time, double max_time, int plot_type)
+{
+       double  *times;
+       double  *values;
+       double  ymin, ymax;
+       int     ymin_i, ymax_i;
+       int     i;
+       int     start = -1, stop = -1;
+       double  start_time = 0, stop_time = 0;
+       int     num;
+
+       for (i = 0; i < d->num; i++) {
+               if (start < 0 && d->data[i].time >= min_time) {
+                       start_time = d->data[i].time;
+                       start = i;
+               }
+               if (d->data[i].time <= max_time) {
+                       stop_time = d->data[i].time;
+                       stop = i;
+               }
+       }
+
+       times = calloc(stop - start + 1, sizeof (double));
+       values = calloc(stop - start + 1, sizeof (double));
+
+       ymin_i = cc_timedata_min(d, min_time, max_time);
+       ymax_i = cc_timedata_max(d, min_time, max_time);
+       ymin = d->data[ymin_i].value;
+       ymax = d->data[ymax_i].value;
+       for (i = start; i <= stop; i++) {
+               times[i-start] = (d->data[i].time - start_time)/100.0;
+               values[i-start] = d->data[i].value;
+       }
+       plscol0(1, 0, 0, 0);
+       plscol0(2, plot_colors[plot_type][0],  plot_colors[plot_type][1],  plot_colors[plot_type][2]);
+       plcol0(1);
+       plenv(times[0], times[stop-start], ymin, ymax, 0, 2);
+       pllab("Time", axis_label, plot_label);
+       plcol0(2);
+       plline(stop - start + 1, times, values);
+       free(times);
+       free(values);
+}
+
+static struct cc_perioddata *
+merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
+{
+       int                     i;
+       struct cc_perioddata    *pd;
+       int                     num;
+       double                  start_time, stop_time;
+       double                  t;
+
+       pd = calloc(1, sizeof (struct cc_perioddata));
+       start_time = first->start;
+       stop_time = last->start + last->step * last->num;
+       num = (stop_time - start_time) / first->step;
+       pd->num = num;
+       pd->data = calloc(num, sizeof (double));
+       pd->start = first->start;
+       pd->step = first->step;
+       for (i = 0; i < num; i++) {
+               t = pd->start + i * pd->step;
+               if (t <= split_time) {
+                       pd->data[i] = first->data[i];
+               } else {
+                       int     j;
+
+                       j = (t - last->start) / last->step;
+                       if (j < 0 || j >= last->num)
+                               pd->data[i] = 0;
+                       else
+                               pd->data[i] = last->data[j];
+               }
+       }
+       return pd;
+}
+
+static const char kml_header[] =
+       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+       "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"
+       "  <Placemark>\n"
+       "    <name>gps</name>\n"
+       "    <Style id=\"khStyle690\">\n"
+       "      <LineStyle id=\"khLineStyle694\">\n"
+       "        <color>ff00ffff</color>\n"
+       "        <width>4</width>\n"
+       "      </LineStyle>\n"
+       "      </Style>\n"
+       "    <MultiGeometry id=\"khMultiGeometry697\">\n"
+       "      <LineString id=\"khLineString698\">\n"
+       "        <tessellate>1</tessellate>\n"
+       "        <altitudeMode>absolute</altitudeMode>\n"
+       "        <coordinates>\n";
+
+static const char kml_footer[] =
+       "</coordinates>\n"
+       "    </LineString>\n"
+       "  </MultiGeometry>\n"
+       "</Placemark>\n"
+       "</kml>\n";
+
+static void
+analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file,
+              FILE *raw_file, char *plot_name, FILE *gps_file, FILE *kml_file)
 {
        double  height;
        double  accel;
        double  speed;
+       double  avg_speed;
        double  boost_start, boost_stop;
        double  min_pres;
        int     i;
@@ -57,8 +204,22 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
        struct cc_flightcooked *cooked;
        double  apogee;
 
-       fprintf(summary_file, "Flight:  %9d\nSerial:  %9d\n",
-               f->flight, f->serial);
+       fprintf(summary_file,
+               "Serial:  %9d\n"
+               "Flight:  %9d\n",
+               f->serial, f->flight);
+       if (f->year) {
+               fprintf(summary_file,
+                       "Date:   %04d-%02d-%02d\n",
+                       f->year, f->month, f->day);
+       }
+       if (f->gps.num) {
+               fprintf(summary_file,
+                       "Time:     %2d:%02d:%02d\n",
+                       f->gps.data[0].hour,
+                       f->gps.data[0].minute,
+                       f->gps.data[0].second);
+       }
        boost_start = f->accel.data[0].time;
        boost_stop = f->accel.data[f->accel.num-1].time;
        for (i = 0; i < f->state.num; i++) {
@@ -128,19 +289,25 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
                }
 
                if (cooked) {
-                       if (state_start < apogee) {
-                               speed_i = cc_perioddata_max(&cooked->accel_speed, state_start, state_stop);
+                       if (state < ao_flight_drogue) {
+                               speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
                                if (speed_i >= 0)
                                        speed = cooked->accel_speed.data[speed_i];
+                               avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
                        } else {
-                               speed_i = cc_perioddata_max(&cooked->pres_speed, state_start, state_stop);
+                               speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
                                if (speed_i >= 0)
                                        speed = cooked->pres_speed.data[speed_i];
+                               avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
                        }
                        if (speed_i >= 0)
+                       {
                                fprintf(summary_file, "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
                                       speed, speed * 100 / 2.4 / 12.0,
                                       (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
+                               fprintf(summary_file, "\tAvg speed:  %9.2fm/s  %9.2fft/s\n",
+                                       avg_speed, avg_speed * 100 / 2.4 / 12.0);
+                       }
                }
                pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
                if (pres_i >= 0)
@@ -154,16 +321,10 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
                }
        }
        if (cooked && detail_file) {
-               double  apogee_time;
                double  max_height = 0;
                int     i;
+               double  *times;
 
-               for (i = 0; i < cooked->pres_pos.num; i++) {
-                       if (cooked->pres_pos.data[i] > max_height) {
-                               max_height = cooked->pres_pos.data[i];
-                               apogee_time = cooked->pres_pos.start + cooked->pres_pos.step * i;
-                       }
-               }
                fprintf(detail_file, "%9s %9s %9s %9s\n",
                       "time", "height", "speed", "accel");
                for (i = 0; i < cooked->pres_pos.num; i++) {
@@ -171,7 +332,7 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
                        double  accel = cooked->accel_accel.data[i];
                        double  pos = cooked->pres_pos.data[i];
                        double  speed;
-                       if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee_time)
+                       if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
                                speed = cooked->accel_speed.data[i];
                        else
                                speed = cooked->pres_speed.data[i];
@@ -179,67 +340,234 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
                               time, pos, speed, accel);
                }
        }
+       if (raw_file) {
+               fprintf(raw_file, "%9s %9s %9s\n",
+                      "time", "height", "accel");
+               for (i = 0; i < cooked->pres.num; i++) {
+                       double time = cooked->pres.data[i].time;
+                       double pres = cooked->pres.data[i].value;
+                       double accel = cooked->accel.data[i].value;
+                       fprintf(raw_file, "%9.2f %9.2f %9.2f %9.2f\n",
+                               time, pres, accel);
+               }
+       }
+       if (gps_file) {
+               int     j = 0;
+               fprintf(gps_file, "%9s %12s %12s %12s\n",
+                       "time", "lat", "lon", "alt");
+               for (i = 0; i < f->gps.num; i++) {
+                       int     nsat = 0;
+                       int     k;
+                       while (j < f->gps.numsats - 1) {
+                               if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
+                                   f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
+                                       break;
+                               j++;
+                       }
+                       fprintf(gps_file, "%12.7f %12.7f %12.7f %12.7f",
+                               (f->gps.data[i].time - boost_start) / 100.0,
+                               f->gps.data[i].lat,
+                               f->gps.data[i].lon,
+                               f->gps.data[i].alt);
+                       nsat = 0;
+                       for (k = 0; k < f->gps.sats[j].nsat; k++) {
+                               fprintf (gps_file, " %12.7f", (double) f->gps.sats[j].sat[k].c_n);
+                               if (f->gps.sats[j].sat[k].svid != 0)
+                                       nsat++;
+                       }
+                       fprintf(gps_file, " %d\n", nsat);
+               }
+       }
+       if (kml_file) {
+               int     j = 0;
+
+               fprintf(kml_file, "%s", kml_header);
+               for (i = 0; i < f->gps.num; i++) {
+                       int     nsat = 0;
+                       int     k;
+                       while (j < f->gps.numsats - 1) {
+                               if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
+                                   f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
+                                       break;
+                               j++;
+                       }
+                       nsat = 0;
+                       if (j < f->gps.numsats) {
+                               for (k = 0; k < f->gps.sats[j].nsat; k++)
+                                       if (f->gps.sats[j].sat[k].svid != 0)
+                                               nsat++;
+                       }
+
+                       fprintf(kml_file, "%12.7f, %12.7f, %12.7f <!-- time %12.7f sats %d -->",
+                               f->gps.data[i].lon,
+                               f->gps.data[i].lat,
+                               f->gps.data[i].alt,
+                               (f->gps.data[i].time - boost_start) / 100.0,
+                               nsat);
+                       if (i < f->gps.num - 1)
+                               fprintf(kml_file, ",\n");
+                       else
+                               fprintf(kml_file, "\n");
+               }
+               fprintf(kml_file, "%s", kml_footer);
+       }
+       if (cooked && plot_name) {
+               struct cc_perioddata    *speed;
+               plsdev("svgcairo");
+               plsfnam(plot_name);
+#define PLOT_DPI       96
+               plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
+               plscolbg(0xff, 0xff, 0xff);
+               plscol0(1,0,0,0);
+               plstar(2, 3);
+               speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
+
+               plot_perioddata(&cooked->pres_pos, "meters", "Height",
+                               -1e10, 1e10, PLOT_HEIGHT);
+               plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
+                               boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
+               plot_perioddata(speed, "meters/second", "Speed",
+                               -1e10, 1e10, PLOT_SPEED);
+               plot_perioddata(speed, "meters/second", "Speed to Apogee",
+                               boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
+               plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
+                               -1e10, 1e10, PLOT_ACCEL);
+/*             plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
+               boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
+               plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
+                               boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
+               free(speed->data);
+               free(speed);
+               plend();
+       }
+       if (cooked)
+               cc_flightcooked_free(cooked);
 }
 
 static const struct option options[] = {
-       { .name = "summary", .has_arg = 1, .val = 'S' },
-       { .name = "detail", .has_arg = 1, .val = 'D' },
+       { .name = "summary", .has_arg = 2, .val = 's' },
+       { .name = "detail", .has_arg = 2, .val = 'd' },
+       { .name = "plot", .has_arg = 2, .val = 'p' },
+       { .name = "raw", .has_arg = 2, .val = 'r' },
+       { .name = "gps", .has_arg = 2, .val = 'g' },
+       { .name = "kml", .has_arg = 2, .val = 'k' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--summary=<summary-file>] [--detail=<detail-file] {flight-log} ...\n", program);
+       fprintf(stderr, "usage: %s\n"
+               "\t[--summary=<summary-file>] [-s <summary-file>]\n"
+               "\t[--detail=<detail-file] [-d <detail-file>]\n"
+               "\t[--raw=<raw-file> -r <raw-file]\n"
+               "\t[--plot=<plot-file> -p <plot-file>]\n"
+               "\t[--gps=<gps-file> -g <gps-file>]\n"
+               "\t[--kml=<kml-file> -k <kml-file>]\n"
+               "\t{flight-log} ...\n", program);
        exit(1);
 }
 
+char *
+replace_extension(char *file, char *extension)
+{
+       char    *slash;
+       char    *dot;
+       char    *new;
+       int     newlen;
+
+       slash = strrchr(file, '/');
+       dot = strrchr(file, '.');
+       if (!dot || (slash && dot < slash))
+               dot = file + strlen(file);
+       newlen = (dot - file) + strlen (extension) + 1;
+       new = malloc (newlen);
+       strncpy (new, file, dot - file);
+       new[dot-file] = '\0';
+       strcat (new, extension);
+       return new;
+}
+
+FILE *
+open_output(char *outname, char *inname, char *extension)
+{
+       char    *o;
+       FILE    *out;
+
+       if (outname)
+               o = outname;
+       else
+               o = replace_extension(inname, extension);
+       out = fopen(o, "w");
+       if (!out) {
+               perror (o);
+               exit(1);
+       }
+       if (o != outname)
+               free(o);
+       return out;
+}
+
 int
 main (int argc, char **argv)
 {
        FILE                    *file;
-       FILE                    *summary_file;
-       FILE                    *detail_file;
+       FILE                    *summary_file = NULL;
+       FILE                    *detail_file = NULL;
+       FILE                    *raw_file = NULL;
+       FILE                    *gps_file = NULL;
+       FILE                    *kml_file = NULL;
        int                     i;
        int                     ret = 0;
        struct cc_flightraw     *raw;
        int                     c;
        int                     serial;
        char                    *s;
-       char                    *summary_name = NULL, *detail_name = NULL;
+       char                    *summary_name = NULL;
+       char                    *detail_name = NULL;
+       char                    *raw_name = NULL;
+       char                    *plot_name = NULL;
+       char                    *gps_name = NULL;
+       char                    *kml_name = NULL;
+       int                     has_summary = 0;
+       int                     has_detail = 0;
+       int                     has_plot = 0;
+       int                     has_raw = 0;
+       int                     has_gps = 0;
+       int                     has_kml = 0;
 
-       while ((c = getopt_long(argc, argv, "S:D:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:", options, NULL)) != -1) {
                switch (c) {
-               case 'S':
+               case 's':
                        summary_name = optarg;
+                       has_summary = 1;
                        break;
-               case 'D':
+               case 'd':
                        detail_name = optarg;
+                       has_detail = 1;
+                       break;
+               case 'p':
+                       plot_name = optarg;
+                       has_plot = 1;
+                       break;
+               case 'r':
+                       raw_name = optarg;
+                       has_raw = 1;
+                       break;
+               case 'g':
+                       gps_name = optarg;
+                       has_gps = 1;
+                       break;
+               case 'k':
+                       kml_name = optarg;
+                       has_kml = 1;
                        break;
                default:
                        usage(argv[0]);
                        break;
                }
        }
-       summary_file = stdout;
-       detail_file = NULL;
-       if (summary_name) {
-               summary_file = fopen(summary_name, "w");
-               if (!summary_file) {
-                       perror (summary_name);
-                       exit(1);
-               }
-       }
-       if (detail_name) {
-               if (!strcmp (summary_name, detail_name))
-                       detail_file = summary_file;
-               else {
-                       detail_file = fopen(detail_name, "w");
-                       if (!detail_file) {
-                               perror(detail_name);
-                               exit(1);
-                       }
-               }
-       }
+       if (!has_summary)
+               summary_file = stdout;
        for (i = optind; i < argc; i++) {
                file = fopen(argv[i], "r");
                if (!file) {
@@ -247,6 +575,16 @@ main (int argc, char **argv)
                        ret++;
                        continue;
                }
+               if (has_summary && !summary_file)
+                       summary_file = open_output(summary_name, argv[i], ".summary");
+               if (has_detail && !detail_file)
+                       detail_file = open_output(detail_name, argv[i], ".detail");
+               if (has_raw && !raw_file)
+                       raw_file = open_output(raw_name, argv[i], ".raw");
+               if (has_gps && !gps_file)
+                       gps_file = open_output(gps_name, argv[i], ".gps");
+               if (has_kml && !kml_file)
+                       kml_file = open_output(gps_name, argv[i], ".kml");
                s = strstr(argv[i], "-serial-");
                if (s)
                        serial = atoi(s + 8);
@@ -260,8 +598,23 @@ main (int argc, char **argv)
                }
                if (!raw->serial)
                        raw->serial = serial;
-               analyse_flight(raw, summary_file, detail_file);
+               analyse_flight(raw, summary_file, detail_file, raw_file, plot_name, gps_file, kml_file);
                cc_flightraw_free(raw);
+               if (has_summary && !summary_name) {
+                       fclose(summary_file); summary_file = NULL;
+               }
+               if (has_detail && !detail_name) {
+                       fclose(detail_file); detail_file = NULL;
+               }
+               if (has_summary && !raw_name) {
+                       fclose(raw_file); raw_file = NULL;
+               }
+               if (has_gps && !gps_name) {
+                       fclose(gps_file); gps_file = NULL;
+               }
+               if (has_kml && !kml_name) {
+                       fclose(kml_file); kml_file = NULL;
+               }
        }
        return ret;
 }