-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add align_to_ping_time function, and replace the other interp functions
- Loading branch information
Showing
7 changed files
with
103 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import xarray as xr | ||
import numpy as np | ||
import pytest | ||
|
||
import echopype as ep | ||
from echopype.utils.align import align_to_ping_time | ||
|
||
|
||
@pytest.mark.unit | ||
def test_align_to_ping_time(): | ||
""" | ||
Test aligning external pitch data to Echosounder ping time | ||
""" | ||
# Open RAW and extract ping time | ||
ping_time_da = ep.open_raw( | ||
raw_file="echopype/test_data/azfp/rutgers_glider_external_nc/18011107.01A", | ||
xml_path="echopype/test_data/azfp/rutgers_glider_external_nc/18011107.XML", | ||
sonar_model="azfp" | ||
)["Sonar/Beam_group1"]["ping_time"] | ||
|
||
# Open external glider dataset | ||
glider_ds = xr.open_dataset( | ||
"echopype/test_data/azfp/rutgers_glider_external_nc/ru32-20180109T0531-profile-sci-delayed-subset.nc", | ||
engine="netcdf4" | ||
) | ||
|
||
# Drop NaNs from pitch and align data array | ||
aligned_da = align_to_ping_time(glider_ds["m_pitch"].dropna("time"), "time", ping_time_da) | ||
|
||
# Extract earliest non-NaN time of the aligned dataset | ||
earliest_non_nan_time_in_aligned_da = aligned_da.dropna("ping_time")["ping_time"].min() | ||
|
||
# Grab all values past the earliest non-NaN time in the aligned DataArray | ||
subset_aligned_da = aligned_da.where(aligned_da["ping_time"] >= earliest_non_nan_time_in_aligned_da, drop=True) | ||
|
||
# Check that all values past the earliest non-NaN time in the aligned DataArray are non-NaN | ||
assert np.all(~np.isnan(subset_aligned_da)) | ||
|
||
# Test if aligned_da matches interpolation | ||
assert np.allclose( | ||
aligned_da, | ||
glider_ds["m_pitch"].dropna("time").interp( | ||
{"time": ping_time_da}, | ||
method="nearest", | ||
kwargs={"fill_value": "extrapolate"}, | ||
), | ||
equal_nan=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import xarray as xr | ||
|
||
|
||
def align_to_ping_time( | ||
external_da: xr.DataArray, | ||
external_time_name: str, | ||
ping_time_da: xr.DataArray, | ||
method: str = "nearest", | ||
) -> xr.DataArray: | ||
""" | ||
Aligns an external DataArray to align time-wise with the Echosounder ping time DataArray. | ||
A wrapper function for https://docs.xarray.dev/en/stable/generated/xarray.DataArray.interp.html. | ||
Parameters | ||
---------- | ||
external_da : xr.DataArray | ||
External Non-Echosounder data. | ||
external_time_name : str, default 'nearest' | ||
Time variable name of the External Non-Echosounder data. | ||
ping_time_da : xr.DataArray | ||
Echosounder ping time. | ||
Returns | ||
------- | ||
aligned_da : xr.DataArray | ||
External Non-Echosounder data that is now aligned with the Echosounder ping time. | ||
""" | ||
# Interpolate only if ping time and external time are not equal | ||
if not ping_time_da.equals( | ||
external_da[external_time_name].rename({external_time_name: "ping_time"}) | ||
): | ||
aligned_da = external_da.interp( | ||
{external_time_name: ping_time_da}, | ||
method=method, | ||
# More details for `fill_value` and `extrapolate` can be found here: | ||
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html # noqa | ||
kwargs={"fill_value": "extrapolate"}, | ||
).drop_vars(external_time_name) | ||
else: | ||
aligned_da = external_da.rename({external_time_name: "ping_time"}) | ||
return aligned_da |