Skip to content

climate_ref.config #

Configuration management

The REF uses a tiered configuration model, where configuration is sourced from a hierarchy of different places.

Each configuration value has a default which is used if not other configuration is available. Then configuration is loaded from a .toml file which overrides any default values. Finally, some configuration can be overridden at runtime using environment variables, which always take precedence over any other configuration values.

BUNDLED_IGNORE_DATASETS = PackagedResource('climate_ref', DEFAULT_IGNORE_DATASETS_FILENAME) module-attribute #

The grey list shipped inside the climate_ref package.

This is the copy that was current when the installed version was released. It is always readable, so a solve never depends on the network or on a writable cache.

env_prefix = 'REF' module-attribute #

Prefix for the environment variables used by the REF

Config #

Configuration that is used by the REF

Source code in packages/climate-ref/src/climate_ref/config.py
@define(auto_attribs=True)
class Config:
    """
    Configuration that is used by the REF
    """

    _prefix = env_prefix

    log_level: str = field(default="INFO")
    """
    Log level of messages that are displayed by the REF via the CLI

    This value is overridden if a value is specified via the CLI.
    """
    log_format: str = env_field("LOG_FORMAT", default=DEFAULT_LOG_FORMAT)
    """
    Format of the log messages that are displayed by the REF via the CLI

    Examples of the formatting options are available in the
    [loguru documentation](https://loguru.readthedocs.io/en/stable/api/logger.html#module-loguru._logger).
    `{extra[execution_prefix]}` names the execution a record was logged during, if any.
    """
    log_json: bool = env_field("LOG_JSON", default=False, converter=_bool)
    """
    Write console log messages as JSON, one object per line

    Use this when logs are collected by an aggregator such as Loki,
    so a multi-line traceback stays a single entry and context such as the execution can be filtered on.
    `log_format` still sets the `text` field of each record.
    """

    cmip6_parser: Literal["drs", "complete"] = env_field("CMIP6_PARSER", default="complete")
    """
    Parser to use for CMIP6 datasets

    This can be either `drs` or `complete`.

    - `drs`: Use the DRS parser, which parses the dataset based on the DRS naming conventions.
    - `complete`: Use the complete parser, which parses the dataset based on all available metadata.
    """

    cmip7_parser: Literal["drs", "complete"] = env_field("CMIP7_PARSER", default="complete")
    """
    Parser to use for CMIP7 datasets

    This can be either `drs` or `complete`.

    - `drs`: Use the DRS parser, which parses the dataset based on the DRS naming conventions.
    - `complete`: Use the complete parser, which parses the dataset based on all available metadata.
    """

    n_jobs: int = env_field("N_JOBS", default=1, converter=int)
    """
    Number of worker processes used to parse dataset files

    `1` parses serially,
    `-1` uses all available CPUs,
    and any other positive value is used as-is.

    Parsing opens netCDF files, so it is bound by (highly parallel) filesystem latency rather than by CPU.
    This applies both to `ref datasets ingest` (where `--n-jobs` overrides it)
    and to the finalisation of datasets that the solver performs
    when they were ingested with the `drs` parser.
    """

    ignore_datasets_file: Path | None = env_field(  # noqa: RUF009
        "IGNORE_DATASETS_FILE",
        converter=_optional_path,
        default=None,
    )
    """
    Path to a file containing the grey list

    This file is a YAML file that contains a list of facets to ignore per diagnostic.

    The format is:
    ```yaml
    provider:
      diagnostic:
        source_type:
          - facet: value
          - another_facet: [another_value1, another_value2]
    ```

    Leave this unset to use the grey list shipped inside the `climate_ref` package,
    refreshed from `ignore_datasets_url` when that is possible.
    Setting it pins the grey list to a file you manage, and disables fetching.
    """

    ignore_datasets_url: str = env_field("IGNORE_DATASETS_URL", default=DEFAULT_IGNORE_DATASETS_URL)
    """
    URL to refresh the grey list from at solve time.

    The download happens during solving only, at most once every 6 hours,
    and never during configuration loading.
    A failed download is not an error, since the copy shipped in the package is used instead.

    Set to an empty string (e.g. `REF_IGNORE_DATASETS_URL=`) to skip the attempt entirely,
    which avoids the request timeout on a host with no route to the internet.
    """

    paths: PathConfig = Factory(PathConfig)
    native_store: NativeStoreConfig = Factory(NativeStoreConfig)
    report_store: ReportStoreConfig = Factory(ReportStoreConfig)
    db: DbConfig = Factory(DbConfig)
    executor: ExecutorConfig = Factory(ExecutorConfig)
    diagnostic_providers: list[DiagnosticProviderConfig] = Factory(default_providers)  # noqa: RUF009, RUF100
    _raw: TOMLDocument | None = field(init=False, default=None, repr=False)
    _config_file: Path | None = field(init=False, default=None, repr=False)

    @property
    def ignore_datasets_resource(self) -> LayeredResource:
        """
        The grey list to apply when solving

        Resolves to `ignore_datasets_file` if it is set,
        then to a copy refreshed from `ignore_datasets_url`,
        and finally to the copy shipped inside `climate_ref`.

        A cache that has not been refreshed for `DEFAULT_IGNORE_DATASETS_MAX_STALE` is skipped.
        Without that bound,
        a copy left behind by an older release would shadow the newer packaged copy
        indefinitely on a host that can never reach the network.

        Returns
        -------
        :
            The resolved grey list resource.
        """
        cached = _ignore_datasets_cache_file()
        cache: Path | None = cached
        if _cache_age(cached) > DEFAULT_IGNORE_DATASETS_MAX_STALE:
            logger.debug(f"Ignoring the grey list cache at {cached} because it is too old to trust.")
            cache = None

        return LayeredResource(
            packaged=BUNDLED_IGNORE_DATASETS,
            override=self.ignore_datasets_file,
            cache=cache,
        )

    @classmethod
    def load(cls, config_file: Path, allow_missing: bool = True) -> "Config":
        """
        Load the configuration from a file

        Parameters
        ----------
        config_file
            Path to the configuration file.
            This should be a TOML file.

        Returns
        -------
        :
            The configuration loaded from the file
        """
        if config_file.is_file():
            with config_file.open() as fh:
                doc = tomlkit.load(fh)
                raw = doc
        else:
            if not allow_missing:
                raise FileNotFoundError(f"Configuration file not found: {config_file}")

            doc = TOMLDocument()
            raw = None

        try:
            config = _load_config(config_file, doc)
        except Exception as exc:
            # If that still fails, error out
            key_validation_errors = transform_error(exc, format_exception=_format_exception)
            for key_error in key_validation_errors:
                logger.error(f"Error loading configuration from {config_file}: {key_error}")

            # Deliberately not raising "from exc" to avoid long tracebacks from cattrs
            # The transformed error messages are sufficient for debugging
            raise ValueError(f"Error loading configuration from {config_file}") from None

        config._raw = raw
        config._config_file = config_file
        return config

    @classmethod
    def collect_validation_errors(cls, config_file: Path) -> list[str]:
        """
        Collect strict validation errors for a configuration file.

        Parameters
        ----------
        config_file
            Path to the configuration file to validate.

        Returns
        -------
        :
            A list of validation errors. An empty list means the file is valid.
        """
        try:
            with config_file.open() as fh:
                doc = tomlkit.load(fh)
            _converter_defaults.structure(doc, cls)
        except Exception as exc:
            return transform_error(exc, format_exception=_format_exception)
        return []

    def refresh(self) -> "Config":
        """
        Refresh the configuration values

        This returns a new instance of the configuration based on the same configuration file and
        any current environment variables.
        """
        if self._config_file is None:
            raise ValueError("No configuration file specified")
        return self.load(self._config_file)

    def save(self, config_file: Path | None = None) -> None:
        """
        Save the configuration as a TOML file

        The configuration will be saved to the specified file.
        If no file is specified, the configuration will be saved to the file
        that was used to load the configuration.

        Parameters
        ----------
        config_file
            The file to save the configuration to

        Raises
        ------
        ValueError
            If no configuration file is specified and the configuration was not loaded from a file
        """
        if config_file is None:
            if self._config_file is None:  # pragma: no cover
                # I'm not sure if this is possible
                raise ValueError("No configuration file specified")
            config_file = self._config_file

        config_file.parent.mkdir(parents=True, exist_ok=True)

        with open(config_file, "w") as fh:
            fh.write(self.dumps())

    @classmethod
    def default(cls) -> "Config":
        """
        Load the default configuration

        This will load the configuration from the default configuration location,
        which is typically the user's configuration directory.
        This location can be overridden by setting the `REF_CONFIGURATION` environment variable.

        Returns
        -------
        :
            The default configuration
        """
        root = env.path("REF_CONFIGURATION")
        path_to_load = root / CONFIG_FILENAME

        logger.debug(f"Loading default configuration from {path_to_load}")
        return cls.load(path_to_load)

    def dumps(self, defaults: bool = True) -> str:
        """
        Dump the configuration to a TOML string

        Parameters
        ----------
        defaults
            If True, include default values in the output

        Returns
        -------
        :
            The configuration as a TOML string
        """
        return self.dump(defaults).as_string()

    def dump(
        self,
        defaults: bool = True,
    ) -> TOMLDocument:
        """
        Dump the configuration to a TOML document

        Parameters
        ----------
        defaults
            If True, include default values in the output

        Returns
        -------
        :
            The configuration as a TOML document
        """
        if defaults:
            converter = _converter_defaults
        else:
            converter = _converter_no_defaults
        dump = converter.unstructure(self)
        # TOML cannot express null, so an unset value is written as an absent key.
        _pop_none(dump)
        if not defaults:
            _pop_empty(dump)
        doc = TOMLDocument()
        doc.update(dump)
        return doc

    def __attrs_post_init__(self) -> None:
        # This is needed to apply the environment variable overrides on initialization
        _environ_post_init(self)

        # Checked after the overrides so an invalid value from the environment is caught too.
        # `0` and negatives below `-1` would otherwise fall back to serial parsing without a word.
        if self.n_jobs != -1 and self.n_jobs < 1:
            raise ValueError(f"n_jobs must be -1 (all CPUs) or a positive integer, got {self.n_jobs}")

