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

add accessor convert to base units #2422

Merged
merged 2 commits into from
Apr 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ def convert_units(self, units):
"""
return self.quantify().copy(data=self.unit_array.to(units))

def convert_to_base_units(self):
"""Return new DataArray with values converted to base units.

See Also
--------
convert_units

Notes
-----
Any cached/lazy-loaded data (except that in a Dask array) will be loaded into memory
by this operation. Do not utilize on moderate- to large-sized remote datasets before
subsetting!

"""
return self.quantify().copy(data=self.unit_array.to_base_units())

def convert_coordinate_units(self, coord, units):
"""Return new DataArray with specified coordinate converted to different units.

Expand Down
13 changes: 13 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ def test_convert_units(test_var):
assert_almost_equal(result[0, 0, 0, 0], 18.44 * units.degC, 2)


def test_convert_to_base_units(test_ds):
"""Test conversion of units."""
uwnd = test_ds.u_wind.metpy.quantify()
result = (uwnd * (500 * units.hPa)).metpy.convert_to_base_units()

# Check that units are updated without modifying original
assert result.metpy.units == units('kg s**-3')
assert test_ds.u_wind.metpy.units == units('m/s')

# Make sure we now get an array back with properly converted values
assert_almost_equal(result[0, 0, 0, 0], -448416.12 * units('kg s**-3'), 2)


def test_convert_coordinate_units(test_ds_generic):
"""Test conversion of coordinate units."""
result = test_ds_generic['test'].metpy.convert_coordinate_units('b', 'percent')
Expand Down
6 changes: 6 additions & 0 deletions tutorials/xarray_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@
temperature_degc = temperature[0].metpy.convert_units('degC')
temperature_degc

#########################################################################
# To base unit conversion:

temperature_degk = temperature_degc.metpy.convert_to_base_units()
temperature_degk

#########################################################################
# Unit conversion for coordinates:
heights_on_hpa_levels = heights.metpy.convert_coordinate_units('isobaric3', 'hPa')
Expand Down