{ "cells": [ { "cell_type": "markdown", "id": "9a73590b", "metadata": {}, "source": [ "## Build a Wflow model from scratch" ] }, { "cell_type": "markdown", "id": "ee510c6e", "metadata": {}, "source": [ "This notebook demonstrates how to prepare **Wflow** model from scratch using the command line interace (CLI)." ] }, { "cell_type": "markdown", "id": "6b96aa15", "metadata": {}, "source": [ "All lines in this notebook which start with `!` are executed from the command line. Within the notebook environment the logging messages are shown after completion. You can also copy these lines and paste these in your shell to get more direct feedback." ] }, { "cell_type": "markdown", "id": "451c8168", "metadata": {}, "source": [ "### HydroMT CLI build interface" ] }, { "cell_type": "markdown", "id": "2c70853e", "metadata": {}, "source": [ "Lets first check if the Wflow model is recognized by HydroMT" ] }, { "cell_type": "code", "execution_count": null, "id": "40470302", "metadata": {}, "outputs": [], "source": [ "# this should return \"wflow, wflow_sediment\"\n", "# as well as the generic HydroMT models \"grid_model, lumped_model, network_model, (mesh_model)\"\n", "!hydromt --models" ] }, { "cell_type": "markdown", "id": "7d9b5a20", "metadata": {}, "source": [ "Using the **HydroMT build** API we can setup a complete model from scratch. Let's get an overview of all the available options:" ] }, { "cell_type": "code", "execution_count": null, "id": "1d39e4a1", "metadata": {}, "outputs": [], "source": [ "!hydromt build --help" ] }, { "cell_type": "markdown", "id": "ebd31242", "metadata": {}, "source": [ "### Model setup configuration" ] }, { "cell_type": "markdown", "id": "fb8282c9", "metadata": {}, "source": [ "The HydroMT configuration file (YAML) contains the model setup configuration and determines which methods are used to prepare the different components of a Wflow model and in which order and optionally sets non-default arguments for each method. This configuration is passed to HydroMT using `-i `. We have prepared several example files which are available in the model repository [examples folder](https://github.com/Deltares/hydromt_wflow/tree/main/examples) and from the [docs (building a model)](https://deltares.github.io/hydromt_wflow/latest/user_guide/wflow_build). \n", "\n", "Each header (without indent) (e.g. `setup_basemaps:`) corresponds to a model method. All model methods are explained in the [docs (model components)](https://deltares.github.io/hydromt_wflow/latest/user_guide/wflow_model_setup.html). " ] }, { "cell_type": "markdown", "id": "cc3fd44d", "metadata": {}, "source": [ "We will load the default wflow build configuration yaml file for inspection:" ] }, { "cell_type": "code", "execution_count": null, "id": "e88d4aed", "metadata": {}, "outputs": [], "source": [ "fn_config = \"wflow_build.yml\"\n", "with open(fn_config, \"r\") as f:\n", " txt = f.read()\n", "print(txt)" ] }, { "cell_type": "markdown", "id": "7584dcae", "metadata": {}, "source": [ "### Data for model setup" ] }, { "cell_type": "markdown", "id": "dad35651", "metadata": {}, "source": [ "The `<...>_fn` arguments correspond to a data source from the `DataCatalog` which is based on a yml-file with references to the data paths/urls and how the data should be read. This file can be provided to hydromt using `-d `. \n", "\n", "By default, the pre-defined `artifact_data` catalog containing some example data for the Piave basin will be downloaded to `~/.hydromt_data/` which is also used for this example. An overview of the available example data is provided [here](https://deltares.github.io/hydromt/latest/user_guide/data_existing_cat.html)\n", "This example data is a based on the data which is available from the Deltares p-drive. \n", "If you have acces to this drive, a pre-configured catalog file can be loaded using the `-d deltares_data` pre-defined catalog. \n", "\n", "More background how to write a data catalog yml file can be found in the [HydroMT core docs](https://deltares.github.io/hydromt/latest/user_guide/data_prepare_cat.html) " ] }, { "cell_type": "markdown", "id": "12860ed2", "metadata": {}, "source": [ "### Setup complete Wflow model " ] }, { "cell_type": "markdown", "id": "a5062c03", "metadata": {}, "source": [ "Once we have prepared our data catalog and model build settings file, we are ready to ask HydroMT to build our Wflow model. Building the model can be done from the command line. Down below is an example for the Northern part of the Piave basin in Italy:" ] }, { "cell_type": "code", "execution_count": null, "id": "hundred-trailer", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# NOTE: copy this line (without !) to your shell for more direct feedback\n", "!hydromt build wflow \"./wflow_test_full\" -r \"{'subbasin': [12.2051, 45.8331], 'strord': 4, 'bounds': [11.70, 45.35, 12.95, 46.70]}\" -i wflow_build.yml -d artifact_data -vv" ] }, { "cell_type": "markdown", "id": "b31e92d2", "metadata": {}, "source": [ "With this example we build a complete Wflow model including forcing. The example means the following: run **hydromt build** with:\n", "\n", "* `wflow` : i.e. build a Wflow model\n", "* `./wflow_test_full` : output model folder\n", "* `-r \"{'subbasin': [12.2051, 45.8331], 'strord': 4, 'bounds': [11.70, 45.35, 12.95, 46.70]}\"` : derive a subbasin with its outlet at the given x,y coordinates and within the given bounds [xmin, ymin, xmax, ymax] (WGS84) snapped to a river with minimum stream order (strord) of 4. All *REGION* options are described in the [docs](https://deltares.github.io/hydromt/latest/user_guide/model_region)\n", "* `-i wflow_build.yml` : setup configuration file including all components to build and their arguments\n", "* `-d artifact_data`: data catalog to use. Here `artifact_data` which contains a global data extract for Northern Italy for demo purposes.\n", "* `-vv` : give some extra verbosity (2 * v) to display feedback on screen. Now debug messages are provided.\n" ] }, { "cell_type": "markdown", "id": "2eed76e4", "metadata": {}, "source": [ "Next we check which files have been created. The model root should contain two netcdf (.nc) files, one for the staticmaps and one for the forcing data, a wflow run configuration (.toml) file. These files are sufficient to run the wflow model. In addition several geometry (.geojson) files are saved in the *staticgeoms* folder. Finally, the setup process is logged in the hydromt.log file. " ] }, { "cell_type": "code", "execution_count": null, "id": "wireless-style", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "root = \"wflow_test_full\"\n", "for path, _, files in os.walk(root):\n", " print(path)\n", " for name in files:\n", " if name.endswith(\".xml\"):\n", " continue\n", " print(f\" - {name}\")" ] }, { "cell_type": "markdown", "id": "adef7fa1", "metadata": {}, "source": [ "### Visualize and/or inspect model schematization" ] }, { "cell_type": "markdown", "id": "20ce9df3", "metadata": {}, "source": [ "* The **wflow plot** example notebook contains scripts to visualize your model\n", "* The **wflow nc to raster files** example notebook contains scripts to write the nc model files to a netcdf which can be used to inspect (and modify) the model layers in e.g. QGIS." ] } ], "metadata": { "kernelspec": { "display_name": "waterbom", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" }, "vscode": { "interpreter": { "hash": "5fda536963eae36dbaef69f7df44483d2713515b4e134e53fb983ec0eb9438d0" } } }, "nbformat": 4, "nbformat_minor": 5 }