From 51003eaed71ea823dc8ce88e7de0e785a50b24cb Mon Sep 17 00:00:00 2001
From: Dirk Hohndel <dirk@hohndel.org>
Date: Wed, 7 Nov 2012 15:06:18 +0100
Subject: [PATCH] Fix partial pressure calculation

The existing implementation failed on dive computers that did gas changes
based on events (instead of tracking them in the sample data like the
Uemis Zurich does that I tested the code with).

This commit moves the calculations slightly later in create_plot_info()
after the gas change events are processed and the plot_info data has been
fixed up. Now this works with the data from Linus' Suunto as well.

Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
---
 profile.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/profile.c b/profile.c
index 419d5a653..9b0dcb600 100644
--- a/profile.c
+++ b/profile.c
@@ -1609,8 +1609,7 @@ static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, str
 	lastindex = 0;
 	lastdepth = -1;
 	for (i = 0; i < nr_samples; i++) {
-		int depth, fo2, fhe;
-		double pressure;
+		int depth;
 		int delay = 0;
 		struct sample *sample = dive_sample+i;
 
@@ -1651,16 +1650,6 @@ static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, str
 		depth = entry->depth = sample->depth.mm;
 		entry->cylinderindex = sample->cylinderindex;
 		SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
-		pressure = (depth + 10000) / 10000.0 * 1.01325;
-		fo2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille;
-		fhe = dive->cylinder[sample->cylinderindex].gasmix.he.permille;
-
-		if (!fo2)
-			fo2 = AIR_PERMILLE;
-		entry->po2 = fo2 / 1000.0 * pressure;
-		entry->phe = fhe / 1000.0 * pressure;
-		entry->pn2 = (1000 - fo2 - fhe) / 1000.0 * pressure;
-
 		entry->temperature = sample->temperature.mkelvin;
 
 		if (depth || lastdepth)
@@ -1686,6 +1675,9 @@ static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, str
 		track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
 	current = track_pr[pi->entry[2].cylinderindex];
 	for (i = 0; i < nr + 1; i++) {
+		int fo2, fhe;
+		double pressure;
+
 		entry = pi->entry + i + 1;
 
 		entry->same_cylinder = entry->cylinderindex == cylinderindex;
@@ -1708,6 +1700,16 @@ static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, str
 					list_add(track_pr[cylinderindex], current);
 			}
 		}
+		pressure = (entry->depth + 10000) / 10000.0 * 1.01325;
+		fo2 = dive->cylinder[cylinderindex].gasmix.o2.permille;
+		fhe = dive->cylinder[cylinderindex].gasmix.he.permille;
+
+		if (!fo2)
+			fo2 = AIR_PERMILLE;
+		entry->po2 = fo2 / 1000.0 * pressure;
+		entry->phe = fhe / 1000.0 * pressure;
+		entry->pn2 = (1000 - fo2 - fhe) / 1000.0 * pressure;
+
 		/* finally, do the discrete integration to get the SAC rate equivalent */
 		current->pressure_time += (entry->sec - (entry-1)->sec) *
 			(1 + (entry->depth + (entry-1)->depth) / 20000.0);