Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop beam dimension #1056

Merged
merged 24 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c589c35
Remove beam dimension previously added during EK60 conversion
emiliom May 26, 2023
09f033e
Handle case in EK calibration where beam dimension is not present
emiliom May 26, 2023
2445370
Merge branch 'dev' into drop_beam_dim
emiliom Jun 5, 2023
39521e6
Merge branch 'dev' into drop_beam_dim
emiliom Jul 14, 2023
3a0d892
Remove beam dimension previously added during AZFP conversion
emiliom Jul 14, 2023
86a833a
Remove handling of beam dimension in AZFP calibration
emiliom Jul 14, 2023
996d939
Update convert and echodata tests to account for elimination of beam …
emiliom Jul 14, 2023
111a3e1
Forgot to undo in previous commit the temporary removal of netcdf4 te…
emiliom Jul 15, 2023
7c6766f
Merge branch 'dev' into drop_beam_dim
emiliom Jul 17, 2023
acca64b
Remove forced addition of beam dim in EK80 when not required. Now use…
emiliom Jul 18, 2023
90a7315
Remove forced handling of beam dim in EK80 calibration when beam is n…
emiliom Jul 18, 2023
acbc8dd
Update convert, echodata and calibration tests to account for selecti…
emiliom Jul 18, 2023
d7cc8e9
Forgot to undo in previous commit the temporary removal of netcdf4 te…
emiliom Jul 18, 2023
4e011ab
fix test_nan_range_entries
leewujung Jul 19, 2023
cbed2ba
fix test mock ata
leewujung Jul 19, 2023
6094ec3
remove added if-else
leewujung Jul 19, 2023
43a9f92
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 19, 2023
2f694ca
remove beam dim handling in calibrate/cal_params.py
leewujung Jul 20, 2023
c53946d
revise comment re one place where the beam dim should be dropped expl…
leewujung Jul 20, 2023
9869507
remove outdated comments re which var has beam dim
leewujung Jul 20, 2023
4cf2c83
remove redundant check for if beam dim exists
leewujung Jul 20, 2023
ffb090c
remove beam dim from test_cal_params.py::beam_AZFP
leewujung Jul 22, 2023
537197b
remove beam dim check cal_params.py::_get_interp_da under the alterna…
leewujung Jul 22, 2023
d85d8c3
Merge branch 'dev' into drop_beam_dim
leewujung Jul 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions echopype/calibrate/cal_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def get_cal_params_AZFP(beam: xr.DataArray, vend: xr.DataArray, user_dict: dict)
if v is None:
# Params from Sonar/Beam_group1
if p == "equivalent_beam_angle":
# equivalent_beam_angle has dims: channel, ping_time, beam --> only need channel
out_dict[p] = beam[p].isel(ping_time=0, beam=0).drop(["ping_time", "beam"])
# equivalent_beam_angle has dims: channel, ping_time --> only need channel
out_dict[p] = beam[p].isel(ping_time=0).drop("ping_time")

# Params from Vendor_specific group
elif p in ["EL", "DS", "TVR", "VTX", "Sv_offset"]:
Expand Down Expand Up @@ -497,9 +497,14 @@ def _get_fs():
)
elif p == "equivalent_beam_angle":
# scaled according to frequency ratio
out_dict[p] = beam[p].isel(beam=0).drop("beam") + 20 * np.log10(
beam["frequency_nominal"] / freq_center
)
if "beam" in beam[p].dims:
out_dict[p] = beam[p].isel(beam=0).drop("beam") + 20 * np.log10(
beam["frequency_nominal"] / freq_center
)
else:
out_dict[p] = beam[p] + 20 * np.log10(
beam["frequency_nominal"] / freq_center
)
leewujung marked this conversation as resolved.
Show resolved Hide resolved
elif p == "gain_correction":
# interpolate or pull from narrowband table
out_dict[p] = _get_interp_da(
Expand Down
5 changes: 2 additions & 3 deletions echopype/calibrate/calibrate_azfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ def _cal_power_samples(self, cal_type, **kwargs):
EL = (
self.cal_params["EL"]
- 2.5 / a
+ self.echodata["Sonar/Beam_group1"]["backscatter_r"].squeeze("beam", drop=True)
/ (26214 * a)
) # eq.(5) # has beam dim due to backscatter_r
+ self.echodata["Sonar/Beam_group1"]["backscatter_r"] / (26214 * a)
) # eq.(5)

