Skip to content

add name based getter for dimension #688

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,28 @@ def band_names(self) -> List[str]:
def band_common_names(self) -> List[str]:
return self.band_dimension.common_names

def dimension(self,name:str) -> Dimension:
"""
Get a dimension by name

:param name: The name of the dimension
:return: The dimension with given name
:raises MetadataException: If no dimension with the given name exists
"""
for dim in self._dimensions:
if dim.name == name:
return dim
raise MetadataException(f"No dimension with name {name!r}, available dimensions: {self.dimension_names()}")


def get_band_index(self, band: Union[int, str]) -> int:
# TODO: eliminate this shortcut for smaller API surface
return self.band_dimension.band_index(band)

def filter_bands(self, band_names: List[Union[int, str]]) -> CubeMetadata:
"""
Create new `CubeMetadata` with filtered band dimension

:param band_names: list of band names/indices to keep
:return:
"""
Expand Down Expand Up @@ -379,6 +394,11 @@ def add_dimension(self, name: str, label: Union[str, float], type: str = None) -
dim = Dimension(type=type or "other", name=name)
return self._clone_and_update(dimensions=self._dimensions + [dim])

def add_dimension(self, dimension: Dimension) -> CubeMetadata:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this overwrites the existing add_dimension method
(which makes some tests fail)

"""Create new CubeMetadata object with added dimension"""
return self._clone_and_update(dimensions=self._dimensions + [dimension])


def drop_dimension(self, name: str = None) -> CubeMetadata:
"""Create new CubeMetadata object without dropped dimension with given name"""
dimension_names = self.dimension_names()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def test_band_dimension_set_labels():
assert metadata.band_dimension.band_names == ["some_name"]
assert newdim.band_names == ["1", "2", "3"]

assert metadata.dimension("bs").band_names == ["some_name"]

metadata = CubeMetadata(dimensions=[bdim])
newdim = metadata.rename_labels("bs", target=["1", "2", "3"]).band_dimension
assert metadata.band_dimension.band_names == ["some_name"]
Expand Down Expand Up @@ -679,6 +681,13 @@ def test_cubemetadata_add_temporal_dimension_duplicate():
_ = metadata.add_dimension("date", "2020-05-15", "temporal")


def test_cubemetadata_add_spatial_dimension():
metadata = CubeMetadata(dimensions=[SpatialDimension(name="x", extent=[4,6])])
updated = metadata.add_dimension(SpatialDimension(name="y", extent=[51,56]))

assert ["x"] == metadata.dimension_names()
assert ["x", "y"] == updated.dimension_names()

def test_collectionmetadata_drop_dimension():
metadata = CollectionMetadata(
{
Expand Down
Loading