Stop using SiRF state info.
[fw/altos] / ao-tools / ao-postflight / ao-postflight.c
index 3a6f04b6958b82fa3d3f5e72234d550bf5d8e1b7..c12939aaedb72b966116eb5d1d228df67d27ad93 100644 (file)
@@ -83,7 +83,7 @@ plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
 
 static void
 plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
-               double min_time, double max_time)
+             double min_time, double max_time, int plot_type)
 {
        double  *times;
        double  *values;
@@ -112,13 +112,16 @@ plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
        ymax_i = cc_timedata_max(d, min_time, max_time);
        ymin = d->data[ymin_i].value;
        ymax = d->data[ymax_i].value;
-       plcol0(1);
-       pllab("Time", axis_label, plot_label);
        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);
@@ -158,8 +161,33 @@ merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split
        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)
+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;
@@ -176,8 +204,10 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file, FI
        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);
        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++) {
@@ -309,6 +339,64 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file, FI
                                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;
+                       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");
@@ -320,12 +408,20 @@ analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file, FI
                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, PLOT_HEIGHT);
-               plot_perioddata(speed, "meters/second", "Speed", -1e10, 1e10, PLOT_SPEED);
-               plot_perioddata(speed, "meters/second", "Speed to Apogee", boost_start, apogee, 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_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();
@@ -339,6 +435,8 @@ static const struct option options[] = {
        { .name = "detail", .has_arg = 1, .val = 'd' },
        { .name = "plot", .has_arg = 1, .val = 'p' },
        { .name = "raw", .has_arg = 1, .val = 'r' },
+       { .name = "gps", .has_arg = 1, .val = 'g' },
+       { .name = "kml", .has_arg = 1, .val = 'k' },
        { 0, 0, 0, 0},
 };
 
@@ -349,6 +447,8 @@ static void usage(char *program)
                "\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);
 }
@@ -360,6 +460,8 @@ main (int argc, char **argv)
        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;
@@ -370,8 +472,10 @@ main (int argc, char **argv)
        char                    *detail_name = NULL;
        char                    *raw_name = NULL;
        char                    *plot_name = NULL;
+       char                    *gps_name = NULL;
+       char                    *kml_name = NULL;
 
-       while ((c = getopt_long(argc, argv, "s:d:p:r:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:", options, NULL)) != -1) {
                switch (c) {
                case 's':
                        summary_name = optarg;
@@ -385,6 +489,12 @@ main (int argc, char **argv)
                case 'r':
                        raw_name = optarg;
                        break;
+               case 'g':
+                       gps_name = optarg;
+                       break;
+               case 'k':
+                       kml_name = optarg;
+                       break;
                default:
                        usage(argv[0]);
                        break;
@@ -416,6 +526,20 @@ main (int argc, char **argv)
                        exit(1);
                }
        }
+       if (gps_name) {
+               gps_file = fopen(gps_name, "w");
+               if (!gps_file) {
+                       perror(gps_name);
+                       exit(1);
+               }
+       }
+       if (kml_name) {
+               kml_file = fopen(kml_name, "w");
+               if (!kml_file) {
+                       perror(kml_name);
+                       exit(1);
+               }
+       }
        for (i = optind; i < argc; i++) {
                file = fopen(argv[i], "r");
                if (!file) {
@@ -436,7 +560,7 @@ main (int argc, char **argv)
                }
                if (!raw->serial)
                        raw->serial = serial;
-               analyse_flight(raw, summary_file, detail_file, raw_file, plot_name);
+               analyse_flight(raw, summary_file, detail_file, raw_file, plot_name, gps_file, kml_file);
                cc_flightraw_free(raw);
        }
        return ret;