cmip6_parser = env_field('CMIP6_PARSER', default='complete') class-attribute instance-attribute #

Parser to use for CMIP6 datasets

This can be either drs or complete.

  • drs: Use the DRS parser, which parses the dataset based on the DRS naming conventions.
  • complete: Use the complete parser, which parses the dataset based on all available metadata.

cmip7_parser = env_field('CMIP7_PARSER', default='complete') class-attribute instance-attribute #

Parser to use for CMIP7 datasets

This can be either drs or complete.

  • drs: Use the DRS parser, which parses the dataset based on the DRS naming conventions.
  • complete: Use the complete parser, which parses the dataset based on all available metadata.

ignore_datasets_file = env_field('IGNORE_DATASETS_FILE', converter=_optional_path, default=None) class-attribute instance-attribute #

Path to a file containing the grey list

This file is a YAML file that contains a list of facets to ignore per diagnostic.

The format is:

provider:
  diagnostic:
    source_type:
      - facet: value
      - another_facet: [another_value1, another_value2]

Leave this unset to use the grey list shipped inside the climate_ref package, refreshed from ignore_datasets_url when that is possible. Setting it pins the grey list to a file you manage, and disables fetching.

ignore_datasets_resource property #

The grey list to apply when solving

Resolves to ignore_datasets_file if it is set, then to a copy refreshed from ignore_datasets_url, and finally to the copy shipped inside climate_ref.

