From cffda88ae85b3a6107205dc9c148d121c3580175 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 18 Jun 2021 21:45:17 +0200 Subject: [PATCH] Use SI units internally Prefer SI units for internal constants (e.g. density) and calculations. This results in more consistent conversion formulas across the different backends. For the Uwatec backend, this changes makes it also much more visible that the unit for the depth is either 1 millibar (maximum depth) or 2 millibar (sample depth). --- src/mclean_extreme_parser.c | 18 ++++++++++-------- src/uwatec_smart_parser.c | 14 +++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/mclean_extreme_parser.c b/src/mclean_extreme_parser.c index 9b5c91c..585c10c 100644 --- a/src/mclean_extreme_parser.c +++ b/src/mclean_extreme_parser.c @@ -22,6 +22,8 @@ #include #include +#include + #include "mclean_extreme.h" #include "context-private.h" #include "parser-private.h" @@ -152,19 +154,19 @@ mclean_extreme_parser_get_field(dc_parser_t *abstract, dc_field_type_t type, uns dc_gasmix_t *gasmix = (dc_gasmix_t *)value; dc_salinity_t *salinity = (dc_salinity_t *)value; - const unsigned int psurf = array_uint16_le(abstract->data + 0x001E); + const unsigned int atmospheric = array_uint16_le(abstract->data + 0x001E); const unsigned int density_index = abstract->data[0x0023]; double density = 0; switch (density_index) { case 0: - density = 1.000; + density = 1000.0; break; case 1: - density = 1.020; + density = 1020.0; break; case 2: - density = 1.030; + density = 1030.0; break; default: ERROR(abstract->context, "Corrupt density index in dive data"); @@ -177,17 +179,17 @@ mclean_extreme_parser_get_field(dc_parser_t *abstract, dc_field_type_t type, uns *((unsigned int *)value) = array_uint32_le(abstract->data + SZ_CFG + 0x000C) - array_uint32_le(abstract->data + SZ_CFG + 0x0000); break; case DC_FIELD_MAXDEPTH: - *((double *)value) = 0.01 * (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0016) - psurf) / density; + *((double *)value) = (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0016) - atmospheric) * (BAR / 1000.0) / (density * 10.0); break; case DC_FIELD_AVGDEPTH: - *((double *)value) = 0.01 * (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0018) - psurf) / density; + *((double *)value) = (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0018) - atmospheric) * (BAR / 1000.0) / (density * 10.0); break; case DC_FIELD_SALINITY: - salinity->density = density * 1000.0; + salinity->density = density; salinity->type = density_index == 0 ? DC_WATER_FRESH : DC_WATER_SALT; break; case DC_FIELD_ATMOSPHERIC: - *((double *)value) = 0.001 * array_uint16_le(abstract->data + 0x001E); + *((double *)value) = atmospheric / 1000.0; break; case DC_FIELD_TEMPERATURE_MINIMUM: *((double *)value) = (double)abstract->data[SZ_CFG + 0x0010]; diff --git a/src/uwatec_smart_parser.c b/src/uwatec_smart_parser.c index c28e1d0..3e4ed09 100644 --- a/src/uwatec_smart_parser.c +++ b/src/uwatec_smart_parser.c @@ -62,8 +62,8 @@ #define HEADER 1 #define PROFILE 2 -#define FRESH 1.000 -#define SALT 1.025 +#define FRESH 1000.0 +#define SALT 1025.0 #define FREEDIVE 0x00000080 #define GAUGE 0x00001000 @@ -789,7 +789,7 @@ uwatec_smart_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi const uwatec_smart_header_info_t *table = parser->header; const unsigned char *data = abstract->data; - double salinity = (parser->watertype == DC_WATER_SALT ? SALT : FRESH); + double density = (parser->watertype == DC_WATER_SALT ? SALT : FRESH); dc_gasmix_t *gasmix = (dc_gasmix_t *) value; dc_tank_t *tank = (dc_tank_t *) value; @@ -801,7 +801,7 @@ uwatec_smart_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi *((unsigned int *) value) = array_uint16_le (data + table->divetime) * 60; break; case DC_FIELD_MAXDEPTH: - *((double *) value) = array_uint16_le (data + table->maxdepth) / 100.0 / salinity; + *((double *) value) = array_uint16_le (data + table->maxdepth) * (BAR / 1000.0) / (density * 10.0); break; case DC_FIELD_GASMIX_COUNT: *((unsigned int *) value) = parser->ngasmixes; @@ -844,7 +844,7 @@ uwatec_smart_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi if (table->settings == UNSUPPORTED) return DC_STATUS_UNSUPPORTED; water->type = parser->watertype; - water->density = salinity * 1000.0; + water->density = density; break; default: return DC_STATUS_UNSUPPORTED; @@ -950,7 +950,7 @@ uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback // Previous gas mix - initialize with impossible value unsigned int gasmix_previous = 0xFFFFFFFF; - double salinity = (parser->watertype == DC_WATER_SALT ? SALT : FRESH); + double density = (parser->watertype == DC_WATER_SALT ? SALT : FRESH); unsigned int interval = 4; if (parser->divemode == DC_DIVEMODE_FREEDIVE) { @@ -1233,7 +1233,7 @@ uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback } if (have_depth) { - sample.depth = (signed int)(depth - depth_calibration) / 50.0 / salinity; + sample.depth = (signed int)(depth - depth_calibration) * (2.0 * BAR / 1000.0) / (density * 10.0); if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata); }