+/*
+ * 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;
+ }
+ for (i = 0; i < dive->samples; i++) {
+ entry = pi->entry + i + 2;
+ 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 */
+ 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);
+ }
+ }
+ }
+}
+