{ "cells": [ { "cell_type": "markdown", "id": "1648cb18", "metadata": { "papermill": { "duration": 0.003515, "end_time": "2025-06-18T09:44:26.092210", "exception": false, "start_time": "2025-06-18T09:44:26.088695", "status": "completed" }, "tags": [] }, "source": [ "# Coastal flood risk\n", "\n", "This example shows a workflow to derive coastal flood risk using the **SFINCS** and **Delft-FIAT** models. The starting point is a user defined region and data catalog. Coastal storm tide hydrographs for different return periods are derived from the **GTSM** reanalysis dataset and used to simulate the flood hazard maps. Note that wave setup / runup are not considered. The hazard maps are combined with exposure and impact data to derive risk." ] }, { "cell_type": "code", "execution_count": 1, "id": "9fd5eff4", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:26.098867Z", "iopub.status.busy": "2025-06-18T09:44:26.098687Z", "iopub.status.idle": "2025-06-18T09:44:29.255023Z", "shell.execute_reply": "2025-06-18T09:44:29.254454Z" }, "papermill": { "duration": 3.160641, "end_time": "2025-06-18T09:44:29.255914", "exception": false, "start_time": "2025-06-18T09:44:26.095273", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - log - hydroflows version: 0.1.0\n" ] } ], "source": [ "# Import modules\n", "from pathlib import Path\n", "\n", "from hydroflows import Workflow, WorkflowConfig\n", "from hydroflows.log import setuplog\n", "from hydroflows.methods import coastal, fiat, sfincs\n", "from hydroflows.methods.utils.example_data import fetch_data\n", "\n", "logger = setuplog(level=\"INFO\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "183dda7b", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:29.263603Z", "iopub.status.busy": "2025-06-18T09:44:29.262962Z", "iopub.status.idle": "2025-06-18T09:44:29.266293Z", "shell.execute_reply": "2025-06-18T09:44:29.265793Z" }, "papermill": { "duration": 0.007772, "end_time": "2025-06-18T09:44:29.267073", "exception": false, "start_time": "2025-06-18T09:44:29.259301", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Define case name and root directory\n", "name = \"coastal_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\n" ] }, { "cell_type": "markdown", "id": "a1c087e4", "metadata": { "papermill": { "duration": 0.002994, "end_time": "2025-06-18T09:44:29.273077", "exception": false, "start_time": "2025-06-18T09:44:29.270083", "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 including the [GTSM reanalysis](https://www.deltares.nl/en/expertise/projects/global-modelling-of-tides-and-storm-surges) dataset. Here we fetch some test datasets for a region in Northern Italy. \n", "- HydroMT configuration files for both models.\n", "- model executables (docker is also possible for SFINCS)" ] }, { "cell_type": "code", "execution_count": 3, "id": "f30d94b2", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:29.279841Z", "iopub.status.busy": "2025-06-18T09:44:29.279495Z", "iopub.status.idle": "2025-06-18T09:44:30.062213Z", "shell.execute_reply": "2025-06-18T09:44:30.061691Z" }, "papermill": { "duration": 0.787071, "end_time": "2025-06-18T09:44:30.063173", "exception": false, "start_time": "2025-06-18T09:44:29.276102", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Fetch the global build data\n", "cache_dir = fetch_data(data=\"global-data\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "aa7a9097", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.070366Z", "iopub.status.busy": "2025-06-18T09:44:30.070207Z", "iopub.status.idle": "2025-06-18T09:44:30.073604Z", "shell.execute_reply": "2025-06-18T09:44:30.073204Z" }, "papermill": { "duration": 0.007731, "end_time": "2025-06-18T09:44:30.074358", "exception": false, "start_time": "2025-06-18T09:44:30.066627", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Setup the configuration\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", " # coastal time series and design events\n", " start_time=\"2014-01-01\",\n", " end_time=\"2021-12-31\",\n", " rps=[2, 5, 10, 50, 100],\n", " # future climate SLR scenarios\n", " slr_scenarios={\"present\": 0, \"rcp85_2050\": 0.2}, \n", ")" ] }, { "cell_type": "markdown", "id": "e4d7b644", "metadata": { "papermill": { "duration": 0.003139, "end_time": "2025-06-18T09:44:30.080607", "exception": false, "start_time": "2025-06-18T09:44:30.077468", "status": "completed" }, "tags": [] }, "source": [ "## Create the workflow" ] }, { "cell_type": "code", "execution_count": 5, "id": "7ccda29a", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.087368Z", "iopub.status.busy": "2025-06-18T09:44:30.087215Z", "iopub.status.idle": "2025-06-18T09:44:30.089739Z", "shell.execute_reply": "2025-06-18T09:44:30.089353Z" }, "papermill": { "duration": 0.006741, "end_time": "2025-06-18T09:44:30.090464", "exception": false, "start_time": "2025-06-18T09:44:30.083723", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "wf = Workflow(config=config, name=name, root=case_root)\n" ] }, { "cell_type": "markdown", "id": "c4ade15b", "metadata": { "papermill": { "duration": 0.003028, "end_time": "2025-06-18T09:44:30.096652", "exception": false, "start_time": "2025-06-18T09:44:30.093624", "status": "completed" }, "tags": [] }, "source": [ "### Build models" ] }, { "cell_type": "markdown", "id": "4a99bc2e", "metadata": { "papermill": { "duration": 0.003055, "end_time": "2025-06-18T09:44:30.102767", "exception": false, "start_time": "2025-06-18T09:44:30.099712", "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": "270213ba", "metadata": { "papermill": { "duration": 0.003085, "end_time": "2025-06-18T09:44:30.108930", "exception": false, "start_time": "2025-06-18T09:44:30.105845", "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": "807a81fd", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.116792Z", "iopub.status.busy": "2025-06-18T09:44:30.116466Z", "iopub.status.idle": "2025-06-18T09:44:30.122923Z", "shell.execute_reply": "2025-06-18T09:44:30.122511Z" }, "papermill": { "duration": 0.011514, "end_time": "2025-06-18T09:44:30.123578", "exception": false, "start_time": "2025-06-18T09:44:30.112064", "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", " # save subgrid output to use in subsequent rules:\n", " subgrid_output=True, \n", ")\n", "wf.create_rule(sfincs_build, rule_id=\"sfincs_build\")" ] }, { "cell_type": "markdown", "id": "516332ab", "metadata": { "papermill": { "duration": 0.003177, "end_time": "2025-06-18T09:44:30.129933", "exception": false, "start_time": "2025-06-18T09:44:30.126756", "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": "bd01f578", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.136973Z", "iopub.status.busy": "2025-06-18T09:44:30.136667Z", "iopub.status.idle": "2025-06-18T09:44:30.141263Z", "shell.execute_reply": "2025-06-18T09:44:30.140766Z" }, "papermill": { "duration": 0.00895, "end_time": "2025-06-18T09:44:30.142054", "exception": false, "start_time": "2025-06-18T09:44:30.133104", "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 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": "550783bd", "metadata": { "papermill": { "duration": 0.003218, "end_time": "2025-06-18T09:44:30.148689", "exception": false, "start_time": "2025-06-18T09:44:30.145471", "status": "completed" }, "tags": [] }, "source": [ "### Derive coastal design events" ] }, { "cell_type": "markdown", "id": "beb1b481", "metadata": { "papermill": { "duration": 0.003254, "end_time": "2025-06-18T09:44:30.155232", "exception": false, "start_time": "2025-06-18T09:44:30.151978", "status": "completed" }, "tags": [] }, "source": [ "Here, we define coastal design events for different return periods from the GTSM surge and tide timeseries data. These time series can also be replaced with observed timeseries if available. The total water level (combined tide and surge) of each event is derived by fitting an extreme value distribution to total water level peaks, using a peak-over-threshold approach. The shape of each event is derived by combining the spring tide signal with an average surge signal which is scaled to match the event total water level.\n", "\n", "Note that in case of multiple water level gauge locations this approach assumes full dependence which might be an oversimplification of the reality.\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "4982d91c", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.162351Z", "iopub.status.busy": "2025-06-18T09:44:30.162187Z", "iopub.status.idle": "2025-06-18T09:44:30.166784Z", "shell.execute_reply": "2025-06-18T09:44:30.166300Z" }, "papermill": { "duration": 0.009027, "end_time": "2025-06-18T09:44:30.167566", "exception": false, "start_time": "2025-06-18T09:44:30.158539", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=get_gtsm_data, method=get_gtsm_data, runs=1)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the GTSM data from reanalysis data for region and time period\n", "get_gtsm_data = coastal.GetGTSMData(\n", " gtsm_catalog=wf.get_ref(\"$config.catalog_path\"),\n", " start_time=wf.get_ref(\"$config.start_time\"),\n", " end_time=wf.get_ref(\"$config.end_time\"),\n", " region=sfincs_build.output.sfincs_region,\n", " data_root=\"data/gtsm\",\n", ")\n", "wf.create_rule(get_gtsm_data, rule_id=\"get_gtsm_data\")\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "1234da48", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.175404Z", "iopub.status.busy": "2025-06-18T09:44:30.175037Z", "iopub.status.idle": "2025-06-18T09:44:30.180335Z", "shell.execute_reply": "2025-06-18T09:44:30.179863Z" }, "papermill": { "duration": 0.009875, "end_time": "2025-06-18T09:44:30.181056", "exception": false, "start_time": "2025-06-18T09:44:30.171181", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - wildcards - Added wildcard 'event' with values: ['h_event_rp002', 'h_event_rp005', 'h_event_rp010', 'h_event_rp050', 'h_event_rp100']\n" ] }, { "data": { "text/plain": [ "Rule(id=coastal_events, method=coastal_design_events, runs=1, expand=['event'])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generate coastal design events\n", "coastal_events = coastal.CoastalDesignEvents(\n", " surge_timeseries=get_gtsm_data.output.surge_nc,\n", " tide_timeseries=get_gtsm_data.output.tide_nc,\n", " bnd_locations=get_gtsm_data.output.bnd_locations,\n", " rps=wf.get_ref(\"$config.rps\"),\n", " event_root=\"data/events/default\",\n", " wildcard=\"event\", # wildcard to use for the pluvial events\n", "\n", ")\n", "\n", "# Note that a new \"events\" wildcard is created for the events\n", "wf.create_rule(coastal_events, rule_id=\"coastal_events\")\n" ] }, { "cell_type": "markdown", "id": "4cc4d2bb", "metadata": { "papermill": { "duration": 0.003481, "end_time": "2025-06-18T09:44:30.188016", "exception": false, "start_time": "2025-06-18T09:44:30.184535", "status": "completed" }, "tags": [] }, "source": [ "The events are scaled for future climate predictions based on the a sea level rise offset. A new wildcard for scenarios is introduced develop hazard maps for each event per scenario." ] }, { "cell_type": "code", "execution_count": 10, "id": "011f5967", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.195596Z", "iopub.status.busy": "2025-06-18T09:44:30.195442Z", "iopub.status.idle": "2025-06-18T09:44:30.201911Z", "shell.execute_reply": "2025-06-18T09:44:30.201520Z" }, "papermill": { "duration": 0.010991, "end_time": "2025-06-18T09:44:30.202573", "exception": false, "start_time": "2025-06-18T09:44:30.191582", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - wildcards - Added wildcard 'scenario' with values: ['present', 'rcp85_2050']\n" ] }, { "data": { "text/plain": [ "Rule(id=future_coastal_events, method=future_slr, runs=1, expand=['event', 'scenario'])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "future_coastal_events = coastal.FutureSLR(\n", " scenarios=wf.get_ref(\"$config.slr_scenarios\"),\n", " event_set_yaml=coastal_events.output.event_set_yaml,\n", " event_names=coastal_events.params.event_names,\n", " event_root=\"data/events\",\n", " event_wildcard=\"event\",\n", " scenario_wildcard=\"scenario\",\n", ")\n", "wf.create_rule(future_coastal_events, rule_id=\"future_coastal_events\")" ] }, { "cell_type": "markdown", "id": "067394e7", "metadata": { "papermill": { "duration": 0.003545, "end_time": "2025-06-18T09:44:30.209854", "exception": false, "start_time": "2025-06-18T09:44:30.206309", "status": "completed" }, "tags": [] }, "source": [ "### Derive flood hazard" ] }, { "cell_type": "markdown", "id": "e9203628", "metadata": { "papermill": { "duration": 0.003551, "end_time": "2025-06-18T09:44:30.216957", "exception": false, "start_time": "2025-06-18T09:44:30.213406", "status": "completed" }, "tags": [] }, "source": [ "To derive flood hazard maps for each event, we \n", "1. Update the SFINCS model using the water level 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." ] }, { "cell_type": "code", "execution_count": 11, "id": "cfee7c18", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.224806Z", "iopub.status.busy": "2025-06-18T09:44:30.224466Z", "iopub.status.idle": "2025-06-18T09:44:30.230503Z", "shell.execute_reply": "2025-06-18T09:44:30.230038Z" }, "papermill": { "duration": 0.010748, "end_time": "2025-06-18T09:44:30.231274", "exception": false, "start_time": "2025-06-18T09:44:30.220526", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_update, method=sfincs_update_forcing, runs=10, repeat=['event', 'scenario'])" ] }, "execution_count": 11, "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_coastal_events.output.future_event_csv,\n", " output_dir=sfincs_build.output.sfincs_inp.parent/\"sim_{scenario}\"/\"{event}\"\n", ")\n", "wf.create_rule(sfincs_update, rule_id=\"sfincs_update\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "73feb3c1", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.239453Z", "iopub.status.busy": "2025-06-18T09:44:30.239101Z", "iopub.status.idle": "2025-06-18T09:44:30.244512Z", "shell.execute_reply": "2025-06-18T09:44:30.244029Z" }, "papermill": { "duration": 0.010178, "end_time": "2025-06-18T09:44:30.245249", "exception": false, "start_time": "2025-06-18T09:44:30.235071", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_run, method=sfincs_run, runs=10, repeat=['event', 'scenario'])" ] }, "execution_count": 12, "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\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "0efb1c4a", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.253343Z", "iopub.status.busy": "2025-06-18T09:44:30.253170Z", "iopub.status.idle": "2025-06-18T09:44:30.258553Z", "shell.execute_reply": "2025-06-18T09:44:30.258077Z" }, "papermill": { "duration": 0.010327, "end_time": "2025-06-18T09:44:30.259308", "exception": false, "start_time": "2025-06-18T09:44:30.248981", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_post, method=sfincs_postprocess, runs=10, repeat=['event', 'scenario'])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Postprocesses SFINCS results to a regular grid of maximum water levels\n", "sfincs_post = sfincs.SfincsPostprocess(\n", " sfincs_map=sfincs_run.output.sfincs_map,\n", ")\n", "wf.create_rule(sfincs_post, rule_id=\"sfincs_post\")" ] }, { "cell_type": "code", "execution_count": 14, "id": "2a0a846d", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.267893Z", "iopub.status.busy": "2025-06-18T09:44:30.267491Z", "iopub.status.idle": "2025-06-18T09:44:30.273281Z", "shell.execute_reply": "2025-06-18T09:44:30.272786Z" }, "papermill": { "duration": 0.010814, "end_time": "2025-06-18T09:44:30.274057", "exception": false, "start_time": "2025-06-18T09:44:30.263243", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=sfincs_downscale, method=sfincs_downscale, runs=10, repeat=['event', 'scenario'])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "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", ")\n", "wf.create_rule(sfincs_downscale, rule_id=\"sfincs_downscale\")" ] }, { "cell_type": "markdown", "id": "db08fda0", "metadata": { "papermill": { "duration": 0.00392, "end_time": "2025-06-18T09:44:30.282023", "exception": false, "start_time": "2025-06-18T09:44:30.278103", "status": "completed" }, "tags": [] }, "source": [ "### Derive flood risk" ] }, { "cell_type": "markdown", "id": "fc355bdc", "metadata": { "papermill": { "duration": 0.00387, "end_time": "2025-06-18T09:44:30.289816", "exception": false, "start_time": "2025-06-18T09:44:30.285946", "status": "completed" }, "tags": [] }, "source": [ "To calculate flood risk, we \n", "- Update Delft-FIAT with *all coastal 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": 15, "id": "63cc9280", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.298201Z", "iopub.status.busy": "2025-06-18T09:44:30.298057Z", "iopub.status.idle": "2025-06-18T09:44:30.302944Z", "shell.execute_reply": "2025-06-18T09:44:30.302547Z" }, "papermill": { "duration": 0.009896, "end_time": "2025-06-18T09:44:30.303624", "exception": false, "start_time": "2025-06-18T09:44:30.293728", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_update, method=fiat_update_hazard, runs=2, repeat=['scenario'], reduce=['event'])" ] }, "execution_count": 15, "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_coastal_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": 16, "id": "decfe7de", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.312317Z", "iopub.status.busy": "2025-06-18T09:44:30.312164Z", "iopub.status.idle": "2025-06-18T09:44:30.316334Z", "shell.execute_reply": "2025-06-18T09:44:30.315865Z" }, "papermill": { "duration": 0.009322, "end_time": "2025-06-18T09:44:30.317024", "exception": false, "start_time": "2025-06-18T09:44:30.307702", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_run, method=fiat_run, runs=2, repeat=['scenario'])" ] }, "execution_count": 16, "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", ")\n", "wf.create_rule(fiat_run, rule_id=\"fiat_run\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "56806203", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.325968Z", "iopub.status.busy": "2025-06-18T09:44:30.325633Z", "iopub.status.idle": "2025-06-18T09:44:30.330079Z", "shell.execute_reply": "2025-06-18T09:44:30.329590Z" }, "papermill": { "duration": 0.009692, "end_time": "2025-06-18T09:44:30.330811", "exception": false, "start_time": "2025-06-18T09:44:30.321119", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Rule(id=fiat_visualize, method=fiat_visualize, runs=2, repeat=['scenario'])" ] }, "execution_count": 17, "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,\n", " output_dir=\"output/risk/{scenario}\",\n", ")\n", "wf.create_rule(fiat_visualize, rule_id=\"fiat_visualize\")" ] }, { "cell_type": "markdown", "id": "7d296280", "metadata": { "papermill": { "duration": 0.004147, "end_time": "2025-06-18T09:44:30.339207", "exception": false, "start_time": "2025-06-18T09:44:30.335060", "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": "f6a707e4", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.348169Z", "iopub.status.busy": "2025-06-18T09:44:30.348024Z", "iopub.status.idle": "2025-06-18T09:44:30.382440Z", "shell.execute_reply": "2025-06-18T09:44:30.382037Z" }, "papermill": { "duration": 0.039684, "end_time": "2025-06-18T09:44:30.383115", "exception": false, "start_time": "2025-06-18T09:44:30.343431", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "coastal_risk\n", "\n", "\n", "\n", "sfincs_build\n", "\n", "sfincs_build\n", " runs=1\n", "\n", "\n", "\n", "get_gtsm_data\n", "\n", "get_gtsm_data\n", " runs=1\n", "\n", "\n", "\n", "sfincs_build->get_gtsm_data\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=['event'\n", " 'scenario']\n", "\n", "\n", "\n", "sfincs_build->sfincs_update\n", "\n", "\n", "\n", "\n", "\n", "sfincs_downscale\n", "\n", "sfincs_downscale\n", " runs=10\n", " repeat=['event'\n", " 'scenario']\n", "\n", "\n", "\n", "sfincs_build->sfincs_downscale\n", "\n", "\n", "\n", "\n", "\n", "coastal_events\n", "\n", "coastal_events\n", " runs=1\n", " expand=['event']\n", "\n", "\n", "\n", "get_gtsm_data->coastal_events\n", "\n", "\n", "\n", "\n", "\n", "future_coastal_events\n", "\n", "future_coastal_events\n", " runs=1\n", " expand=['event'\n", " 'scenario']\n", "\n", "\n", "\n", "coastal_events->future_coastal_events\n", "\n", "\n", "\n", "\n", "\n", "future_coastal_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_coastal_events->fiat_update\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=['event'\n", " 'scenario']\n", "\n", "\n", "\n", "sfincs_update->sfincs_run\n", "\n", "\n", "\n", "\n", "\n", "sfincs_run->sfincs_downscale\n", "\n", "\n", "\n", "\n", "\n", "sfincs_post\n", "\n", "sfincs_post\n", " runs=10\n", " repeat=['event'\n", " 'scenario']\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": "7101c867", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.392921Z", "iopub.status.busy": "2025-06-18T09:44:30.392541Z", "iopub.status.idle": "2025-06-18T09:44:30.403144Z", "shell.execute_reply": "2025-06-18T09:44:30.402667Z" }, "papermill": { "duration": 0.016205, "end_time": "2025-06-18T09:44:30.403934", "exception": false, "start_time": "2025-06-18T09:44:30.387729", "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: get_gtsm_data (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 3/12: coastal_events (1 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 4/12: future_coastal_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: sfincs_update (10 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 7/12: sfincs_run (10 runs)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO - workflow - Dryrun rule 8/12: sfincs_downscale (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", "# to run the workflow in HydroFlows use wf.run(), or (preferred) use wf.to_snakemake() to create a snakemake file\n", "wf.dryrun()" ] }, { "cell_type": "markdown", "id": "1f0ee9e9", "metadata": { "papermill": { "duration": 0.004756, "end_time": "2025-06-18T09:44:30.413502", "exception": false, "start_time": "2025-06-18T09:44:30.408746", "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": "06e9ff0d", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.423816Z", "iopub.status.busy": "2025-06-18T09:44:30.423475Z", "iopub.status.idle": "2025-06-18T09:44:30.439432Z", "shell.execute_reply": "2025-06-18T09:44:30.439048Z" }, "papermill": { "duration": 0.021878, "end_time": "2025-06-18T09:44:30.440179", "exception": false, "start_time": "2025-06-18T09:44:30.418301", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cases/coastal_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", " print(f\"- {f.name}\")" ] }, { "cell_type": "code", "execution_count": 21, "id": "69612d22", "metadata": { "execution": { "iopub.execute_input": "2025-06-18T09:44:30.450545Z", "iopub.status.busy": "2025-06-18T09:44:30.450399Z", "iopub.status.idle": "2025-06-18T09:44:30.452471Z", "shell.execute_reply": "2025-06-18T09:44:30.452085Z" }, "papermill": { "duration": 0.008073, "end_time": "2025-06-18T09:44:30.453192", "exception": false, "start_time": "2025-06-18T09:44:30.445119", "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": 5.966536, "end_time": "2025-06-18T09:44:31.273751", "environment_variables": {}, "exception": null, "input_path": "/home/runner/work/HydroFlows/HydroFlows/docs/../examples/coastal_risk.ipynb", "output_path": "/home/runner/work/HydroFlows/HydroFlows/docs/_examples/coastal_risk.ipynb", "parameters": {}, "start_time": "2025-06-18T09:44:25.307215", "version": "2.6.0" } }, "nbformat": 4, "nbformat_minor": 5 }