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 .metpy.units checking .data for units and loading data into memory #1832

Merged
merged 3 commits into from Apr 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/metpy/xarray.py
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
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