]> git.tdb.fi Git - ext/subsurface.git/blobdiff - profile.c
Fix profile and average depth for freedives
[ext/subsurface.git] / profile.c
index 5ec4998d2388965f65ef2731b424c825eb04c04e..0039867efc29355737294cb1ac02a0cb85743ce6 100644 (file)
--- a/profile.c
+++ b/profile.c
@@ -1,5 +1,5 @@
 /* profile.c */
-/* creates all the necessary data for drawing the dive profile 
+/* creates all the necessary data for drawing the dive profile
  * uses cairo to draw it
  */
 #include <stdio.h>
@@ -468,7 +468,7 @@ static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi
        maxtime = get_maxtime(pi);
        maxdepth = get_maxdepth(pi);
 
-       /* Time markers: at most every 5 min, but no more than 12 markers.
+       /* Time markers: at most every 10 seconds, but no more than 12 markers.
         * We start out with 10 seconds and increment up to 30 minutes,
         * depending on the dive time.
         * This allows for 6h dives - enough (I hope) for even the craziest
@@ -498,7 +498,7 @@ static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi
        if (maxtime < 600) {
                /* Be a bit more verbose with shorter dives */
                for (i = incr; i < maxtime; i += incr)
-                       plot_text(gc, &tro, i, 1, "%d:%d", i/60, i%60);
+                       plot_text(gc, &tro, i, 1, "%02d:%02d", i/60, i%60);
        } else {
                /* Only render the time on every second marker for normal dives */
                for (i = incr; i < maxtime; i += 2 * incr)
@@ -899,7 +899,7 @@ static velocity_t velocity(int speed)
        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 
+                               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 */
@@ -955,7 +955,7 @@ static struct plot_info *analyze_plot_info(struct plot_info *pi)
                                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->velocity = velocity((entry[0].depth - entry[past].depth) /
                                                        (entry[0].sec - entry[past].sec));
                        }
                } else
@@ -967,7 +967,7 @@ static struct plot_info *analyze_plot_info(struct plot_info *pi)
                struct plot_data *entry = pi->entry +i;
                analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
        }
-       
+
        return pi;
 }
 
@@ -1074,16 +1074,18 @@ static void fill_missing_tank_pressures(struct plot_info *pi, pr_track_t **track
                                /* 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;
+                               if (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;
                                        }
-                                       pt += nlist->pressure_time;
                                }
                                if (!nlist) {
                                        /* just continue without calculating
@@ -1380,12 +1382,17 @@ void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, stru
        int nr = dive->samples;
 
        if (!nr) {
+               /* The dive has no samples, so create a few fake ones.  This assumes an
+               ascent/descent rate of 9 m/min, which is just below the limit for FAST. */
                int duration = dive->duration.seconds;
                int maxdepth = dive->maxdepth.mm;
+               int asc_desc_time = dive->maxdepth.mm*60/9000;
+               if (asc_desc_time * 2 >= duration)
+                       asc_desc_time = duration / 2;
                sample = fake;
-               fake[1].time.seconds = duration * 0.05;
+               fake[1].time.seconds = asc_desc_time;
                fake[1].depth.mm = maxdepth;
-               fake[2].time.seconds = duration * 0.95;
+               fake[2].time.seconds = duration - asc_desc_time;
                fake[2].depth.mm = maxdepth;
                fake[3].time.seconds = duration * 1.00;
                nr = 4;