From 63cdf950145def028f9b38a9b8c4690d512b678d Mon Sep 17 00:00:00 2001 From: Caesar Tuguinay <87830138+ctuguinay@users.noreply.github.com> Date: Wed, 4 Sep 2024 09:27:21 -0700 Subject: [PATCH] Skip Parsing Invalid EK80 Environment Datagram(s) (#1387) * dont parse invalid environment datagram * add parsing test * add wu-jung's suggestion Co-authored-by: Wu-Jung Lee --------- Co-authored-by: Wu-Jung Lee --- echopype/convert/parse_base.py | 8 +++++++- echopype/tests/convert/test_convert_ek80.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/echopype/convert/parse_base.py b/echopype/convert/parse_base.py index 620b7db9f..09f87bef2 100644 --- a/echopype/convert/parse_base.py +++ b/echopype/convert/parse_base.py @@ -495,7 +495,13 @@ def _read_datagrams(self, fid): # XML datagrams store environment or instrument parameters for EK80 if new_datagram["type"].startswith("XML"): - if new_datagram["subtype"] == "environment": + # Check that environment datagrams contain more than + # just drop_keel_offset and drop_keel_offset_is_manual + # Temporary fix for handling >1 EK80 environment datagrams described in: + # https://github.com/OSOceanAcoustics/echopype/issues/1386 + if new_datagram["subtype"] == "environment" and set( + ["drop_keel_offset", "drop_keel_offset_is_manual"] + ) != set(new_datagram["environment"].keys()): self.environment = new_datagram["environment"] self.environment["xml"] = new_datagram["xml"] self.environment["timestamp"] = new_datagram["timestamp"] diff --git a/echopype/tests/convert/test_convert_ek80.py b/echopype/tests/convert/test_convert_ek80.py index b9bc7bdd3..092116af9 100644 --- a/echopype/tests/convert/test_convert_ek80.py +++ b/echopype/tests/convert/test_convert_ek80.py @@ -482,3 +482,22 @@ def test_parse_mru0_mru1(ek80_path): ] for mru_var_name in mru_var_names: assert not np.any(np.isnan(echodata["Platform"][mru_var_name])) + + +@pytest.mark.unit +def test_parse_ek80_with_invalid_env_datagrams(): + """ + Tests parsing EK80 RAW file with invalid environment datagrams. Checks that the EchoData object + contains the necessary environment variables for calibration. + """ + + # Parse RAW + ed = open_raw( + "echopype/test_data/ek80_invalid_env_datagrams/SH24-replay-D20240705-T070536.raw", + sonar_model="EK80", + ) + + # Check that each calibration specific variable exists, is not NaN, and is of type float64 + for var in ["acidity", "depth", "salinity", "temperature", "sound_speed_indicative"]: + env_var = ed["Environment"][var] + assert env_var.notnull().all() and env_var.dtype == np.float64