+ double airuse = 0;
+ int i;
+
+ for (i = 0; i < MAX_CYLINDERS; i++) {
+ cylinder_t *cyl = dive->cylinder + i;
+ int size = cyl->type.size.mliter;
+ double kilo_atm;
+
+ if (!size)
+ continue;
+
+ kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
+
+ /* Liters of air at 1 atm == milliliters at 1k atm*/
+ airuse += kilo_atm * size;
+ }
+ return airuse;
+}
+
+static void plot_info(struct dive *dive, struct graphics_context *gc)
+{
+ text_render_options_t tro = {10, 0.2, 1.0, 0.2, RIGHT, BOTTOM};
+ const double liters_per_cuft = 28.317;
+ const char *unit, *format, *desc;
+ double airuse;
+ char buffer1[80];
+ char buffer2[80];
+ int len;
+
+ airuse = calculate_airuse(dive);
+ if (!airuse) {
+ update_air_info(NULL);
+ return;
+ }
+ switch (output_units.volume) {
+ case LITER:
+ unit = "l";
+ format = "vol: %4.0f %s";
+ break;
+ case CUFT:
+ unit = "cuft";
+ format = "vol: %4.2f %s";
+ airuse /= liters_per_cuft;
+ break;
+ }
+ tro.vpos = -1.0;
+ plot_text(gc, &tro, 0.98, 0.98, format, airuse, unit);
+ len = snprintf(buffer1, sizeof(buffer1), format, airuse, unit);
+ tro.vpos = -2.2;
+ if (dive->duration.seconds) {
+ double pressure = 1 + (dive->meandepth.mm / 10000.0);
+ double sac = airuse / pressure * 60 / dive->duration.seconds;
+ plot_text(gc, &tro, 0.98, 0.98, "SAC: %4.2f %s/min", sac, unit);
+ snprintf(buffer1+len, sizeof(buffer1)-len,
+ "\nSAC: %4.2f %s/min", sac, unit);
+ }
+ len = 0;
+ tro.vpos = -3.4;
+ desc = dive->cylinder[0].type.description;
+ if (desc || dive->cylinder[0].gasmix.o2.permille) {
+ int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
+ if (!desc)
+ desc = "";
+ if (!o2)
+ o2 = 21;
+ plot_text(gc, &tro, 0.98, 0.98, "%s (%d%%)", desc, o2);
+ len = snprintf(buffer2, sizeof(buffer2), "%s (%d%%): used ", desc, o2);
+ }
+ snprintf(buffer2+len, sizeof(buffer2)-len, buffer1);
+ update_air_info(buffer2);
+}
+
+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 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 */
+ if (entry[0].sec - entry[-1].sec) {
+ val = (entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec);
+ if (val < -304) /* ascent faster than -60ft/min */
+ entry->velocity = CRAZY;
+ else if (val < -152) /* above -30ft/min */
+ entry->velocity = FAST;
+ else if (val < -76) /* -15ft/min */
+ entry->velocity = MODERATE;
+ else if (val < -25) /* -5ft/min */
+ entry->velocity = SLOW;
+ else if (val < 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 */
+ entry->velocity = STABLE;
+ else if (val < 152) /* between 5 and 30ft/min is considered slow */
+ entry->velocity = SLOW;
+ else if (val < 304) /* up to 60ft/min is moderate */
+ entry->velocity = MODERATE;
+ else if (val < 507) /* up to 100ft/min is fast */
+ entry->velocity = FAST;
+ else /* more than that is just crazy - you'll blow your ears out */
+ entry->velocity = CRAZY;
+ } 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;
+}
+
+/*
+ * Create a plot-info with smoothing and ranged min/max
+ *
+ * This also makes sure that we have extra empty events on both
+ * sides, so that you can do end-points without having to worry
+ * about it.
+ */
+static struct plot_info *create_plot_info(struct dive *dive)
+{
+ int lastdepth, lastindex;
+ int i, nr = dive->samples + 4, sec;
+ size_t alloc_size = plot_info_size(nr);
+ struct plot_info *pi;
+
+ pi = malloc(alloc_size);
+ if (!pi)
+ return pi;
+ memset(pi, 0, alloc_size);
+ pi->nr = nr;
+ sec = 0;
+ lastindex = 0;
+ lastdepth = -1;
+ for (i = 0; i < dive->samples; i++) {
+ int depth;
+ struct sample *sample = dive->sample+i;
+ struct plot_data *entry = pi->entry + i + 2;
+
+ sec = entry->sec = sample->time.seconds;
+ depth = entry->val = sample->depth.mm;
+ entry->pressure = sample->cylinderpressure.mbar;
+ entry->temperature = sample->temperature.mkelvin;
+
+ if (depth || lastdepth)
+ lastindex = i+2;
+
+ lastdepth = depth;
+ if (depth > pi->maxdepth)
+ pi->maxdepth = depth;
+ }
+ if (lastdepth)
+ lastindex = i + 2;
+ /* Fill in the last two entries with empty values but valid times */
+ i = dive->samples + 2;
+ pi->entry[i].sec = sec + 20;
+ pi->entry[i+1].sec = sec + 40;
+
+ pi->nr = lastindex+1;
+ pi->maxtime = pi->entry[lastindex].sec;
+
+ pi->minpressure = dive->cylinder[0].end.mbar;
+ pi->maxpressure = dive->cylinder[0].start.mbar;
+
+ pi->meandepth = dive->meandepth.mm;
+
+ return analyze_plot_info(pi);
+}
+
+void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
+{
+ double topx, topy;
+ struct plot_info *pi = create_plot_info(dive);