Skip to content

Commit

Permalink
Merge pull request Unidata#2312 from lpilz/fix_quantify
Browse files Browse the repository at this point in the history
Retaining global attrs on MetPyDatasetAccessor.quantify()
  • Loading branch information
dopplershift authored Jan 31, 2022
2 parents 067a01b + 1046fa2 commit 99f60ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,11 +1000,15 @@ def quantify(self):
by this operation. Do not utilize on moderate- to large-sized remote datasets before
subsetting!
"""
return self._dataset.map(lambda da: da.metpy.quantify())
return self._dataset.map(lambda da: da.metpy.quantify()).assign_attrs(
self._dataset.attrs
)

def dequantify(self):
"""Return new dataset with variables cast to magnitude and units on attribute."""
return self._dataset.map(lambda da: da.metpy.dequantify())
return self._dataset.map(lambda da: da.metpy.dequantify()).assign_attrs(
self._dataset.attrs
)


def _assign_axis(attributes, axis):
Expand Down
10 changes: 6 additions & 4 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_ds_generic():
'c': xr.DataArray(np.arange(3), dims='c'),
'd': xr.DataArray(np.arange(5), dims='d'),
'e': xr.DataArray(np.arange(5), dims='e')
}, dims=['a', 'b', 'c', 'd', 'e'], name='test').to_dataset()
}, dims=['a', 'b', 'c', 'd', 'e'], attrs={'units': 'kelvin'}, name='test').to_dataset()


@pytest.fixture
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_quantify(test_ds_generic):
original = test_ds_generic['test'].values
result = test_ds_generic['test'].metpy.quantify()
assert isinstance(result.data, units.Quantity)
assert result.data.units == units.dimensionless
assert result.data.units == units.kelvin
assert 'units' not in result.attrs
np.testing.assert_array_almost_equal(result.data, units.Quantity(original))

Expand All @@ -185,24 +185,26 @@ def test_dataset_quantify(test_ds_generic):
"""Test quantify method for converting data to Quantity on Datasets."""
result = test_ds_generic.metpy.quantify()
assert isinstance(result['test'].data, units.Quantity)
assert result['test'].data.units == units.dimensionless
assert result['test'].data.units == units.kelvin
assert 'units' not in result['test'].attrs
np.testing.assert_array_almost_equal(
result['test'].data,
units.Quantity(test_ds_generic['test'].data)
)
assert result.attrs == test_ds_generic.attrs


def test_dataset_dequantify():
"""Test dequantify method for converting data away from Quantity on Datasets."""
original = xr.Dataset({
'test': ('x', units.Quantity([280, 290, 300], 'K')),
'x': np.arange(3)
})
}, attrs={'test': 'test'})
result = original.metpy.dequantify()
assert isinstance(result['test'].data, np.ndarray)
assert result['test'].attrs['units'] == 'kelvin'
np.testing.assert_array_almost_equal(result['test'].data, original['test'].data.magnitude)
assert result.attrs == original.attrs


def test_radian_projection_coords():
Expand Down

0 comments on commit 99f60ff

Please sign in to comment.