Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes to CMIP6 Properties model to allow serialize #22

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
STACpopulator.egg-info/
.vscode/
.venv/
jupyter/
jupyter/
.idea
.vscode
38 changes: 27 additions & 11 deletions STACpopulator/extensions/cmip6.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""CMIP6 extension based on https://stac-extensions.github.io/cmip6/v1.0.0/schema.json"""

from typing import Generic, TypeVar, Dict, Any, cast
import json
from typing import Generic, TypeVar, Union, cast

import pystac
from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension
from pystac.extensions.hooks import ExtensionHooks

from datetime import datetime
from datetime import date, datetime
from typing import Any, Dict, List, Literal
import pyessv
from pydantic import (AnyHttpUrl, BaseModel, Field, FieldValidationInfo, field_validator, model_serializer,
FieldSerializationInfo)
from pydantic import (
AnyHttpUrl,
FieldValidationInfo,
field_validator,
model_serializer,
)
from pydantic.networks import Url


from STACpopulator.stac_utils import ItemProperties
Expand Down Expand Up @@ -59,8 +65,8 @@ class Properties(ItemProperties, validate_assignment=True):
source: str
source_id: SourceID
source_type: List[SourceType]
sub_experiment: str | Literal["none"]
sub_experiment_id: SubExperimentID | Literal["none"]
sub_experiment: Union[str, Literal["none"]]
sub_experiment_id: Union[SubExperimentID, Literal["none"]]
table_id: TableID
variable_id: str
variant_label: str
Expand All @@ -75,10 +81,19 @@ class Properties(ItemProperties, validate_assignment=True):
grid: str
mip_era: str

@model_serializer
def serialize_extension(self):
#@model_serializer
def serialize_extension(self) -> str:
"""Add prefix to all fields."""
return {prefix + k: v for k, v in self.model_dump_json()}

def json_encode(obj):
if isinstance(obj, Url):
return str(obj)
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError(f"Type {type(obj)} not serializable")

data = {prefix + k: v for k, v in self.model_dump().items()}
return json.dumps(data, default=json_encode)

@field_validator("initialization_index", "physics_index", "realization_index", "forcing_index", mode="before")
@classmethod
Expand All @@ -101,7 +116,6 @@ def validate_version(cls, v: str, info: FieldValidationInfo):
return v



class CMIP6Extension(Generic[T], ExtensionManagementMixin[pystac.Item], PropertiesExtension):
"""An abstract class that can be used to extend the properties of a
:class:`~pystac.Item` with properties from the :stac-ext:`CMIP6 Extension <cmip6>`.
Expand All @@ -118,7 +132,7 @@ def apply(self, attrs: Dict[str, Any]) -> None:
variables : Dictionary mapping variable name to a :class:`Variable`
object.
"""
self.properties.update(**Properties(**attrs).model_dump_json())
self.properties.update(**Properties(**attrs).model_dump())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one could be a problem, because the json serializer from pystac does not know how to serialize some of the content.


@classmethod
def get_schema_uri(cls) -> str:
Expand All @@ -139,6 +153,7 @@ def ext(cls, obj: T, add_if_missing: bool = False):
else:
raise pystac.ExtensionTypeError(cls._ext_error_message(obj))


class ItemCMIP6Extension(CMIP6Extension[pystac.Item]):
"""A concrete implementation of :class:`DatacubeExtension` on an
:class:`~pystac.Item` that extends the properties of the Item to include properties
Expand All @@ -164,4 +179,5 @@ class CMIP6ExtensionHooks(ExtensionHooks):
prev_extension_ids = {"cmip6"}
stac_object_types = {pystac.STACObjectType.ITEM}


CMIP6_EXTENSION_HOOKS: ExtensionHooks = CMIP6ExtensionHooks()
Loading