From a2a98b6e487e33d4699653e1442a65c843e7a870 Mon Sep 17 00:00:00 2001 From: Caesar Tuguinay <87830138+ctuguinay@users.noreply.github.com> Date: Wed, 10 Jul 2024 20:44:27 -0700 Subject: [PATCH] Padding Short Pings (#1353) --- echopype/convert/parse_base.py | 10 ++-- echopype/tests/convert/test_convert_ek.py | 57 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/echopype/convert/parse_base.py b/echopype/convert/parse_base.py index 987aea039..b43d60dbe 100644 --- a/echopype/convert/parse_base.py +++ b/echopype/convert/parse_base.py @@ -639,13 +639,15 @@ def pad_shorter_ping(data_list) -> np.ndarray: # Create output array from mask out_array = np.full(mask.shape, np.nan) + # Concatenate short pings + concat_short_pings = np.concatenate(data_list).reshape(-1) # reshape in case data > 1D + # Take care of problem of np.nan being implicitly "real" - arr_dtype = data_list[0].dtype - if np.issubdtype(arr_dtype, np.complex_): - out_array = out_array.astype(arr_dtype) + if concat_short_pings.dtype == np.complex64: + out_array = out_array.astype(np.complex64) # Fill in values - out_array[mask] = np.concatenate(data_list).reshape(-1) # reshape in case data > 1D + out_array[mask] = concat_short_pings else: out_array = np.array(data_list) return out_array diff --git a/echopype/tests/convert/test_convert_ek.py b/echopype/tests/convert/test_convert_ek.py index 10e400f37..e5d82e29b 100644 --- a/echopype/tests/convert/test_convert_ek.py +++ b/echopype/tests/convert/test_convert_ek.py @@ -4,6 +4,7 @@ from echopype import open_raw from echopype.convert.utils.ek_raw_io import RawSimradFile, SimradEOF +from echopype.convert.parse_base import ParseEK def expected_array_shape(file, datagram_type, datagram_item): @@ -151,3 +152,59 @@ def test_convert_ek_with_idx_file(file, sonar_model): + "This is different from longitude from the NMEA datagram.", } ) + + +@pytest.mark.unit +def test_pad_short_complex_pings(): + """ + Test padding of short complex pings. + """ + # Create empty parser + empty_parser = ParseEK(None, None, None, None, None) + + # Create ping list with sizes 3, 2, 1 samples per ping + data_list = [ + np.array( + [ + 1.0 + 2.5j, + 7.3 - 3.2j, + 3.2 - 9.1j, + ], + dtype=np.complex64 + ), + np.array( + [ + 1.8 - 4.1j, + 1.2 - 8.9j, + ], + dtype=np.complex64 + ), + np.array( + [ + 6.1 - 4.8j, + ], + dtype=np.complex64 + ), + ] + + # Pad shorter pings + output = empty_parser.pad_shorter_ping(data_list) + + # Set expected output + expected_output = np.array( + [ + [1. +2.5j, 7.3-3.2j, 3.2-9.1j], + [1.8-4.1j, 1.2-8.9j, np.nan+0.j ], + [6.1-4.8j, np.nan+0.j , np.nan+0.j ] + ], + dtype=np.complex64 + ) + # Check dtype + assert output.dtype == np.complex64 + + # Check output against expected + assert np.allclose( + output, + expected_output, + equal_nan=True + )