A cache that has not been refreshed for DEFAULT_IGNORE_DATASETS_MAX_STALE is skipped. Without that bound, a copy left behind by an older release would shadow the newer packaged copy indefinitely on a host that can never reach the network.

Returns:

Type Description
LayeredResource

The resolved grey list resource.

ignore_datasets_url = env_field('IGNORE_DATASETS_URL', default=DEFAULT_IGNORE_DATASETS_URL) class-attribute instance-attribute #

URL to refresh the grey list from at solve time.

The download happens during solving only, at most once every 6 hours, and never during configuration loading. A failed download is not an error, since the copy shipped in the package is used instead.

Set to an empty string (e.g. REF_IGNORE_DATASETS_URL=) to skip the attempt entirely, which avoids the request timeout on a host with no route to the internet.

log_format = env_field('LOG_FORMAT', default=DEFAULT_LOG_FORMAT) class-attribute instance-attribute #

Format of the log messages that are displayed by the REF via the CLI

Examples of the formatting options are available in the loguru documentation. {extra[execution_prefix]} names the execution a record was logged during, if any.

log_json = env_field('LOG_JSON', default=False, converter=_bool) class-attribute instance-attribute #

Write console log messages as JSON, one object per line

Use this when logs are collected by an aggregator such as Loki, so a multi-line traceback stays a single entry and context such as the execution can be filtered on. log_format still sets the text field of each record.

log_level = field(default='INFO') class-attribute instance-attribute #

Log level of messages that are displayed by the REF via the CLI

This value is overridden if a value is specified via the CLI.

n_jobs = env_field('N_JOBS', default=1, converter=int) class-attribute instance-attribute #

Number of worker processes used to parse dataset files

1 parses serially, -1 uses all available CPUs, and any other positive value is used as-is.

Parsing opens netCDF files, so it is bound by (highly parallel) filesystem latency rather than by CPU. This applies both to ref datasets ingest (where --n-jobs overrides it) and to the finalisation of datasets that the solver performs when they were ingested with the drs parser.

collect_validation_errors(config_file) classmethod #

Collect strict validation errors for a configuration file.

Parameters:

Name Type Description Default
config_file Path

Path to the configuration file to validate.

required

Returns:

Type Description
list[str]

A list of validation errors. An empty list means the file is valid.

Source code in packages/climate-ref/src/climate_ref/config.py
@classmethod
def collect_validation_errors(cls, config_file: Path) -> list[str]:
    """
    Collect strict validation errors for a configuration file.

    Parameters
    ----------
    config_file
        Path to the configuration file to validate.

    Returns
    -------
    :
        A list of validation errors. An empty list means the file is valid.
    """
    try:
        with config_file.open() as fh:
            doc = tomlkit.load(fh)
        _converter_defaults.structure(doc, cls)
    except Exception as exc:
        return transform_error(exc, format_exception=_format_exception)
    return []

default() classmethod #

Load the default configuration

This will load the configuration from the default configuration location, which is typically the user's configuration directory. This location can be overridden by setting the REF_CONFIGURATION environment variable.

Returns:

Type Description
Config

The default configuration

Source code in packages/climate-ref/src/climate_ref/config.py
@classmethod
def default(cls) -> "Config":
    """
    Load the default configuration

    This will load the configuration from the default configuration location,
    which is typically the user's configuration directory.
    This location can be overridden by setting the `REF_CONFIGURATION` environment variable.

    Returns
    -------
    :
        The default configuration
    """
    root = env.path("REF_CONFIGURATION")
    path_to_load = root / CONFIG_FILENAME

    logger.debug(f"Loading default configuration from {path_to_load}")
    return cls.load(path_to_load)

dump(defaults=True) #

Dump the configuration to a TOML document

Parameters:

Name Type Description Default
defaults bool

If True, include default values in the output

True

Returns:

Type Description
TOMLDocument

The configuration as a TOML document

Source code in packages/climate-ref/src/climate_ref/config.py
def dump(
    self,
    defaults: bool = True,
) -> TOMLDocument:
    """
    Dump the configuration to a TOML document

    Parameters
    ----------
    defaults
        If True, include default values in the output

    Returns
    -------
    :
        The configuration as a TOML document
    """
    if defaults:
        converter = _converter_defaults
    else:
        converter = _converter_no_defaults
    dump = converter.unstructure(self)
    # TOML cannot express null, so an unset value is written as an absent key.
    _pop_none(dump)
    if not defaults:
        _pop_empty(dump)
    doc = TOMLDocument()
    doc.update(dump)
    return doc

dumps(defaults=True) #

Dump the configuration to a TOML string

Parameters:

Name Type Description Default
defaults bool

If True, include default values in the output

True

Returns:

Type Description
str

The configuration as a TOML string

Source code in packages/climate-ref/src/climate_ref/config.py
def dumps(self, defaults: bool = True) -> str:
    """
    Dump the configuration to a TOML string

    Parameters
    ----------
    defaults
        If True, include default values in the output

    Returns
    -------
    :
        The configuration as a TOML string
    """
    return self.dump(defaults).as_string()

load(config_file, allow_missing=True) classmethod #

Load the configuration from a file

Parameters:

Name Type Description Default
config_file Path

Path to the configuration file. This should be a TOML file.

required

Returns:

Type Description
Config

The configuration loaded from the file

