+ line_to(gc, pi->maxtime, pi->minpressure);
+ cairo_stroke(gc->cr);
+}
+
+static int mbar_to_PSI(int mbar)
+{
+ pressure_t p = {mbar};
+ return to_PSI(p);
+}
+
+static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
+{
+ if (get_cylinder_pressure_range(gc, pi)) {
+ int start, end;
+ const char *unit = "bar";
+
+ switch (output_units.pressure) {
+ case PASCAL:
+ start = pi->maxpressure * 100;
+ end = pi->minpressure * 100;
+ unit = "pascal";
+ break;
+ case BAR:
+ start = (pi->maxpressure + 500) / 1000;
+ end = (pi->minpressure + 500) / 1000;
+ unit = "bar";
+ break;
+ case PSI:
+ start = mbar_to_PSI(pi->maxpressure);
+ end = mbar_to_PSI(pi->minpressure);
+ unit = "psi";
+ break;
+ }
+
+ text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
+ plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
+ plot_text(gc, &tro, pi->maxtime, pi->minpressure,
+ "%d %s", end, unit);
+ }
+}
+
+static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
+{
+ struct plot_data *p = entry;
+ int time = entry->sec;
+ int seconds = 90*(index+1);
+ struct plot_data *min, *max;
+ int avg, nr;
+
+ /* Go back 'seconds' in time */
+ while (p > first) {
+ if (p[-1].sec < time - seconds)
+ break;
+ p--;
+ }
+
+ /* Then go forward until we hit an entry past the time */
+ min = max = p;
+ avg = p->val;
+ nr = 1;
+ while (++p < last) {
+ int val = p->val;
+ if (p->sec > time + seconds)
+ break;
+ avg += val;
+ nr ++;
+ if (val < min->val)
+ min = p;
+ if (val > max->val)
+ max = p;
+ }
+ entry->min[index] = min;
+ entry->max[index] = max;
+ entry->avg[index] = (avg + nr/2) / nr;
+}
+
+static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
+{
+ analyze_plot_info_minmax_minute(entry, first, last, 0);
+ analyze_plot_info_minmax_minute(entry, first, last, 1);
+ analyze_plot_info_minmax_minute(entry, first, last, 2);
+}
+
+static velocity_t velocity(int speed)
+{
+ velocity_t v;
+
+ if (speed < -304) /* ascent faster than -60ft/min */
+ v = CRAZY;
+ else if (speed < -152) /* above -30ft/min */
+ v = FAST;
+ else if (speed < -76) /* -15ft/min */
+ v = MODERATE;
+ else if (speed < -25) /* -5ft/min */
+ v = SLOW;
+ else if (speed < 25) /* very hard to find data, but it appears that the recommendations
+ for descent are usually about 2x ascent rate; still, we want
+ stable to mean stable */
+ v = STABLE;
+ else if (speed < 152) /* between 5 and 30ft/min is considered slow */
+ v = SLOW;
+ else if (speed < 304) /* up to 60ft/min is moderate */
+ v = MODERATE;
+ else if (speed < 507) /* up to 100ft/min is fast */
+ v = FAST;
+ else /* more than that is just crazy - you'll blow your ears out */
+ v = CRAZY;
+
+ return v;
+}
+static struct plot_info *analyze_plot_info(struct plot_info *pi)
+{
+ int i;
+ int nr = pi->nr;
+
+ /* Do pressure min/max based on the non-surface data */
+ for (i = 0; i < nr; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int pressure = entry->pressure;
+ int temperature = entry->temperature;
+
+ if (pressure) {
+ if (!pi->minpressure || pressure < pi->minpressure)
+ pi->minpressure = pressure;
+ if (pressure > pi->maxpressure)
+ pi->maxpressure = pressure;
+ }
+
+ if (temperature) {
+ if (!pi->mintemp || temperature < pi->mintemp)
+ pi->mintemp = temperature;
+ if (temperature > pi->maxtemp)
+ pi->maxtemp = temperature;
+ }
+ }
+
+ /* Smoothing function: 5-point triangular smooth */
+ for (i = 2; i < nr-1; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int val;
+
+ if (i < nr-2) {
+ val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
+ entry->smoothed = (val+4) / 9;
+ }
+ /* vertical velocity in mm/sec */
+ /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
+ if (entry[0].sec - entry[-1].sec) {
+ entry->velocity = velocity((entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec));
+ /* if our samples are short and we aren't too FAST*/
+ if (entry[0].sec - entry[-1].sec < 30 && entry->velocity < FAST) {
+ int past = -2;
+ while (pi->entry <= entry-past && entry[0].sec - entry[past].sec < 30)
+ past--;
+ entry->velocity = velocity((entry[0].val - entry[past].val) /
+ (entry[0].sec - entry[past].sec));
+ }
+ } else
+ entry->velocity = STABLE;
+ }
+
+ /* One-, two- and three-minute minmax data */
+ for (i = 0; i < nr; i++) {
+ struct plot_data *entry = pi->entry +i;
+ analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
+ }
+
+ return pi;