]> git.tdb.fi Git - ext/subsurface.git/commitdiff
Do better cylinder information management
authorLinus Torvalds <torvalds@linux-foundation.org>
Sun, 4 Sep 2011 03:31:18 +0000 (20:31 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sun, 4 Sep 2011 03:31:18 +0000 (20:31 -0700)
Instead of just tracking gasmix, track the size and workng pressure of
the cylinder too.

And use "cylinder" instead of "tank" throughout.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
dive.c
dive.h
parse-xml.c
profile.c
save-xml.c

diff --git a/dive.c b/dive.c
index 70d6d4269e50f47690ceec905fbd1c1615d9b11c..9cd6da0ebfa710c284d55b2975720fc0606d0ec7 100644 (file)
--- a/dive.c
+++ b/dive.c
@@ -42,7 +42,7 @@ struct dive *fixup_dive(struct dive *dive)
                struct sample *sample = dive->sample + i;
                int time = sample->time.seconds;
                int depth = sample->depth.mm;
-               int press = sample->tankpressure.mbar;
+               int press = sample->cylinderpressure.mbar;
                int temp = sample->temperature.mkelvin;
 
                if (lastdepth)
@@ -168,10 +168,10 @@ add_sample_b:
                        sample.depth = as->depth;
                if (as->temperature.mkelvin)
                        sample.temperature = as->temperature;
-               if (as->tankpressure.mbar)
-                       sample.tankpressure = as->tankpressure;
-               if (as->tankindex)
-                       sample.tankindex = as->tankindex;
+               if (as->cylinderpressure.mbar)
+                       sample.cylinderpressure = as->cylinderpressure;
+               if (as->cylinderindex)
+                       sample.cylinderindex = as->cylinderindex;
 
                res = add_sample(&sample, at, res);
 
@@ -199,6 +199,27 @@ static char *merge_text(const char *a, const char *b)
        return res;
 }
 
+/* Pick whichever has any info (if either). Prefer 'a' */
+static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
+{
+       if (a->size.mliter)
+               b = a;
+       *res = *b;
+}
+
+static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
+{
+       if (a->o2.permille)
+               b = a;
+       *res = *b;
+}
+
+static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
+{
+       merge_cylinder_type(&res->type, &a->type, &b->type);
+       merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
+}
+
 /*
  * This could do a lot more merging. Right now it really only
  * merges almost exact duplicates - something that happens easily
@@ -230,12 +251,8 @@ struct dive *try_to_merge(struct dive *a, struct dive *b)
        MERGE_MIN(res, a, b, watertemp.mkelvin);
        MERGE_MAX(res, a, b, beginning_pressure.mbar);
        MERGE_MAX(res, a, b, end_pressure.mbar);
-       for (i = 0; i < MAX_MIXES; i++) {
-               if (a->gasmix[i].o2.permille) {
-                       res->gasmix[i] = a->gasmix[i];
-                       continue;
-               }
-               res->gasmix[i] = b->gasmix[i];
-       }
+       for (i = 0; i < MAX_CYLINDERS; i++)
+               merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
+
        return merge_samples(res, a, b, 0);
 }
diff --git a/dive.h b/dive.h
index e54d3a8624361e7cb9a1085a35c576cf62bf3536..c6c545dcc11e133a748113f8529fce9a5128aeb6 100644 (file)
--- a/dive.h
+++ b/dive.h
@@ -14,7 +14,7 @@
  *
  * We also strive to make '0' a meaningless number saying "not
  * initialized", since many values are things that may not have
- * been reported (eg tank pressure or temperature from dive
+ * been reported (eg cylinder pressure or temperature from dive
  * computers that don't support them). But sometimes -1 is an even
  * more explicit way of saying "not there".
  *
@@ -22,7 +22,7 @@
  * temperatures. Doing temperatures in celsius or fahrenheit would
  * make for loss of precision when converting from one to the other,
  * and using millikelvin is SI-like but also means that a temperature
- * of '0' is clearly just a missing temperature or tank pressure.
+ * of '0' is clearly just a missing temperature or cylinder pressure.
  *
  * Also strive to use units that can not possibly be mistaken for a
  * valid value in a "normal" system without conversion. If the max
@@ -74,8 +74,13 @@ typedef struct {
 
 typedef struct {
        volume_t size;
-       pressure_t pressure;
-} tank_type_t;
+       pressure_t workingpressure;
+} cylinder_type_t;
+
+typedef struct {
+       cylinder_type_t type;
+       gasmix_t gasmix;
+} cylinder_t;
 
 static inline int to_feet(depth_t depth)
 {
@@ -98,11 +103,11 @@ struct sample {
        duration_t time;
        depth_t depth;
        temperature_t temperature;
-       pressure_t tankpressure;
-       int tankindex;
+       pressure_t cylinderpressure;
+       int cylinderindex;
 };
 
-#define MAX_MIXES (4)
+#define MAX_CYLINDERS (4)
 
 struct dive {
        const char *name;
@@ -114,7 +119,7 @@ struct dive {
        depth_t visibility;
        temperature_t airtemp, watertemp;
        pressure_t beginning_pressure, end_pressure;
-       gasmix_t gasmix[MAX_MIXES];
+       cylinder_t cylinder[MAX_CYLINDERS];
        int samples;
        struct sample sample[];
 };
index e6ace216fb7c17336f3f4e9456dc259ba5d55469..635bf56f208b24730761c8e60143dc54e064bd4a 100644 (file)
@@ -92,7 +92,7 @@ static struct dive *dive;
 static struct sample *sample;
 static struct tm tm;
 static int suunto, uemis;
-static int event_index, gasmix_index;
+static int event_index, cylinder_index;
 
 static time_t utc_mktime(struct tm *tm)
 {
@@ -349,7 +349,7 @@ static void gasmix(char *buffer, void *_fraction)
        /* libdivecomputer does negative percentages. */
        if (*buffer == '-')
                return;
