{ "cells": [ { "cell_type": "markdown", "id": "9a73590b", "metadata": {}, "source": [ "## Clip a Wflow model" ] }, { "cell_type": "markdown", "id": "ee510c6e", "metadata": {}, "source": [ "Let's imagine you have built and calibrated a **Wflow** model for a whole country or continent. And now you have a new project for a small basin belonging to your big calibrated Wflow model. Instead of re-building the smaller model fomr scratch with HydroMT and re-doing the calibration steps, you can easily use the **clip** method of HydroMT to directly extract your smaller catchment out of your already existing big calibrated model!\n", "\n", "This notebook demonstrates how to clip a **Wflow** model from an already existing one 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 clip interface" ] }, { "cell_type": "markdown", "id": "7d9b5a20", "metadata": {}, "source": [ "Using the **HydroMT clip** API we can extract a smaller wflow model out of an already existing bigger one. Let's get an overview of all the available options:" ] }, { "cell_type": "code", "execution_count": null, "id": "1d39e4a1", "metadata": {}, "outputs": [], "source": [ "!hydromt clip --help" ] }, { "cell_type": "markdown", "id": "f286fccb", "metadata": {}, "source": [ "### Clip a small sub-basin" ] }, { "cell_type": "code", "execution_count": null, "id": "closed-excerpt", "metadata": {}, "outputs": [], "source": [ "# NOTE: copy this line (without !) to your shell for more direct feedback\n", "!hydromt clip wflow \"wflow_piave_subbasin\" \"./wflow_test_clip\" \"{'subbasin': [12.3006, 46.4324], 'wflow_streamorder': 4}\" -v" ] }, { "cell_type": "markdown", "id": "cb9d0032", "metadata": {}, "source": [ "The example above means the following: run **hydromt clip** with:\n", "\n", "* `wflow` : i.e. clip a wflow model\n", "* `wflow_piave_subbasin`: original model to clip\n", "* `./wflow_test_clip` : output model folder\n", "* `\"{'subbasin': [12.3006, 46.4324], 'wflow_streamorder': 4}\"` : derive a subbasin with its outlet at the given x,y coordinates (WGS84) snapped to a river with minimum stream order (wflow_streamorder) of 4. All *REGION* options are described in the [docs](https://deltares.github.io/hydromt/latest/user_guide/model_region)\n", "* `-vv` : give some extra verbosity (2 * v) to display feedback on screen. Now debug messages are provided.\n", "\n", "NOTE: Compared to build, you may notice here that the streamorder argument is called 'wflow_streamorder'. As we are clipping a wflow model, the name here should correspond to the name of the stream order map inside of your wflow model." ] }, { "cell_type": "markdown", "id": "a614daf5", "metadata": {}, "source": [ "Compared to build, the clip command line has less options (no configuration file and no data catalog or resolution). Clip simply re-uses the original model data and extract all the informations from the model for our clipped region location." ] }, { "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", "\n", "Here we will just simply plot the region of the different model (original and clipped) to check the differences between them." ] }, { "cell_type": "code", "execution_count": null, "id": "2c73e3ef", "metadata": {}, "outputs": [], "source": [ "# Import plot packages\n", "import matplotlib.pyplot as plt\n", "import cartopy.crs as ccrs\n", "import cartopy.io.img_tiles as cimgt\n", "\n", "# import descartes # used under the hood to plot polygons" ] }, { "cell_type": "code", "execution_count": null, "id": "af229d95", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import hydromt\n", "from hydromt_wflow import WflowModel" ] }, { "cell_type": "code", "execution_count": null, "id": "850b8018", "metadata": {}, "outputs": [], "source": [ "# Load both models with hydromt\n", "mod0 = WflowModel(root=\"wflow_piave_subbasin\", mode=\"r\")\n", "mod1 = WflowModel(root=\"wflow_test_clip\", mode=\"r\")" ] }, { "cell_type": "code", "execution_count": null, "id": "fe13011b", "metadata": {}, "outputs": [], "source": [ "# read/derive river geometries\n", "gdf_riv0 = mod0.rivers\n", "gdf_riv1 = mod1.rivers\n", "# read/derive model basin boundary\n", "gdf_bas0 = mod0.basins\n", "gdf_bas1 = mod1.basins" ] }, { "cell_type": "code", "execution_count": null, "id": "10fa049a", "metadata": {}, "outputs": [], "source": [ "# we assume the model maps are in the geographic CRS EPSG:4326\n", "proj = ccrs.PlateCarree()\n", "# adjust zoomlevel and figure size to your basis size & aspect\n", "zoom_level = 10\n", "figsize = (10, 8)\n", "shaded = False\n", "\n", "\n", "# initialize image with geoaxes\n", "fig = plt.figure(figsize=figsize)\n", "ax = fig.add_subplot(projection=proj)\n", "extent = np.array(mod0.grid.raster.box.buffer(0.02).total_bounds)[[0, 2, 1, 3]]\n", "ax.set_extent(extent, crs=proj)\n", "\n", "# add sat background image\n", "ax.add_image(cimgt.QuadtreeTiles(), zoom_level, alpha=0.5)\n", "\n", "# plot rivers with increasing width with stream order\n", "gdf_riv0.plot(ax=ax, lw=gdf_riv0[\"strord\"] / 2, color=\"blue\", zorder=3, label=\"river\")\n", "gdf_riv1.plot(\n", " ax=ax, lw=gdf_riv1[\"strord\"] / 2, color=\"purple\", zorder=3, label=\"river clip\"\n", ")\n", "# plot the basin boundary\n", "gdf_bas0.boundary.plot(ax=ax, color=\"k\", linewidth=0.8, label=\"basin original\")\n", "gdf_bas1.boundary.plot(ax=ax, color=\"r\", linewidth=0.8, label=\"basin clip\")\n", "\n", "ax.xaxis.set_visible(True)\n", "ax.yaxis.set_visible(True)\n", "ax.set_ylabel(f\"latitude [degree north]\")\n", "ax.set_xlabel(f\"longitude [degree east]\")\n", "_ = ax.set_title(f\"wflow base map\")\n", "legend = ax.legend(\n", " handles=[*ax.get_legend_handles_labels()[0]],\n", " title=\"Legend\",\n", " loc=\"lower right\",\n", " frameon=True,\n", " framealpha=0.7,\n", " edgecolor=\"k\",\n", " facecolor=\"white\",\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }