Skip to content

Commit bc05a1c

Browse files
Merge configuration object from multiple files (instead of one single file) (#2448)
Co-authored-by: Bouwe Andela <[email protected]>
1 parent 4b0dd41 commit bc05a1c

33 files changed

+1414
-693
lines changed

doc/api/esmvalcore.config.rst

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
.. _api_configuration:
2+
13
Configuration
24
=============
35

46
This section describes the :py:class:`~esmvalcore.config` module.
57

6-
Config
7-
******
8+
CFG
9+
***
810

9-
Configuration of ESMValCore/Tool is done via the :py:class:`~esmvalcore.config.Config` object.
10-
The global configuration can be imported from the :py:mod:`esmvalcore.config` module as :py:data:`~esmvalcore.config.CFG`:
11+
Configuration of ESMValCore/Tool is done via :py:data:`~esmvalcore.config.CFG`
12+
object:
1113

1214
.. code-block:: python
1315
@@ -16,7 +18,6 @@ The global configuration can be imported from the :py:mod:`esmvalcore.config` mo
1618
Config({'auxiliary_data_dir': PosixPath('/home/user/auxiliary_data'),
1719
'compress_netcdf': False,
1820
'config_developer_file': None,
19-
'config_file': PosixPath('/home/user/.esmvaltool/config-user.yml'),
2021
'drs': {'CMIP5': 'default', 'CMIP6': 'default'},
2122
'exit_on_warning': False,
2223
'log_level': 'info',
@@ -30,9 +31,10 @@ The global configuration can be imported from the :py:mod:`esmvalcore.config` mo
3031
'default': '~/default_inputpath'},
3132
'save_intermediary_cubes': False)
3233
33-
The parameters for the user configuration file are listed :ref:`here <user configuration file>`.
34+
All configuration parameters are listed :ref:`here <config_options>`.
3435
35-
:py:data:`~esmvalcore.config.CFG` is essentially a python dictionary with a few extra functions, similar to :py:data:`matplotlib.rcParams`.
36+
:py:data:`~esmvalcore.config.CFG` is essentially a python dictionary with a few
37+
extra functions, similar to :py:data:`matplotlib.rcParams`.
3638
This means that values can be updated like this:
3739
3840
.. code-block:: python
@@ -41,8 +43,10 @@ This means that values can be updated like this:
4143
>>> CFG['output_dir']
4244
PosixPath('/home/user/esmvaltool_output')
4345
44-
Notice that :py:data:`~esmvalcore.config.CFG` automatically converts the path to an instance of ``pathlib.Path`` and expands the home directory.
45-
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:
46+
Notice that :py:data:`~esmvalcore.config.CFG` automatically converts the path
47+
to an instance of :class:`pathlib.Path` and expands the home directory.
48+
All values entered into the config are validated to prevent mistakes, for
49+
example, it will warn you if you make a typo in the key:
4650
4751
.. code-block:: python
4852
@@ -56,43 +60,53 @@ Or, if the value entered cannot be converted to the expected type:
5660
>>> CFG['max_parallel_tasks'] = '🐜'
5761
InvalidConfigParameter: Key `max_parallel_tasks`: Could not convert '🐜' to int
5862
59-
:py:class:`~esmvalcore.config.Config` is also flexible, so it tries to correct the type of your input if possible:
63+
:py:data:`~esmvalcore.config.CFG` is also flexible, so it tries to correct the
64+
type of your input if possible:
6065
6166
.. code-block:: python
6267
6368
>>> CFG['max_parallel_tasks'] = '8' # str
6469
>>> type(CFG['max_parallel_tasks'])
6570
int
6671
67-
By default, the config is loaded from the default location (``/home/user/.esmvaltool/config-user.yml``).
68-
If it does not exist, it falls back to the default values.
69-
to load a different file:
72+
By default, the configuration is loaded from YAML files in the user's home
73+
directory at ``~/.config/esmvaltool``.
74+
If set, this can be overwritten with the ``ESMVALTOOL_CONFIG_DIR`` environment
75+
variable.
76+
Defaults for options that are not specified explicitly are listed :ref:`here
77+
<config_options>`.
78+
To reload the current configuration object according to these rules, use:
7079
7180
.. code-block:: python
7281
73-
>>> CFG.load_from_file('~/my-config.yml')
82+
>>> CFG.reload()
7483
75-
Or to reload the current config:
84+
To load the configuration object from custom directories, use:
7685
7786
.. code-block:: python
7887
79-
>>> CFG.reload()
88+
>>> dirs = ['my/default/config', 'my/custom/config']
89+
>>> CFG.load_from_dirs(dirs)
8090
8191
8292
Session
8393
*******
8494
8595
Recipes and diagnostics will be run in their own directories.
86-
This behaviour can be controlled via the :py:data:`~esmvalcore.config.Session` object.
87-
A :py:data:`~esmvalcore.config.Session` can be initiated from the global :py:class:`~esmvalcore.config.Config`.
96+
This behavior can be controlled via the :py:data:`~esmvalcore.config.Session`
97+
object.
98+
A :py:data:`~esmvalcore.config.Session` must always be initiated from the
99+
global :py:data:`~esmvalcore.config.CFG` object:
88100
89101
.. code-block:: python
90102
91103
>>> session = CFG.start_session(name='my_session')
92104
93105
A :py:data:`~esmvalcore.config.Session` is very similar to the config.
94-
It is also a dictionary, and copies all the keys from the :py:class:`~esmvalcore.config.Config`.
95-
At this moment, ``session`` is essentially a copy of :py:data:`~esmvalcore.config.CFG`:
106+
It is also a dictionary, and copies all the keys from the
107+
:py:data:`~esmvalcore.config.CFG` object.
108+
At this moment, ``session`` is essentially a copy of
109+
:py:data:`~esmvalcore.config.CFG`:
96110
97111
.. code-block:: python
98112
@@ -102,7 +116,8 @@ At this moment, ``session`` is essentially a copy of :py:data:`~esmvalcore.confi
102116
>>> print(session == CFG) # False
103117
False
104118
105-
A :py:data:`~esmvalcore.config.Session` also knows about the directories where the data will stored.
119+
A :py:data:`~esmvalcore.config.Session` also knows about the directories where
120+
the data will stored.
106121
The session name is used to prefix the directories.
107122
108123
.. code-block:: python
@@ -118,7 +133,8 @@ The session name is used to prefix the directories.
118133
>>> session.plot_dir
119134
/home/user/my_output_dir/my_session_20201203_155821/plots
120135
121-
Unlike the global configuration, of which only one can exist, multiple sessions can be initiated from :py:class:`~esmvalcore.config.Config`.
136+
Unlike the global configuration, of which only one can exist, multiple sessions
137+
can be initiated from :py:data:`~esmvalcore.config.CFG`.
122138
123139
124140
API reference

doc/contributing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ users.
571571

572572
When making changes, e.g. to the :ref:`recipe format <recipe_overview>`, the
573573
:ref:`diagnostic script interface <interfaces>`, the public
574-
:ref:`Python API <api>`, or the :ref:`configuration file format <config>`,
574+
:ref:`Python API <api>`, or the :ref:`configuration format <config>`,
575575
keep in mind that this may affect many users.
576576
To keep the tool user friendly, try to avoid making changes that are not
577577
backward compatible, i.e. changes that require users to change their existing

doc/develop/fixing_data.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ severity. From highest to lowest:
329329
330330
Users can have control about which levels of issues are interpreted as errors,
331331
and therefore make the checker fail or warnings or debug messages.
332-
For this purpose there is an optional command line option `--check-level`
333-
that can take a number of values, listed below from the lowest level of
334-
strictness to the highest:
332+
For this purpose there is an optional :ref:`configuration option
333+
<config_options>` ``check_level`` that can take a number of values, listed
334+
below from the lowest level of strictness to the highest:
335335
336336
- ``ignore``: all issues, regardless of severity, will be reported as
337337
warnings. Checker will never fail. Use this at your own risk.
@@ -375,8 +375,8 @@ To allow ESMValCore to locate the data files, use the following steps:
375375
376376
- If you want to use the ``native6`` project (recommended for datasets whose
377377
input files can be easily moved to the usual ``native6`` directory
378-
structure given by the ``rootpath`` in your :ref:`user configuration
379-
file`; this is usually the case for native reanalysis/observational
378+
structure given by the :ref:`configuration option <config_options>`
379+
``rootpath``; this is usually the case for native reanalysis/observational
380380
datasets):
381381
382382
The entry ``native6`` of ``config-developer.yml`` should be complemented
@@ -399,17 +399,17 @@ To allow ESMValCore to locate the data files, use the following steps:
399399
400400
To find your native data (e.g., called ``MYDATA``) that is for example
401401
located in ``{rootpath}/MYDATA/amip/run1/42-0/atm/run1_1979.nc``
402-
(``{rootpath}`` is ESMValTool's ``rootpath`` for the project ``native6``
403-
defined in your :ref:`user configuration file`), use the following dataset
402+
(``{rootpath}`` is ESMValTool's ``rootpath`` :ref:`configuration option
403+
<config_options>` for the project ``native6``), use the following dataset
404404
entry in your recipe
405405
406406
.. code-block:: yaml
407407
408408
datasets:
409409
- {project: native6, dataset: MYDATA, exp: amip, simulation: run1, version: 42-0, type: atm}
410410
411-
and make sure to use the following DRS for the project ``native6`` in your
412-
:ref:`user configuration file`:
411+
and make sure to use the following :ref:`configuration option
412+
<config_options>` ``drs``:
413413
414414
.. code-block:: yaml
415415
@@ -437,9 +437,8 @@ To allow ESMValCore to locate the data files, use the following steps:
437437
438438
To find your ICON data that is for example located in files like
439439
``{rootpath}/amip/amip_atm_2d_ml_20000101T000000Z.nc`` (``{rootpath}`` is
440-
ESMValTool ``rootpath`` for the project ``ICON`` defined in your
441-
:ref:`user configuration file`), use the following dataset entry in your
442-
recipe:
440+
ESMValCore's :ref:`configuration option <config_options>` ``rootpath`` for
441+
the project ``ICON``), use the following dataset entry in your recipe:
443442
444443
.. code-block:: yaml
445444

0 commit comments

Comments
 (0)