if cal_type == "Sv":
# eq.(9)
Expand Down
11 changes: 7 additions & 4 deletions echopype/calibrate/calibrate_ek.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ def _cal_power_samples(self, cal_type: str) -> xr.Dataset:
if "time1" in out.coords:
out = out.drop("time1")

# Squeeze out the beam dim
# doing it here because both out and self.cal_params["equivalent_beam_angle"] has beam dim
return out.squeeze("beam", drop=True)
# Squeeze out the beam dim, if present.
# Doing it here because both out and self.cal_params["equivalent_beam_angle"] have` beam dim
if "beam" in out.dims:
return out.squeeze("beam", drop=True)
else:
return out
leewujung marked this conversation as resolved.
Show resolved Hide resolved


class CalibrateEK60(CalibrateEK):
Expand Down Expand Up @@ -250,7 +253,7 @@ def __init__(
# use true center frequency to interpolate for various cal params
self.freq_center = (beam["frequency_start"] + beam["frequency_end"]).sel(
channel=self.chan_sel
).isel(beam=0).drop("beam") / 2
) / 2
else:
# use nominal channel frequency for CW pulse
self.freq_center = beam["frequency_nominal"].sel(channel=self.chan_sel)
Expand Down
11 changes: 8 additions & 3 deletions echopype/convert/set_groups_azfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ class SetGroupsAZFP(SetGroupsBase):
# these sets are applied to all Sonar/Beam_groupX groups.

# Variables that need only the beam dimension added to them.
beam_only_names = {"backscatter_r"}
beam_only_names = set()

# Variables that need only the ping_time dimension added to them.
ping_time_only_names = {"sample_interval", "transmit_duration_nominal"}
ping_time_only_names = {
"sample_interval",
"transmit_duration_nominal",
"equivalent_beam_angle",
"gain_correction",
leewujung marked this conversation as resolved.
Show resolved Hide resolved
}

# Variables that need beam and ping_time dimensions added to them.
beam_ping_time_names = {"equivalent_beam_angle", "gain_correction"}
beam_ping_time_names = set()

