+static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
+{
+ int i = 0, cylinderindex = 0;
+ struct event *ev = get_next_gaschange(dive->events);
+
+ if (!ev)
+ return;
+
+ do {
+ i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
+ cylinderindex = get_cylinder_index(dive, ev);
+ ev = get_next_gaschange(ev->next);
+ } while (ev);
+ set_cylinder_index(pi, i, cylinderindex, ~0u);
+}
+
+/*
+ * 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 cylinderindex = -1;
+ int lastdepth, lastindex;
+ int i, nr = dive->samples + 4, sec, cyl;
+ size_t alloc_size = plot_info_size(nr);
+ struct plot_info *pi;
+ pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
+ pr_track_t *pr_track, *current;
+ gboolean missing_pr = FALSE;
+ struct plot_data *entry;
+
+ 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;
+
+ entry = pi->entry + i + 2;
+ sec = entry->sec = sample->time.seconds;
+ depth = entry->depth = sample->depth.mm;
+ entry->cylinderindex = sample->cylinderindex;
+ SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
+ entry->temperature = sample->temperature.mkelvin;
+
+ if (depth || lastdepth)
+ lastindex = i+2;
+
+ lastdepth = depth;
+ if (depth > pi->maxdepth)
+ pi->maxdepth = depth;
+ }
+
+ check_gas_change_events(dive, pi);
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
+ track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
+ current = track_pr[dive->sample[0].cylinderindex];
+ for (i = 0; i < dive->samples; i++) {
+ entry = pi->entry + i + 2;
+
+ entry->same_cylinder = entry->cylinderindex == cylinderindex;
+ cylinderindex = entry->cylinderindex;
+
+ /* track the segments per cylinder and their pressure/time integral */
+ if (!entry->same_cylinder) {
+ current->end = SENSOR_PRESSURE(entry-1);
+ current->t_end = (entry-1)->sec;
+ current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
+ track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
+ } else { /* same cylinder */
+ if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
+ (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
+ /* transmitter changed its working status */
+ current->end = SENSOR_PRESSURE(entry-1);
+ current->t_end = (entry-1)->sec;
+ current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
+ track_pr[cylinderindex] =
+ list_add(track_pr[cylinderindex], current);
+ }
+ }
+ /* finally, do the discrete integration to get the SAC rate equivalent */
+ current->pressure_time += (entry->sec - (entry-1)->sec) *
+ (1 + entry->depth / 10000.0);
+ missing_pr |= !SENSOR_PRESSURE(entry);
+ }
+
+ current->t_end = entry->sec;
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
+ int pr = dive->cylinder[cyl].end.mbar;
+ if (pr && track_pr[cyl]) {
+ pr_track = list_last(track_pr[cyl]);
+ pr_track->end = pr;
+ }
+ }
+ 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->endpressure = pi->minpressure = dive->cylinder[0].end.mbar;
+ pi->maxpressure = dive->cylinder[0].start.mbar;
+
+ pi->meandepth = dive->meandepth.mm;
+
+ if (missing_pr) {
+ fill_missing_tank_pressures(dive, pi, track_pr);