[PATCH] Fix broken average SAC calculation

Miika Turkia miika.turkia at gmail.com
Thu Mar 29 21:32:27 PDT 2012


old_sac_time was always 0 when calculating average air consumption. Thus
the results were incorrect.
---
 statistics.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/statistics.c b/statistics.c
index 65efdf1..dce279c 100644
--- a/statistics.c
+++ b/statistics.c
@@ -73,6 +73,7 @@ typedef struct {
 
 static stats_t stats;
 static stats_t stats_selection;
+int old_sac_time = 0;
 
 
 static void process_dive(struct dive *dp, stats_t *stats)
@@ -93,14 +94,14 @@ static void process_dive(struct dive *dp, stats_t *stats)
 	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;
+		sac_time = old_sac_time + dp->duration.seconds;
 		stats->avg_sac.mliter = (1.0 * old_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;
+		old_sac_time = sac_time;
 	}
 	if (dp->watertemp.mkelvin) {
 		if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
@@ -119,6 +120,7 @@ static void process_all_dives(struct dive *dive, struct dive **prev_dive)
 
 	*prev_dive = NULL;
 	memset(&stats, 0, sizeof(stats));
+	old_sac_time = 0;
 	if (dive_table.nr > 0) {
 		stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
 		stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
@@ -146,6 +148,7 @@ void process_selected_dives(GList *selected_dives, GtkTreeModel *model)
 
 	memset(&stats_selection, 0, sizeof(stats_selection));
 	stats_selection.selection_size = amount_selected;
+	old_sac_time = 0;
 
 	for (i = 0; i < amount_selected; ++i) {
 		GValue value = {0, };
-- 
1.7.5.4



More information about the subsurface mailing list