]> git.tdb.fi Git - ext/subsurface.git/blobdiff - dive.c
Merge branch 'ui-tweaks' of https://github.com/nathansamson/diveclog
[ext/subsurface.git] / dive.c
diff --git a/dive.c b/dive.c
index f758785d940c65fba7a702f0b5b0b91f9b95e170..7fe6eb17b863f0ceeef887d81636fcb77991a3e8 100644 (file)
--- a/dive.c
+++ b/dive.c
  */
 static void update_depth(depth_t *depth, int new)
 {
-       int old = depth->mm;
+       if (new) {
+               int old = depth->mm;
 
-       if (abs(old - new) > 1000)
-               depth->mm = new;
+               if (abs(old - new) > 1000)
+                       depth->mm = new;
+       }
+}
+
+static void update_pressure(pressure_t *pressure, int new)
+{
+       if (new) {
+               int old = pressure->mbar;
+
+               if (abs(old - new) > 1000)
+                       pressure->mbar = new;
+       }
+}
+
+static void update_duration(duration_t *duration, int new)
+{
+       if (new)
+               duration->seconds = new;
+}
+
+static void update_temperature(temperature_t *temperature, int new)
+{
+       if (new) {
+               int old = temperature->mkelvin;
+
+               if (abs(old - new) > 1000)
+                       temperature->mkelvin = new;
+       }
 }
 
 struct dive *fixup_dive(struct dive *dive)
@@ -36,6 +64,8 @@ struct dive *fixup_dive(struct dive *dive)
        int startpress = 0, endpress = 0;
        int maxdepth = 0, mintemp = 0;
        int lastdepth = 0;
+       int lasttemp = 0;
+       temperature_t *redundant_temp = NULL;
 
        for (i = 0; i < dive->samples; i++) {
                struct sample *sample = dive->sample + i;
@@ -59,6 +89,21 @@ struct dive *fixup_dive(struct dive *dive)
                                startpress = press;
                }
                if (temp) {
+                       /*
+                        * If we have consecutive identical
+                        * temperature readings, throw away
+                        * the redundant ones. We care about
+                        * the "edges" only.
+                        */
+                       if (lasttemp == temp) {
+                               if (redundant_temp)
+                                       redundant_temp->mkelvin = 0;
+                               redundant_temp = &sample->temperature;
+                       } else {
+                               redundant_temp = NULL;
+                               lasttemp = temp;
+                       }
+
                        if (!mintemp || temp < mintemp)
                                mintemp = temp;
                }
@@ -68,18 +113,16 @@ struct dive *fixup_dive(struct dive *dive)
        }
        if (end < 0)
                return dive;
-       dive->duration.seconds = end - start;
+
+       update_duration(&dive->duration, end - start);
        if (start != end)
-               update_depth(&dive->meandepth, depthtime / (end - start));
-       if (startpress)
-               dive->beginning_pressure.mbar = startpress;
-       if (endpress)
-               dive->end_pressure.mbar = endpress;
-       if (mintemp)
-               dive->watertemp.mkelvin = mintemp;
+               depthtime /= (end - start);
 
-       if (maxdepth)
-               update_depth(&dive->maxdepth, maxdepth);
+       update_depth(&dive->meandepth, depthtime);
+       update_pressure(&dive->beginning_pressure, startpress);
+       update_pressure(&dive->end_pressure, endpress);
+       update_temperature(&dive->watertemp, mintemp);
+       update_depth(&dive->maxdepth, maxdepth);
 
        return dive;
 }
@@ -236,7 +279,6 @@ struct dive *try_to_merge(struct dive *a, struct dive *b)
        memset(res, 0, dive_size(alloc_samples));
 
        res->when = a->when;
-       res->name = merge_text(a->name, b->name);
        res->location = merge_text(a->location, b->location);
        res->notes = merge_text(a->notes, b->notes);
        MERGE_MAX(res, a, b, maxdepth.mm);