- if (!cyl->start.mbar)
- cyl->start.mbar = pressure;
- /*
- * If we already have an end pressure, without
- * ever having seen a sample for this cylinder,
- * that means that somebody set the end pressure
- * by hand
- */
- mask = 1ul << index;
- if (cyl->end.mbar) {
- if (!(flags & mask))
- return flags;
+ if (!cyl->sample_start.mbar)
+ cyl->sample_start.mbar = pressure;
+ cyl->sample_end.mbar = pressure;
+}
+
+/*
+ * If the cylinder tank pressures are within half a bar
+ * (about 8 PSI) of the sample pressures, we consider it
+ * to be a rounding error, and throw them away as redundant.
+ */
+static int same_rounded_pressure(pressure_t a, pressure_t b)
+{
+ return abs(a.mbar - b.mbar) <= 500;
+}
+
+static void sanitize_gasmix(struct gasmix *mix)
+{
+ unsigned int o2, he;
+
+ 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));
+}
+
+/*
+ * See if the size/workingpressure looks like some standard cylinder
+ * size, eg "AL80".
+ */
+static void match_standard_cylinder(cylinder_type_t *type)
+{
+ double cuft;
+ int psi, len;
+ const char *fmt;
+ char buffer[20], *p;
+
+ /* Do we already have a cylinder description? */
+ if (type->description)
+ return;
+
+ cuft = ml_to_cuft(type->size.mliter);
+ cuft *= to_ATM(type->workingpressure);
+ psi = to_PSI(type->workingpressure);
+
+ switch (psi) {
+ case 2300 ... 2500: /* 2400 psi: LP tank */
+ fmt = "LP%d";
+ break;
+ case 2600 ... 2700: /* 2640 psi: LP+10% */
+ fmt = "LP%d";
+ break;
+ case 2900 ... 3100: /* 3000 psi: ALx tank */
+ fmt = "AL%d";
+ break;
+ case 3400 ... 3500: /* 3442 psi: HP tank */
+ fmt = "HP%d";
+ break;
+ case 3700 ... 3850: /* HP+10% */
+ fmt = "HP%d+";
+ break;
+ default:
+ return;