diff --git a/pyproject.toml b/pyproject.toml index f7f2c6d5..5dddd3b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ dependencies = [ "cfgrib>=0.9.10.1", "dask", "deprecation", - "earthkit-utils>=1.0.0rc0", + "earthkit-utils>=1.0.0rc2", "eccodeslib==2.46.2.17", "eckit==2.0.6.17", "entrypoints", diff --git a/src/earthkit/data/field/component/vertical.py b/src/earthkit/data/field/component/vertical.py index 261cbea5..f69b3d63 100644 --- a/src/earthkit/data/field/component/vertical.py +++ b/src/earthkit/data/field/component/vertical.py @@ -83,7 +83,7 @@ class VerticalBase(SimpleFieldComponent): @mark_get_key @abstractmethod - def level(self) -> Union[int, float]: + def level(self, units=None) -> Union[int, float]: """Return the level.""" pass @@ -252,7 +252,7 @@ def abbreviation(self) -> None: """ return None - def units(self) -> None: + def units(self, units=None) -> None: """Return the units of the level type. The units are not available for this vertical type, and this method returns None. @@ -370,8 +370,13 @@ def __init__( self._type = get_level_type(level_type) self._check() - def level(self) -> Union[int, float]: - return self._level + def level(self, units=None) -> Union[int, float]: + if self._level is None or units is None: + return self._level + else: + from earthkit.utils.units import convert_array + + return convert_array(self._level, source_units=self.units(), target_units=units) def layer(self) -> Optional[tuple[float, float]]: return self._layer diff --git a/tests/field/test_vertical_component.py b/tests/field/test_vertical_component.py index f9b874a9..22e30256 100644 --- a/tests/field/test_vertical_component.py +++ b/tests/field/test_vertical_component.py @@ -26,6 +26,19 @@ def test_vertical_component_alias_1(): assert r.level_type() == "pressure" +def test_vertical_component_convert_level_units(): + r = Vertical(level=1000, level_type="pressure") + assert r.level() == 1000 + assert r.level_type() == "pressure" + + # convert to Pa + level_pa = r.level(units="Pa") + assert level_pa == 100000 + + level_hpa = r.level(units="hPa") + assert level_hpa == 1000 + + @pytest.mark.parametrize( "input_d,ref", [