Source code in packages/climate-ref/src/climate_ref/config.py
@classmethod
def load(cls, config_file: Path, allow_missing: bool = True) -> "Config":
    """
    Load the configuration from a file

    Parameters
    ----------
    config_file
        Path to the configuration file.
        This should be a TOML file.

    Returns
    -------
    :
        The configuration loaded from the file
    """
    if config_file.is_file():
        with config_file.open() as fh:
            doc = tomlkit.load(fh)
            raw = doc
    else:
        if not allow_missing:
            raise FileNotFoundError(f"Configuration file not found: {config_file}")

        doc = TOMLDocument()
        raw = None

    try:
        config = _load_config(config_file, doc)
    except Exception as exc:
        # If that still fails, error out
        key_validation_errors = transform_error(exc, format_exception=_format_exception)
        for key_error in key_validation_errors:
            logger.error(f"Error loading configuration from {config_file}: {key_error}")

        # Deliberately not raising "from exc" to avoid long tracebacks from cattrs
        # The transformed error messages are sufficient for debugging
        raise ValueError(f"Error loading configuration from {config_file}") from None

    config._raw = raw
    config._config_file = config_file
    return config

refresh() #

Refresh the configuration values

This returns a new instance of the configuration based on the same configuration file and any current environment variables.

Source code in packages/climate-ref/src/climate_ref/config.py
def refresh(self) -> "Config":
    """
    Refresh the configuration values

    This returns a new instance of the configuration based on the same configuration file and
    any current environment variables.
    """
    if self._config_file is None:
        raise ValueError("No configuration file specified")
    return self.load(self._config_file)

save(config_file=None) #

Save the configuration as a TOML file

The configuration will be saved to the specified file. If no file is specified, the configuration will be saved to the file that was used to load the configuration.

Parameters:

Name Type Description Default
config_file Path | None

The file to save the configuration to

None

Raises:

Type Description
ValueError

If no configuration file is specified and the configuration was not loaded from a file

Source code in packages/climate-ref/src/climate_ref/config.py
def save(self, config_file: Path | None = None) -> None:
    """
    Save the configuration as a TOML file

    The configuration will be saved to the specified file.
    If no file is specified, the configuration will be saved to the file
    that was used to load the configuration.

    Parameters
    ----------
    config_file
        The file to save the configuration to

    Raises
    ------
    ValueError
        If no configuration file is specified and the configuration was not loaded from a file
    """
    if config_file is None:
        if self._config_file is None:  # pragma: no cover
            # I'm not sure if this is possible
            raise ValueError("No configuration file specified")
        config_file = self._config_file

    config_file.parent.mkdir(parents=True, exist_ok=True)

    with open(config_file, "w") as fh:
        fh.write(self.dumps())

DbConfig #

Database configuration

We support SQLite and PostgreSQL databases. The default is to use SQLite, which is a file-based database that is stored in the REF_CONFIGURATION directory. This is a good option for testing and development, but not recommended for production use.

For production use, we recommend using PostgreSQL.

Source code in packages/climate-ref/src/climate_ref/config.py
@config(prefix=env_prefix)
class DbConfig:
    """
    Database configuration

    We support SQLite and PostgreSQL databases.
    The default is to use SQLite, which is a file-based database that is stored in the
    `REF_CONFIGURATION` directory.
    This is a good option for testing and development, but not recommended for production use.

    For production use, we recommend using PostgreSQL.
    """

    database_url: str = env_field(name="DATABASE_URL")
    """
    Database URL that describes the connection to the database.

    Defaults to `sqlite:///{config.paths.db}/climate_ref.db`.
    This configuration value will be overridden by the `REF_DATABASE_URL` environment variable.

    **Schemas**

    The following schemas are supported:
    ```
    postgresql://USER:PASSWORD@HOST:PORT/NAME

    sqlite:///RELATIVE_PATH or sqlite:////ABS_PATH or sqlite:///:memory:
    ```
    """
    run_migrations: bool = field(default=True)

    max_backups: int = env_field(name="MAX_BACKUPS", default=5)
    """
    Maximum number of database backups to keep.


    When running migrations for on-disk SQLite databases, a backup of the database is created.
    This setting controls how many of these backups are retained.
    The oldest backups are automatically removed when this limit is exceeded.
    """

    @database_url.default
    def _connection_url_factory(self) -> str:
        filename = env.path("REF_CONFIGURATION") / "db" / "climate_ref.db"
        sqlite_url = f"sqlite:///{filename}"
        return sqlite_url

database_url = env_field(name='DATABASE_URL') class-attribute instance-attribute #

Database URL that describes the connection to the database.

Defaults to sqlite:///{config.paths.db}/climate_ref.db. This configuration value will be overridden by the REF_DATABASE_URL environment variable.

Schemas

The following schemas are supported:

postgresql://USER:PASSWORD@HOST:PORT/NAME

sqlite:///RELATIVE_PATH or sqlite:////ABS_PATH or sqlite:///:memory:

max_backups = env_field(name='MAX_BACKUPS', default=5) class-attribute instance-attribute #

Maximum number of database backups to keep.

When running migrations for on-disk SQLite databases, a backup of the database is created. This setting controls how many of these backups are retained. The oldest backups are automatically removed when this limit is exceeded.

DiagnosticProviderConfig #

Defining the diagnostic providers used by the REF.

Each diagnostic provider is a package that contains the logic for running a specific set of diagnostics. This configuration determines which diagnostic providers are loaded and used when solving.

Multiple diagnostic providers can be specified as shown in the example below.

[[diagnostic_providers]]
provider = "climate_ref_esmvaltool:provider"

[diagnostic_providers.config]

[[diagnostic_providers]]
provider = "climate_ref_ilamb:provider"

[diagnostic_providers.config]

[[diagnostic_providers]]
provider = "climate_ref_pmp:provider"

