]> git.tdb.fi Git - ext/subsurface.git/blobdiff - statistics.c
Fix profile and average depth for freedives
[ext/subsurface.git] / statistics.c
index 65efdf145dea8d17d55152777d5169b5667998a1..a53617337fa712b8864a3fdc4aee20bd8b7f9682 100644 (file)
@@ -69,6 +69,7 @@ typedef struct {
        unsigned int combined_temp;
        unsigned int combined_count;
        unsigned int selection_size;
+       unsigned int total_sac_time;
 } stats_t;
 
 static stats_t stats;
@@ -90,25 +91,29 @@ static void process_dive(struct dive *dp, stats_t *stats)
                stats->max_depth.mm = dp->maxdepth.mm;
        if (stats->min_depth.mm == 0 || dp->maxdepth.mm < stats->min_depth.mm)
                stats->min_depth.mm = dp->maxdepth.mm;
+       if (dp->watertemp.mkelvin) {
+               if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
+                       stats->min_temp = dp->watertemp.mkelvin;
+               if (dp->watertemp.mkelvin > stats->max_temp)
+                       stats->max_temp = dp->watertemp.mkelvin;
+               stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
+               stats->combined_count++;
+       }
+
+       /* Maybe we should drop zero-duration dives */
+       if (!dp->duration.seconds)
+               return;
        stats->avg_depth.mm = (1.0 * old_tt * stats->avg_depth.mm +
                        dp->duration.seconds * dp->meandepth.mm) / stats->total_time.seconds;
        if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
-               int old_sac_time = sac_time;
-               sac_time += dp->duration.seconds;
-               stats->avg_sac.mliter = (1.0 * old_sac_time * stats->avg_sac.mliter +
+               sac_time = stats->total_sac_time + dp->duration.seconds;
+               stats->avg_sac.mliter = (1.0 * stats->total_sac_time * stats->avg_sac.mliter +
                                dp->duration.seconds * dp->sac) / sac_time ;
                if (dp->sac > stats->max_sac.mliter)
                        stats->max_sac.mliter = dp->sac;
                if (stats->min_sac.mliter == 0 || dp->sac < stats->min_sac.mliter)
                        stats->min_sac.mliter = dp->sac;
-       }
-       if (dp->watertemp.mkelvin) {
-               if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
-                       stats->min_temp = dp->watertemp.mkelvin;
-               if (dp->watertemp.mkelvin > stats->max_temp)
-                       stats->max_temp = dp->watertemp.mkelvin;
-               stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
-               stats->combined_count++;
+               stats->total_sac_time = sac_time;
        }
 }
 
@@ -137,25 +142,22 @@ static void process_all_dives(struct dive *dive, struct dive **prev_dive)
        }
 }
 
-void process_selected_dives(GList *selected_dives, GtkTreeModel *model)
+/* make sure we skip the selected summary entries */
+void process_selected_dives(void)
 {
-       struct dive *dp;
-       unsigned int i;
-       GtkTreeIter iter;
-       GtkTreePath *path;
+       struct dive *dive;
+       unsigned int i, nr;
 
        memset(&stats_selection, 0, sizeof(stats_selection));
-       stats_selection.selection_size = amount_selected;
-
-       for (i = 0; i < amount_selected; ++i) {
-               GValue value = {0, };
-               path = g_list_nth_data(selected_dives, i);
-               if (gtk_tree_model_get_iter(model, &iter, path)) {
-                       gtk_tree_model_get_value(model, &iter, 0, &value);
-                       dp = get_dive(g_value_get_int(&value));
+
+       nr = 0;
+       for_each_dive(i, dive) {
+               if (dive->selected) {
+                       process_dive(dive, &stats_selection);
+                       nr++;
                }
-               process_dive(dp, &stats_selection);
        }
+       stats_selection.selection_size = nr;
 }
 
 static void set_label(GtkWidget *w, const char *fmt, ...)
@@ -209,7 +211,7 @@ static void show_single_dive_stats(struct dive *dive)
        set_label(single_w.date, buf);
        set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
        if (prev_dive)
-               set_label(single_w.surf_intv, 
+               set_label(single_w.surf_intv,
                        get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
        else
                set_label(single_w.surf_intv, "unknown");
@@ -265,14 +267,11 @@ static void show_single_dive_stats(struct dive *dive)
 static void show_total_dive_stats(struct dive *dive)
 {
        double value;
-       int decimals;
+       int decimals, seconds;
        const char *unit;
        stats_t *stats_ptr;
 
-       if (amount_selected < 2)
-               stats_ptr = &stats;
-       else
-               stats_ptr = &stats_selection;
+       stats_ptr = &stats_selection;
 
        set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
        if (stats_ptr->min_temp) {
@@ -286,7 +285,10 @@ static void show_total_dive_stats(struct dive *dive)
                set_label(stats_w.max_temp, "%.1f %s", value, unit);
        }
        set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
-       set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds / stats_ptr->selection_size, 0));
+       seconds = stats_ptr->total_time.seconds;
+       if (stats_ptr->selection_size)
+               seconds /= stats_ptr->selection_size;
+       set_label(stats_w.avg_time, get_time_string(seconds, 0));
        set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
        set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
        value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);