Skip to content

Commit

Permalink
adding collection resource link for Magpie
Browse files Browse the repository at this point in the history
  • Loading branch information
dchandan committed Oct 18, 2023
1 parent 3e88591 commit 8d66fba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ STAC_HOST = http://localhost:8880/stac
testcmip6:
python $(IMP_DIR)/CMIP6-UofT/add_CMIP6.py $(STAC_HOST) https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/catalog/birdhouse/testdata/xclim/cmip6/catalog.html $(IMP_DIR)/CMIP6-UofT/CMIP6.yml

delcmip6:
curl --location --request DELETE '$(STAC_HOST)/collections/CMIP6'
@echo ""

starthost:
docker compose up
Expand Down
16 changes: 14 additions & 2 deletions STACpopulator/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import ABC, abstractmethod
from typing import Any, Iterator, MutableMapping, Optional, Tuple

import pystac
import requests
import siphon
import xncml
Expand All @@ -22,7 +23,7 @@

class GenericLoader(ABC):
def __init__(self) -> None:
pass
self.links = []

@abstractmethod
def __iter__(self):
Expand Down Expand Up @@ -58,6 +59,16 @@ def __init__(self, thredds_catalog_url: str, depth: Optional[int] = None) -> Non
self.thredds_catalog_URL = thredds_catalog_url
self.catalog = TDSCatalog(self.thredds_catalog_URL)
self.catalog_head = self.catalog
self.links.append(self.magpie_collection_link())

def magpie_collection_link(self):
"""Return Link to THREDDS catalog."""
url = self.thredds_catalog_URL
parts = url.split("/")
i = parts.index("catalog")
service = parts[i - 1]
path = "/".join(parts[i + 1 : -1])
return pystac.Link(rel="source", target=url, media_type="text/xml", title=f"{service}:{path}")

def reset(self):
"""Reset the generator."""
Expand All @@ -81,7 +92,8 @@ def extract_metadata(self, ds: siphon.catalog.Dataset) -> MutableMapping[str, An
url = ds.access_urls["NCML"]

LOGGER.info("Requesting NcML dataset description")
r = requests.get(url)
# r = requests.get(url)
r = requests.get(url, params={"catalog": self.catalog_head, "dataset": ds})

# Convert NcML to CF-compliant dictionary
attrs = xncml.Dataset.from_text(r.content).to_cf_dict()
Expand Down
3 changes: 3 additions & 0 deletions STACpopulator/populator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def create_stac_collection(self):
self._collection_info["summaries"] = pystac.Summaries({"needs_summaries_update": ["true"]})

collection = pystac.Collection(id=self.collection_id, **self._collection_info)

collection.add_links(self._ingest_pipeline.links)

post_stac_collection(self.stac_host, collection.to_dict())

def ingest(self) -> None:
Expand Down

0 comments on commit 8d66fba

Please sign in to comment.