[diagnostic_providers.config]
Source code in packages/climate-ref/src/climate_ref/config.py
@define
class DiagnosticProviderConfig:
    """
    Defining the diagnostic providers used by the REF.

    Each diagnostic provider is a package that contains the logic for running a specific
    set of diagnostics.
    This configuration determines which diagnostic providers are loaded and used when solving.

    Multiple diagnostic providers can be specified as shown in the example below.

    ```toml
    [[diagnostic_providers]]
    provider = "climate_ref_esmvaltool:provider"

    [diagnostic_providers.config]

    [[diagnostic_providers]]
    provider = "climate_ref_ilamb:provider"

    [diagnostic_providers.config]

    [[diagnostic_providers]]
    provider = "climate_ref_pmp:provider"

    [diagnostic_providers.config]
    ```
    """

    provider: str
    """
    Package that contains the diagnostic provider

    This should be the fully qualified name of the diagnostic provider.
    """

    config: dict[str, Any] = field(factory=dict)
    """
    Additional configuration for the diagnostic provider.

    See the documentation for the diagnostic package for the available configuration options.
    """

config = field(factory=dict) class-attribute instance-attribute #

Additional configuration for the diagnostic provider.

See the documentation for the diagnostic package for the available configuration options.

provider instance-attribute #

Package that contains the diagnostic provider

This should be the fully qualified name of the diagnostic provider.

ExecutorConfig #

Configuration to define the executor to use for running diagnostics

Source code in packages/climate-ref/src/climate_ref/config.py
@config(prefix=env_prefix)
class ExecutorConfig:
    """
    Configuration to define the executor to use for running diagnostics
    """

    executor: str = env_field(name="EXECUTOR", default="climate_ref.executor.LocalExecutor")
    """
    Executor class to use for running diagnostics

    This should be the fully qualified name of the executor class
    (e.g. `climate_ref.executor.LocalExecutor`).
    The default is to use the local executor which runs the executions locally, in-parallel
    using a process pool.

    This class will be used for all executions of diagnostics.
    """

    measure_resources: bool = env_field(name="EXECUTOR_MEASURE_RESOURCES", default=True, converter=_bool)
    """
    Whether to record what each execution costs in wall time, CPU time and peak memory.

    The measurement is what `ref executions resources` aggregates,
    so turning it off leaves that command without new samples.
    Set it to `false` where the sampling thread is unwelcome,
    for example in a memory-constrained container or a profiling run.
    """

    config: dict[str, Any] = field(factory=dict)
    """
    Additional configuration for the executor.

    See the documentation for the executor for the available configuration options.
    These options will be passed to the executor class when it is created.
    """

    def build(self, config: "Config", database: "Database") -> "Executor":
        """
        Create an instance of the executor

        Returns
        -------
        :
            An executor that can be used to run diagnostics
        """
        # Import lazily to avoid loading heavy dependencies (pandas, xarray)
        # at module load time - these are only needed when actually running diagnostics
        from climate_ref_core.executor import Executor, import_executor_cls  # noqa: PLC0415

        ExecutorCls = import_executor_cls(self.executor)
        kwargs = {
            "config": config,
            "database": database,
            **self.config,
        }
        executor = ExecutorCls(**kwargs)

        if not isinstance(executor, Executor):
            raise InvalidExecutorException(executor, f"Expected an Executor, got {type(executor)}")
        return executor

config = field(factory=dict) class-attribute instance-attribute #

Additional configuration for the executor.

See the documentation for the executor for the available configuration options. These options will be passed to the executor class when it is created.

executor = env_field(name='EXECUTOR', default='climate_ref.executor.LocalExecutor') class-attribute instance-attribute #

Executor class to use for running diagnostics

This should be the fully qualified name of the executor class (e.g. climate_ref.executor.LocalExecutor). The default is to use the local executor which runs the executions locally, in-parallel using a process pool.

This class will be used for all executions of diagnostics.

measure_resources = env_field(name='EXECUTOR_MEASURE_RESOURCES', default=True, converter=_bool) class-attribute instance-attribute #

Whether to record what each execution costs in wall time, CPU time and peak memory.

The measurement is what ref executions resources aggregates, so turning it off leaves that command without new samples. Set it to false where the sampling thread is unwelcome, for example in a memory-constrained container or a profiling run.

build(config, database) #

Create an instance of the executor

Returns:

Type Description
Executor

An executor that can be used to run diagnostics

Source code in packages/climate-ref/src/climate_ref/config.py
def build(self, config: "Config", database: "Database") -> "Executor":
    """
    Create an instance of the executor

    Returns
    -------
    :
        An executor that can be used to run diagnostics
    """
    # Import lazily to avoid loading heavy dependencies (pandas, xarray)
    # at module load time - these are only needed when actually running diagnostics
    from climate_ref_core.executor import Executor, import_executor_cls  # noqa: PLC0415

    ExecutorCls = import_executor_cls(self.executor)
    kwargs = {
        "config": config,
        "database": database,
        **self.config,
    }
    executor = ExecutorCls(**kwargs)

    if not isinstance(executor, Executor):
        raise InvalidExecutorException(executor, f"Expected an Executor, got {type(executor)}")
    return executor

NativeStoreConfig #

Configuration for the content-addressed native-bundle object store.

The native store holds the curated native outputs (NetCDF, PNG, ...) produced by each test case, keyed by their sha256 digest. Read operations (has, fetch) are always anonymous and credential-free. Write operations are gated to the mint verb only.

