HydroMT has the following high-level functionality from the Command Line Interface (CLI) for setting up models from raw data or adjusting models:
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
To build a model you always follow the next four steps.
Here we focus steps 2-4 and use data from the predefined artifact_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]}" --opt "setup_grid.res"=0.05 -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)--opt "setup_grid.res" = 0.05
: choose the resolution of the grid to generate within the bounding box. Unit (degree or meter) depends on the chosen coordinate system.-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) and the grid definition have been defined and are respectively saved in the geoms folder and the grid.nc file. 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, VectorModel, 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)
In this configuration, you see that we will prepare quite a lot of data for our grid model using some of the generic model methods for grid. We will prepare:
!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.Let's check some of the outputs that were produced:
# Files created
print_dir("tmp_grid_model1")
# checkout the content of the hypothetical model simulation configuration
fn_yaml = "tmp_grid_model1/model.ini"
with open(fn_yaml, "r") as f:
txt = f.read()
print(txt)
# List of variables in grid.nc
import xarray as xr
ds = xr.open_dataset("tmp_grid_model1/grid/grid.nc")
print(f"Variables available in grid.nc: {list(ds.data_vars)}")
# Plot one of the variable (change the name below to plot a different variable)
var = "elevtn"
ds[var].plot()
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.
%%writefile ./tmp_grid_model1/grid_model_update.yaml
setup_grid_from_rasterdataset:
raster_fn: merit_hydro_1k
variables: uparea
reproject_method: max
write_grid:
!hydromt update grid_model ./tmp_grid_model1 -o ./tmp_grid_model1_update -i ./tmp_grid_model1/grid_model_update.yaml -d artifact_data -vv
The example above means the following: run hydromt update with:
grid_model
: i.e. update a generic GridModel instance./tmp_grid_model1
: the folder of the to-be updated model./tmp_grid_model1_update
: the folder of the 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_update")