Skip to content

Commit

Permalink
Merge pull request #33 from Ouranosinc/fix-datacube
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault authored Nov 21, 2023
2 parents 668c80a + c8669c1 commit 341349f
Show file tree
Hide file tree
Showing 5 changed files with 673 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased](https://github.com/crim-ca/stac-populator) (latest)

<!-- insert list items of new changes here -->
* Fix datacube extension creation to match schema.

## [0.3.0](https://github.com/crim-ca/stac-populator/tree/0.3.0) (2023-11-16)

Expand Down
34 changes: 22 additions & 12 deletions STACpopulator/implementations/CMIP6_UofT/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class DataCubeHelper:
"""Return STAC Item from CF JSON metadata, as provided by `xncml.Dataset.to_cf_dict`."""

axis = {"X": "x", "Y": "y", "Z": "z", "T": "t", "longitude": "x", "latitude": "y", "vertical": "z", "time": "t"}
axis = {"X": "x", "Y": "y", "Z": "z", "T": None, "longitude": "x", "latitude": "y", "vertical": "z", "time": "t"}

def __init__(self, attrs: dict):
"""
Expand Down Expand Up @@ -145,6 +145,7 @@ def __init__(self, attrs: dict):
def dimensions(self) -> dict:
"""Return Dimension objects required for Datacube extension."""


dims = {}
for name, length in self.attrs["dimensions"].items():
v = self.attrs["variables"].get(name)
Expand All @@ -163,17 +164,20 @@ def dimensions(self) -> dict:
extent = bbox[0], bbox[2]
elif key == "Y":
extent = bbox[1], bbox[3]
elif key in ["T", "time"]:
extent = self.temporal_extent()
else:
extent = None

dims[name] = Dimension(
properties=dict(
axis=axis,
type=type_,
extent=extent,
description=v.get("description", v.get("long_name", criteria["standard_name"])),
)
extent = ["", ""]

properties = dict(
type=type_.value,
extent=extent,
description=v.get("description", v.get("long_name", criteria["standard_name"][0])) or "",
)
if type_ == DimensionType.SPATIAL:
properties["axis"] = axis

dims[name] = Dimension(properties=properties)

return dims

Expand All @@ -192,8 +196,8 @@ def variables(self) -> dict:
properties=dict(
dimensions=meta["shape"],
type=VariableType.AUXILIARY.value if self.is_coordinate(attrs) else VariableType.DATA.value,
description=attrs.get("description", attrs.get("long_name")),
unit=attrs.get("units", None),
description=attrs.get("description", attrs.get("long_name", "")),
unit=attrs.get("units", ""),
)
)
return variables
Expand All @@ -207,3 +211,9 @@ def is_coordinate(self, attrs: dict) -> bool:
if attrs.get(criterion, None) in expected:
return True
return False

def temporal_extent(self):
cfmeta = self.attrs["groups"]["CFMetadata"]["attributes"]
start_datetime = cfmeta["time_coverage_start"]
end_datetime = cfmeta["time_coverage_end"]
return [start_datetime, end_datetime]
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dev = [
"coverage",
"responses",
"bump-my-version",
"jsonschema",
]

[tool.pytest.ini_options]
Expand Down
Loading

0 comments on commit 341349f

Please sign in to comment.