+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)