Subject: [PATCH] CCR patch: Adapt pressure interpolation for CCR (2)

This is the second patch in this series of four, allowing the
calculation of cylinder pressures for CCR equipment.
   Change function fill_missing_tank_pressures in order to
   enable working with the diluent gas, comprising a complete
   set of pressures kept separate from all other tank pressures.
   Flag diluent_flag indicates calculations for the diluent
   cylinder.

Signed-off-by: willem ferguson <[email protected]>

>From 6912b7574c3d7f8c85aa4bbf5d3903a4cc861b6b Mon Sep 17 00:00:00 2001
From: willem ferguson <[email protected]>
Date: Thu, 28 Aug 2014 07:51:16 +0200
Subject: [PATCH] CCR patch: Adapt pressure interpolation for CCR (2)

This is the second pathch in this series of four, allowing the
calculation of cylinder pressures for CCR equipment.
   Change function fill_missing_tank_pressures in order to
   enable working with the diuent gas, comprising a complete
   set of pressures kept separate from all other tank pressures.
   Flag diluent_flag indicates calculations for the diluent
   cylinder.

Signed-off-by: willem ferguson <[email protected]>
---
 gaspressures.c | 84 ++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 52 insertions(+), 32 deletions(-)

diff --git a/gaspressures.c b/gaspressures.c
index fa1d21f..ccfc52c 100644
--- a/gaspressures.c
+++ b/gaspressures.c
@@ -212,69 +212,89 @@ static struct pr_interpolate_struct get_pr_interpolate_data(pr_track_t *segment,
 	return interpolate;
 }
 
-static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi, pr_track_t **track_pr)
+static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi, pr_track_t **track_pr, int diluent_flag)
 {
 	int cyl, i;
 	struct plot_data *entry;
-	int cur_pr[MAX_CYLINDERS];
-	int diluent_flag = 0;
+	int cur_pr[MAX_CYLINDERS];				// cur_pr[MAX_CYLINDERS] is the CCR diluent cylinder
 
-#ifdef DEBUG_PR_TRACK
-	/* another great debugging tool */
-	dump_pr_track(track_pr);
-#endif
 	for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
 		if (!track_pr[cyl]) {
 			/* no segment where this cylinder is used */
 			cur_pr[cyl] = -1;
 			continue;
 		}
-		fill_missing_segment_pressures(track_pr[cyl]);
-		cur_pr[cyl] = track_pr[cyl]->start;
-	}
+		fill_missing_segment_pressures(track_pr[cyl]);	// Interpolate the missing tank pressure values .. 
+		cur_pr[cyl] = track_pr[cyl]->start;		// ..in the pr_track_t lists of structures
+	}							// and keep the starting pressure for each cylinder.
+
+#ifdef DEBUG_PR_TRACK
+	/* another great debugging tool */
+	dump_pr_track(track_pr);
+#endif
 
-	/* The first two are "fillers", but in case we don't have a sample
-	 * at time 0 we need to process the second of them here */
-	for (i = 1; i < pi->nr; i++) {
+	/* Transfer interpolated cylinder pressures from pr_track strucktures to plotdata
+	 * Go down the list of tank pressures in plot_info. Align them with the start &
+	 * end times of each profile segment represented by a pr_track_t structure. Get
+	 * the accumulated pressure_depths from the pr_track_t structures and then 
+	 * interpolate the pressure where these do not exist in the plot_info pressure
+	 * variables. Pressure values are transferred from the pr_track_t structures
+	 * to the plot_info structure, allowing us to plot the tank pressure.
+	 *
+	 * The first two pi structures are "fillers", but in case we don't have a sample
+	 * at time 0 we need to process the second of them here, therefore i=1 */
+	for (i = 1; i < pi->nr; i++) {				// For each point on the profile:
 		double magic;
 		pr_track_t *segment;
 		pr_interpolate_t interpolate;
+		int pressure;
+		int *save_pressure, *save_interpolated;
 
 		entry = pi->entry + i;
-		cyl = entry->cylinderindex;
 
-		if (SENSOR_PRESSURE(entry)) {
-			cur_pr[cyl] = SENSOR_PRESSURE(entry);
-			continue;
+		if (diluent_flag) {				// Find the cylinder index (cyl) ..
+			cyl = DILUENT_CYLINDER;			// .. as well as the cylinder pressure
+			pressure = DILUENT_PRESSURE(entry);
+			save_pressure = &(entry->diluentpressure[SENSOR_PR]);
+			save_interpolated = &(entry->diluentpressure[INTERPOLATED_PR]);
+		}
+		else {
+			pressure = SENSOR_PRESSURE(entry);
+			save_pressure = &(entry->pressure[SENSOR_PR]);
+			save_interpolated = &(entry->pressure[INTERPOLATED_PR]);
+			cyl = entry->cylinderindex;
 		}
 
-		/* Find the right pressure segment for this entry.. */
+		if (pressure) {					// If there is a valid pressure value,
+			cur_pr[cyl] = pressure;			// set current pressure
+			continue;				// and skip to next point.
+}
+								// If there is NO valid pressure value..
+								// Find the pressure segment corresponding to this entry.. 
 		segment = track_pr[cyl];
-		while (segment && segment->t_end < entry->sec)
-			segment = segment->next;
+		while (segment && segment->t_end < entry->sec)  // Find the track_pr with end time..
+			segment = segment->next;		// ..that matches the plot_info time (entry->sec)
 
-		/* No (or empty) segment? Just use our current pressure */
-		if (!segment || !segment->pressure_time) {
-			SENSOR_PRESSURE(entry) = cur_pr[cyl];
-			continue;
+		if (!segment || !segment->pressure_time) {	// No (or empty) segment?
+			*save_pressure = cur_pr[cyl];		// Just use our current pressure
+			continue;				// and skip to next point.
 		}
+								// If there is a valid segment but no tank pressure ..
+		interpolate = get_pr_interpolate_data(segment, pi, i, diluent_flag);	// Set up an interpolation structure
 
-		interpolate = get_pr_interpolate_data(segment, pi, i, diluent_flag);
-#ifdef DEBUG_PR_INTERPOLATE
-		dump_pr_interpolate(i, interpolate);
-#endif
-		/* if this segment has pressure time, calculate a new interpolated pressure */
-		if (interpolate.pressure_time) {
+		/* if this segment has pressure_time, then calculate a new interpolated pressure */
+		if (interpolate.pressure_time) { 
 			/* Overall pressure change over total pressure-time for this segment*/
 			magic = (interpolate.end - interpolate.start) / (double)interpolate.pressure_time;
 
 			/* Use that overall pressure change to update the current pressure */
 			cur_pr[cyl] = rint(interpolate.start + magic * interpolate.acc_pressure_time);
 		}
-		INTERPOLATED_PRESSURE(entry) = cur_pr[cyl];
+		*save_interpolated = cur_pr[cyl]; // and store the interpolated data in plot_info
 	}
 }
 
+
 /*
  * What's the pressure-time between two plot data entries?
  * We're calculating the integral of pressure over time by
@@ -342,7 +362,7 @@ void populate_pressure_information(struct dive *dive, struct divecomputer *dc, s
 	}
 
 	if (missing_pr) {
-		fill_missing_tank_pressures(dive, pi, track_pr);
+		fill_missing_tank_pressures(dive, pi, track_pr, 0);
 	}
 	for (i = 0; i < MAX_CYLINDERS; i++)
 		list_free(track_pr[i]);
-- 
1.9.1

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to