Skip to content

Commit 053f33d

Browse files
committed
validate collection_id, normalize osc_themes, fix variable description fallback, add visualisation link, osc_project_title, and STAC item/catalog log messages
1 parent 3e17cca commit 053f33d

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

deep_code/utils/dataset_stac_generator.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,29 @@ def __init__(
5050
osc_missions: list[str] | None = None,
5151
cf_params: list[dict[str]] | None = None,
5252
osc_project: str = "deep-earth-system-data-lab",
53+
osc_project_title: str | None = None,
54+
visualisation_link: str | None = None,
5355
):
56+
if " " in collection_id:
57+
raise ValueError(
58+
f"collection_id must not contain spaces: {collection_id!r}. "
59+
"Use hyphens as word separators (e.g. 'My-Dataset-2024')."
60+
)
5461
self.dataset_id = dataset_id
5562
self.collection_id = collection_id
5663
self.workflow_id = workflow_id
5764
self.workflow_title = workflow_title
5865
self.license_type = license_type
5966
self.osc_project = osc_project
67+
self.osc_project_title = osc_project_title or self.format_string(osc_project)
6068
self.access_link = access_link or f"s3://deep-esdl-public/{dataset_id}"
6169
self.documentation_link = documentation_link
6270
self.osc_status = osc_status
6371
self.osc_region = osc_region
64-
self.osc_themes = osc_themes or []
72+
self.osc_themes = [t.lower() for t in (osc_themes or [])]
6573
self.osc_missions = osc_missions or []
6674
self.cf_params = cf_params or {}
75+
self.visualisation_link = visualisation_link
6776
self.logger = logging.getLogger(__name__)
6877
self.dataset = open_dataset(dataset_id=dataset_id, logger=self.logger)
6978
self.variables_metadata = self.get_variables_metadata()
@@ -215,7 +224,7 @@ def build_variable_catalog(self, var_metadata) -> Catalog:
215224
# Create a PySTAC Catalog object
216225
var_catalog = Catalog(
217226
id=var_id,
218-
description=var_metadata.get("description"),
227+
description=var_metadata.get("description") or self.format_string(var_id),
219228
title=self.format_string(var_id),
220229
stac_extensions=[
221230
"https://stac-extensions.github.io/themes/v1.0.0/schema.json"
@@ -474,6 +483,7 @@ def build_zarr_stac_item(self, stac_catalog_s3_root: str) -> Item:
474483
Returns:
475484
A :class:`pystac.Item` ready to be serialised to S3.
476485
"""
486+
self.logger.info(f"Building STAC Item for collection '{self.collection_id}'.")
477487
spatial_extent = self._get_spatial_extent()
478488
temporal_extent = self._get_temporal_extent()
479489
general_metadata = self._get_general_metadata()
@@ -542,6 +552,7 @@ def build_zarr_stac_item(self, stac_catalog_s3_root: str) -> Item:
542552
title="Consolidated Zarr Metadata",
543553
roles=["metadata"],
544554
))
555+
self.logger.info(f"STAC Item built: {item_href}")
545556
return item
546557

547558
def build_zarr_stac_catalog_file_dict(
@@ -563,6 +574,10 @@ def build_zarr_stac_catalog_file_dict(
563574
Returns:
564575
``{s3_path: content_dict}`` for every file to be written to S3.
565576
"""
577+
self.logger.info(
578+
f"Building STAC Catalog file dict for collection '{self.collection_id}' "
579+
f"at root '{stac_catalog_s3_root}'."
580+
)
566581
root = stac_catalog_s3_root.rstrip("/")
567582
catalog_href = f"{root}/catalog.json"
568583

@@ -581,9 +596,11 @@ def build_zarr_stac_catalog_file_dict(
581596
title=self.collection_id,
582597
))
583598

599+
item_href = f"{root}/{self.collection_id}/item.json"
600+
self.logger.info(f"STAC Catalog file dict ready: {catalog_href}, {item_href}")
584601
return {
585602
catalog_href: catalog.to_dict(transform_hrefs=False),
586-
f"{root}/{self.collection_id}/item.json": item.to_dict(transform_hrefs=False),
603+
item_href: item.to_dict(transform_hrefs=False),
587604
}
588605

589606
def build_dataset_stac_collection(self, mode: str, stac_catalog_s3_root: str | None = None) -> Collection:
@@ -642,6 +659,10 @@ def build_dataset_stac_collection(self, mode: str, stac_catalog_s3_root: str | N
642659
collection.add_link(
643660
Link(rel="via", target=self.documentation_link, title="Documentation")
644661
)
662+
if self.visualisation_link:
663+
collection.add_link(
664+
Link(rel="visualisation", target=self.visualisation_link, title="Dataset visualisation")
665+
)
645666
collection.add_link(
646667
Link(
647668
rel="parent",
@@ -689,7 +710,7 @@ def build_dataset_stac_collection(self, mode: str, stac_catalog_s3_root: str | N
689710
rel="related",
690711
target=f"../../projects/{self.osc_project}/collection.json",
691712
media_type="application/json",
692-
title=f"Project: {self.format_string(self.osc_project)}",
713+
title=f"Project: {self.osc_project_title}",
693714
)
694715
)
695716

0 commit comments

Comments
 (0)