Skip to content

Commit

Permalink
Merge pull request #48 from crim-ca/cmip6_schema_update
Browse files Browse the repository at this point in the history
Cmip6 schema update
  • Loading branch information
dchandan authored Feb 21, 2024
2 parents ba179dc + 56c8bc4 commit 1e83327
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
45 changes: 25 additions & 20 deletions STACpopulator/extensions/cmip6.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,48 @@
List,
Literal,
MutableMapping,
Optional,
Type,
TypeVar,
Union,
Optional,
cast,
get_args
get_args,
)

import pystac
import pyessv
from pydantic import AnyHttpUrl, BaseModel, ConfigDict, Field, FieldValidationInfo, field_validator
import pystac
from pydantic import (
AnyHttpUrl,
BaseModel,
ConfigDict,
Field,
FieldValidationInfo,
field_validator,
)
from pydantic.fields import FieldInfo
from pystac.extensions import item_assets
from pystac.extensions.base import S # generic pystac.STACObject
from pystac.extensions.base import (
ExtensionManagementMixin,
PropertiesExtension,
SummariesExtension,
S, # generic pystac.STACObject
)

from STACpopulator.models import AnyGeometry
from STACpopulator.stac_utils import ServiceType, collection2literal, ncattrs_to_bbox, ncattrs_to_geometry
from STACpopulator.stac_utils import (
ServiceType,
collection2literal,
ncattrs_to_bbox,
ncattrs_to_geometry,
)

T = TypeVar("T", pystac.Collection, pystac.Item, pystac.Asset, item_assets.AssetDefinition)

SchemaName = Literal["cmip6"]
# FIXME: below reference (used as ID in the schema itself) should be updated once the extension is officially released
# SCHEMA_URI: str = "https://stac-extensions.github.io/cmip6/v1.0.0/schema.json"
# below is the temporary resolvable URI
SCHEMA_URI: str = "https://raw.githubusercontent.com/TomAugspurger/cmip6/main/json-schema/schema.json"
SCHEMA_URI: str = "https://raw.githubusercontent.com/dchandan/stac-extension-cmip6/main/json-schema/schema.json"
PREFIX = f"{get_args(SchemaName)[0]}:"

# CMIP6 controlled vocabulary (CV)
Expand Down Expand Up @@ -240,9 +252,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "CMIP6Extension[T]":
raise pystac.ExtensionTypeError(cls._ext_error_message(obj))

@classmethod
def summaries(
cls, obj: pystac.Collection, add_if_missing: bool = False
) -> "SummariesCMIP6Extension":
def summaries(cls, obj: pystac.Collection, add_if_missing: bool = False) -> "SummariesCMIP6Extension":
"""Returns the extended summaries object for the given collection."""
cls.ensure_has_extension(obj, add_if_missing)
return SummariesCMIP6Extension(obj)
Expand Down Expand Up @@ -278,19 +288,14 @@ def get_assets(
return {
key: asset
for key, asset in self.item.get_assets().items()
if (
service_type is ServiceType and service_type.value in asset.extra_fields
)
or any(
ServiceType.from_value(field, default=None) is ServiceType
for field in asset.extra_fields
)
if (service_type is ServiceType and service_type.value in asset.extra_fields)
or any(ServiceType.from_value(field, default=None) is ServiceType for field in asset.extra_fields)
}

def __repr__(self) -> str:
return f"<ItemCMIP6Extension Item id={self.item.id}>"


class ItemAssetsCMIP6Extension(CMIP6Extension[item_assets.AssetDefinition]):
properties: dict[str, Any]
asset_defn: item_assets.AssetDefinition
Expand Down Expand Up @@ -334,6 +339,7 @@ class SummariesCMIP6Extension(SummariesExtension):
the ``summaries`` field of a :class:`~pystac.Collection` to include properties
defined in the :stac-ext:`CMIP6 <cmip6>`.
"""

def _check_cmip6_property(self, prop: str) -> FieldInfo:
try:
return CMIP6Properties.model_fields[prop]
Expand Down Expand Up @@ -363,6 +369,5 @@ def __setattr__(self, prop, value):


class CollectionCMIP6Extension(CMIP6Extension[pystac.Collection]):

def __init__(self, collection: pystac.Collection):
self.collection = collection
2 changes: 1 addition & 1 deletion tests/data/stac_item_testdata_xclim_cmip6_ncml.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@
89.74176788330078
],
"stac_extensions": [
"https://raw.githubusercontent.com/TomAugspurger/cmip6/main/json-schema/schema.json"
"https://raw.githubusercontent.com/dchandan/stac-extension-cmip6/main/json-schema/schema.json"
]
}

0 comments on commit 1e83327

Please sign in to comment.