Tip

For an interactive online version click here: Binder badge

Update forcing conditions to model from Python#

In this example, the previously made SFINCS compound flood model will be updated with more boundary conditions..

The model is situated in Northern Italy, where a small selection of topography and bathymetry data has already been made available for you to try the examples.

[1]:
from pathlib import Path

from hydromt_sfincs import SfincsModel
from hydromt._utils import log
/tmp/ipykernel_4912/36717968.py:4: DeprecationWarning: importing 'log' from 'hydromt._utils' is deprecated. use 'from hydromt import log' instead.
  from hydromt._utils import log

Steps followed in this notebook to update your SFINCS model:

  1. Initialize SfincsModel class, set data library, mode and root folder

  2. Add an upstream discharge time-series as forcing

  3. Add spatially varying rainfall data

  4. Add infiltration to the model

  5. Save all files

Let’s get started!

1. Initialize SfincsModel class, set data library, mode and root folder:#

Before we can use all the tools provided by HydroMT-SFINCS, we have to initialize the SfincsModel instance. This creates a shortcut to all the model components and methods to read, write and create these components.

In contrast to the previous notebook, we now initialize the model in “append” mode: “r+”, to make sure we can upgrade some of its components.

[2]:
model_root = "tmp_sfincs_compound"

# Initialize logging, the lower the log level number, the more verbose (more info) the output
# NOTSET=0-9, DEBUG=10, INFO=20, WARNING=30, ERROR=40, CRITICAL=50
log.initialize_logging()
log.set_log_level(log_level=20)

# Add file handler to log to a file
log_file = Path(model_root) / "hydromt_sfincs.log"
logger = log._add_filehandler(log_file)

# Initialize SfincsModel Python class with the artifact data catalog which contains publically available data for North Italy
sf = SfincsModel(
    data_libs=["artifact_data"],  # specify which data libraries to use
    root=model_root,  # specify the root directory for the model
    mode="r+",  # specify the mode for opening the model (r=read only, r+=append, w=write, w+=overwrite
    write_gis=True,  # specify whether to write GIS data
)
2026-08-18 09:58:56,020 - hydromt - log - INFO - HydroMT version: 1.4.1
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 10
      6 log.set_log_level(log_level=20)
      7
      8 # Add file handler to log to a file
      9 log_file = Path(model_root) / "hydromt_sfincs.log"
---> 10 logger = log._add_filehandler(log_file)
     11
     12 # Initialize SfincsModel Python class with the artifact data catalog which contains publically available data for North Italy
     13 sf = SfincsModel(

AttributeError: module 'hydromt._utils.log' has no attribute '_add_filehandler'

2. Add an upstream discharge time-series as forcing:#

Similar to the water levels, there is many ways to specify the discharge points and the discahrge timeseries, but only one will be discussed here.

[3]:
# optionally, points can be added manually
sf.discharge_points.add_point(
    x=321483.2, y=5047503.0, value=1000.0, name="Piave_inflow"
)

sf.discharge_points.create_timeseries(
    index=[0],
    shape="gaussian",
    offset=0,
    peak=5,
    tpeak=86400,
    duration=2 * 86400,
    timestep=3600,
)
# and plot
sf.plot_basemap(
    variable="dep", plot_geoms=True, plot_bounds=True, bmap="sat", zoomlevel=12
)
sf.plot_forcing()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 2
      1 # optionally, points can be added manually
----> 2 sf.discharge_points.add_point(
      3     x=321483.2, y=5047503.0, value=1000.0, name="Piave_inflow"
      4 )
      5

NameError: name 'sf' is not defined

3. Add spatially varying rainfall data:#

[4]:
# # hourly rainfall rates of ECMWF' ERA5 data for the specific area and period have been made available for this period in the artefact data
sf.precipitation.create(precip="era5_hourly", aggregate=False)

# NOTE: when specifying an output name, the image is also saved to file
sf.plot_forcing(fn_out="forcing.png")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 2
      1 # # hourly rainfall rates of ECMWF' ERA5 data for the specific area and period have been made available for this period in the artefact data
----> 2 sf.precipitation.create(precip="era5_hourly", aggregate=False)
      3
      4 # NOTE: when specifying an output name, the image is also saved to file
      5 sf.plot_forcing(fn_out="forcing.png")

NameError: name 'sf' is not defined

💡 Tip: In case you want to add other types of forcing, read more in the SFINCS manual.

4. Add spatially varying infiltration data:#

SFINCS (and HydroMT-SFINCS) contains a couple of different methods to specify infiltration. In the example below, the curve number method is used, but we recommend to checkout the documentation to explore other options. Note that curve number infiltration only works when the model is forced with precipitation.

[5]:
# independent from subgrid files
# curve number infiltration based on global CN dataset
sf.infiltration.create_cn("gcn250", antecedent_moisture="avg")

_ = sf.plot_basemap(variable="scs", plot_bounds=False, bmap="sat", zoomlevel=12)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 3
      1 # independent from subgrid files
      2 # curve number infiltration based on global CN dataset
----> 3 sf.infiltration.create_cn("gcn250", antecedent_moisture="avg")
      4
      5 _ = sf.plot_basemap(variable="scs", plot_bounds=False, bmap="sat", zoomlevel=12)

NameError: name 'sf' is not defined

5. Write all files#

[6]:
sf.write()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 sf.write()

NameError: name 'sf' is not defined

Now your model has new boundary conditions, you can progress (or run again) to the notebook: Run SFINCS model. In case you want to add some geometries and structures to your model first, continue with notebook Update Geometries