Skip to content
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 9 additions & 4 deletions src/earthkit/data/field/component/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/field/test_vertical_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
Loading