|
4 | 4 | import numpy as np
|
5 | 5 | import pandas as pd
|
6 | 6 | from scipy.io import loadmat
|
| 7 | +import xarray as xr |
7 | 8 |
|
8 | 9 | from echopype import open_raw, open_converted
|
9 | 10 | from echopype.testing import TEST_DATA_FOLDER
|
10 | 11 | from echopype.convert.parse_ek80 import ParseEK80
|
11 | 12 | from echopype.convert.set_groups_ek80 import WIDE_BAND_TRANS, PULSE_COMPRESS, FILTER_IMAG, FILTER_REAL, DECIMATION
|
| 13 | +from echopype.utils import log |
| 14 | +from echopype.convert.utils.ek_duplicates import check_unique_ping_time_duplicates |
12 | 15 |
|
13 | 16 |
|
14 | 17 | @pytest.fixture
|
@@ -512,6 +515,62 @@ def test_parse_missing_sound_velocity_profile():
|
512 | 515 | shutil.rmtree(save_path)
|
513 | 516 |
|
514 | 517 |
|
| 518 | +@pytest.mark.unit |
| 519 | +def test_duplicate_ping_times(caplog): |
| 520 | + """ |
| 521 | + Tests that RAW file with duplicate ping times can be parsed and that the correct warning has been raised. |
| 522 | + """ |
| 523 | + # Turn on logger verbosity |
| 524 | + log.verbose(override=False) |
| 525 | + |
| 526 | + # Open RAW |
| 527 | + ed = open_raw("echopype/test_data/ek80_duplicate_ping_times/Hake-D20210913-T130612.raw", sonar_model="EK80") |
| 528 | + |
| 529 | + # Check that there are no ping time duplicates in Beam group |
| 530 | + assert ed["Sonar/Beam_group1"].equals( |
| 531 | + ed["Sonar/Beam_group1"].drop_duplicates(dim="ping_time") |
| 532 | + ) |
| 533 | + |
| 534 | + # Check that no warning is logged since the data for all duplicate pings is unique |
| 535 | + not_expected_warning = ("All duplicate ping_time entries' will be removed, resulting in potential data loss.") |
| 536 | + assert not any(not_expected_warning in record.message for record in caplog.records) |
| 537 | + |
| 538 | + # Turn off logger verbosity |
| 539 | + log.verbose(override=True) |
| 540 | + |
| 541 | + |
| 542 | +@pytest.mark.unit |
| 543 | +def test_check_unique_ping_time_duplicates(caplog): |
| 544 | + """ |
| 545 | + Checks that `check_unique_ping_time_duplicates` raises a warning when the data for duplicate ping times is not unique. |
| 546 | + """ |
| 547 | + # Initialize logger |
| 548 | + logger = log._init_logger(__name__) |
| 549 | + |
| 550 | + # Turn on logger verbosity |
| 551 | + log.verbose(override=False) |
| 552 | + |
| 553 | + # Open duplicate ping time beam dataset |
| 554 | + ds_data = xr.open_zarr("echopype/test_data/ek80_duplicate_ping_times/duplicate_beam_ds.zarr") |
| 555 | + |
| 556 | + # Modify a single entry to ensure that there exists duplicate ping times that do not share the same backscatter data |
| 557 | + ds_data["backscatter_r"][0,0,0] = 0 |
| 558 | + |
| 559 | + # Check for ping time duplicates |
| 560 | + check_unique_ping_time_duplicates(ds_data, logger) |
| 561 | + |
| 562 | + # Turn off logger verbosity |
| 563 | + log.verbose(override=True) |
| 564 | + |
| 565 | + # Check if the expected warning is logged |
| 566 | + expected_warning = ( |
| 567 | + "Duplicate slices in variable 'backscatter_r' corresponding to 'ping_time' " |
| 568 | + f"{str(ds_data['ping_time'].values[0])} differ in data. All duplicate " |
| 569 | + "'ping_time' entries will be removed, which will result in data loss." |
| 570 | + ) |
| 571 | + assert any(expected_warning in record.message for record in caplog.records) |
| 572 | + |
| 573 | + |
515 | 574 | @pytest.mark.unit
|
516 | 575 | def test_parse_ek80_with_invalid_env_datagrams():
|
517 | 576 | """
|
|
0 commit comments