[PATCH] Add a simple table-based otu-calculator

Anton Lundin glance at acc.umu.se
Tue Apr 2 14:15:51 PDT 2013


For dives where divecomputers haven't provided us with a maxCNS, we
calculate our cns accumulated during that dive based on a simple table.
---
 divelist.c |   46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/divelist.c b/divelist.c
index 198e2d6..1d888c0 100644
--- a/divelist.c
+++ b/divelist.c
@@ -712,7 +712,7 @@ static int active_o2(struct dive *dive, struct divecomputer *dc, duration_t time
 	return o2permille;
 }
 
-/* calculate OTU for a dive - this only takes the first diveomputer into account */
+/* calculate OTU for a dive - this only takes the first divecomputer into account */
 static int calculate_otu(struct dive *dive)
 {
 	int i;
@@ -736,6 +736,48 @@ static int calculate_otu(struct dive *dive)
 	}
 	return otu + 0.5;
 }
+/* calculate CNS for a dive - this only takes the first divecomputer into account */
+int const cns_table[][3] = {
+/* po2, Maximum Single Exposure, Maximum 24 hour Exposure */
+	{1600,  45 * 60, 150 * 60},
+	{1500, 120 * 60, 180 * 60},
+	{1400, 150 * 60, 180 * 60},
+	{1300, 180 * 60, 210 * 60},
+	{1200, 210 * 60, 240 * 60},
+	{1100, 240 * 60, 270 * 60},
+	{1000, 300 * 60, 300 * 60},
+	{ 900, 360 * 60, 360 * 60},
+	{ 800, 450 * 60, 450 * 60},
+	{ 700, 570 * 60, 570 * 60},
+	{ 600, 720 * 60, 720 * 60}
+};
+static int calculate_cns(struct dive *dive)
+{
+	int i, j;
+	double cns = 0.0;
+	struct divecomputer *dc = &dive->dc;
+
+	for (i = 1; i < dc->samples; i++) {
+		int t;
+		int po2;
+		struct sample *sample = dc->sample + i;
+		struct sample *psample = sample - 1;
+		t = sample->time.seconds - psample->time.seconds;
+		if (sample->po2) {
+			po2 = sample->po2;
+		} else {
+			int o2 = active_o2(dive, dc, sample->time);
+			po2 = o2 / 1000.0 * depth_to_mbar(sample->depth.mm, dive);
+		}
+		/* Find what table-row we should calculate % for */
+		for (j = 1; j < sizeof(cns_table)/(sizeof(int) * 3); j++)
+			if (po2 > cns_table[j][0])
+				break;
+		j--;
+		cns += ((double)t)/((double)cns_table[j][1]);
+	}
+	return cns * 100;
+}
 /*
  * Return air usage (in liters).
  */
@@ -895,6 +937,8 @@ void update_cylinder_related_info(struct dive *dive)
 	if (dive != NULL) {
 		dive->sac = calculate_sac(dive);
 		dive->otu = calculate_otu(dive);
+		if (dive->maxcns == 0)
+			dive->maxcns = calculate_cns(dive);
 	}
 }
 
-- 
1.7.10.4



More information about the subsurface mailing list