From 71cf9974751eb9e04388dd5a3287a9e18c48ecb9 Mon Sep 17 00:00:00 2001 From: Drew Camron Date: Fri, 23 Apr 2021 17:24:56 -0600 Subject: [PATCH 1/3] Remove check_axis from parse_cf CRS check --- src/metpy/xarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/metpy/xarray.py b/src/metpy/xarray.py index 41e9dc01a0b..fa5c206ab8a 100644 --- a/src/metpy/xarray.py +++ b/src/metpy/xarray.py @@ -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 From 8e4015b7101fb2850539682a5e09cdbe9bbbf06f Mon Sep 17 00:00:00 2001 From: Drew Camron Date: Fri, 23 Apr 2021 17:25:40 -0600 Subject: [PATCH 2/3] Check internal .variable._data in .units to avoid loading --- src/metpy/xarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/metpy/xarray.py b/src/metpy/xarray.py index fa5c206ab8a..ac0f3a63d7b 100644 --- a/src/metpy/xarray.py +++ b/src/metpy/xarray.py @@ -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')) From adecf7e19ed6be10bc0af227aaba59b65487925d Mon Sep 17 00:00:00 2001 From: Drew Camron Date: Mon, 26 Apr 2021 16:47:10 -0600 Subject: [PATCH 3/3] Add test for .metpy.units loading thru variable.data --- tests/test_xarray.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_xarray.py b/tests/test_xarray.py index b51982b6086..9dd4ed8b3be 100644 --- a/tests/test_xarray.py +++ b/tests/test_xarray.py @@ -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 @@ -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(