|
| 1 | +"""CF Extension Module.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import functools |
| 6 | +from typing import ( |
| 7 | + Any, |
| 8 | + Dict, |
| 9 | + Generic, |
| 10 | + Iterable, |
| 11 | + List, |
| 12 | + Literal, |
| 13 | + Optional, |
| 14 | + TypeVar, |
| 15 | + Union, |
| 16 | + cast, |
| 17 | + get_args, |
| 18 | +) |
| 19 | + |
| 20 | +import pystac |
| 21 | +from pydantic import BaseModel |
| 22 | +from pystac.extensions import item_assets |
| 23 | +from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension |
| 24 | + |
| 25 | +from STACpopulator.extensions.base import ExtensionHelper |
| 26 | +from STACpopulator.stac_utils import ServiceType |
| 27 | + |
| 28 | +T = TypeVar("T", pystac.Collection, pystac.Item, pystac.Asset) |
| 29 | +SchemaName = Literal["cf"] |
| 30 | +SCHEMA_URI = "https://stac-extensions.github.io/cf/v0.2.0/schema.json" |
| 31 | +PREFIX = f"{get_args(SchemaName)[0]}:" |
| 32 | +PARAMETER_PROP = PREFIX + "parameter" |
| 33 | + |
| 34 | + |
| 35 | +class CFParameter(BaseModel): |
| 36 | + """CFParameter.""" |
| 37 | + |
| 38 | + name: str |
| 39 | + unit: str |
| 40 | + |
| 41 | + def __repr__(self) -> str: |
| 42 | + """Return string repr.""" |
| 43 | + return f"<CFParameter name={self.name}, unit={self.unit}>" |
| 44 | + |
| 45 | + |
| 46 | +class CFHelper(ExtensionHelper): |
| 47 | + """CFHelper.""" |
| 48 | + |
| 49 | + _prefix: str = "cf" |
| 50 | + variables: Dict[str, Any] |
| 51 | + |
| 52 | + @functools.cached_property |
| 53 | + def parameters(self) -> List[CFParameter]: |
| 54 | + """Extracts cf:parameter-like information from item_data.""" |
| 55 | + parameters = [] |
| 56 | + |
| 57 | + for var in self.variables.values(): |
| 58 | + attrs = var.get("attributes", {}) |
| 59 | + name = attrs.get("standard_name") # Get the required standard name |
| 60 | + if not name: |
| 61 | + continue # Skip if no valid name |
| 62 | + unit = attrs.get("units", "") |
| 63 | + parameters.append(CFParameter(name=name, unit=unit)) |
| 64 | + |
| 65 | + return parameters |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def from_data( |
| 69 | + cls, |
| 70 | + data: dict[str, Any], |
| 71 | + **kwargs, |
| 72 | + ) -> "CFHelper": |
| 73 | + """Create a CFHelper instance from raw data.""" |
| 74 | + return cls(variables=data["data"]["variables"], **kwargs) |
| 75 | + |
| 76 | + def apply(self, item: T, add_if_missing: bool = True) -> T: |
| 77 | + """Apply the Datacube extension to an item.""" |
| 78 | + ext = CFExtension.ext(item, add_if_missing=add_if_missing) |
| 79 | + ext.apply(parameters=self.parameters) |
| 80 | + |
| 81 | + # FIXME: This temporary workaround has been added to comply with the (most certainly buggy) validation schema for CF extension |
| 82 | + # It should be remove once the PR is integrated since applying on the item should be enough |
| 83 | + asset = item.assets["HTTPServer"] |
| 84 | + cf_asset_ext = CFExtension.ext(asset, add_if_missing=True) |
| 85 | + cf_asset_ext.apply(parameters=self.parameters) |
| 86 | + return item |
| 87 | + |
| 88 | + |
| 89 | +class CFExtension( |
| 90 | + Generic[T], |
| 91 | + PropertiesExtension, |
| 92 | + ExtensionManagementMixin[Union[pystac.Asset, pystac.Item, pystac.Collection]], |
| 93 | +): |
| 94 | + """CF Metadata Extension.""" |
| 95 | + |
| 96 | + @property |
| 97 | + def name(self) -> SchemaName: |
| 98 | + """Return the schema name.""" |
| 99 | + return get_args(SchemaName)[0] |
| 100 | + |
| 101 | + @property |
| 102 | + def parameter(self) -> List[dict[str, Any]] | None: |
| 103 | + """Get or set the CF parameter(s).""" |
| 104 | + return self._get_property(PARAMETER_PROP, int) |
| 105 | + |
| 106 | + @parameter.setter |
| 107 | + def parameter(self, v: List[dict[str, Any]] | None) -> None: |
| 108 | + self._set_property(PARAMETER_PROP, v) |
| 109 | + |
| 110 | + def apply( |
| 111 | + self, |
| 112 | + parameters: Union[List[CFParameter], List[dict[str, Any]]], |
| 113 | + ) -> None: |
| 114 | + """Apply CF Extension properties to the extended :class:`~pystac.Item` or :class:`~pystac.Asset`.""" |
| 115 | + if not isinstance(parameters[0], dict): |
| 116 | + parameters = [p.model_dump() for p in parameters] |
| 117 | + self.parameter = parameters |
| 118 | + |
| 119 | + @classmethod |
| 120 | + def get_schema_uri(cls) -> str: |
| 121 | + """Return this extension's schema URI.""" |
| 122 | + return SCHEMA_URI |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def ext(cls, obj: T, add_if_missing: bool = False) -> CFExtension[T]: |
| 126 | + """Extend the given STAC Object with properties from the :stac-ext:`CF Extension <cf>`. |
| 127 | +
|
| 128 | + This extension can be applied to instances of :class:`~pystac.Item`, :class:`~pystac.Asset`, or :class:`~pystac.Collection`. |
| 129 | +
|
| 130 | + Raises |
| 131 | + ------ |
| 132 | + pystac.ExtensionTypeError : If an invalid object type is passed. |
| 133 | + """ |
| 134 | + if isinstance(obj, pystac.Collection): |
| 135 | + cls.ensure_has_extension(obj, add_if_missing) |
| 136 | + return cast(CFExtension[T], CollectionCFExtension(obj)) |
| 137 | + elif isinstance(obj, pystac.Item): |
| 138 | + cls.ensure_has_extension(obj, add_if_missing) |
| 139 | + return cast(CFExtension[T], ItemCFExtension(obj)) |
| 140 | + elif isinstance(obj, pystac.Asset): |
| 141 | + cls.ensure_owner_has_extension(obj, add_if_missing) |
| 142 | + return cast(CFExtension[T], AssetCFExtension(obj)) |
| 143 | + elif isinstance(obj, item_assets.AssetDefinition): |
| 144 | + cls.ensure_owner_has_extension(obj, add_if_missing) |
| 145 | + return cast(CFExtension[T], ItemAssetsCFExtension(obj)) |
| 146 | + else: |
| 147 | + raise pystac.ExtensionTypeError(cls._ext_error_message(obj)) |
| 148 | + |
| 149 | + |
| 150 | +class ItemCFExtension(CFExtension[pystac.Item]): |
| 151 | + """ |
| 152 | + A concrete implementation of :class:`CFExtension` on an :class:`~pystac.Item`. |
| 153 | +
|
| 154 | + Extends the properties of the Item to include properties defined in the |
| 155 | + :stac-ext:`CF Extension <cf>`. |
| 156 | +
|
| 157 | + This class should generally not be instantiated directly. Instead, call |
| 158 | + :meth:`CFExtension.ext` on an :class:`~pystac.Item` to extend it. |
| 159 | + """ |
| 160 | + |
| 161 | + def __init__(self, item: pystac.Item) -> None: |
| 162 | + self.item = item |
| 163 | + self.properties = item.properties |
| 164 | + |
| 165 | + def get_assets( |
| 166 | + self, |
| 167 | + service_type: Optional[ServiceType] = None, |
| 168 | + ) -> dict[str, pystac.Asset]: |
| 169 | + """Get the item's assets where eo:bands are defined. |
| 170 | +
|
| 171 | + Args: |
| 172 | + service_type: If set, filter the assets such that only those with a |
| 173 | + matching :class:`~STACpopulator.stac_utils.ServiceType` are returned. |
| 174 | +
|
| 175 | + Returns |
| 176 | + ------- |
| 177 | + Dict[str, Asset]: A dictionary of assets that match ``service_type`` |
| 178 | + if set or else all of this item's assets were service types are defined. |
| 179 | + """ |
| 180 | + return { |
| 181 | + key: asset |
| 182 | + for key, asset in self.item.get_assets().items() |
| 183 | + if (isinstance(service_type, ServiceType) and service_type.value in asset.extra_fields) |
| 184 | + or any(ServiceType.from_value(field, default=False) for field in asset.extra_fields) |
| 185 | + } |
| 186 | + |
| 187 | + def __repr__(self) -> str: |
| 188 | + """Return repr.""" |
| 189 | + return f"<ItemCFExtension Item id={self.item.id}>" |
| 190 | + |
| 191 | + |
| 192 | +class ItemAssetsCFExtension(CFExtension[item_assets.AssetDefinition]): |
| 193 | + """Extention for CF item assets.""" |
| 194 | + |
| 195 | + properties: dict[str, Any] |
| 196 | + asset_defn: item_assets.AssetDefinition |
| 197 | + |
| 198 | + def __init__(self, item_asset: item_assets.AssetDefinition) -> None: |
| 199 | + self.asset_defn = item_asset |
| 200 | + self.properties = item_asset.properties |
| 201 | + |
| 202 | + |
| 203 | +class AssetCFExtension(CFExtension[pystac.Asset]): |
| 204 | + """ |
| 205 | + A concrete implementation of :class:`CFExtension` on an :class:`~pystac.Asset`. |
| 206 | +
|
| 207 | + Extends the Asset fields to include properties defined in the |
| 208 | + :stac-ext:`CF Extension <cf>`. |
| 209 | +
|
| 210 | + This class should generally not be instantiated directly. Instead, call |
| 211 | + :meth:`CFExtension.ext` on an :class:`~pystac.Asset` to extend it. |
| 212 | + """ |
| 213 | + |
| 214 | + asset_href: str |
| 215 | + """The ``href`` value of the :class:`~pystac.Asset` being extended.""" |
| 216 | + |
| 217 | + properties: dict[str, Any] |
| 218 | + """The :class:`~pystac.Asset` fields, including extension properties.""" |
| 219 | + |
| 220 | + additional_read_properties: Optional[Iterable[dict[str, Any]]] = None |
| 221 | + """If present, this will be a list containing 1 dictionary representing the |
| 222 | + properties of the owning :class:`~pystac.Item`.""" |
| 223 | + |
| 224 | + def __init__(self, asset: pystac.Asset) -> None: |
| 225 | + self.asset_href = asset.href |
| 226 | + self.properties = asset.extra_fields |
| 227 | + if asset.owner and isinstance(asset.owner, pystac.Item): |
| 228 | + self.additional_read_properties = [asset.owner.properties] |
| 229 | + |
| 230 | + def __repr__(self) -> str: |
| 231 | + """Return repr.""" |
| 232 | + return f"<AssetCFExtension Asset href={self.asset_href}>" |
| 233 | + |
| 234 | + |
| 235 | +class CollectionCFExtension(CFExtension[pystac.Collection]): |
| 236 | + """Extension for CF data.""" |
| 237 | + |
| 238 | + def __init__(self, collection: pystac.Collection) -> None: |
| 239 | + self.collection = collection |
0 commit comments