From 9dfe5ba9cd656eb2f5dc9007fcb960231108cf1e Mon Sep 17 00:00:00 2001 From: Praneeth Ratna <63547155+praneethratna@users.noreply.github.com> Date: Tue, 24 Oct 2023 05:02:42 +0530 Subject: [PATCH] `parse_azfp` now parses AZFP pressure data according to given matlab code (#1189) * added support for parsing azfp pressure data * fixed failing ci tests * Revert "fixed failing ci tests" This reverts commit c378dc0cc68b70daadb04cd5a06a5ca296581b51. * minor changes to tests * Update test_convert_azfp.py * Update echopype/tests/convert/test_convert_azfp.py --------- Co-authored-by: Emilio Mayorga --- echopype/convert/parse_azfp.py | 24 +++++++++++++++++++ echopype/convert/set_groups_azfp.py | 11 ++++++++- echopype/tests/convert/test_convert_azfp.py | 26 +++++++++++++++++++-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/echopype/convert/parse_azfp.py b/echopype/convert/parse_azfp.py index 476be16bf..ae9f928e2 100644 --- a/echopype/convert/parse_azfp.py +++ b/echopype/convert/parse_azfp.py @@ -197,6 +197,25 @@ def _compute_battery(self, ping_num, battery_type): return N * USL5_BAT_CONSTANT + def _compute_pressure(self, ping_num, is_valid): + """ + Compute pressure in decibar + + Parameters + ---------- + ping_num + ping number + is_valid + whether the associated parameters have valid values + """ + if not is_valid or self.parameters["sensors_flag_pressure_sensor_installed"] == "no": + return np.nan + + counts = self.unpacked_data["ancillary"][ping_num][3] + v_in = 2.5 * (counts / 65535) + P = v_in * self.parameters["a1"] + self.parameters["a0"] - 10.125 + return P + def parse_raw(self): """ Parse raw data file from AZFP echosounder. @@ -214,6 +233,7 @@ def _test_valid_params(params): return True temperature_is_valid = _test_valid_params(["ka", "kb", "kc"]) + pressure_is_valid = _test_valid_params(["a0", "a1"]) tilt_x_is_valid = _test_valid_params(["X_a", "X_b", "X_c"]) tilt_y_is_valid = _test_valid_params(["Y_a", "Y_b", "Y_c"]) @@ -235,6 +255,10 @@ def _test_valid_params(params): self.unpacked_data["temperature"].append( self._compute_temperature(ping_num, temperature_is_valid) ) + # Compute pressure from unpacked_data[ii]['ancillary'][3] + self.unpacked_data["pressure"].append( + self._compute_pressure(ping_num, pressure_is_valid) + ) # compute x tilt from unpacked_data[ii]['ancillary][0] self.unpacked_data["tilt_x"].append( self._compute_tilt(ping_num, "X", tilt_x_is_valid) diff --git a/echopype/convert/set_groups_azfp.py b/echopype/convert/set_groups_azfp.py index 6049d58b8..7dc3fdf21 100644 --- a/echopype/convert/set_groups_azfp.py +++ b/echopype/convert/set_groups_azfp.py @@ -114,7 +114,16 @@ def set_env(self) -> xr.Dataset: "standard_name": "sea_water_temperature", "units": "deg_C", }, - ) + ), + "pressure": ( + ["time1"], + self.parser_obj.unpacked_data["pressure"], + { + "long_name": "Sea water pressure", + "standard_name": "sea_water_pressure_due_to_sea_water", + "units": "dbar", + }, + ), }, coords={ "time1": ( diff --git a/echopype/tests/convert/test_convert_azfp.py b/echopype/tests/convert/test_convert_azfp.py index 48d4139bb..487bec243 100644 --- a/echopype/tests/convert/test_convert_azfp.py +++ b/echopype/tests/convert/test_convert_azfp.py @@ -156,8 +156,8 @@ def test_convert_azfp_01a_different_ranges(azfp_path): check_platform_required_scalar_vars(echodata) -def test_convert_azfp_01a_notemperature_notilt(azfp_path): - """Test converting file with no valid temperature or tilt data.""" +def test_convert_azfp_01a_no_temperature_pressure_tilt(azfp_path): + """Test converting file with no valid temperature, pressure and tilt data.""" azfp_01a_path = azfp_path / 'rutgers_glider_notemperature/22052500.01A' azfp_xml_path = azfp_path / 'rutgers_glider_notemperature/22052501.XML' @@ -169,6 +169,10 @@ def test_convert_azfp_01a_notemperature_notilt(azfp_path): assert "temperature" in echodata["Environment"] assert echodata["Environment"]["temperature"].isnull().all() + # Pressure variable is present in the Environment group and its values are all nan + assert "pressure" in echodata["Environment"] + assert echodata["Environment"]["pressure"].isnull().all() + # Tilt variables are present in the Platform group and their values are all nan assert "tilt_x" in echodata["Platform"] assert "tilt_y" in echodata["Platform"] @@ -176,6 +180,24 @@ def test_convert_azfp_01a_notemperature_notilt(azfp_path): assert echodata["Platform"]["tilt_y"].isnull().all() +def test_convert_azfp_01a_pressure_temperature(azfp_path): + """Test converting file with valid pressure and temperature data.""" + azfp_01a_path = azfp_path / 'pressure' / '22042221.01A' + azfp_xml_path = azfp_path / 'pressure' / '22042220.XML' + + echodata = open_raw( + raw_file=azfp_01a_path, sonar_model='AZFP', xml_path=azfp_xml_path + ) + + # Pressure variable is present in the Environment group and its values are not all nan + assert "pressure" in echodata["Environment"] + assert not echodata["Environment"]["pressure"].isnull().all() + + # Temperature variable is present in the Environment group and its values are not all nan + assert "temperature" in echodata["Environment"] + assert not echodata["Environment"]["temperature"].isnull().all() + + def test_load_parse_azfp_xml(azfp_path): azfp_xml_path = azfp_path / '23081211.XML'