Skip to content

Commit

Permalink
For AZFP Env group, create temperature and pressure only if they have…
Browse files Browse the repository at this point in the history
… valid data; add missing mandatory absorption_indicative & sound_speed_indicative vars (currently all nan); and update tests (#1226)
  • Loading branch information
emiliom authored Nov 20, 2023
1 parent 3335cae commit 31e35f7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 35 deletions.
96 changes: 71 additions & 25 deletions echopype/convert/set_groups_azfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,44 +101,90 @@ def _create_unique_channel_name(self):

def set_env(self) -> xr.Dataset:
"""Set the Environment group."""
# TODO Look at why this cannot be encoded without the modifications
# @ngkavin: what modification?
ping_time = self.parser_obj.ping_time

# Mandatory variables
ds = xr.Dataset(
{
"temperature": (
["time1"],
self.parser_obj.unpacked_data["temperature"],
"absorption_indicative": (
["channel"],
[np.nan] * len(self.channel_ids_sorted),
{
"long_name": "Water temperature",
"standard_name": "sea_water_temperature",
"units": "deg_C",
"long_name": "Indicative acoustic absorption",
"units": "dB/m",
"valid_min": 0.0,
},
),
"pressure": (
["time1"],
self.parser_obj.unpacked_data["pressure"],
"sound_speed_indicative": (
[],
np.nan,
{
"long_name": "Sea water pressure",
"standard_name": "sea_water_pressure_due_to_sea_water",
"units": "dbar",
"long_name": "Indicative sound speed",
"standard_name": "speed_of_sound_in_sea_water",
"units": "m/s",
"valid_min": 0.0,
},
),
},
coords={
"time1": (
["time1"],
ping_time,
"frequency_nominal": (
["channel"],
self.freq_sorted,
{
"axis": "T",
"long_name": "Timestamp of each ping",
"standard_name": "time",
"comment": "Time coordinate corresponding to environmental variables.",
"units": "Hz",
"long_name": "Transducer frequency",
"valid_min": 0.0,
"standard_name": "sound_frequency",
},
)
),
},
coords={
"channel": (
["channel"],
self.channel_ids_sorted,
self._varattrs["beam_coord_default"]["channel"],
),
},
)

# Additional variables, if present
temp_press = dict()
if not np.isnan(self.parser_obj.unpacked_data["temperature"]).all():
temp_press["temperature"] = (
["time1"],
self.parser_obj.unpacked_data["temperature"],
{
"long_name": "Water temperature",
"standard_name": "sea_water_temperature",
"units": "deg_C",
},
)
if not np.isnan(self.parser_obj.unpacked_data["pressure"]).all():
temp_press["pressure"] = (
["time1"],
self.parser_obj.unpacked_data["pressure"],
{
"long_name": "Sea water pressure",
"standard_name": "sea_water_pressure_due_to_sea_water",
"units": "dbar",
},
)

if len(temp_press) > 0:
ds_temp_press = xr.Dataset(
temp_press,
coords={
"time1": (
["time1"],
self.parser_obj.ping_time,
{
"axis": "T",
"long_name": "Timestamp of each ping",
"standard_name": "time",
"comment": "Time coordinate corresponding to environmental variables.",
},
)
},
)
ds = xr.merge([ds, ds_temp_press], combine_attrs="override")

return set_time_encodings(ds)

def set_sonar(self) -> xr.Dataset:
Expand Down
15 changes: 5 additions & 10 deletions echopype/tests/convert/test_convert_azfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,9 @@ def test_convert_azfp_01a_no_temperature_pressure_tilt(azfp_path):
raw_file=azfp_01a_path, sonar_model='AZFP', xml_path=azfp_xml_path
)

# Temperature variable is present in the Environment group and its values are all nan
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()
# Temperature and pressure variables are not present in the Environment group
assert "temperature" not in echodata["Environment"]
assert "pressure" not in echodata["Environment"]

# Tilt variables are present in the Platform group and their values are all nan
assert "tilt_x" in echodata["Platform"]
Expand All @@ -182,8 +178,8 @@ def test_convert_azfp_01a_no_temperature_pressure_tilt(azfp_path):

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'
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
Expand All @@ -199,7 +195,6 @@ def test_convert_azfp_01a_pressure_temperature(azfp_path):


def test_load_parse_azfp_xml(azfp_path):

azfp_xml_path = azfp_path / '23081211.XML'
parseAZFP = ParseAZFP(None, str(azfp_xml_path))
parseAZFP.load_AZFP_xml()
Expand Down

0 comments on commit 31e35f7

Please sign in to comment.