Source code in packages/climate-ref/src/climate_ref/config.py
@config(prefix=env_prefix)
class NativeStoreConfig:
    """
    Configuration for the content-addressed native-bundle object store.

    The native store holds the curated native outputs (NetCDF, PNG, ...) produced by each
    test case, keyed by their sha256 digest.
    Read operations (``has``, ``fetch``) are always anonymous and credential-free.
    Write operations are gated to the ``mint`` verb only.
    """

    url: str = env_field(name="NATIVE_STORE_URL", default="https://baselines.climate-ref.org")
    """
    Base URL of the native-bundle object store.

    Blobs are served at ``{url}/{digest}``.
    Defaults to the production Climate-REF baselines endpoint.

    Set ``REF_NATIVE_STORE_URL`` to a local ``file:///path/to/dir`` (or a plain filesystem path)
    for offline development and testing.
    """

    s3_endpoint_url: str = env_field(
        name="NATIVE_STORE_S3_ENDPOINT_URL",
        default="https://2aa5172b2bba093c516027d6fa13cdc8.r2.cloudflarestorage.com",
    )
    """
    S3 API endpoint for the writable (Cloudflare R2) backend, without the bucket.

    Non-secret routing config, consumed only by the ``mint`` verb. Defaults to the
    production Climate-REF R2 account endpoint (default jurisdiction — note there is no
    ``.eu`` in the host). Anonymous read (``fetch`` / ``has``) uses :attr:`url` instead and
    never touches this.
    Set ``REF_NATIVE_STORE_S3_ENDPOINT_URL`` to override (e.g. a staging account).
    """

    bucket: str = env_field(name="NATIVE_STORE_BUCKET", default="ref-baselines-public")
    """
    Name of the writable (Cloudflare R2) bucket.

    Non-secret routing config, consumed only by the ``mint`` verb.
    Set ``REF_NATIVE_STORE_BUCKET`` to override.

    Write credentials are **not** stored here: the access-key id and secret-access-key are
    read from ``REF_NATIVE_STORE_ACCESS_KEY_ID`` / ``REF_NATIVE_STORE_SECRET_ACCESS_KEY``
    (falling back to boto3's default credential chain) at upload time only, so secrets never
    land in a serialised config.
    """

    cache_dir: Path = env_field(name="NATIVE_STORE_CACHE_DIR", converter=Path)
    """
    Local pooch cache directory for downloaded native blobs.

    Defaults via :func:`~climate_ref_core.dataset_registry.resolve_cache_dir`,
    so the ``REF_DATASET_CACHE_DIR`` environment variable applies here too.
    """

    @cache_dir.default
    def _cache_dir_factory(self) -> Path:
        return resolve_cache_dir("native-baselines")

bucket = env_field(name='NATIVE_STORE_BUCKET', default='ref-baselines-public') class-attribute instance-attribute #

Name of the writable (Cloudflare R2) bucket.

Non-secret routing config, consumed only by the mint verb. Set REF_NATIVE_STORE_BUCKET to override.

