From 0adc8e4b8a93e46fd6e9f75fab56fd7fcd0bf285 Mon Sep 17 00:00:00 2001 From: zbruick Date: Mon, 10 Jun 2019 13:32:03 -0600 Subject: [PATCH] Due to lack of support for the '%' as a unit within pint, this update converts '%' units to 'percent' units within the MetPy xarray accessor. Fixes #1038. --- metpy/tests/test_xarray.py | 8 ++++++++ metpy/xarray.py | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/metpy/tests/test_xarray.py b/metpy/tests/test_xarray.py index a36b85f0347..b2c9d15c044 100644 --- a/metpy/tests/test_xarray.py +++ b/metpy/tests/test_xarray.py @@ -84,6 +84,14 @@ def test_units(test_var): assert test_var.metpy.units == units('kelvin') +def test_units_percent(): + """Test that '%' is converted to 'percent'.""" + test_var_percent = xr.open_dataset( + get_test_data('irma_gfs_example.nc', + as_file_obj=False))['Relative_humidity_isobaric'] + assert test_var_percent.metpy.units == units('percent') + + def test_convert_units(test_var): """Test in-place conversion of units.""" test_var.metpy.convert_units('degC') diff --git a/metpy/xarray.py b/metpy/xarray.py index 567ca817a63..64443759f83 100644 --- a/metpy/xarray.py +++ b/metpy/xarray.py @@ -34,7 +34,10 @@ def __init__(self, data_array): @property def units(self): - return units(self._units) + if self._units != '%': + return units(self._units) + else: + return units('percent') @property def unit_array(self):