Skip to content

Commit

Permalink
harmonized datacube and cmip extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
huard committed Sep 29, 2023
1 parent 6d32e14 commit 0c829a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
26 changes: 4 additions & 22 deletions STACpopulator/stac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class Item(BaseModel):

class CFJsonItem:
"""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"}

def __init__(self, iid: str, attrs: dict, datamodel=None):
"""
Create STAC Item from CF JSON metadata.
Expand Down Expand Up @@ -249,28 +251,8 @@ def ncattrs_to_bbox(self) -> list:
float(attrs["geospatial_lat_max"][0]),
]


class DatacubeExt:
"""Extend STAC Item with Datacube properties."""
axis = {"X": "x", "Y": "y", "Z": "z", "T": "t", "longitude": "x", "latitude": "y", "vertical": "z", "time": "t"}

def __init__(self, obj: CFJsonItem):
"""
Add Datacube extension to STAC Item.
Parameters
----------
obj : CFJsonItem
STAC Item created from CF JSON metadata.
"""
self.obj = obj
self.attrs = obj.attrs

self.ext = DatacubeExtension.ext(self.obj.item, add_if_missing=True)
self.ext.apply(dimensions=self.dimensions(), variables=self.variables())

def dimensions(self) -> dict:
"""Return Dimension objects."""
"""Return Dimension objects required for Datacube extension."""

dims = {}
for name, length in self.attrs["dimensions"].items():
Expand Down Expand Up @@ -304,7 +286,7 @@ def dimensions(self) -> dict:
return dims

def variables(self)->dict:
"""Return Variable objects"""
"""Return Variable objects required for Datacube extension."""
variables = {}

for name, meta in self.attrs["variables"].items():
Expand Down
16 changes: 13 additions & 3 deletions implementations/CMIP6-UofT/add_CMIP6.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import argparse
import pyessv
from pydantic import AnyHttpUrl, BaseModel, Field, FieldValidationInfo, field_validator

from pystac.extensions.datacube import DatacubeExtension

from STACpopulator import STACpopulatorBase
from STACpopulator.extensions import cmip6
from STACpopulator.input import THREDDSLoader
from STACpopulator.stac_utils import ItemProperties
from STACpopulator.stac_utils import collection2literal, DatacubeExt, CFJsonItem
from STACpopulator.stac_utils import collection2literal, CFJsonItem


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -148,8 +149,17 @@ def create_stac_item(self, item_name: str, item_data: MutableMapping[str, Any])

obj = CFJsonItem(iid, item_data, self.props_model)

# Add CMIP6 extension
try:
cmip6_ext = cmip6.CMIP6Extension.ext(obj.item, add_if_missing=True)
cmip6_ext.apply(item_data["attributes"])
except:
LOGGER.warning(f"Failed to add CMIP6 extension to item {item_name}")

# Add datacube extension
try:
DatacubeExt(obj)
dc_ext = DatacubeExtension.ext(obj.item, add_if_missing=True)
dc_ext.apply(dimensions=obj.dimensions(), variables=obj.variables())
except:
LOGGER.warning(f"Failed to add Datacube extension to item {item_name}")

Expand Down

0 comments on commit 0c829a1

Please sign in to comment.