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

Fix interpolate_to_slice unit handling #3347

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ docs/api/generated
docs/api/areas.rst
examples/scripts
test_output/
docs/sg_execution_times.rst
4 changes: 3 additions & 1 deletion src/metpy/interpolate/slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import xarray as xr

from ..package_tools import Exporter
from ..units import is_quantity
from ..xarray import check_axis

exporter = Exporter(globals())
Expand Down Expand Up @@ -49,14 +50,15 @@ def interpolate_to_slice(data, points, interp_type='linear'):
'your data has been parsed by MetPy with proper x and y '
'dimension coordinates.') from None

need_quantify = is_quantity(data.data)
data = data.metpy.dequantify()
data_sliced = data.interp({
x.name: xr.DataArray(points[:, 0], dims='index', attrs=x.attrs),
y.name: xr.DataArray(points[:, 1], dims='index', attrs=y.attrs)
}, method=interp_type)
data_sliced.coords['index'] = range(len(points))

return data_sliced.metpy.quantify()
return data_sliced.metpy.quantify() if need_quantify else data_sliced


@exporter.export
Expand Down
12 changes: 10 additions & 2 deletions tests/interpolate/test_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,24 @@ def test_ds_xy():
return ds.metpy.parse_cf()


def test_interpolate_to_slice_against_selection(test_ds_lonlat):
@pytest.mark.parametrize('bad_units', [False, True])
def test_interpolate_to_slice_against_selection(test_ds_lonlat, bad_units):
"""Test interpolate_to_slice on a simple operation."""
data = test_ds_lonlat['temperature']

# interpolate_to_slice shouldn't care about units
if bad_units:
# Needed so we can go back to using attribute metadata
data = data.metpy.dequantify()
data.attrs['units'] = 'my_bad_units'

path = np.array([[265.0, 30.],
[265.0, 36.],
[265.0, 42.]])
test_slice = interpolate_to_slice(data, path)
true_slice = data.sel({'lat': [30., 36., 42.], 'lon': 265.0})
# Coordinates differ, so just compare the data
assert_array_almost_equal(true_slice.metpy.unit_array, test_slice.metpy.unit_array, 5)
assert_array_almost_equal(true_slice.data, test_slice.data, 5)


@needs_cartopy
Expand Down