Skip to content

Commit cf431ad

Browse files
authored
Add variables argument to the DatacubeExtension apply method (#782)
* let apply set variables * add test for datacube apply * pacify mypy * add to changelog
1 parent 07816f5 commit cf431ad

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
### Fixed
1717

1818
- "How to create STAC catalogs" tutorial ([#775](https://github.com/stac-utils/pystac/pull/775))
19+
- 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))
1920

2021
## [v1.4.0]
2122

pystac/extensions/datacube.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ class VariableType(StringEnum):
411411

412412

413413
class Variable:
414+
"""Object representing a variable in the datacube. The dimensions field lists
415+
zero or more :stac-ext:`Datacube Dimension Object <datacube#dimension-object>`
416+
instances. See the :stac-ext:`Datacube Variable Object
417+
<datacube#variable-object>` docs for details.
418+
"""
419+
414420
properties: Dict[str, Any]
415421

416422
def __init__(self, properties: Dict[str, Any]) -> None:
@@ -522,15 +528,22 @@ class DatacubeExtension(
522528
>>> dc_ext = DatacubeExtension.ext(item)
523529
"""
524530

525-
def apply(self, dimensions: Dict[str, Dimension]) -> None:
531+
def apply(
532+
self,
533+
dimensions: Dict[str, Dimension],
534+
variables: Optional[Dict[str, Variable]] = None,
535+
) -> None:
526536
"""Applies label extension properties to the extended
527537
:class:`~pystac.Collection`, :class:`~pystac.Item` or :class:`~pystac.Asset`.
528538
529539
Args:
530540
dimensions : Dictionary mapping dimension name to a :class:`Dimension`
531541
object.
542+
variables : Dictionary mapping variable name to a :class:`Variable`
543+
object.
532544
"""
533545
self.dimensions = dimensions
546+
self.variables = variables
534547

535548
@property
536549
def dimensions(self) -> Dict[str, Dimension]:

tests/extensions/test_datacube.py

+13
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@ def test_set_variables(self) -> None:
8787
self.assertEqual(
8888
item.properties["cube:variables"], {"temp": new_variable.to_dict()}
8989
)
90+
91+
def test_apply_variables(self) -> None:
92+
item = pystac.Item.from_file(self.example_uri)
93+
cube = DatacubeExtension.ext(item)
94+
variables = cube.variables
95+
assert variables is not None
96+
key, value = variables.popitem()
97+
target = value.to_dict()
98+
cube.variables = None
99+
cube.apply(dimensions={}, variables={key: value})
100+
variables = cube.variables
101+
assert variables is not None
102+
self.assertEqual(target, cube.variables[key].to_dict())

0 commit comments

Comments
 (0)