ewatercycle.config
==================
.. py:module:: ewatercycle.config
.. autoapi-nested-parse::
Config.
Configuration of eWaterCycle is done via the
:py:class:`~ewatercycle.config.Configuration` object. The global configuration can be
imported from the :py:mod:`ewatercycle` module as :py:data:`~ewatercycle.CFG`:
.. code-block:: python
>>> from ewatercycle import CFG
>>> CFG
Configuration(
grdc_location=PosixPath('.'),
container_engine='docker',
apptainer_dir=PosixPath('.'),
singularity_dir=None,
output_dir=PosixPath('.'),
parameterset_dir=PosixPath('.'),
parameter_sets={},
ewatercycle_config=None
)
By default all values have usable values.
:py:data:`~ewatercycle.CFG` is a `Pydantic model `_.
This means that values can be updated like this:
.. code-block:: python
>>> CFG.output_dir = '~/output'
>>> CFG.output_dir
PosixPath('/home/user/output')
Notice that :py:data:`~ewatercycle.CFG` automatically converts the path to an
instance of ``pathlib.Path`` and expands the home directory. All values entered
into the config are validated to prevent mistakes, for example, it will warn you
if you make a typo in the key:
.. code-block:: python
>>> CFG.output_directory = '/output'
ValidationError: 1 validation error for Configuration
output_directory
extra fields not permitted (type=value_error.extra)
Or, if the value entered cannot be converted to the expected type:
.. code-block:: python
>>> CFG.output_dir = 123
ValidationError: 1 validation error for Configuration
output_dir
value is not a valid path (type=type_error.path)
By default, the config is loaded from the default location (i.e.
``~/.config/ewatercycle/ewatercycle.yaml``). If it does not exist, it falls back
to the default values. to load a different file:
.. code-block:: python
>>> CFG.load_from_file('~/my-config.yml')
Or to reload the current config:
.. code-block:: python
>>> CFG.reload()
Package Contents
----------------
.. py:class:: ParameterSet
Bases: :py:obj:`pydantic.BaseModel`
Container object for parameter set options.
Is directory containing data that does not change over time.
Should be passed to a models constructor like
:py:class:`~ewatercycle.base.model.eWaterCycleModel`.
.. rubric:: Example
>>> from ewatercycle.base import ParameterSet
>>> parameter_set = ParameterSet(name='test',
>>> directory='test_dir',
>>> config='test_dir/config.yaml')
>>> from ewatercycle.models import Wflow
>>> model = Wflow(parameter_set=parameter_set)
.. py:attribute:: name
:type: str
:value: ''
Name of parameter set
.. py:attribute:: directory
:type: pathlib.Path
Location on disk where files of parameter set are stored.
If Path is relative then relative to CFG.parameterset_dir.
.. py:attribute:: config
:type: pathlib.Path
Model configuration file which uses files from :py:attr:`~directory`.
If Path is relative then relative to :py:attr:`~directory`.
.. py:attribute:: doi
:type: str
:value: 'N/A'
Persistent identifier of parameter set.
For a example a DOI for a Zenodo record.
.. py:attribute:: target_model
:type: str
:value: 'generic'
Name of model that parameter set can work with.
.. py:attribute:: supported_model_versions
:type: set[str]
Set of model versions that are compatible with this parameter set.
If not set then parameter set compability check silently passes.
.. py:attribute:: downloader
:type: GitHubDownloader | ZenodoDownloader | ArchiveDownloader | None
:value: None
Method to download parameter set from somewhere.
.. py:attribute:: model_config
.. py:method:: download(download_dir: pathlib.Path, force: bool = False) -> None
Download parameter set to directory.
:param download_dir: Directory where parameter set should be downloaded to.
:param force: If True then download even if directory already exists.
:raises ValueError: If no downloader is defined.
.. py:method:: from_github(org: str, repo: str, branch: str, subfolder: str | None = None, **kwargs)
:classmethod:
Create a parameter set from a GitHub repository.
:param org: GitHub organization (e.g., 'UU-Hydro')
:param repo: Repository name (e.g, 'PCR-GLOBWB_input_example')
:param branch: Branch name (e.g., 'master')
:param subfolder: Subfolder within the github repo to extract. E.g.
'pcrglobwb_rhinemeuse_30min'.
If not given then downloads the entire repository.
:param \*\*kwargs: See :py:class:`ParameterSet` for other arguments.
The example Github arguments would download all files from
https://github.com/UU-Hydro/PCR-GLOBWB_input_example/tree/master/RhineMeuse30min
.. py:method:: from_zenodo(doi: str, **kwargs) -> ParameterSet
:classmethod:
Download a parameter set from Zenodo.
:param doi: DOI of Zenodo record. E.g. '10.5281/zenodo.1045339'.
:param \*\*kwargs: See :py:class:`ParameterSet` for other arguments.
.. py:method:: from_archive_url(url: str, **kwargs) -> ParameterSet
:classmethod:
Download a parameter set from an archive file on the Internet.
:param url: Link to archive file.
:param \*\*kwargs: See :py:class:`ParameterSet` for other arguments.
.. rubric:: Example
>>> from ewatercycle.base import ParameterSet
>>> url = ''
>>> parameter_set = ParameterSet.from_archive_url(url)
.. py:method:: make_absolute(parameterset_dir: pathlib.Path) -> ParameterSet
Make self.directory and self.config absolute paths.
:param parameterset_dir: Directory to which relative paths should be made absolute.
:returns: self
.. py:function:: to_absolute_path(input_path: str | pathlib.Path, parent: pathlib.Path | None = None, must_exist: bool = False, must_be_in_parent=True) -> pathlib.Path
Parse input string as :py:class:`pathlib.Path` object.
:param input_path: Input string path that can be a relative or absolute path.
:param parent: Optional parent path of the input path
:param must_exist: Optional argument to check if the input path exists.
:param must_be_in_parent: Optional argument to check if the input path is
subpath of parent path
:returns: The input path that is an absolute path and a :py:class:`pathlib.Path` object.
.. py:data:: logger
.. py:data:: ContainerEngine
.. py:data:: ExpandedDirectoryPath
.. py:data:: ExpandedFilePath
.. py:class:: Configuration
Bases: :py:obj:`pydantic.BaseModel`
Configuration object.
Do not instantiate this class directly, but use
:obj:`ewatercycle.CFG` instead.
.. py:attribute:: grdc_location
:type: ExpandedDirectoryPath
Where can GRDC observation files be found.
Assumes file names like _Q_Day.Cmd.txt
.. py:attribute:: container_engine
:type: ContainerEngine
:value: 'docker'
Which container engine is used to run the hydrological models.
.. py:attribute:: apptainer_dir
:type: ExpandedDirectoryPath
Where the apptainer images files (.sif) be found.
.. py:attribute:: singularity_dir
:type: pydantic.DirectoryPath | None
:value: None
Where the singularity images files (.sif) be found.
DEPRECATED, use apptainer_dir.
.. py:attribute:: output_dir
:type: ExpandedDirectoryPath
Directory in which output of model runs is stored.
Each model run will generate a sub directory inside output_dir
.. py:attribute:: parameterset_dir
:type: ExpandedDirectoryPath
Root directory for all parameter sets.
.. py:attribute:: parameter_sets
:type: dict[str, ewatercycle.base.parameter_set.ParameterSet]
Dictionary of parameter sets.
Data source for :py:func:`ewatercycle.parameter_sets.available_parameter_sets` and
:py:func:`ewatercycle.base.parameter_set.ParameterSet.download` methods.
.. py:attribute:: ewatercycle_config
:type: ExpandedFilePath | None
:value: None
Where is the configuration saved or loaded from.
If None then the configuration was not loaded from a file.
.. py:attribute:: model_config
.. py:method:: singularity_dir_is_deprecated(data: Any) -> Any
:classmethod:
Deprecate singularity_dir field.
.. py:method:: load_from_file(filename: os.PathLike[str] | str) -> None
Load user configuration from the given file.
The config is cleared and updated in-place.
.. py:method:: reload() -> None
Reload the config file.
.. py:method:: reset() -> None
Reset to empty configuration.
.. py:method:: dump_to_yaml() -> str
Dumps YAML formatted string of Config object.
.. py:method:: save_to_file(config_file: os.PathLike[str] | str | None = None) -> str
Write conf object to a file.
:param config_file: File to write configuration object to.
If not given then will try to use `self.ewatercycle_config`
location and if `self.ewatercycle_config` is not set then will use
the location in users home directory.
:returns: Path to the file where the configuration was saved to.
.. py:method:: overwrite(other: Configuration)
Overwrite own fields by the ones of the other configuration object.
:param other: The other configuration object.
.. py:data:: USER_HOME_CONFIG
.. py:data:: SYSTEM_CONFIG
.. py:data:: USER_CONFIG
.. py:data:: CFG
eWaterCycle configuration object.
The configuration is loaded from:
1. ``$XDG_CONFIG_HOME/ewatercycle/ewatercycle.yaml``
2. ``~/.config/ewatercycle/ewatercycle.yaml``
3. ``/etc/ewatercycle.yaml``
4. Fall back to empty configuration
The ``ewatercycle.yaml`` is formatted in YAML and could for example look like:
.. code-block:: yaml
grdc_location: /data/grdc
container_engine: apptainer
apptainer_dir: /data/apptainer-images
output_dir: /scratch
# Filled apptainer_dir with
# cd /data/apptainer-images
# apptainer pull docker://ewatercycle/wflow-grpc4bmi:2020.1.1