Skip to content

stac

Creates STAC collections and items for Met Office deterministic forecast data.

create_collection

create_collection(model: Model, theme: Theme) -> Collection

Creates a STAC collection for a model and theme combination.

Parameters:

Name Type Description Default
model Model

The Met Office model (global or UK).

required
theme Theme

The theme (height, pressure-level, near-surface, or whole-atmosphere).

required

Returns:

Type Description
Collection

A STAC Collection object configured for the model and theme.

Source code in src/stactools/met_office_deterministic/stac.py
def create_collection(model: Model, theme: Theme) -> Collection:
    """Creates a STAC collection for a model and theme combination.

    Args:
        model: The Met Office model (global or UK).
        theme: The theme (height, pressure-level, near-surface, or whole-atmosphere).

    Returns:
        A STAC Collection object configured for the model and theme.
    """
    collection = Collection(
        id=model.get_collection_id(theme),
        description=DESCRIPTIONS[model][theme],
        title=TITLES[model][theme],
        license="CC-BY-SA-4.0",
        keywords=["MetOffice"] + KEYWORDS[model][theme],
        providers=[
            Provider(
                url="https://www.metoffice.gov.uk/",
                name="Met Office",
                roles=[
                    ProviderRole.PRODUCER,
                    ProviderRole.LICENSOR,
                    ProviderRole.PROCESSOR,
                ],
            ),
        ],
        extent=model.extent,
        stac_extensions=[
            "https://stac-extensions.github.io/storage/v1.0.0/schema.json",
            "https://stac-extensions.github.io/authentication/v1.1.0/schema.json",
        ],
        extra_fields={
            "storage:schemes": {
                "aws": {
                    "type": "aws-s3",
                    "platform": "https://{bucket}.s3.{region}.amazonaws.com",
                    "bucket": "met-office-atmospheric-model-data",
                    "region": "eu-west-2",
                    "requester_pays": False,
                }
            },
            "auth:schemes": {"aws": {"type": "s3"}},
        },
    )
    collection.links = [
        Link(
            rel="license",
            target="https://creativecommons.org/licenses/by-sa/4.0/deed.en",
            media_type="text/html",
            title="Creative Commons Attribution-ShareAlike 4.0",
        ),
        Link(
            rel="describedby",
            target="https://www.metoffice.gov.uk/services/data/external-data-channels",
            title="Met Office Dataset Documentation",
        ),
    ]
    return collection

create_items

create_items(
    source_hrefs: Sequence[str | Href],
    model: Model | None = None,
    theme: Theme | None = None,
) -> list[Item]

Creates one or more STAC items from a sequence of hrefs.

Groups hrefs by collection and item ID, then creates STAC items with assets for each unique combination of valid time and forecast horizon.

Parameters:

Name Type Description Default
source_hrefs Sequence[str | Href]

A sequence of href strings or Href objects pointing to NetCDF forecast files.

required
model Model | None

Optional model to override automatic detection from hrefs.

None
theme Theme | None

Optional theme to override automatic detection from hrefs.

None

Returns:

Type Description
list[Item]

A list of STAC Item objects, one for each unique combination of

list[Item]

valid time and forecast horizon.

Source code in src/stactools/met_office_deterministic/stac.py
def create_items(
    source_hrefs: Sequence[str | Href],
    model: Model | None = None,
    theme: Theme | None = None,
) -> list[Item]:
    """Creates one or more STAC items from a sequence of hrefs.

    Groups hrefs by collection and item ID, then creates STAC items with assets
    for each unique combination of valid time and forecast horizon.

    Args:
        source_hrefs: A sequence of href strings or Href objects pointing to
            NetCDF forecast files.
        model: Optional model to override automatic detection from hrefs.
        theme: Optional theme to override automatic detection from hrefs.

    Returns:
        A list of STAC Item objects, one for each unique combination of
        valid time and forecast horizon.
    """
    hrefs: defaultdict[str, defaultdict[str, list[Href]]] = defaultdict(
        lambda: defaultdict(list)
    )
    for source_href in source_hrefs:
        if isinstance(source_href, Href):
            href = source_href
        else:
            href = Href.parse(source_href, model=model, theme=theme)
        hrefs[href.collection_id][href.item_id].append(href)
    items = list()
    for items_hrefs in hrefs.values():
        for item_id, item_hrefs in items_hrefs.items():
            items.append(_create_item(item_id, item_hrefs))
    return items