+ /* only loop over the actual events from the dive computer
+ * plus the second synthetic event at the start (to make sure
+ * we get "time=0" right)
+ * sadly with a recent change that first entry may no longer
+ * have any pressure reading - in that case just grab the
+ * pressure from the second entry */
+ if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
+ INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+
+ if (!entry->same_cylinder) {
+ cyl = entry->cylinderindex;
+ if (!seen_cyl[cyl]) {
+ mbar = GET_PRESSURE(entry);
+ plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
+ seen_cyl[cyl] = TRUE;
+ }
+ if (i > 2) {
+ /* remember the last pressure and time of
+ * the previous cylinder */
+ cyl = (entry - 1)->cylinderindex;
+ last_pressure[cyl] = GET_PRESSURE(entry - 1);
+ last_time[cyl] = (entry - 1)->sec;
+ }
+ }
+ }
+ cyl = entry->cylinderindex;
+ if (GET_PRESSURE(entry))
+ last_pressure[cyl] = GET_PRESSURE(entry);
+ last_time[cyl] = entry->sec;
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ if (last_time[cyl]) {
+ plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
+ }
+ }
+}
+
+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->depth;
+ nr = 1;
+ while (++p < last) {
+ int depth = p->depth;
+ if (p->sec > time + seconds)
+ break;
+ avg += depth;
+ nr ++;
+ if (depth < min->depth)
+ min = p;
+ if (depth > max->depth)
+ 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 = GET_PRESSURE(entry);
+ int temperature = entry->temperature;
+
+ if (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; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int depth;
+
+ if (i < nr-2) {
+ depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
+ entry->smoothed = (depth+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].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
+ /* if our samples are short and we aren't too FAST*/
+ if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
+ int past = -2;
+ while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
+ past--;
+ entry->velocity = velocity((entry[0].depth - entry[past].depth) /
+ (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;
+}
+
+/*
+ * simple structure to track the beginning and end tank pressure as
+ * well as the integral of depth over time spent while we have no
+ * pressure reading from the tank */
+typedef struct pr_track_struct pr_track_t;
+struct pr_track_struct {
+ int start;
+ int end;
+ int t_start;
+ int t_end;
+ double pressure_time;
+ pr_track_t *next;
+};
+
+static pr_track_t *pr_track_alloc(int start, int t_start) {
+ pr_track_t *pt = malloc(sizeof(pr_track_t));
+ pt->start = start;
+ pt->t_start = t_start;
+ pt->end = 0;
+ pt->t_end = 0;
+ pt->pressure_time = 0.0;
+ pt->next = NULL;
+ return pt;
+}
+
+/* poor man's linked list */
+static pr_track_t *list_last(pr_track_t *list)
+{
+ pr_track_t *tail = list;
+ if (!tail)
+ return NULL;
+ while (tail->next) {
+ tail = tail->next;
+ }
+ return tail;
+}
+
+static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
+{
+ pr_track_t *tail = list_last(list);
+ if (!tail)
+ return element;
+ tail->next = element;
+ return list;
+}
+
+static void list_free(pr_track_t *list)
+{
+ if (!list)
+ return;
+ list_free(list->next);
+ free(list);
+}
+
+static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
+ pr_track_t **track_pr)
+{
+ pr_track_t *list = NULL;
+ pr_track_t *nlist = NULL;
+ double pt, magic;
+ int cyl, i;
+ struct plot_data *entry;
+ int cur_pr[MAX_CYLINDERS];
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ cur_pr[cyl] = track_pr[cyl]->start;
+ }
+
+ /* The first two are "fillers", but in case we don't have a sample
+ * at time 0 we need to process the second of them here */
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+ if (SENSOR_PRESSURE(entry)) {
+ cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
+ } else {
+ if(!list || list->t_end < entry->sec) {
+ nlist = track_pr[entry->cylinderindex];
+ list = NULL;
+ while (nlist && nlist->t_start <= entry->sec) {
+ list = nlist;
+ nlist = list->next;
+ }
+ /* there may be multiple segments - so
+ * let's assemble the length */
+ nlist = list;
+ pt = list->pressure_time;
+ while (!nlist->end) {
+ nlist = nlist->next;
+ if (!nlist) {
+ /* oops - we have no end pressure,
+ * so this means this is a tank without
+ * gas consumption information */
+ break;
+ }
+ pt += nlist->pressure_time;
+ }
+ if (!nlist) {
+ /* just continue without calculating
+ * interpolated values */
+ INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
+ list = NULL;
+ continue;
+ }
+ magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
+ if (pt != 0.0) {
+ double cur_pt = (entry->sec - (entry-1)->sec) *
+ (1 + entry->depth / 10000.0);
+ INTERPOLATED_PRESSURE(entry) =
+ cur_pr[entry->cylinderindex] + cur_pt * magic;
+ cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
+ } else
+ INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
+ }
+ }
+}
+
+static int get_cylinder_index(struct dive *dive, struct event *ev)
+{
+ int i;
+
+ /*
+ * Try to find a cylinder that matches the O2 percentage
+ * in the gas change event 'value' field.
+ *
+ * Crazy suunto gas change events. We really should do
+ * this in libdivecomputer or something.
+ */
+ for (i = 0; i < MAX_CYLINDERS; i++) {
+ cylinder_t *cyl = dive->cylinder+i;
+ int o2 = (cyl->gasmix.o2.permille + 5) / 10;
+ if (o2 == ev->value)
+ return i;
+ }
+
+ return 0;
+}
+
+static struct event *get_next_gaschange(struct event *event)
+{
+ while (event) {
+ if (!strcmp(event->name, "gaschange"))
+ return event;
+ event = event->next;
+ }
+ return event;
+}
+
+static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
+{
+ while (i < pi->nr) {
+ struct plot_data *entry = pi->entry+i;
+ if (entry->sec > end)
+ break;
+ if (entry->cylinderindex != cylinderindex) {
+ entry->cylinderindex = cylinderindex;
+ entry->pressure[0] = 0;
+ }
+ i++;
+ }
+ return i;
+}
+
+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;