Skip to content

Commit c5aed56

Browse files
authored
Merge pull request #3103 from dopplershift/fix-1889
Fix interpolation with mixed types
2 parents 77447f1 + 9e65078 commit c5aed56

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/metpy/interpolate/one_dimension.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,6 @@ def _strip_matching_units(*args):
239239
if all(hasattr(arr, 'units') for arr in args):
240240
return [arr.to(args[0].units).magnitude for arr in args]
241241
else:
242-
return args
242+
# Handle the case where we get mixed 'dimensionless' and bare array. This happens e.g.
243+
# when you pass in a DataArray with no units for one arg.
244+
return [arr.m_as('dimensionless') if hasattr(arr, 'units') else arr for arr in args]

tests/interpolate/test_one_dimension.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77
import pytest
8+
import xarray as xr
89

910
from metpy.interpolate import interpolate_1d, interpolate_nans_1d, log_interpolate_1d
1011
from metpy.testing import assert_array_almost_equal
@@ -49,6 +50,16 @@ def test_log_interpolate_1d():
4950
assert_array_almost_equal(y_interp, y_interp_truth, 7)
5051

5152

53+
def test_log_interpolate_1d_mixed():
54+
"""Test log interpolation with a mix of compatible input types."""
55+
x_log = xr.DataArray([1e3, 1e4, 1e5, 1e6])
56+
y_log = np.log(x_log) * 2 + 3
57+
x_interp = np.array([5e3, 5e4, 5e5])
58+
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
59+
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
60+
assert_array_almost_equal(y_interp, y_interp_truth, 7)
61+
62+
5263
def test_log_interpolate_1d_units():
5364
"""Test interpolating with log x-scale with units."""
5465
x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa

0 commit comments

Comments
 (0)