Tip

For an interactive online version click here: Binder badge

Clip a Wflow model#

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!

This notebook demonstrates how to clip a Wflow model from an already existing one using the command line interace (CLI).

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.

HydroMT CLI clip interface#

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:

[1]:
!hydromt clip --help
Usage: hydromt clip [OPTIONS] MODEL MODEL_ROOT MODEL_DESTINATION REGION

  Create a new model based on clipped region of an existing model. If the
  existing model contains forcing, they will also be clipped to the new model.

  For options to build wflow models see:

  Example usage to clip a wflow model for a subbasin derived from point coordinates
  snapped to cells with stream order >= 4
  hydromt clip wflow /path/to/model_root /path/to/model_destination "{'subbasin': [-7.24, 62.09], 'wflow_streamorder': 4}"

  Example usage basin based on ID from model_root basins map
  hydromt clip wflow /path/to/model_root /path/to/model_destination "{'basin': 1}"

  Example usage basins whose outlets are inside a geometry
  hydromt clip wflow /path/to/model_root /path/to/model_destination "{'outlet': 'geometry.geojson'}"

  All available option in the clip_staticmaps function help.

Options:
  -q, --quiet    Decrease verbosity.
  -v, --verbose  Increase verbosity.
  --help         Show this message and exit.

Clip a small sub-basin#

[2]:
# NOTE: copy this line (without !) to your shell for more direct feedback
!hydromt clip wflow "wflow_piave_subbasin" "./wflow_test_clip" "{'subbasin': [12.3006, 46.4324], 'wflow_streamorder': 4}" -v
2022-08-10 11:49:18,583 - clip - log - INFO - HydroMT version: 0.5.1.dev
2022-08-10 11:49:18,583 - clip - main - INFO - Clipping instance of wflow model.
2022-08-10 11:49:18,583 - clip - main - INFO - Region: {'subbasin': [12.3006, 46.4324], 'wflow_streamorder': 4}
2022-08-10 11:49:18,588 - clip - model_api - INFO - Initializing wflow model from hydromt_wflow (v0.2.1.dev0).
2022-08-10 11:49:18,588 - clip - main - INFO - Reading model to clip
2022-08-10 11:49:18,591 - clip - wflow - INFO - Read staticmaps from /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_piave_subbasin/staticmaps.nc
2022-08-10 11:49:18,729 - clip - wflow - INFO - Reading model intbl files.
2022-08-10 11:49:18,729 - clip - wflow - INFO - Reading model staticgeom files.
2022-08-10 11:49:18,811 - clip - wflow - INFO - Read forcing from /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_piave_subbasin/inmaps.nc
2022-08-10 11:49:18,819 - clip - wflow - INFO - Model read
2022-08-10 11:49:18,820 - clip - main - INFO - Clipping staticmaps
2022-08-10 11:49:21,472 - clip - basin_mask - INFO - subbasin bbox: [12.0083, 46.3417, 12.3250, 46.6750]
2022-08-10 11:49:22,963 - clip - main - INFO - Clipping forcing
2022-08-10 11:49:22,963 - clip - wflow - INFO - Clipping NetCDF forcing..
2022-08-10 11:49:22,970 - clip - model_api - WARNING - Replacing forcing: precip
2022-08-10 11:49:22,970 - clip - model_api - WARNING - Replacing forcing: pet
2022-08-10 11:49:22,970 - clip - model_api - WARNING - Replacing forcing: temp
2022-08-10 11:49:22,971 - clip - main - INFO - Writting clipped model
2022-08-10 11:49:22,971 - clip - wflow - INFO - Write model data to /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_test_clip
2022-08-10 11:49:22,995 - clip - data_catalog - INFO - Reading data catalog artifact_data v0.0.6 from archive
2022-08-10 11:49:22,995 - clip - data_catalog - INFO - Parsing data catalog from /home/runner/.hydromt_data/artifact_data/v0.0.6/data_catalog.yml
2022-08-10 11:49:23,089 - clip - model_api - INFO - Writing model config to /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_test_clip/wflow_sbm.toml
2022-08-10 11:49:23,191 - clip - wflow - INFO - Write staticmaps to /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_test_clip/staticmaps.nc
2022-08-10 11:49:23,459 - clip - wflow - INFO - Writing model staticgeom to file.
2022-08-10 11:49:23,482 - clip - wflow - INFO - Write forcing file
2022-08-10 11:49:23,502 - clip - wflow - INFO - Process forcing; saving to /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_test_clip/inmaps.nc
[########################################] | 100% Completed | 101.43 ms

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

  • wflow : i.e. clip a wflow model

  • wflow_piave_subbasin: original model to clip

  • ./wflow_test_clip : output model folder

  • "{'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

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

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.

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.

Visualize and/or inspect model schematization#

The wflow plot example notebook contains scripts to visualize your model.

Here we will just simply plot the region of the different model (original and clipped) to check the differences between them.

[3]:
# Import plot packages
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

# import descartes  # used under the hood to plot polygons
[4]:
import numpy as np
import hydromt
from hydromt_wflow import WflowModel
[5]:
# Load both models with hydromt
mod0 = WflowModel(root="wflow_piave_subbasin", mode="r")
mod1 = WflowModel(root="wflow_test_clip", mode="r")
[6]:
# read/derive river geometries
gdf_riv0 = mod0.rivers
gdf_riv1 = mod1.rivers
# read/derive model basin boundary
gdf_bas0 = mod0.basins
gdf_bas1 = mod1.basins
[7]:
plt.style.use("seaborn-whitegrid")  # set nice style
# we assume the model maps are in the geographic CRS EPSG:4326
proj = ccrs.PlateCarree()
# adjust zoomlevel and figure size to your basis size & aspect
zoom_level = 10
figsize = (10, 8)
shaded = False


# initialize image with geoaxes
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(projection=proj)
extent = np.array(mod0.staticmaps.raster.box.buffer(0.02).total_bounds)[[0, 2, 1, 3]]
ax.set_extent(extent, crs=proj)

# add sat background image
ax.add_image(cimgt.QuadtreeTiles(), zoom_level, alpha=0.5)

# plot rivers with increasing width with stream order
gdf_riv0.plot(ax=ax, lw=gdf_riv0["strord"] / 2, color="blue", zorder=3, label="river")
gdf_riv1.plot(
    ax=ax, lw=gdf_riv1["strord"] / 2, color="purple", zorder=3, label="river clip"
)
# plot the basin boundary
gdf_bas0.boundary.plot(ax=ax, color="k", linewidth=0.8, label="basin original")
gdf_bas1.boundary.plot(ax=ax, color="r", linewidth=0.8, label="basin clip")

ax.xaxis.set_visible(True)
ax.yaxis.set_visible(True)
ax.set_ylabel(f"latitude [degree north]")
ax.set_xlabel(f"longitude [degree east]")
_ = ax.set_title(f"wflow base map")
legend = ax.legend(
    handles=[*ax.get_legend_handles_labels()[0]],
    title="Legend",
    loc="lower right",
    frameon=True,
    framealpha=0.7,
    edgecolor="k",
    facecolor="white",
)
../_images/_examples_clip_model_16_0.png