Skip to content

Commit

Permalink
Merge pull request #1832 from dcamron/issue/1748
Browse files Browse the repository at this point in the history
  • Loading branch information
dopplershift authored Apr 27, 2021
2 parents 350a989 + adecf7e commit 36281bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def __init__(self, data_array): # noqa: D107
@property
def units(self):
"""Return the units of this DataArray as a `pint.Unit`."""
if isinstance(self._data_array.data, units.Quantity):
return self._data_array.data.units
if isinstance(self._data_array.variable._data, units.Quantity):
return self._data_array.variable._data.units
else:
return units.parse_units(self._data_array.attrs.get('units', 'dimensionless'))

Expand Down Expand Up @@ -747,7 +747,7 @@ def parse_cf(self, varname=None, coordinates=None):
else:
crs = CFProjection(proj_var.attrs)

if crs is None and not check_axis(var, 'latitude', 'longitude'):
if crs is None:
# This isn't a lat or lon coordinate itself, so determine if we need to fall back
# to creating a latitude_longitude CRS. We do so if there exists valid *at most
# 1D* coordinates for latitude and longitude (usually dimension coordinates, but
Expand Down
8 changes: 8 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: BSD-3-Clause
"""Test the operation of MetPy's XArray accessors."""
from collections import OrderedDict
from unittest.mock import patch, PropertyMock

import numpy as np
import pyproj
Expand Down Expand Up @@ -115,6 +116,13 @@ def test_units(test_var):
assert test_var.metpy.units == units.kelvin


def test_units_data(test_var):
"""Test units property fetching does not touch variable.data."""
with patch.object(xr.Variable, 'data', new_callable=PropertyMock) as mock_data_property:
test_var.metpy.units
mock_data_property.assert_not_called()


def test_units_percent():
"""Test that '%' is handled as 'percent'."""
test_var_percent = xr.open_dataset(
Expand Down

0 comments on commit 36281bd

Please sign in to comment.