Skip to content

Commit

Permalink
MNT: Avoid some unit stripped warnings from cross-section
Browse files Browse the repository at this point in the history
  • Loading branch information
dopplershift committed Nov 8, 2023
1 parent 8f2b3e8 commit 4101659
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/metpy/calc/cross_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def unit_vectors_from_cross_section(cross, index='index'):
"""
x, y = distances_from_cross_section(cross)
dx_di = first_derivative(x, axis=index).values
dy_di = first_derivative(y, axis=index).values
dx_di = first_derivative(x, axis=index).data
dy_di = first_derivative(y, axis=index).data
tangent_vector_mag = np.hypot(dx_di, dy_di)
unit_tangent_vector = np.vstack([dx_di / tangent_vector_mag, dy_di / tangent_vector_mag])
unit_normal_vector = np.vstack([-dy_di / tangent_vector_mag, dx_di / tangent_vector_mag])
Expand Down
8 changes: 2 additions & 6 deletions src/metpy/interpolate/slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import xarray as xr

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

exporter = Exporter(globals())
Expand Down Expand Up @@ -50,17 +49,14 @@ 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

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))

# Bug in xarray: interp strips units
if is_quantity(data.data) and not is_quantity(data_sliced.data):
data_sliced.data = units.Quantity(data_sliced.data, data.data.units)

return data_sliced
return data_sliced.metpy.quantify()


@exporter.export
Expand Down
2 changes: 1 addition & 1 deletion tests/calc/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_weighted_continuous_average_elevated():
def test_precipitable_water_xarray():
"""Test precipitable water with xarray input."""
data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC')
press = xr.DataArray(data['pressure'], attrs={'units': str(data['pressure'].units)})
press = xr.DataArray(data['pressure'].m, attrs={'units': str(data['pressure'].units)})
dewp = xr.DataArray(data['dewpoint'], dims=('press',), coords=(press,))
pw = precipitable_water(press, dewp, top=400 * units.hPa)
truth = 22.60430651 * units.millimeters
Expand Down

0 comments on commit 4101659

Please sign in to comment.