Skip to content

Commit 1f9274c

Browse files
committed
add name based getter for dimension
1 parent e106045 commit 1f9274c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

openeo/metadata.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,28 @@ def band_names(self) -> List[str]:
298298
def band_common_names(self) -> List[str]:
299299
return self.band_dimension.common_names
300300

301+
def dimension(self,name:str) -> Dimension:
302+
"""
303+
Get a dimension by name
304+
305+
:param name: The name of the dimension
306+
:return: The dimension with given name
307+
:raises MetadataException: If no dimension with the given name exists
308+
"""
309+
for dim in self._dimensions:
310+
if dim.name == name:
311+
return dim
312+
raise MetadataException(f"No dimension with name {name!r}, available dimensions: {self.dimension_names()}")
313+
314+
301315
def get_band_index(self, band: Union[int, str]) -> int:
302316
# TODO: eliminate this shortcut for smaller API surface
303317
return self.band_dimension.band_index(band)
304318

305319
def filter_bands(self, band_names: List[Union[int, str]]) -> CubeMetadata:
306320
"""
307321
Create new `CubeMetadata` with filtered band dimension
322+
308323
:param band_names: list of band names/indices to keep
309324
:return:
310325
"""

tests/test_metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def test_band_dimension_set_labels():
139139
assert metadata.band_dimension.band_names == ["some_name"]
140140
assert newdim.band_names == ["1", "2", "3"]
141141

142+
assert metadata.dimension("bs").band_names == ["some_name"]
143+
142144
metadata = CubeMetadata(dimensions=[bdim])
143145
newdim = metadata.rename_labels("bs", target=["1", "2", "3"]).band_dimension
144146
assert metadata.band_dimension.band_names == ["some_name"]

0 commit comments

Comments
 (0)