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 interpolation with mixed types #3103

Merged
merged 1 commit into from
Jul 6, 2023
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
4 changes: 3 additions & 1 deletion src/metpy/interpolate/one_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,6 @@ def _strip_matching_units(*args):
if all(hasattr(arr, 'units') for arr in args):
return [arr.to(args[0].units).magnitude for arr in args]
else:
return args
# Handle the case where we get mixed 'dimensionless' and bare array. This happens e.g.
# when you pass in a DataArray with no units for one arg.
return [arr.m_as('dimensionless') if hasattr(arr, 'units') else arr for arr in args]
11 changes: 11 additions & 0 deletions tests/interpolate/test_one_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import pytest
import xarray as xr

from metpy.interpolate import interpolate_1d, interpolate_nans_1d, log_interpolate_1d
from metpy.testing import assert_array_almost_equal
Expand Down Expand Up @@ -49,6 +50,16 @@ def test_log_interpolate_1d():
assert_array_almost_equal(y_interp, y_interp_truth, 7)


def test_log_interpolate_1d_mixed():
"""Test log interpolation with a mix of compatible input types."""
x_log = xr.DataArray([1e3, 1e4, 1e5, 1e6])
y_log = np.log(x_log) * 2 + 3
x_interp = np.array([5e3, 5e4, 5e5])
y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
y_interp = log_interpolate_1d(x_interp, x_log, y_log)
assert_array_almost_equal(y_interp, y_interp_truth, 7)


def test_log_interpolate_1d_units():
"""Test interpolating with log x-scale with units."""
x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa
Expand Down