HydroMT-SFINCS | Example 05A
Example: plot maximum water depth
Visualize maximum water depths directly from regular-grid or quadtree SFINCS output.
This notebook plots the maximum water depth (hmax) stored in sfincs_map.nc. It applies to models without subgrid tables, including regular-grid and quadtree models.
For models with subgrid tables, use maximum water levels and a high-resolution DEM instead. Follow Example 05B: Downscale maximum water depth.
Steps#
Read model results and inspect the model layout.
Optionally write maximum water depth to a GeoTIFF (regular grids only).
Optionally mask permanent water and apply a minimum depth threshold.
Plot maximum water depth.
For models without subgrid tables, SFINCS computes water depth from water levels and bed elevations and stores maximum water depth (hmax) in sfincs_map.nc. HydroMT-SFINCS reads both regular-grid and quadtree results, and plot_basemap can plot the resulting maximum-depth field.
The optional GSWO mask below is a regular-grid raster calculation. For quadtree results, skip that step and plot hmax directly or use a vector-based mask.
[1]:
from os.path import join
import matplotlib.pyplot as plt
from hydromt_sfincs import SfincsModel
Step 1: Read model results#
The map output uses staggered-grid conventions for regular grids and an unstructured mesh for quadtree grids. mod.output.read() handles these formats and makes the output variables available through mod.output.data.
[2]:
sfincs_root = "sfincs_compound" # (relative) path to sfincs root
mod = SfincsModel(sfincs_root, data_libs=["artifact_data"], mode="r")
[3]:
# we can simply read the model results (sfincs_map.nc and sfincs_his.nc) using the output.read method
mod.output.read()
# the following variables have been found
list(mod.output.data.keys())
[3]:
['inp',
'msk',
'zb',
'zs',
'h',
'zsmax',
'hmax',
'total_runtime',
'average_dt',
'status',
'point_zb',
'point_zs',
'point_h']
Inspect the model layout. Boundary-cell plotting is disabled because it is not supported for quadtree grids.
[4]:
fig, ax = mod.plot_basemap(
fn_out=None,
bmap="sat",
figsize=(11, 7),
zoomlevel=12,
plot_bounds=False,
)
The provided timeseries does not cover the entire model time period.
Step 2: Optionally write maximum water depth to GeoTIFF (regular grids)#
💡 NOTE This optional raster export is for regular-grid DataArrays. Quadtree output is unstructured; plot it directly or rasterize it with an appropriate workflow before writing a GeoTIFF.
[5]:
# Optional regular-grid GeoTIFF export
# from hydromt_sfincs import writers
# hmax = mod.output.data["hmax"].max(dim="timemax")
# hmax.attrs.update(long_name="maximum water depth", unit="m")
# writers.write_raster(
# hmax,
# join(mod.root.path, "gis"),
# compress="LZW",
# )
Step 3: Optionally mask permanent water and apply a minimum depth threshold#
A Global Surface Water Occurrence (GSWO) raster can be used to mask permanent water on a regular grid. This reprojection step is raster-specific and should be skipped/replaced for quadtree output.
[6]:
# Maximum water depth from the model output
da_hmax = mod.output.data["hmax"].max(dim="timemax")
if mod.grid_type == "regular":
# Permanent water is where water occurrence is greater than 5%.
gswo = mod.data_catalog.get_rasterdataset("gswo", geom=mod.region, buffer=10)
gswo_mask = gswo.raster.reproject_like(mod.grid.data, method="max") <= 5
da_hmax = da_hmax.where(gswo_mask)
else:
print("Skipping GSWO raster masking for quadtree output; plotting hmax directly.")
da_hmax.attrs.update(long_name="flood depth", unit="m")
⚠️ NOTE To read GSWO data for another region, add the source to your data catalog (for example, with mod.data_catalog.from_yml(‘/path/to/data_catalog.yml’)). This raster-mask step applies only to regular grids.
Step 4: Plot maximum water depth#
Plot the maximum water depth, with model geometries such as discharge source points and observation gauges where available.
[7]:
# minimum flood depth [m] to plot
hmin = 0.05
da_hmax = da_hmax.where(da_hmax > hmin)
[8]:
fig, ax = mod.plot_basemap(
fn_out=None,
figsize=(8, 6),
variable=da_hmax,
plot_bounds=False,
plot_geoms=False,
bmap="sat",
zoomlevel=12,
vmin=0,
vmax=2.0,
cmap=plt.cm.viridis,
cbar_kwargs={"shrink": 0.6, "anchor": (0, 0)},
)
ax.set_title(f"SFINCS maximum water depth")
# plt.savefig(join(mod.root, 'figs', 'hmax.png'), dpi=225, bbox_inches="tight")
[8]:
Text(0.5, 1.0, 'SFINCS maximum water depth')