Write credentials are not stored here: the access-key id and secret-access-key are read from REF_NATIVE_STORE_ACCESS_KEY_ID / REF_NATIVE_STORE_SECRET_ACCESS_KEY (falling back to boto3's default credential chain) at upload time only, so secrets never land in a serialised config.

cache_dir = env_field(name='NATIVE_STORE_CACHE_DIR', converter=Path) class-attribute instance-attribute #

Local pooch cache directory for downloaded native blobs.

Defaults via :func:~climate_ref_core.dataset_registry.resolve_cache_dir, so the REF_DATASET_CACHE_DIR environment variable applies here too.

s3_endpoint_url = env_field(name='NATIVE_STORE_S3_ENDPOINT_URL', default='https://2aa5172b2bba093c516027d6fa13cdc8.r2.cloudflarestorage.com') class-attribute instance-attribute #

S3 API endpoint for the writable (Cloudflare R2) backend, without the bucket.

Non-secret routing config, consumed only by the mint verb. Defaults to the production Climate-REF R2 account endpoint (default jurisdiction — note there is no .eu in the host). Anonymous read (fetch / has) uses :attr:url instead and never touches this. Set REF_NATIVE_STORE_S3_ENDPOINT_URL to override (e.g. a staging account).

url = env_field(name='NATIVE_STORE_URL', default='https://baselines.climate-ref.org') class-attribute instance-attribute #

Base URL of the native-bundle object store.

Blobs are served at {url}/{digest}. Defaults to the production Climate-REF baselines endpoint.

Set REF_NATIVE_STORE_URL to a local file:///path/to/dir (or a plain filesystem path) for offline development and testing.

PathConfig #

Common paths used by the REF application

Warning

These paths must be common across all systems that the REF is being run. Generally, this means that they should be mounted in the same location on all systems.

If any of these paths are specified as relative paths, they will be resolved to absolute paths. These absolute paths will be used for all operations in the REF.

Source code in packages/climate-ref/src/climate_ref/config.py
@config(prefix=env_prefix)
class PathConfig:
    """
    Common paths used by the REF application

    /// admonition | Warning
        type: warning

    These paths must be common across all systems that the REF is being run.
    Generally, this means that they should be mounted in the same location on all systems.
    ///

    If any of these paths are specified as relative paths,
    they will be resolved to absolute paths.
    These absolute paths will be used for all operations in the REF.
    """

    log: Path = env_field(name="LOG_ROOT", converter=ensure_absolute_path)
    """
    Directory to store log files from the compute engine

    This is not currently used by the REF, but is included for future use.
    """

    scratch: Path = env_field(name="SCRATCH_ROOT", converter=ensure_absolute_path)
    """
    Shared scratch space for the REF.

    This directory is used to write the intermediate executions of a diagnostic execution.
    After the diagnostic has been run, the executions will be copied to the executions directory.

    This directory must be accessible by all the diagnostic services that are used to run the diagnostics,
    but does not need to be mounted in the same location on all the diagnostic services.
    """

    software: Path = env_field(name="SOFTWARE_ROOT", converter=ensure_absolute_path)
    """
    Shared software space for the REF.

    This directory is used to store software environments.

    This directory must be accessible by all the diagnostic services that are used to run the diagnostics,
    and should be mounted in the same location on all the diagnostic services.
    """

    # TODO: This could be another data source option
    results: Path = env_field(name="RESULTS_ROOT", converter=ensure_absolute_path)
    """
    Path to store the executions
    """

    dimensions_cv: Path | None = env_field(name="DIMENSIONS_CV_PATH", converter=_optional_path, default=None)
    """
    Path to a file containing the controlled vocabulary for the dimensions in a CMEC diagnostics bundle

    Leave this unset to use the controlled vocabulary for the CMIP7 Assessment Fast Track
    diagnostics, which is shipped inside the `climate_ref_core` package.
    That copy is read straight out of the installed package,
    so it needs no network access and no writable filesystem.

    This controlled vocabulary is used to validate the dimensions in the diagnostics bundle.
    If custom diagnostics are implemented,
    point this at a copy that has been extended with any new dimensions.
    """

    @log.default
    def _log_factory(self) -> Path:
        return env.path("REF_CONFIGURATION").resolve() / "log"

    @scratch.default
    def _scratch_factory(self) -> Path:
        return env.path("REF_CONFIGURATION").resolve() / "scratch"

    @software.default
    def _software_factory(self) -> Path:
        return env.path("REF_CONFIGURATION").resolve() / "software"

    @results.default
    def _results_factory(self) -> Path:
        return env.path("REF_CONFIGURATION").resolve() / "results"

    @property
    def dimensions_cv_resource(self) -> LayeredResource:
        """
        The controlled vocabulary to validate diagnostic bundles against

        Resolves to `dimensions_cv` if it is set, and to the copy shipped
        inside `climate_ref_core` otherwise.

        Returns
        -------
        :
            The resolved controlled vocabulary resource.
        """
        return LayeredResource(packaged=BUNDLED_AFT_CV, override=self.dimensions_cv)

dimensions_cv = env_field(name='DIMENSIONS_CV_PATH', converter=_optional_path, default=None) class-attribute instance-attribute #

Path to a file containing the controlled vocabulary for the dimensions in a CMEC diagnostics bundle

Leave this unset to use the controlled vocabulary for the CMIP7 Assessment Fast Track diagnostics, which is shipped inside the climate_ref_core package. That copy is read straight out of the installed package, so it needs no network access and no writable filesystem.

This controlled vocabulary is used to validate the dimensions in the diagnostics bundle. If custom diagnostics are implemented, point this at a copy that has been extended with any new dimensions.

dimensions_cv_resource property #

The controlled vocabulary to validate diagnostic bundles against

Resolves to dimensions_cv if it is set, and to the copy shipped inside climate_ref_core otherwise.

Returns:

Type Description
LayeredResource

The resolved controlled vocabulary resource.

log = env_field(name='LOG_ROOT', converter=ensure_absolute_path) class-attribute instance-attribute #

Directory to store log files from the compute engine

This is not currently used by the REF, but is included for future use.

results = env_field(name='RESULTS_ROOT', converter=ensure_absolute_path) class-attribute instance-attribute #

Path to store the executions

scratch = env_field(name='SCRATCH_ROOT', converter=ensure_absolute_path) class-attribute instance-attribute #

Shared scratch space for the REF.

This directory is used to write the intermediate executions of a diagnostic execution. After the diagnostic has been run, the executions will be copied to the executions directory.

This directory must be accessible by all the diagnostic services that are used to run the diagnostics, but does not need to be mounted in the same location on all the diagnostic services.

software = env_field(name='SOFTWARE_ROOT', converter=ensure_absolute_path) class-attribute instance-attribute #

Shared software space for the REF.

This directory is used to store software environments.

This directory must be accessible by all the diagnostic services that are used to run the diagnostics, and should be mounted in the same location on all the diagnostic services.

ReportStoreConfig #

Configuration for the public store that hosts baseline diff reports.

A diff report is a small static site keyed by pull request and head sha, so this store is addressed by path rather than by digest. It is a different bucket to the native store because an R2 token cannot be scoped to a prefix, so a token that can write reports must not also be able to overwrite baseline blobs.

Write credentials are not stored here: the access-key id and secret-access-key are read from REF_REPORT_STORE_ACCESS_KEY_ID / REF_REPORT_STORE_SECRET_ACCESS_KEY (falling back to boto3's default credential chain) at upload time only, so secrets never land in a serialised config.

Source code in packages/climate-ref/src/climate_ref/config.py
@config(prefix=env_prefix)
class ReportStoreConfig:
    """
    Configuration for the public store that hosts baseline diff reports.

    A diff report is a small static site keyed by pull request and head sha,
    so this store is addressed by path rather than by digest.
    It is a different bucket to the native store because an R2 token cannot be scoped to a prefix,
    so a token that can write reports must not also be able to overwrite baseline blobs.

    Write credentials are **not** stored here: the access-key id and secret-access-key are read from
    ``REF_REPORT_STORE_ACCESS_KEY_ID`` / ``REF_REPORT_STORE_SECRET_ACCESS_KEY``
    (falling back to boto3's default credential chain) at upload time only,
    so secrets never land in a serialised config.
    """

    url: str = env_field(name="REPORT_STORE_URL", default="https://reports.baselines.climate-ref.org")
    """
    Base URL the reports are served from.

    Reports are served at ``{url}/{key}``.
    Defaults to the production Climate-REF reports endpoint.

    Set ``REF_REPORT_STORE_URL`` to a local ``file:///path/to/dir`` (or a plain filesystem path)
    to render and inspect an upload offline.
    """

    s3_endpoint_url: str = env_field(
        name="REPORT_STORE_S3_ENDPOINT_URL",
        default="https://2aa5172b2bba093c516027d6fa13cdc8.r2.cloudflarestorage.com",
    )
    """
    S3 API endpoint for the writable (Cloudflare R2) backend, without the bucket.

    Non-secret routing config, consumed only when uploading a report.
    Defaults to the production Climate-REF R2 account endpoint.
    Set ``REF_REPORT_STORE_S3_ENDPOINT_URL`` to override (e.g. a staging account).
    """

    bucket: str = env_field(name="REPORT_STORE_BUCKET", default="ref-baselines-reports")
    """
    Name of the writable (Cloudflare R2) reports bucket.

    Non-secret routing config, consumed only when uploading a report.
    Set ``REF_REPORT_STORE_BUCKET`` to override.
    """

bucket = env_field(name='REPORT_STORE_BUCKET', default='ref-baselines-reports') class-attribute instance-attribute #

Name of the writable (Cloudflare R2) reports bucket.

Non-secret routing config, consumed only when uploading a report. Set REF_REPORT_STORE_BUCKET to override.

s3_endpoint_url = env_field(name='REPORT_STORE_S3_ENDPOINT_URL', default='https://2aa5172b2bba093c516027d6fa13cdc8.r2.cloudflarestorage.com') class-attribute instance-attribute #

S3 API endpoint for the writable (Cloudflare R2) backend, without the bucket.

Non-secret routing config, consumed only when uploading a report. Defaults to the production Climate-REF R2 account endpoint. Set REF_REPORT_STORE_S3_ENDPOINT_URL to override (e.g. a staging account).

url = env_field(name='REPORT_STORE_URL', default='https://reports.baselines.climate-ref.org') class-attribute instance-attribute #

Base URL the reports are served from.

Reports are served at {url}/{key}. Defaults to the production Climate-REF reports endpoint.

Set REF_REPORT_STORE_URL to a local file:///path/to/dir (or a plain filesystem path) to render and inspect an upload offline.

default_providers() #

Return default diagnostic providers.

Used if no diagnostic providers are specified in the configuration

Returns:

Type Description
list[DiagnosticProviderConfig]

List of default diagnostic providers

Source code in packages/climate-ref/src/climate_ref/config.py
def default_providers() -> list[DiagnosticProviderConfig]:
    """
    Return default diagnostic providers.

    Used if no diagnostic providers are specified in the configuration

    Returns
    -------
    :
        List of default diagnostic providers
    """
    env_providers = env.list("REF_DIAGNOSTIC_PROVIDERS", default=None)
    if env_providers:
        return [DiagnosticProviderConfig(provider=provider) for provider in env_providers]

    # Refer to https://setuptools.pypa.io/en/latest/userguide/entry_point.html#entry-points-for-plugins
    # and https://packaging.python.org/en/latest/specifications/entry-points/
    # to learn more about entry points.
    return [
        DiagnosticProviderConfig(provider=entry_point.value, config={})
        for entry_point in importlib.metadata.entry_points(group="climate-ref.providers")
    ]

ensure_absolute_path(path) #

Ensure that the path is absolute

Parameters:

Name Type Description Default
path str | Path

Path to check

required

Returns:

Type Description
Absolute path
Source code in packages/climate-ref/src/climate_ref/config.py
def ensure_absolute_path(path: str | Path) -> Path:
    """
    Ensure that the path is absolute

    Parameters
    ----------
    path
        Path to check

    Returns
    -------
        Absolute path
    """
    if isinstance(path, str):
        path = Path(path)
    path = Path(*[os.path.expandvars(p) for p in path.parts])
    return path.resolve()

refresh_ignore_datasets_file(config) #

Refresh the cached grey list from config.ignore_datasets_url.

This is called at solve time so that configuration loading never performs network I/O. The download happens at most once every DEFAULT_IGNORE_DATASETS_REFRESH_INTERVAL. A fresh cached file is reused untouched.

Refreshing is best effort. An unset URL, an unreachable network, and an unwritable cache directory are all non-fatal: the solve falls back to the cached copy if there is one, and to the copy shipped in the package otherwise. This keeps read-only and air-gapped deployments working without any configuration.

Refreshing is skipped entirely when ignore_datasets_file is set, since an explicit file is the operator's to manage.

Parameters:

Name Type Description Default
config Config

The configuration providing ignore_datasets_file and ignore_datasets_url.

required
Source code in packages/climate-ref/src/climate_ref/config.py
def refresh_ignore_datasets_file(config: "Config") -> None:
    """
    Refresh the cached grey list from `config.ignore_datasets_url`.

    This is called at solve time so that configuration loading never performs network I/O.
    The download happens at most once every `DEFAULT_IGNORE_DATASETS_REFRESH_INTERVAL`.
    A fresh cached file is reused untouched.

    Refreshing is best effort.
    An unset URL, an unreachable network, and an unwritable cache directory are all
    non-fatal: the solve falls back to the cached copy if there is one,
    and to the copy shipped in the package otherwise.
    This keeps read-only and air-gapped deployments working without any configuration.

    Refreshing is skipped entirely when `ignore_datasets_file` is set,
    since an explicit file is the operator's to manage.

    Parameters
    ----------
    config
        The configuration providing `ignore_datasets_file` and `ignore_datasets_url`.
    """
    url = config.ignore_datasets_url

    if not url or config.ignore_datasets_file is not None:
        return

    path: Path | None = None
    try:
        path = _ignore_datasets_cache_file()

        if path.is_dir():
            logger.warning(f"The grey list cache path {path} is a directory, not a file. Skipping refresh.")
            return

        if _cache_age(path) < DEFAULT_IGNORE_DATASETS_REFRESH_INTERVAL:
            return

        path.parent.mkdir(parents=True, exist_ok=True)
        logger.info(f"Downloading the grey list from {url} to {path}")
        response = requests.get(url, timeout=120)
        response.raise_for_status()
        _write_atomically(path, response.content)
    except Exception as exc:
        # The packaged copy is always available, so a failed refresh is never fatal.
        usable_cache = (
            path is not None and path.is_file() and _cache_age(path) <= DEFAULT_IGNORE_DATASETS_MAX_STALE
        )
        fallback = path if usable_cache else BUNDLED_IGNORE_DATASETS
        logger.warning(f"Could not refresh the grey list from {url}, using {fallback}: {exc}")