From e4b7afe254ebd05b8de0e6dc0ca1f2b6c16cf02b Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Thu, 31 Mar 2022 13:45:26 -0400 Subject: [PATCH 1/4] let apply set variables --- pystac/extensions/datacube.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pystac/extensions/datacube.py b/pystac/extensions/datacube.py index d02264a64..ea0857c9b 100644 --- a/pystac/extensions/datacube.py +++ b/pystac/extensions/datacube.py @@ -411,6 +411,12 @@ class VariableType(StringEnum): class Variable: + """Object representing a variable in the datacube. The dimensions field lists + zero or more :stac-ext:`Datacube Dimension Object ` + instances. See the :stac-ext:`Datacube Variable Object + ` docs for details. + """ + properties: Dict[str, Any] def __init__(self, properties: Dict[str, Any]) -> None: @@ -522,15 +528,22 @@ class DatacubeExtension( >>> dc_ext = DatacubeExtension.ext(item) """ - def apply(self, dimensions: Dict[str, Dimension]) -> None: + def apply( + self, + dimensions: Dict[str, Dimension], + variables: Optional[Dict[str, Variable]] = None, + ) -> None: """Applies label extension properties to the extended :class:`~pystac.Collection`, :class:`~pystac.Item` or :class:`~pystac.Asset`. Args: dimensions : Dictionary mapping dimension name to a :class:`Dimension` object. + variables : Dictionary mapping variable name to a :class:`Variable` + object. """ self.dimensions = dimensions + self.variables = variables @property def dimensions(self) -> Dict[str, Dimension]: From 1c16ede5d0542d1ceb3d427ece1062ee5bc689e2 Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Thu, 31 Mar 2022 17:18:03 -0400 Subject: [PATCH 2/4] add test for datacube apply --- tests/extensions/test_datacube.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/extensions/test_datacube.py b/tests/extensions/test_datacube.py index ef1f60e8c..47be525f7 100644 --- a/tests/extensions/test_datacube.py +++ b/tests/extensions/test_datacube.py @@ -87,3 +87,12 @@ def test_set_variables(self) -> None: self.assertEqual( item.properties["cube:variables"], {"temp": new_variable.to_dict()} ) + + def test_apply_variables(self) -> None: + item = pystac.Item.from_file(self.example_uri) + cube = DatacubeExtension.ext(item) + key, value = cube.variables.popitem() + target = value.to_dict() + cube.variables = None + cube.apply(dimensions={}, variables={key: value}) + self.assertEqual(target, cube.variables[key].to_dict()) From 6126cf0ca28b6ba10be0430ad6bfbe8f044f446e Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Thu, 31 Mar 2022 19:38:07 -0400 Subject: [PATCH 3/4] pacify mypy --- tests/extensions/test_datacube.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/extensions/test_datacube.py b/tests/extensions/test_datacube.py index 47be525f7..f7ee17217 100644 --- a/tests/extensions/test_datacube.py +++ b/tests/extensions/test_datacube.py @@ -91,8 +91,12 @@ def test_set_variables(self) -> None: def test_apply_variables(self) -> None: item = pystac.Item.from_file(self.example_uri) cube = DatacubeExtension.ext(item) - key, value = cube.variables.popitem() + variables = cube.variables + assert variables is not None + key, value = variables.popitem() target = value.to_dict() cube.variables = None cube.apply(dimensions={}, variables={key: value}) + variables = cube.variables + assert variables is not None self.assertEqual(target, cube.variables[key].to_dict()) From 8964f5cc0a075a9ada53f79ca14b02029d9e89bb Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Mon, 4 Apr 2022 10:21:40 -0400 Subject: [PATCH 4/4] add to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f640fdfd4..76378e76b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ ### Fixed - "How to create STAC catalogs" tutorial ([#775](https://github.com/stac-utils/pystac/pull/775)) +- Add a `variables` argument, to accompany `dimensions`, for the `apply` method of stac objects extended with datacube ([#782](https://github.com/stac-utils/pystac/pull/782)) ## [v1.4.0]