Tip

For an interactive online version click here: Binder badge

Example: Working with models from CLI#

HydroMT has the following high-level functionality from the Command Line Interface (CLI) for setting up models from raw data or adjusting models:

  • building a model: building a model from scratch.

  • updating a model: adding or changing model components of an existing model.

  • clipping a model: changing the spatial domain of an existing model (e.g. select subbasins from a larger model).

Here we show how to build and update a hypothetical distributed model from the command line interface (CLI) based on the generic HydroMT grid_model.

Lets first check which models are available in our environment:

[ ]:
!hydromt --models

Build a model from CLI#

To build a model you always follow the next four steps.

  1. Prepare or use a pre-defined data catalog with all the required data sources

  2. Define your model region, see the overview of model region options.

  3. Prepare a model configuration which describes the complete pipeline to build your model, see preparing a model configuration.

  4. Build you model using the CLI or Python interface

Here we focus steps 2-4 and use data from the predefined aritifact_data data catalog

Using the hydromt build method we can setup a complete model from scratch. Let’s get an overview of the method and its arguments.

Note the required MODEL (i.e. name of the model), MODEL_ROOT (i.e. folder where to save the model) arguments. As of version v0.7.0 the REGION (i.e. area of interest) argument is optional. This argument can be used by adding -r or –region flag.

[ ]:
!hydromt build --help
[ ]:
!hydromt build grid_model ./tmp_grid_model --region "{'bbox': [11.70, 45.35, 12.95, 46.70]}" -vv

The example above means the following: run hydromt build with:

  • grid_model: i.e. build a generic GridModel instance

  • ./tmp_grid_model: output model folder

  • --region "{'bbox': [11.70, 45.35, 12.95, 46.70]}": set the region of interest using a bounding box defined by its [xmin, ymin, xmax, ymax] coordinates (in WGS84)

  • -vv : give some extra verbosity (2 * v) to display feedback on screen. Now debug messages are provided.

As we did not specify a hydromt configuration, besides the hydromt.log file, an empty model simulation configuration file, only the model region (area of interest) has been defined and is saved in the geoms folder. To build a complete model we need the use a hydromt configuration .yaml file.

[ ]:
# print MODEL_ROOT folder
import os


def print_dir(root):
    for path, _, files in os.walk(root):
        print(path)
        for name in files:
            if name.endswith(".xml"):
                continue
            print(f" - {name}")


print_dir("tmp_grid_model")

The configuration file exists of available setup_methods for your model, listed in the order of execution. Most methods start with reading input data using the DataAdapter, transforming the data using workflows (e.g. reprojection, reclassification, aggregation, etc…) and adding the new model data to the right model component. An overview of the available methods can be found in the online API reference for the GridModel, LumpedModel, and MeshModel

Note that these methods for the generic model classes are still quite limited. To get an idea of potential setup_ methods, checkout the model plugins

The configuration yaml file can be passed to the hydromt build method using the -i flag.

[ ]:
# check the content of the .yaml file (for sake of the example only)
fn_yaml = "grid_model_build.yaml"
with open(fn_yaml, "r") as f:
    txt = f.read()
print(txt)
[ ]:
!hydromt build grid_model ./tmp_grid_model1 -r "{'bbox': [11.70, 45.35, 12.95, 46.70]}" -i grid_model_build.yaml -d artifact_data -d data/vito_reclass.yml -vv

The example above means the following: run hydromt build with:

  • grid_model: i.e. build a generic GridModel instance

  • ./tmp_grid_model1: output model folder

  • -r "{'bbox': [11.70, 45.35, 12.95, 46.70]}": set the region of interest using a bounding box defined by its [xmin, ymin, xmax, ymax] coordinates (in WGS84)

  • -i grid_model_build.yaml: use this .yaml file to configure the model build

  • -d artifact_data -d data/vito_reclass.yml: parse the pre-defined artifact_data and the local vito_reclass data catalogs

  • -vv : give some extra verbosity (2 * v) to display feedback on screen. Now debug messages are provided.

[ ]:
print_dir("tmp_grid_model1")
[ ]:
# checkout the content of the hypothetical model simulation configuration
fn_yaml = "tmp_grid_model1/model.yaml"
with open(fn_yaml, "r") as f:
    txt = f.read()
print(txt)

Update a model from CLI#

Using the hydromt update method we can update an existing model with new components or modify existing components. Let’s get an overview of the method and its arguments.

Note that the MODEL (i.e. name of the model), and MODEL_ROOT (i.e. folder of existing model) are still required. There is an optional -o --model-out option to save the updated model in a different directory.

[ ]:
!hydromt update --help

In this basic example we use the hydromt update method to update the GridModel instance with an upstream area raster map. Then we write only the updated model map component to file.

The %%writefile magic saves the content below to a file. The content of the .yaml file start from the second line.

[1]:
%%writefile ./tmp_grid_model1/grid_model_update.yaml
setup_maps_from_raster:
    raster_fn: merit_hydro_1k
    variables: uparea

write_maps:

Writing ./tmp_grid_model1/grid_model_update.yaml
[ ]:
!hydromt update grid_model ./tmp_grid_model1 -i ./tmp_grid_model1/grid_model_update.yaml -vv

The example above means the following: run hydromt update with:

  • grid_model: i.e. update a generic GridModel instance

  • ./tmp_model1: the folder of the to-be updated model

  • -i ./tmp_grid_model1/grid_model_update.yaml: the hydromt configuration listing the methods to be executed

  • -vv : give some extra verbosity (2 * v) to display feedback on screen. Now debug messages are provided.

[ ]:
# note the difference with the original model
print_dir("tmp_grid_model1")