{ "cells": [ { "cell_type": "markdown", "id": "511a4bfe", "metadata": { "papermill": { "duration": 0.003419, "end_time": "2025-06-18T09:44:38.028762", "exception": false, "start_time": "2025-06-18T09:44:38.025343", "status": "completed" }, "tags": [] }, "source": [ "# Pluvial flood risk\n", "\n", "This example shows a workflow to derive pluvial flood risk using the **SFINCS** and **Delft-FIAT** models. The starting point is a user defined region and data catalog. Rainfall IDF curves from the GPEX dataset are translated into design events (hyetographs) for different return periods and used to simulate the flood hazard maps. The hazard maps are combined with exposure and impact data to derive risk." ] }, { "cell_type": "code", "execution_count": 1, "id": "abbe6566", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:38.035658Z", "iopub.status.busy": "2025-06-18T09:44:38.035272Z", "iopub.status.idle": "2025-06-18T09:44:41.467880Z", "shell.execute_reply": "2025-06-18T09:44:41.467389Z" }, "papermill": { "duration": 3.436812, "end_time": "2025-06-18T09:44:41.468671", "exception": false, "start_time": "2025-06-18T09:44:38.031859", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - log - hydroflows version: 0.1.0\n" ] } ], "source": [ "# Import packages and setup logging\n", "from pathlib import Path\n", "\n", "from hydroflows import Workflow, WorkflowConfig\n", "from hydroflows.log import setuplog\n", "from hydroflows.methods import fiat, flood_adapt, rainfall, sfincs\n", "from hydroflows.methods.utils.example_data import fetch_data\n", "\n", "logger = setuplog(level=\"INFO\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "fdfdb74d", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:41.476283Z", "iopub.status.busy": "2025-06-18T09:44:41.475796Z", "iopub.status.idle": "2025-06-18T09:44:41.479091Z", "shell.execute_reply": "2025-06-18T09:44:41.478657Z" }, "papermill": { "duration": 0.007747, "end_time": "2025-06-18T09:44:41.479855", "exception": false, "start_time": "2025-06-18T09:44:41.472108", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# General setup of workflow\n", "name = \"pluvial_risk\"\n", "pwd = Path().resolve() # Get the current file location\n", "case_root = Path(pwd, \"cases\", name) # output directory\n", "pwd_rel = \"../../\" # relative path from the case directory to the current file" ] }, { "cell_type": "markdown", "id": "67cc059e", "metadata": { "papermill": { "duration": 0.003064, "end_time": "2025-06-18T09:44:41.485989", "exception": false, "start_time": "2025-06-18T09:44:41.482925", "status": "completed" }, "tags": [] }, "source": [ "## Workflow inputs\n", "\n", "The example requires the following inputs which are provided via a configuration file:\n", "- a user defined region that can be used to delineate the SFINCS model domain\n", "- a data catalog file describing all input datasets. Here we fetch some test datasets for a region in Northern Italy. \n", "- The [GPEX IDF dataset](https://data.4tu.nl/articles/dataset/GPEX_Global_Precipitation_EXtremes/12764429/4). Note that rainfall timeseries data can also be used to derive design events.\n", "- HydroMT configuration files for both models\n", "- model executables (docker is also possible for SFINCS)" ] }, { "cell_type": "code", "execution_count": 3, "id": "b55fe5d9", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:41.492879Z", "iopub.status.busy": "2025-06-18T09:44:41.492515Z", "iopub.status.idle": "2025-06-18T09:44:42.276383Z", "shell.execute_reply": "2025-06-18T09:44:42.275806Z" }, "papermill": { "duration": 0.788186, "end_time": "2025-06-18T09:44:42.277235", "exception": false, "start_time": "2025-06-18T09:44:41.489049", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Fetch the test global-data data catalog \n", "cache_dir = fetch_data(data=\"global-data\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "623c1273", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.284911Z", "iopub.status.busy": "2025-06-18T09:44:42.284416Z", "iopub.status.idle": "2025-06-18T09:44:42.288323Z", "shell.execute_reply": "2025-06-18T09:44:42.287799Z" }, "papermill": { "duration": 0.008359, "end_time": "2025-06-18T09:44:42.289104", "exception": false, "start_time": "2025-06-18T09:44:42.280745", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Setup the config file\n", "config = WorkflowConfig(\n", " # general settings\n", " region=Path(pwd_rel, \"data/build/region.geojson\"),\n", " catalog_path=Path(cache_dir, \"data_catalog.yml\"),\n", " plot_fig=True,\n", " # sfincs settings\n", " hydromt_sfincs_config=Path(pwd_rel, \"hydromt_config/sfincs_config.yml\"),\n", " sfincs_exe=Path(pwd_rel, \"bin/sfincs_v2.1.1/sfincs.exe\"),\n", " # fiat settings\n", " hydromt_fiat_config=Path(pwd_rel, \"hydromt_config/fiat_config.yml\"),\n", " fiat_exe=Path(pwd_rel, \"bin/fiat_v0.2.0/fiat.exe\"),\n", " risk=True,\n", " # rainfall and design events settings\n", " start_date=\"2000-01-01\",\n", " end_date=\"2021-12-31\",\n", " rps=[2, 5, 10, 50, 100],\n", " # delta temp climate scenarios\n", " dt_scenarios={\"present\": 0, \"rcp85_2050\": 1.5}, \n", ")\n" ] }, { "cell_type": "markdown", "id": "c085edea", "metadata": { "papermill": { "duration": 0.00311, "end_time": "2025-06-18T09:44:42.295307", "exception": false, "start_time": "2025-06-18T09:44:42.292197", "status": "completed" }, "tags": [] }, "source": [ "## Create the workflow" ] }, { "cell_type": "code", "execution_count": 5, "id": "42d022b8", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.302228Z", "iopub.status.busy": "2025-06-18T09:44:42.302072Z", "iopub.status.idle": "2025-06-18T09:44:42.304796Z", "shell.execute_reply": "2025-06-18T09:44:42.304305Z" }, "papermill": { "duration": 0.007024, "end_time": "2025-06-18T09:44:42.305509", "exception": false, "start_time": "2025-06-18T09:44:42.298485", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# create and empty workflow\n", "wf = Workflow(name=name, config=config, root=case_root)" ] }, { "cell_type": "markdown", "id": "2f0df5e1", "metadata": { "papermill": { "duration": 0.003055, "end_time": "2025-06-18T09:44:42.311766", "exception": false, "start_time": "2025-06-18T09:44:42.308711", "status": "completed" }, "tags": [] }, "source": [ "### Build models\n", "\n", "In this section we build a model cascade and make sure these are configured correctly for offline coupling, i.e. Delft-FIAT uses the same ground elevation as SFINCS. Note that you can also skip these steps and use your own models instead." ] }, { "cell_type": "markdown", "id": "c5b312de", "metadata": { "papermill": { "duration": 0.003123, "end_time": "2025-06-18T09:44:42.317997", "exception": false, "start_time": "2025-06-18T09:44:42.314874", "status": "completed" }, "tags": [] }, "source": [ "First, we build a **SFINCS** model for the user defined region using. \n", " - setting from the hydromt_sfincs_config, see the [HydroMT-SFINCS docs](https://deltares.github.io/hydromt_sfincs/latest/) for more info.\n", " - data from the catalog_path, see the [HydroMT docs](https://deltares.github.io/hydromt/v0.10.0/user_guide/data_prepare_cat.html) for more info." ] }, { "cell_type": "code", "execution_count": 6, "id": "ac237958", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.324727Z", "iopub.status.busy": "2025-06-18T09:44:42.324577Z", "iopub.status.idle": "2025-06-18T09:44:42.330937Z", "shell.execute_reply": "2025-06-18T09:44:42.330432Z" }, "papermill": { "duration": 0.010634, "end_time": "2025-06-18T09:44:42.331716", "exception": false, "start_time": "2025-06-18T09:44:42.321082", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_build, method=sfincs_build, runs=1)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Build a SFINCS model\n", "sfincs_build = sfincs.SfincsBuild(\n", " region=wf.get_ref(\"$config.region\"),\n", " sfincs_root=\"models/sfincs\",\n", " config=wf.get_ref(\"$config.hydromt_sfincs_config\"),\n", " catalog_path=wf.get_ref(\"$config.catalog_path\"),\n", " plot_fig=wf.get_ref(\"$config.plot_fig\"),\n", " subgrid_output=True, # save subgrid output for subsequent methods\n", ")\n", "wf.create_rule(sfincs_build, rule_id=\"sfincs_build\")" ] }, { "cell_type": "markdown", "id": "7cdc1c03", "metadata": { "papermill": { "duration": 0.003152, "end_time": "2025-06-18T09:44:42.338162", "exception": false, "start_time": "2025-06-18T09:44:42.335010", "status": "completed" }, "tags": [] }, "source": [ "Next, we build a **FIAT** model using:\n", "- the sfincs_build output for the model region and ground elevation\n", "- settings from the hydromt_fiat_config, see the [hydromt_fiat docs](https://deltares.github.io/hydromt_fiat/latest/)\n", "- data from the data catalog" ] }, { "cell_type": "code", "execution_count": 7, "id": "a4a39619", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.345370Z", "iopub.status.busy": "2025-06-18T09:44:42.345044Z", "iopub.status.idle": "2025-06-18T09:44:42.349737Z", "shell.execute_reply": "2025-06-18T09:44:42.349338Z" }, "papermill": { "duration": 0.009109, "end_time": "2025-06-18T09:44:42.350439", "exception": false, "start_time": "2025-06-18T09:44:42.341330", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_build, method=fiat_build, runs=1)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Build a Delft-FIAT model\n", "fiat_build = fiat.FIATBuild(\n", " region=sfincs_build.output.sfincs_region,\n", " ground_elevation=sfincs_build.output.sfincs_subgrid_dep,\n", " fiat_root=\"models/fiat\",\n", " catalog_path=wf.get_ref(\"$config.catalog_path\"),\n", " config=wf.get_ref(\"$config.hydromt_fiat_config\"),\n", ")\n", "wf.create_rule(fiat_build, rule_id=\"fiat_build\")" ] }, { "cell_type": "markdown", "id": "62d0cfbd", "metadata": { "papermill": { "duration": 0.003269, "end_time": "2025-06-18T09:44:42.357027", "exception": false, "start_time": "2025-06-18T09:44:42.353758", "status": "completed" }, "tags": [] }, "source": [ "### Derive pluvial design events" ] }, { "cell_type": "markdown", "id": "74cb00de", "metadata": { "papermill": { "duration": 0.003197, "end_time": "2025-06-18T09:44:42.363582", "exception": false, "start_time": "2025-06-18T09:44:42.360385", "status": "completed" }, "tags": [] }, "source": [ "Here, we define pluvial design events from GPEX IDF data using the alternating block method.\n", "We derive the IDF curves for the centroid of the SFINCS model region.\n", "Alternatively, you can use the `PluvialDesignEvents` class to define pluvial events from a rainfall time series." ] }, { "cell_type": "code", "execution_count": 8, "id": "0a350cac", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.371002Z", "iopub.status.busy": "2025-06-18T09:44:42.370624Z", "iopub.status.idle": "2025-06-18T09:44:42.375682Z", "shell.execute_reply": "2025-06-18T09:44:42.375302Z" }, "papermill": { "duration": 0.00955, "end_time": "2025-06-18T09:44:42.376415", "exception": false, "start_time": "2025-06-18T09:44:42.366865", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - wildcards - Added wildcard 'events' with values: ['p_event_rp002', 'p_event_rp005', 'p_event_rp010', 'p_event_rp050', 'p_event_rp100']\n" ] }, { "data": { "text/plain": [ "Rule(id=pluvial_events, method=pluvial_design_events_GPEX, runs=1, expand=['events'])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pluvial_events = rainfall.PluvialDesignEventsGPEX(\n", " gpex_nc=Path(cache_dir, \"gpex.nc\"), \n", " region=sfincs_build.output.sfincs_region,\n", " event_root=\"data/events/default\",\n", " rps=wf.get_ref(\"$config.rps\"),\n", " wildcard=\"events\", # wildcard to use for the pluvial events\n", ")\n", "\n", "# Note that a new \"events\" wildcard is created for the events\n", "wf.create_rule(pluvial_events, rule_id=\"pluvial_events\")\n" ] }, { "cell_type": "markdown", "id": "9a66a4db", "metadata": { "papermill": { "duration": 0.00344, "end_time": "2025-06-18T09:44:42.383430", "exception": false, "start_time": "2025-06-18T09:44:42.379990", "status": "completed" }, "tags": [] }, "source": [ "The events are scaled for future climate predictions based on the Clausius-Clapeyron relation. A new wildcard for scenarios is introduced develop hazard maps for each event per scenario." ] }, { "cell_type": "code", "execution_count": 9, "id": "983d60a5", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.391253Z", "iopub.status.busy": "2025-06-18T09:44:42.391091Z", "iopub.status.idle": "2025-06-18T09:44:42.397038Z", "shell.execute_reply": "2025-06-18T09:44:42.396609Z" }, "papermill": { "duration": 0.010621, "end_time": "2025-06-18T09:44:42.397762", "exception": false, "start_time": "2025-06-18T09:44:42.387141", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - wildcards - Added wildcard 'event' with values: ['p_event_rp002', 'p_event_rp005', 'p_event_rp010', 'p_event_rp050', 'p_event_rp100']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - wildcards - Added wildcard 'scenario' with values: ['present', 'rcp85_2050']\n" ] }, { "data": { "text/plain": [ "Rule(id=future_pluvial_events, method=future_climate_rainfall, runs=1, expand=['scenario', 'event'])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "future_pluvial_events = rainfall.FutureClimateRainfall(\n", " scenarios=wf.get_ref(\"$config.dt_scenarios\"),\n", " event_set_yaml=pluvial_events.output.event_set_yaml,\n", " event_names=pluvial_events.params.event_names,\n", " event_root=\"data/events\",\n", " event_wildcard=\"event\",\n", " scenario_wildcard=\"scenario\",\n", ")\n", "wf.create_rule(future_pluvial_events, rule_id=\"future_pluvial_events\")" ] }, { "cell_type": "markdown", "id": "96cfaee7", "metadata": { "papermill": { "duration": 0.003643, "end_time": "2025-06-18T09:44:42.405060", "exception": false, "start_time": "2025-06-18T09:44:42.401417", "status": "completed" }, "tags": [] }, "source": [ "### Derive flood hazard" ] }, { "cell_type": "markdown", "id": "bfcd4892", "metadata": { "papermill": { "duration": 0.003565, "end_time": "2025-06-18T09:44:42.412259", "exception": false, "start_time": "2025-06-18T09:44:42.408694", "status": "completed" }, "tags": [] }, "source": [ "To derive flood hazard maps for each event, we \n", "1. Update the SFINCS model using the rainfall event timeseries. This will create new SFINCS instances for each event.\n", "2. Run the SFINCS model. This will create simulated water levels for each event.\n", "3. Postprocess the SFINCS output. This will postprocess the SFINCS results to a regular grid of maximum water levels.\n", "4. Optionally, downscale the SFINCS output. This will downscale the max simulated SFINCS water levels to a high-res flood depth map.\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "6a380caa", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.420452Z", "iopub.status.busy": "2025-06-18T09:44:42.420032Z", "iopub.status.idle": "2025-06-18T09:44:42.426494Z", "shell.execute_reply": "2025-06-18T09:44:42.426081Z" }, "papermill": { "duration": 0.011253, "end_time": "2025-06-18T09:44:42.427214", "exception": false, "start_time": "2025-06-18T09:44:42.415961", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_update, method=sfincs_update_forcing, runs=10, repeat=['scenario', 'event'])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Update the SFINCS model with pluvial events\n", "sfincs_update = sfincs.SfincsUpdateForcing(\n", " sfincs_inp=sfincs_build.output.sfincs_inp,\n", " event_yaml=future_pluvial_events.output.future_event_yaml,\n", " output_dir=sfincs_build.output.sfincs_inp.parent/\"sim_{scenario}\"/\"{event}\"\n", ")\n", "wf.create_rule(sfincs_update, rule_id=\"sfincs_update\")\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "f72f5e63", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.435664Z", "iopub.status.busy": "2025-06-18T09:44:42.435320Z", "iopub.status.idle": "2025-06-18T09:44:42.440980Z", "shell.execute_reply": "2025-06-18T09:44:42.440488Z" }, "papermill": { "duration": 0.010692, "end_time": "2025-06-18T09:44:42.441758", "exception": false, "start_time": "2025-06-18T09:44:42.431066", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_run, method=sfincs_run, runs=10, repeat=['scenario', 'event'])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Run the SFINCS model for each pluvial event\n", "sfincs_run = sfincs.SfincsRun(\n", " sfincs_inp=sfincs_update.output.sfincs_out_inp,\n", " sfincs_exe=wf.get_ref(\"$config.sfincs_exe\"),\n", " run_method=\"exe\", # alternatively use \"docker\" to run in a docker container\n", ")\n", "wf.create_rule(sfincs_run, rule_id=\"sfincs_run\")\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "bc3239cf", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.450016Z", "iopub.status.busy": "2025-06-18T09:44:42.449855Z", "iopub.status.idle": "2025-06-18T09:44:42.455352Z", "shell.execute_reply": "2025-06-18T09:44:42.454865Z" }, "papermill": { "duration": 0.010452, "end_time": "2025-06-18T09:44:42.456070", "exception": false, "start_time": "2025-06-18T09:44:42.445618", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_post, method=sfincs_postprocess, runs=10, repeat=['scenario', 'event'])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Postprocesses SFINCS results\n", "sfincs_post = sfincs.SfincsPostprocess(\n", " sfincs_map=sfincs_run.output.sfincs_map,\n", " output_root=sfincs_run.output.sfincs_map.parent\n", ")\n", "wf.create_rule(sfincs_post, rule_id=\"sfincs_post\")\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "c80b5f5c", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.464780Z", "iopub.status.busy": "2025-06-18T09:44:42.464353Z", "iopub.status.idle": "2025-06-18T09:44:42.467160Z", "shell.execute_reply": "2025-06-18T09:44:42.466755Z" }, "papermill": { "duration": 0.007901, "end_time": "2025-06-18T09:44:42.467884", "exception": false, "start_time": "2025-06-18T09:44:42.459983", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Downscale the SFINCS waterlevels to high-resolution water\n", "sfincs_downscale = sfincs.SfincsDownscale(\n", " sfincs_map=sfincs_run.output.sfincs_map,\n", " sfincs_subgrid_dep=sfincs_build.output.sfincs_subgrid_dep,\n", " output_root=\"output/hazard/{scenario}/{event}\",\n", ")" ] }, { "cell_type": "markdown", "id": "9b211b03", "metadata": { "papermill": { "duration": 0.003929, "end_time": "2025-06-18T09:44:42.475727", "exception": false, "start_time": "2025-06-18T09:44:42.471798", "status": "completed" }, "tags": [] }, "source": [ "### Derive flood risk" ] }, { "cell_type": "markdown", "id": "d9b86b02", "metadata": { "papermill": { "duration": 0.003954, "end_time": "2025-06-18T09:44:42.483562", "exception": false, "start_time": "2025-06-18T09:44:42.479608", "status": "completed" }, "tags": [] }, "source": [ "To calculate flood risk, we \n", "- Update Delft-FIAT with *all pluvial events* which are combined in an event set. This will create a new Delft-FIAT instance for the event set.\n", "- Run Delft-FIAT to calculate flood impact and risk. This will create impact and risk data at the individual and aggregated asset level.\n", "- Visualize the risk results at the aggregated asset level.\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "0a1409d7", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.492315Z", "iopub.status.busy": "2025-06-18T09:44:42.492000Z", "iopub.status.idle": "2025-06-18T09:44:42.497059Z", "shell.execute_reply": "2025-06-18T09:44:42.496636Z" }, "papermill": { "duration": 0.010235, "end_time": "2025-06-18T09:44:42.497761", "exception": false, "start_time": "2025-06-18T09:44:42.487526", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_update, method=fiat_update_hazard, runs=2, repeat=['scenario'], reduce=['event'])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Update FIAT hazard forcing with the pluvial eventset to compute pluvial flood risk\n", "fiat_update = fiat.FIATUpdateHazard(\n", " fiat_cfg=fiat_build.output.fiat_cfg,\n", " event_set_yaml=future_pluvial_events.output.future_event_set_yaml,\n", " map_type=\"water_level\",\n", " hazard_maps=sfincs_post.output.sfincs_zsmax,\n", " risk=wf.get_ref(\"$config.risk\"),\n", " output_dir=fiat_build.output.fiat_cfg.parent/\"sim_{scenario}\"\n", ")\n", "wf.create_rule(fiat_update, rule_id=\"fiat_update\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "3016484d", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.506276Z", "iopub.status.busy": "2025-06-18T09:44:42.506130Z", "iopub.status.idle": "2025-06-18T09:44:42.510455Z", "shell.execute_reply": "2025-06-18T09:44:42.509972Z" }, "papermill": { "duration": 0.009477, "end_time": "2025-06-18T09:44:42.511223", "exception": false, "start_time": "2025-06-18T09:44:42.501746", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_run, method=fiat_run, runs=2, repeat=['scenario'])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Run FIAT to compute pluvial flood risk\n", "fiat_run = fiat.FIATRun(\n", " fiat_cfg=fiat_update.output.fiat_out_cfg,\n", " fiat_exe=wf.get_ref(\"$config.fiat_exe\"),\n", " run_method=\"exe\", \n", ")\n", "wf.create_rule(fiat_run, rule_id=\"fiat_run\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "02b23311", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.520030Z", "iopub.status.busy": "2025-06-18T09:44:42.519881Z", "iopub.status.idle": "2025-06-18T09:44:42.524324Z", "shell.execute_reply": "2025-06-18T09:44:42.523856Z" }, "papermill": { "duration": 0.00976, "end_time": "2025-06-18T09:44:42.525113", "exception": false, "start_time": "2025-06-18T09:44:42.515353", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_visualize, method=fiat_visualize, runs=2, repeat=['scenario'])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Visualize Fiat \n", "fiat_visualize = fiat.FIATVisualize(\n", " fiat_cfg=fiat_update.output.fiat_out_cfg,\n", " fiat_output_csv=fiat_run.output.fiat_out_csv,\n", " spatial_joins_cfg=fiat_build.output.spatial_joins_cfg, # aggregation level for visualization\n", " output_dir=\"output/risk/{scenario}\",\n", ")\n", "wf.create_rule(fiat_visualize, rule_id=\"fiat_visualize\")" ] }, { "cell_type": "markdown", "id": "2dfa9e86", "metadata": { "papermill": { "duration": 0.004111, "end_time": "2025-06-18T09:44:42.533346", "exception": false, "start_time": "2025-06-18T09:44:42.529235", "status": "completed" }, "tags": [] }, "source": [ "### Prepare FloodAdapt database input" ] }, { "cell_type": "markdown", "id": "9fddfee6", "metadata": { "papermill": { "duration": 0.004074, "end_time": "2025-06-18T09:44:42.541504", "exception": false, "start_time": "2025-06-18T09:44:42.537430", "status": "completed" }, "tags": [] }, "source": [ "Optionally, a FloodAdapt instance can be created using the SFINCS and Delft-FIAT models and the derived pluvial event set.\n", "The `SetupFloodAdapt` method prepares the models and a configuration file which can readily be used by the FloodAdapt database builder. " ] }, { "cell_type": "code", "execution_count": 17, "id": "83ca9ec8", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.550323Z", "iopub.status.busy": "2025-06-18T09:44:42.550169Z", "iopub.status.idle": "2025-06-18T09:44:42.555432Z", "shell.execute_reply": "2025-06-18T09:44:42.554952Z" }, "papermill": { "duration": 0.010531, "end_time": "2025-06-18T09:44:42.556135", "exception": false, "start_time": "2025-06-18T09:44:42.545604", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=setup_floodadapt, method=setup_flood_adapt, runs=1)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# %% Prepare Sfincs Models for FloodAdapt\n", "prep_sfincs_run = flood_adapt.PrepSfincsModels(\n", " sfincs_inp=sfincs_build.output.sfincs_inp,\n", ")\n", "wf.create_rule(prep_sfincs_run, rule_id=\"prep_sfincs_model\")\n", "\n", "# %% Prepare FloodAdapt\n", "setup_floodadapt = flood_adapt.SetupFloodAdapt(\n", " fiat_cfg=fiat_build.output.fiat_cfg,\n", " sfincs_inp=prep_sfincs_run.output.sfincs_out_inp,\n", " db_name= \"Pluvial_risk\",\n", " description= \"This is a pluvial risk event set database\",\n", " event_set_yaml=pluvial_events.output.event_set_yaml,\n", " output_dir=Path(\"output/floodadapt\"),\n", ")\n", "wf.create_rule(setup_floodadapt, rule_id=\"setup_floodadapt\")\n" ] }, { "cell_type": "markdown", "id": "7c7da44e", "metadata": { "papermill": { "duration": 0.004214, "end_time": "2025-06-18T09:44:42.564616", "exception": false, "start_time": "2025-06-18T09:44:42.560402", "status": "completed" }, "tags": [] }, "source": [ "## Visualize and execute the workflow\n", "\n", "To inspect the workflow we can plot the rulegraph which shows all rules their dependencies.\n", "The nodes are colored based on the type, for instance the red nodes show the result rules." ] }, { "cell_type": "code", "execution_count": 18, "id": "73568387", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.573621Z", "iopub.status.busy": "2025-06-18T09:44:42.573473Z", "iopub.status.idle": "2025-06-18T09:44:42.607721Z", "shell.execute_reply": "2025-06-18T09:44:42.607310Z" }, "papermill": { "duration": 0.03967, "end_time": "2025-06-18T09:44:42.608466", "exception": false, "start_time": "2025-06-18T09:44:42.568796", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "pluvial_risk\n", "\n", "\n", "\n", "sfincs_build\n", "\n", "sfincs_build\n", " runs=1\n", "\n", "\n", "\n", "prep_sfincs_model\n", "\n", "prep_sfincs_model\n", " runs=1\n", "\n", "\n", "\n", "sfincs_build->prep_sfincs_model\n", "\n", "\n", "\n", "\n", "\n", "pluvial_events\n", "\n", "pluvial_events\n", " runs=1\n", " expand=['events']\n", "\n", "\n", "\n", "sfincs_build->pluvial_events\n", "\n", "\n", "\n", "\n", "\n", "fiat_build\n", "\n", "fiat_build\n", " runs=1\n", "\n", "\n", "\n", "sfincs_build->fiat_build\n", "\n", "\n", "\n", "\n", "\n", "sfincs_update\n", "\n", "sfincs_update\n", " runs=10\n", " repeat=['scenario'\n", " 'event']\n", "\n", "\n", "\n", "sfincs_build->sfincs_update\n", "\n", "\n", "\n", "\n", "\n", "setup_floodadapt\n", "\n", "setup_floodadapt\n", " runs=1\n", "\n", "\n", "\n", "prep_sfincs_model->setup_floodadapt\n", "\n", "\n", "\n", "\n", "\n", "future_pluvial_events\n", "\n", "future_pluvial_events\n", " runs=1\n", " expand=['scenario'\n", " 'event']\n", "\n", "\n", "\n", "pluvial_events->future_pluvial_events\n", "\n", "\n", "\n", "\n", "\n", "pluvial_events->setup_floodadapt\n", "\n", "\n", "\n", "\n", "\n", "future_pluvial_events->sfincs_update\n", "\n", "\n", "\n", "\n", "\n", "fiat_update\n", "\n", "fiat_update\n", " runs=2\n", " repeat=['scenario']\n", " reduce=['event']\n", "\n", "\n", "\n", "future_pluvial_events->fiat_update\n", "\n", "\n", "\n", "\n", "\n", "fiat_build->setup_floodadapt\n", "\n", "\n", "\n", "\n", "\n", "fiat_build->fiat_update\n", "\n", "\n", "\n", "\n", "\n", "fiat_visualize\n", "\n", "fiat_visualize\n", " runs=2\n", " repeat=['scenario']\n", "\n", "\n", "\n", "fiat_build->fiat_visualize\n", "\n", "\n", "\n", "\n", "\n", "sfincs_run\n", "\n", "sfincs_run\n", " runs=10\n", " repeat=['scenario'\n", " 'event']\n", "\n", "\n", "\n", "sfincs_update->sfincs_run\n", "\n", "\n", "\n", "\n", "\n", "sfincs_post\n", "\n", "sfincs_post\n", " runs=10\n", " repeat=['scenario'\n", " 'event']\n", "\n", "\n", "\n", "sfincs_run->sfincs_post\n", "\n", "\n", "\n", "\n", "\n", "sfincs_post->fiat_update\n", "\n", "\n", "\n", "\n", "\n", "fiat_run\n", "\n", "fiat_run\n", " runs=2\n", " repeat=['scenario']\n", "\n", "\n", "\n", "fiat_update->fiat_run\n", "\n", "\n", "\n", "\n", "\n", "fiat_update->fiat_visualize\n", "\n", "\n", "\n", "\n", "\n", "fiat_run->fiat_visualize\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# plot the rulegraph using graphviz\n", "wf.plot_rulegraph(filename=\"rulegraph.svg\", plot_rule_attrs=True)" ] }, { "cell_type": "code", "execution_count": 19, "id": "de734cbe", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.618565Z", "iopub.status.busy": "2025-06-18T09:44:42.618247Z", "iopub.status.idle": "2025-06-18T09:44:42.628894Z", "shell.execute_reply": "2025-06-18T09:44:42.628511Z" }, "papermill": { "duration": 0.016533, "end_time": "2025-06-18T09:44:42.629672", "exception": false, "start_time": "2025-06-18T09:44:42.613139", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 1/12: sfincs_build (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 2/12: prep_sfincs_model (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 3/12: pluvial_events (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 4/12: future_pluvial_events (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 5/12: fiat_build (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 6/12: setup_floodadapt (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 7/12: sfincs_update (10 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 8/12: sfincs_run (10 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 9/12: sfincs_post (10 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 10/12: fiat_update (2 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 11/12: fiat_run (2 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 12/12: fiat_visualize (2 runs)\n" ] } ], "source": [ "# dryrun workflow. Make sure no warnings are raised\n", "wf.dryrun()\n" ] }, { "cell_type": "markdown", "id": "9fc405fe", "metadata": { "papermill": { "duration": 0.004923, "end_time": "2025-06-18T09:44:42.639628", "exception": false, "start_time": "2025-06-18T09:44:42.634705", "status": "completed" }, "tags": [] }, "source": [ "The workflow can be executed using HydroFlows or a workflow engine. \n", "To run the workflow in HydroFlows use ``wf.run()``. \n", "To run the workflow with SnakeMake (preferred) use ``wf.to_snakemake()`` to create a snakemake file, see below.\n", "You can then use the Snakemake CLI to execute the workflow, see the [snakemake documentation](https://snakemake.readthedocs.io/en/stable/executing/cli.html)" ] }, { "cell_type": "code", "execution_count": 20, "id": "065782ab", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.650042Z", "iopub.status.busy": "2025-06-18T09:44:42.649868Z", "iopub.status.idle": "2025-06-18T09:44:42.666641Z", "shell.execute_reply": "2025-06-18T09:44:42.666248Z" }, "papermill": { "duration": 0.022892, "end_time": "2025-06-18T09:44:42.667375", "exception": false, "start_time": "2025-06-18T09:44:42.644483", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cases/pluvial_risk:\n", "- Snakefile\n", "- rulegraph.svg\n", "- Snakefile.config.yml\n" ] } ], "source": [ "# Write the workflow to a Snakefile\n", "wf.to_snakemake()\n", "\n", "# show the files in the case directory\n", "print(f\"{wf.root.relative_to(pwd)}:\")\n", "for f in wf.root.iterdir():\n", " if f.is_file():\n", " print(f\"- {f.name}\")" ] }, { "cell_type": "code", "execution_count": 21, "id": "e664a3f6", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:42.678135Z", "iopub.status.busy": "2025-06-18T09:44:42.677783Z", "iopub.status.idle": "2025-06-18T09:44:42.680002Z", "shell.execute_reply": "2025-06-18T09:44:42.679608Z" }, "papermill": { "duration": 0.008339, "end_time": "2025-06-18T09:44:42.680671", "exception": false, "start_time": "2025-06-18T09:44:42.672332", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# uncomment to run the workflow with snakemake\n", "# import subprocess\n", "# subprocess.run([\"snakemake\", \"-c\", \"1\"], cwd=wf.root)" ] } ], "metadata": { "kernelspec": { "display_name": "full", "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.12" }, "papermill": { "default_parameters": {}, "duration": 6.255351, "end_time": "2025-06-18T09:44:43.502689", "environment_variables": {}, "exception": null, "input_path": "/home/runner/work/HydroFlows/HydroFlows/docs/../examples/pluvial_risk.ipynb", "output_path": "/home/runner/work/HydroFlows/HydroFlows/docs/_examples/pluvial_risk.ipynb", "parameters": {}, "start_time": "2025-06-18T09:44:37.247338", "version": "2.6.0" } }, "nbformat": 4, "nbformat_minor": 5 }