Skip to content

href

Parse Met Office deterministic forecast hrefs into structured objects.

Href dataclass

Href(
    href: str,
    model: Model,
    theme: Theme,
    parameter: str,
    reference_datetime: str,
    forecast_horizon: str,
)

collection_id property

collection_id: str

Gets the STAC collection ID for this href.

Returns:

Type Description
str

The collection ID string.

datetime property

datetime: datetime

Gets the datetime from the valid time string.

Returns:

Type Description
datetime

A datetime object parsed from the valid time.

duration property

duration: str | None

Extracts the duration from the parameter if present.

Returns:

Type Description
str | None

The duration string (ISO 8601 format) if the parameter includes one,

str | None

None otherwise.

item_id property

item_id: str

Gets the STAC item ID for this href.

Returns:

Type Description
str

The item ID string combining valid time and forecast horizon.

variable property

variable: str

The CF-standard name for this parameter.

__str__

__str__() -> str

Returns the href as a string.

Returns:

Type Description
str

The href string.

Source code in src/stactools/met_office_deterministic/href.py
def __str__(self) -> str:
    """Returns the href as a string.

    Returns:
        The href string.
    """
    return self.href

parse classmethod

parse(
    href: str,
    model: Model | None = None,
    theme: Theme | None = None,
) -> Href

Parses a Met Office deterministic forecast href into an Href object.

Parses hrefs in the format: [scheme://bucket/]collection/reference_datetime/reference_datetime-forecast_horizon-parameter.nc

The model and theme are automatically extracted from the collection name and parameter, but can be overridden with the optional parameters.

Parameters:

Name Type Description Default
href str

The href string to parse. Can be a full S3 URL or a relative path.

required
model Model | None

Optional model to override automatic detection. If None, inferred from the collection name (global-deterministic-10km or uk-deterministic-2km).

None
theme Theme | None

Optional theme to override automatic detection. If None, inferred from the parameter name.

None

Returns:

Type Description
Href

An Href object containing parsed components including model, theme,

Href

parameter, reference_datetime, valid_time, and forecast_horizon.

Raises:

Type Description
ValueError

If the href format is invalid, contains an unknown collection, or contains an unknown parameter (when theme inference is required).

Source code in src/stactools/met_office_deterministic/href.py
@classmethod
def parse(
    cls, href: str, model: Model | None = None, theme: Theme | None = None
) -> Href:
    """Parses a Met Office deterministic forecast href into an Href object.

    Parses hrefs in the format:
    [scheme://bucket/]collection/reference_datetime/reference_datetime-forecast_horizon-parameter.nc

    The model and theme are automatically extracted from the collection name and
    parameter, but can be overridden with the optional parameters.

    Args:
        href: The href string to parse. Can be a full S3 URL or a relative path.
        model: Optional model to override automatic detection. If None, inferred
            from the collection name (global-deterministic-10km or
            uk-deterministic-2km).
        theme: Optional theme to override automatic detection. If None, inferred
            from the parameter name.

    Returns:
        An Href object containing parsed components including model, theme,
        parameter, reference_datetime, valid_time, and forecast_horizon.

    Raises:
        ValueError: If the href format is invalid, contains an unknown collection,
            or contains an unknown parameter (when theme inference is required).
    """
    parts = href.split("/")
    if model is None:
        match collection := parts[-3]:
            case "global-deterministic-10km":
                model = Model.global_
            case "uk-deterministic-2km":
                model = Model.uk
            case _:
                raise ValueError(f"Invalid collection: {collection}")
    matched_dict = FILE_NAME_REGEX.match(parts[-1])
    if matched_dict is None:
        raise ValueError(f"Invalid file name: {href}")
    parameter = matched_dict["parameter"]
    if theme is None:
        theme = Theme.from_parameter(parameter)
    return Href(
        href=href,
        model=model,
        theme=theme,
        parameter=parameter,
        reference_datetime=parts[-2],
        forecast_horizon=matched_dict["forecast_horizon"],
    )