hydromt_wflow.components.WflowConfigComponent#

Summary of Methods and Attributes#

class WflowConfigComponent(model: Model, *, filename: str = 'wflow_sbm.toml', default_template_filename: str | None = None)[source]#

Bases: ConfigComponent

Manage the wflow TOML configuration file for model simulations/settings.

WflowConfigComponent data is stored in a dictionary. The component is used to prepare and update model simulations/settings of the wflow model.

property data: dict#

Model config values.

read(filename: str | None = None)[source]#

Read the wflow configuration file from <root/filename>.

If filename is not provided, will default to <root>/{self._filename} or default template configuration file if the model is in write only mode (ie build).

If filename is provided, it will check if the path is absolute and else will assume the given path is relative to the model root.

write(filename: str | None = None, config_root: Path | str | None = None)[source]#

Write the configuration to a file.

The file is written to <root>/<filename> by default, or to <config_root>/<filename> if a config_root is provided.

Parameters:
  • filename (str, optional) – Name of the config file. By default None to use the default name self._filename.

  • config_root (str, optional) – Root folder to write the config file if different from model root (default). Can be absolute or relative to model root.

get_value(key: str, fallback: Any | None = None, abs_path: bool = False) Any | None[source]#

Get config options.

Parameters:
  • key (str) – Keys are a string with ‘.’ indicating a new level: (‘key1.key2’)

  • fallback (Any, optional) – Fallback value if key(s) not found in config, by default None.

  • abs_path (bool, optional) – If True return the absolute path relative to the model root, by default False.

remove(*args: str, errors: str = 'raise') Any[source]#

Remove a config key and return its value.

Parameters:
  • key (str, tuple[str, ...]) – Key to remove from the config. Can be a dotted toml string when providing a list of strings.

  • errors (str, optional) – What to do if the key is not found. Can be “raise” (default) or “ignore”.

Return type:

The popped value, or raises a KeyError if the key is not found.

remove_reservoirs(input: list[str | None] = [], state: list[str | None] = [])[source]#

Remove all reservoir related config options.

close() None#

Clean up all open datasets. Method to be called before finish_write.

create(template: str | Path | None = None)#

Create a new config file based on a template file.

It the template is not provided, the default template will be used if available. Only yaml and toml files are supported.

Parameters:

template (str or Path, optional) – Path to a template config file, by default None

Examples

>> self.create()
>> self.data
    {}

>> self.create(template='path/to/template.yml')
>> self.data
    {'a': 1, 'b': {'c': {'d': 2}}}
property data_catalog: DataCatalog#

Return the data catalog of the model this component is associated with.

finish_write()#

Finish the write functionality after cleanup was called for all components in the model.

All DeferredFileClose objects can overwrite any lazy loaded files now.

property model: Model#

Return the model object this component is associated with.

property name_in_model: str#

Find the name of the component in the parent model components.

property root: ModelRoot#

Return the root of the model this component is associated with.

set(key: str, value: Any)#

Update the config dictionary at key(s) with values.

Parameters:
  • key (str) – a string with ‘.’ indicating a new level: ‘key1.key2’ will translate to {“key1”:{“key2”: value}}

  • value (Any) – the value to set the config to

Examples

>> self.set({'a': 1, 'b': {'c': {'d': 2}}})
>> self.data
    {'a': 1, 'b': {'c': {'d': 2}}}
>> self.set_value('a', 99)
>> {'a': 99, 'b': {'c': {'d': 2}}}

>> self.set_value('b.d.e', 24)
>> {'a': 99, 'b': {'c': {'d': 24}}}
test_equal(other: ModelComponent) Tuple[bool, Dict[str, str]]#

Test if two components are equal.

Parameters:

other (ModelComponent) – The component to compare against.

Returns:

True if the components are equal, and a dict with the associated errors per property checked.

Return type:

tuple[bool, Dict[str, str]]

update(data: Dict[str, Any])#

Set the config dictionary at key(s) with values.

Parameters:

data (Dict[str,Any]) – A dictionary with the values to be set. keys can be dotted like in set_value()

Examples

Setting data as a nested dictionary:

>> self.update({'a': 1, 'b': {'c': {'d': 2}}})
>> self.data
{'a': 1, 'b': {'c': {'d': 2}}}

Setting data using dotted notation:

>> self.update({'a.d.f.g': 1, 'b': {'c': {'d': 2}}})
>> self.data
{'a': {'d':{'f':{'g': 1}}}, 'b': {'c': {'d': 2}}}