-       if (gasmix_index < MAX_MIXES)
+       if (cylinder_index < MAX_CYLINDERS)
                percent(buffer, _fraction);
 }
 
@@ -358,6 +358,23 @@ static void gasmix_nitrogen(char *buffer, void *_gasmix)
        /* Ignore n2 percentages. There's no value in them. */
 }
 
+static void cylindersize(char *buffer, void *_volume)
+{
+       volume_t *volume = _volume;
+       union int_or_float val;
+
+       switch (integer_or_float(buffer, &val)) {
+       case FLOAT:
+               volume->mliter = val.fp * 1000 + 0.5;
+               break;
+
+       default:
+               printf("Strange volume reading %s\n", buffer);
+               break;
+       }
+       free(buffer);
+}
+
 static void utf8_string(char *buffer, void *_res)
 {
        *(char **)_res = buffer;
@@ -444,8 +461,8 @@ static int uemis_fill_sample(struct sample *sample, const char *name, int len, c
 {
        return  MATCH(".reading.dive_time", sampletime, &sample->time) ||
                MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
-               MATCH(".reading.active_tank", get_index, &sample->tankindex) ||
-               MATCH(".reading.tank_pressure", centibar, &sample->tankpressure) ||
+               MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
+               MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
                MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
                0;
 }
@@ -456,9 +473,9 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu
        int len = strlen(name);
 
        start_match("sample", name, buf);
-       if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
+       if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
                return;
-       if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
+       if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
                return;
        if (MATCH(".sample.depth", depth, &sample->depth))
                return;
@@ -484,14 +501,17 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu
  */
 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
 {
-       return  MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
-               MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
-               MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
-               MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
-               MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
-               MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
-               MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
-               MATCH(".hepct_3", percent, &dive->gasmix[3].he);
+       return  MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
+               MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
+               MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
+               MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
+               MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
+               MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
+               MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
+               MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
+               MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
+               MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
+               0;
 }
 
 static int buffer_value(char *buffer)
@@ -619,11 +639,16 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
        if (MATCH(".notes", utf8_string, &dive->notes))
                return;
 
-       if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
+       if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
+               return;
+       if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
+               return;
+
+       if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
                return;
-       if (MATCH(".n2", gasmix_nitrogen, &dive->gasmix[gasmix_index]))
+       if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
                return;
-       if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
+       if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
                return;
 
        /* Suunto XML files are some crazy sh*t. */
@@ -680,33 +705,66 @@ static char *generate_name(struct dive *dive)
        return p;
 }
 
-static void sanitize_gasmix(struct dive *dive)
+static void sanitize_gasmix(gasmix_t *mix)
 {
-       int i;
+       unsigned int o2, he;
 
-       for (i = 0; i < MAX_MIXES; i++) {
-               gasmix_t *mix = dive->gasmix+i;
-               unsigned int o2, he;
-
-               o2 = mix->o2.permille;
-               he = mix->he.permille;
-
-               /* Regular air: leave empty */
-               if (!he) {
-                       if (!o2)
-                               continue;
-                       /* 20.9% or 21% O2 is just air */
-                       if (o2 >= 209 && o2 <= 210) {
-                               mix->o2.permille = 0;
-                               continue;
-                       }
+       o2 = mix->o2.permille;
+       he = mix->he.permille;
+
+       /* Regular air: leave empty */
+       if (!he) {
+               if (!o2)
+                       return;
+               /* 20.9% or 21% O2 is just air */
+               if (o2 >= 209 && o2 <= 210) {
+                       mix->o2.permille = 0;
+                       return;
                }
+       }
+
+       /* Sane mix? */
+       if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
+               return;
+       fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
+       memset(mix, 0, sizeof(*mix));
+}
+
+/*
+ * There are two ways to give cylinder size information:
+ *  - total amount of gas in cuft (depends on working pressure and physical size)
+ *  - physical size
+ *
+ * where "physical size" is the one that actually matters and is sane.
+ *
+ * We internally use physical size only. But we save the workingpressure
+ * so that we can do the conversion if required.
+ */
+static void sanitize_cylinder_type(cylinder_type_t *type)
+{
+       /* If we have no working pressure, it had *better* be just a physical size! */
+       if (!type->workingpressure.mbar)
+               return;
+
+       /*
+        * 35l tanks? Do they exist?
+        * Assume this is a "size in cuft" thing.
+        */
+       if (type->size.mliter > 35000) {
+               double volume_of_air = type->size.mliter * 28.317;      /* cu ft to milliliter */
+               double atm = type->workingpressure.mbar / 1013.25;      /* working pressure in atm */
+               double volume = volume_of_air / atm;                    /* milliliters at 1 atm: "true size" */
+               type->size.mliter = volume;
+       }
+}
+
+static void sanitize_cylinder_info(struct dive *dive)
+{
+       int i;
 
-               /* Sane mix? */
-               if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
-                       continue;
-               fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
-               memset(mix, 0, sizeof(*mix));
+       for (i = 0; i < MAX_CYLINDERS; i++) {
+               sanitize_gasmix(&dive->cylinder[i].gasmix);
+               sanitize_cylinder_type(&dive->cylinder[i].type);
        }
 }
 
@@ -716,10 +774,10 @@ static void dive_end(void)
                return;
        if (!dive->name)
                dive->name = generate_name(dive);
-       sanitize_gasmix(dive);
+       sanitize_cylinder_info(dive);
        record_dive(dive);
        dive = NULL;
-       gasmix_index = 0;
+       cylinder_index = 0;
 }
 
 static void suunto_start(void)
@@ -752,13 +810,13 @@ static void event_end(void)
        event_index++;
 }
 
-static void gasmix_start(void)
+static void cylinder_start(void)
 {
 }
 
-static void gasmix_end(void)
+static void cylinder_end(void)
 {
-       gasmix_index++;
+       cylinder_index++;
 }
 
 static void sample_start(void)
@@ -901,7 +959,8 @@ static struct nesting {
        { "SAMPLE", sample_start, sample_end },
        { "reading", sample_start, sample_end },
        { "event", event_start, event_end },
-       { "gasmix", gasmix_start, gasmix_end },
+       { "gasmix", cylinder_start, cylinder_end },
+       { "cylinder", cylinder_start, cylinder_end },
        { "pre_dive", uemis_start, uemis_end },
        { NULL, }
 };
index 5ddc43d30442c2e9f60c1e0824e7237312597046..7a0a20828f07c433ed3625bc54bcd8def420619e 100644 (file)
--- a/profile.c
+++ b/profile.c
@@ -93,7 +93,7 @@ static void plot_profile(struct dive *dive, cairo_t *cr,
        cairo_stroke(cr);
 }
 
-static int get_tank_pressure_range(struct dive *dive, double *scalex, double *scaley)
+static int get_cylinder_pressure_range(struct dive *dive, double *scalex, double *scaley)
 {
        int i;
        double min, max;
@@ -106,9 +106,9 @@ static int get_tank_pressure_range(struct dive *dive, double *scalex, double *sc
                struct sample *sample = dive->sample + i;
                double bar;
 
-               if (!sample->tankpressure.mbar)
+               if (!sample->cylinderpressure.mbar)
                        continue;
-               bar = sample->tankpressure.mbar;
+               bar = sample->cylinderpressure.mbar;
                if (bar < min)
                        min = bar;
                if (bar > max)
@@ -120,13 +120,13 @@ static int get_tank_pressure_range(struct dive *dive, double *scalex, double *sc
        return 1;
 }
 
-static void plot_tank_pressure(struct dive *dive, cairo_t *cr,
+static void plot_cylinder_pressure(struct dive *dive, cairo_t *cr,
        double topx, double topy, double maxx, double maxy)
 {
        int i;
        double scalex, scaley;
 
-       if (!get_tank_pressure_range(dive, &scalex, &scaley))
+       if (!get_cylinder_pressure_range(dive, &scalex, &scaley))
                return;
 
        cairo_set_source_rgba(cr, 0.2, 1.0, 0.2, 0.80);
@@ -137,7 +137,7 @@ static void plot_tank_pressure(struct dive *dive, cairo_t *cr,
                struct sample *sample = dive->sample + i;
 
                sec = sample->time.seconds;
-               mbar = sample->tankpressure.mbar;
+               mbar = sample->cylinderpressure.mbar;
                if (!mbar)
                        continue;
                cairo_line_to(cr, SCALE(sec, mbar));
@@ -159,8 +159,8 @@ static void plot(cairo_t *cr, int w, int h, struct dive *dive)
        /* Depth profile */
        plot_profile(dive, cr, topx, topy, maxx, maxy);
 
-       /* Tank pressure plot? */
-       plot_tank_pressure(dive, cr, topx, topy, maxx, maxy);
+       /* Cylinder pressure plot? */
+       plot_cylinder_pressure(dive, cr, topx, topy, maxx, maxy);
 
        /* Bounding box last */
        scalex = scaley = 1.0;
index a1553193d63965212a13f16def8be0f323b0a011..309f7cc08c452c28283e415b7028aee759e38531 100644 (file)
@@ -120,21 +120,34 @@ static void save_overview(FILE *f, struct dive *dive)
        show_utf8(f, dive->notes, "  <notes>","</notes>\n");
 }
 
-static void save_gasmix(FILE *f, struct dive *dive)
+static void save_cylinder_info(FILE *f, struct dive *dive)
 {
        int i;
 
-       for (i = 0; i < MAX_MIXES; i++) {
-               gasmix_t *mix = dive->gasmix+i;
-               int o2 = mix->o2.permille, he = mix->he.permille;
-               int n2 = 1000 - o2 - he;
+       for (i = 0; i < MAX_CYLINDERS; i++) {
+               cylinder_t *cylinder = dive->cylinder+i;
+               int volume = cylinder->type.size.mliter;
+               int pressure = cylinder->type.workingpressure.mbar;
+               int o2 = cylinder->gasmix.o2.permille;
+               int he = cylinder->gasmix.he.permille;
 
-               if (!mix->o2.permille)
+               /* No cylinder information at all? */
+               if (!o2 && !volume)
                        return;
-               fprintf(f, "  <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
-               if (mix->he.permille)
-                       fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
-               fprintf(f, " n2='%u.%u%%' />\n", FRACTION(n2, 10));
+               fprintf(f, "  <cylinder");
+               if (o2) {
+                       int n2 = 1000 - o2 - he;
+                       fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
+                       if (he)
+                               fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
+                       fprintf(f, " n2='%u.%u%%'", FRACTION(n2, 10));
+               }
+               if (volume) {
+                       fprintf(f, " size='%u.%03u l'", FRACTION(volume, 1000));
+                       if (pressure)
+                               fprintf(f, " workpressure='%u.%03u bar'", FRACTION(pressure, 1000));
+               }
+               fprintf(f, " />\n");
        }
 }
 
@@ -144,9 +157,9 @@ static void save_sample(FILE *f, struct sample *sample)
                FRACTION(sample->time.seconds,60),
                FRACTION(sample->depth.mm, 1000));
        show_temperature(f, sample->temperature, " temp='", "'");
-       show_pressure(f, sample->tankpressure, " pressure='", "'");
-       if (sample->tankindex)
-               fprintf(f, " tankindex='%d'", sample->tankindex);
+       show_pressure(f, sample->cylinderpressure, " pressure='", "'");
+       if (sample->cylinderindex)
+               fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
        fprintf(f, " />\n");
 }
 
@@ -159,7 +172,7 @@ static void save_dive(FILE *f, struct dive *dive)
                tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
                tm->tm_hour, tm->tm_min, tm->tm_sec);
        save_overview(f, dive);
-       save_gasmix(f, dive);
+       save_cylinder_info(f, dive);
        for (i = 0; i < dive->samples; i++)
                save_sample(f, dive->sample+i);
        fprintf(f, "</dive>\n");