beamgroups_possible = [
{
Expand Down
15 changes: 10 additions & 5 deletions echopype/convert/set_groups_ek60.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class SetGroupsEK60(SetGroupsBase):
# in converting from v0.5.x to v0.6.0. The values within
# these sets are applied to all Sonar/Beam_groupX groups.

# 2023-05-25 note: We have decided to remove the beam dimension from EK60,
# where it was added as a length-1 dimension only to more closely match
# the SONAR-netCDF4 v1 convention. For the time being, we are retaining the
# infrastructure that adds this dimension, but updating the variables lists.

# Variables that need only the beam dimension added to them.
beam_only_names = {"backscatter_r", "angle_athwartship", "angle_alongship"}
beam_only_names = set()

# Variables that need only the ping_time dimension added to them.
ping_time_only_names = {"beam_type"}

# Variables that need beam and ping_time dimensions added to them.
beam_ping_time_names = {
ping_time_only_names = {
"beam_direction_x",
"beam_direction_y",
"beam_direction_z",
Expand All @@ -45,6 +47,9 @@ class SetGroupsEK60(SetGroupsBase):
"gain_correction",
}

# Variables that need beam and ping_time dimensions added to them.
beam_ping_time_names = set()
leewujung marked this conversation as resolved.
Show resolved Hide resolved

beamgroups_possible = [
{
"name": "Beam_group1",
Expand Down
18 changes: 6 additions & 12 deletions echopype/convert/set_groups_ek80.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,11 @@ class SetGroupsEK80(SetGroupsBase):
# these sets are applied to all Sonar/Beam_groupX groups.

# Variables that need only the beam dimension added to them.
beam_only_names = {
"backscatter_r",
"backscatter_i",
"angle_athwartship",
"angle_alongship",
"frequency_start",
"frequency_end",
}
beam_only_names = set()

# Variables that need only the ping_time dimension added to them.
ping_time_only_names = {"beam_type"}

# Variables that need beam and ping_time dimensions added to them.
beam_ping_time_names = {
ping_time_only_names = {
"beam_type",
"beam_direction_x",
"beam_direction_y",
"beam_direction_z",
Expand All @@ -53,6 +44,9 @@ class SetGroupsEK80(SetGroupsBase):
"beamwidth_twoway_athwartship",
}

# Variables that need beam and ping_time dimensions added to them.
beam_ping_time_names = set()

beamgroups_possible = [
{
"name": "Beam_group1",
Expand Down
4 changes: 1 addition & 3 deletions echopype/tests/calibrate/test_cal_params_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def test_cal_params_intake_EK80_BB_complex(ek80_cal_path):
beam = ed["Sonar/Beam_group1"].sel(channel=chan_sel)
vend = ed["Vendor_specific"].sel(channel=chan_sel)
freq_center = (
(beam["frequency_start"] + beam["frequency_end"])
.sel(channel=chan_sel).isel(beam=0).drop("beam") / 2
)
(beam["frequency_start"] + beam["frequency_end"]).sel(channel=chan_sel) / 2)
cal_params_manual = ep.calibrate.cal_params.get_cal_params_EK(
"BB", freq_center, beam, vend, {"gain_correction": gain_freq_dep}
)
Expand Down
2 changes: 1 addition & 1 deletion echopype/tests/calibrate/test_ecs_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_ecs_intake_ek80_BB_complex(ek80_path, ecs_path):
chan_w_BB_param = "WBT 549762-15 ES70-7C_ES"
freq_center = (
(beam["frequency_start"] + beam["frequency_end"]) / 2
).sel(channel=chan_w_BB_param).isel(beam=0).drop_vars(["beam", "channel"])
).sel(channel=chan_w_BB_param).drop_vars(["channel"])

for p_name in [
"gain_correction", "angle_offset_alongship", "angle_offset_athwartship",
Expand Down
8 changes: 4 additions & 4 deletions echopype/tests/convert/test_convert_azfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_convert_azfp_01a_matlab_raw(azfp_path):
np.array(
[ds_matlab_output['Output'][0]['N'][fidx] for fidx in range(4)]
),
echodata["Sonar/Beam_group1"].backscatter_r.isel(beam=0).drop_vars('beam').values,
echodata["Sonar/Beam_group1"].backscatter_r.values,
)

# Test vendor group
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_convert_azfp_01a_raw_echoview(azfp_path):
echodata = open_raw(
raw_file=azfp_01a_path, sonar_model='AZFP', xml_path=azfp_xml_path
)
assert np.array_equal(test_power, echodata["Sonar/Beam_group1"].backscatter_r.isel(beam=0).drop_vars('beam')) # noqa
assert np.array_equal(test_power, echodata["Sonar/Beam_group1"].backscatter_r)

# check convention-required variables in the Platform group
check_platform_required_scalar_vars(echodata)
Expand All @@ -145,10 +145,10 @@ def test_convert_azfp_01a_different_ranges(azfp_path):
)
assert echodata["Sonar/Beam_group1"].backscatter_r.sel(channel='55030-125-1').dropna(
'range_sample'
).shape == (360, 438, 1)
).shape == (360, 438)
assert echodata["Sonar/Beam_group1"].backscatter_r.sel(channel='55030-769-4').dropna(
'range_sample'
).shape == (360, 135, 1)
).shape == (360, 135)

# check convention-required variables in the Platform group
check_platform_required_scalar_vars(echodata)
Expand Down
7 changes: 3 additions & 4 deletions echopype/tests/convert/test_convert_ek60.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_convert_ek60_matlab_raw(ek60_path):
ds_matlab['rawData'][0]['pings'][0]['power'][0][fidx]
for fidx in range(5)
],
echodata["Sonar/Beam_group1"].backscatter_r.isel(beam=0).transpose(
echodata["Sonar/Beam_group1"].backscatter_r.transpose(
'channel', 'range_sample', 'ping_time'
),
rtol=0,
Expand All @@ -82,7 +82,7 @@ def test_convert_ek60_matlab_raw(ek60_path):
ds_matlab['rawData'][0]['pings'][0][angle][0][fidx]
for fidx in range(5)
],
echodata["Sonar/Beam_group1"]['angle_' + angle].isel(beam=0).transpose(
echodata["Sonar/Beam_group1"]['angle_' + angle].transpose(
'channel', 'range_sample', 'ping_time'
),
)
Expand Down Expand Up @@ -121,8 +121,7 @@ def test_convert_ek60_echoview_raw(ek60_path):
echodata["Sonar/Beam_group1"].backscatter_r.isel(
channel=sorted_freq_ind[fidx],
ping_time=slice(None, 10),
range_sample=slice(1, None),
beam=0
range_sample=slice(1, None)
),
atol=9e-6,
rtol=atol,
Expand Down
7 changes: 3 additions & 4 deletions echopype/tests/convert/test_convert_ek80.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ def test_convert_ek80_cw_power_angle_echoview(ek80_path):
test_power = pd.read_csv(file, delimiter=';').iloc[:, 13:].values
assert np.allclose(
test_power,
echodata["Sonar/Beam_group1"].backscatter_r.sel(channel=chan,
beam='1').dropna('range_sample'),
echodata["Sonar/Beam_group1"].backscatter_r.sel(channel=chan).dropna('range_sample'),
rtol=0,
atol=1.1e-5,
)
Expand All @@ -216,15 +215,15 @@ def test_convert_ek80_cw_power_angle_echoview(ek80_path):
for ping_idx in df_angle['Ping_index'].value_counts().index:
assert np.allclose(
df_angle.loc[df_angle['Ping_index'] == ping_idx, ' Major'],
major.sel(channel=chan, beam='1')
major.sel(channel=chan)
.isel(ping_time=ping_idx)
.dropna('range_sample'),
rtol=0,
atol=5e-5,
)
assert np.allclose(
df_angle.loc[df_angle['Ping_index'] == ping_idx, ' Minor'],
minor.sel(channel=chan, beam='1')
minor.sel(channel=chan)
.isel(ping_time=ping_idx)
.dropna('range_sample'),
rtol=0,
Expand Down
8 changes: 6 additions & 2 deletions echopype/tests/echodata/test_echodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,16 @@ def test_nan_range_entries(range_check_files):
echodata, env_params=None, cal_params=None, ecs_file=None, waveform_mode="BB", encode_mode="complex",
)
range_output = cal_obj.range_meter
nan_locs_backscatter_r = ~echodata["Sonar/Beam_group1"].backscatter_r.isel(beam=0).drop("beam").isnull()
if "beam" in echodata["Sonar/Beam_group1"].dims:
nan_locs_backscatter_r = ~echodata["Sonar/Beam_group1"].backscatter_r.isel(beam=0).drop(
"beam").isnull()
else:
nan_locs_backscatter_r = ~echodata["Sonar/Beam_group1"].backscatter_r.isnull()
leewujung marked this conversation as resolved.
Show resolved Hide resolved
else:
ds_Sv = echopype.calibrate.compute_Sv(echodata)
cal_obj = CalibrateEK60(echodata, env_params={}, cal_params=None, ecs_file=None)
range_output = cal_obj.range_meter
nan_locs_backscatter_r = ~echodata["Sonar/Beam_group1"].backscatter_r.isel(beam=0).drop("beam").isnull()
nan_locs_backscatter_r = ~echodata["Sonar/Beam_group1"].backscatter_r.isnull()

nan_locs_Sv_range = ~ds_Sv.echo_range.isnull()
nan_locs_range = ~range_output.isnull()
Expand Down