Skip to content

Commit 059cc1a

Browse files
authored
Merge pull request #3347 from dopplershift/fix-3341
Fix interpolate_to_slice unit handling
2 parents 07a837f + 09769b1 commit 059cc1a

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ docs/api/generated
2929
docs/api/areas.rst
3030
examples/scripts
3131
test_output/
32+
docs/sg_execution_times.rst

src/metpy/interpolate/slices.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import xarray as xr
88

99
from ..package_tools import Exporter
10+
from ..units import is_quantity
1011
from ..xarray import check_axis
1112

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

53+
need_quantify = is_quantity(data.data)
5254
data = data.metpy.dequantify()
5355
data_sliced = data.interp({
5456
x.name: xr.DataArray(points[:, 0], dims='index', attrs=x.attrs),
5557
y.name: xr.DataArray(points[:, 1], dims='index', attrs=y.attrs)
5658
}, method=interp_type)
5759
data_sliced.coords['index'] = range(len(points))
5860

59-
return data_sliced.metpy.quantify()
61+
return data_sliced.metpy.quantify() if need_quantify else data_sliced
6062

6163

6264
@exporter.export

tests/interpolate/test_slices.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,24 @@ def test_ds_xy():
9595
return ds.metpy.parse_cf()
9696

9797

98-
def test_interpolate_to_slice_against_selection(test_ds_lonlat):
98+
@pytest.mark.parametrize('bad_units', [False, True])
99+
def test_interpolate_to_slice_against_selection(test_ds_lonlat, bad_units):
99100
"""Test interpolate_to_slice on a simple operation."""
100101
data = test_ds_lonlat['temperature']
102+
103+
# interpolate_to_slice shouldn't care about units
104+
if bad_units:
105+
# Needed so we can go back to using attribute metadata
106+
data = data.metpy.dequantify()
107+
data.attrs['units'] = 'my_bad_units'
108+
101109
path = np.array([[265.0, 30.],
102110
[265.0, 36.],
103111
[265.0, 42.]])
104112
test_slice = interpolate_to_slice(data, path)
105113
true_slice = data.sel({'lat': [30., 36., 42.], 'lon': 265.0})
106114
# Coordinates differ, so just compare the data
107-
assert_array_almost_equal(true_slice.metpy.unit_array, test_slice.metpy.unit_array, 5)
115+
assert_array_almost_equal(true_slice.data, test_slice.data, 5)
108116

109117

110118
@needs_cartopy

0 commit comments

Comments
 (0)