{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "## Convert Wflow staticmaps netcdf to raster files" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "In order to inspect or (manually) modify Wflow staticmaps it is convenient to export the maps to a raster format. Here we show how to read the model maps and save to a so-called mapstack (i.e.: a set of raster files with identical grid) using HydroMT. " ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "### Load dependencies" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "import shutil\n", "from os.path import join, exists\n", "from os import listdir\n", "import hydromt\n", "from hydromt_wflow import WflowSbmModel" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "### Read wflow staticmaps" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "HydroMT provides an easy method to read the model schematization through the Model API." ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "root = \"wflow_piave_subbasin\"\n", "wflow = WflowSbmModel(root, mode=\"r\")\n", "ds = wflow.staticmaps.data # here the staticmaps netcdf is loaded\n", "print(ds)" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Write netcdf to mapstack" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "The raster module provides many raster GIS methods through the **raster** Dataset accessor. To write a Dataset into several raster files (eg one geotiff file per map), one line with code is sufficient using [raster.to_mapstack](https://deltares.github.io/hydromt/latest/_generated/hydromt.gis.Dataset.raster.to_mapstack.html#hydromt.gis.Dataset.raster.to_mapstack). We only need to provide the output folder in which all raster files are saved. The default output format is *GeoTIFF*, but this can be changed with the `driver` argument." ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "# Name of the folder where the tif files will be saved\n", "updated_staticmaps = \"updated_staticmaps\"\n", "\n", "# First remove the folder if it already exists\n", "if exists(updated_staticmaps):\n", " shutil.rmtree(updated_staticmaps)\n", "\n", "# Save the tif files\n", "ds.raster.to_mapstack(updated_staticmaps)\n", "# Print the content of the folder\n", "listdir(updated_staticmaps)" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "Now the model files can easily be inspected and modified for example using QGIS (e.g. with the Serval plugin).\n", "\n", "> **Note:** in QGIS, you can also visualize netcdf files but direct modification is not (yet) possible." ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "### Update a specific map using `setup_grid_from_raster`\n", "\n", "Let's say you have updated (manually or not) the `soil_ksat_vertical.tif` static map and that you would like to include it in your model. For demonstration purpose, we will make a copy and name it `soil_ksat_vertical_updated.tif`." ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "old_map = join(updated_staticmaps, \"soil_ksat_vertical.tif\")\n", "updated_map = join(updated_staticmaps, \"soil_ksat_vertical_updated.tif\")\n", "shutil.copyfile(old_map, updated_map)" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "You will need to find the `wflow variable` that corresponds to the map you updated, which in the case of `soil_ksat_vertical.tif` is `soil_surface_water__vertical_saturated_hydraulic_conductivity`. For information on the available wflow variables, see the [Wflow documentation](https://deltares.github.io/Wflow.jl/stable/model_docs/intro/)\n", "\n", "And then you can simply use [setup_grid_from_raster](https://deltares.github.io/hydromt_wflow/latest/_generated/hydromt_wflow.WflowBaseModel.setup_grid_from_raster.html).\n", "\n", "In the below example, we will use python to update our model. If you wish to update via command line, the steps are:\n", "\n", "1. Create a data catalog entry for the maps in the \"updated_staticmaps\" folder\n", "2. Prepare a hydromt config file with the steps: `setup_grid_from_raster`, `statimaps.write` and `config.write`\n", "3. Call the `hydromt update` command line re-using the catalog and configuration file you created." ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "# Instantiate a new WflowSbmModel object in read/write mode (r+) to be able to update it\n", "wflow = WflowSbmModel(root, mode=\"r+\")\n", "\n", "# `KsatVer` corresponds to the `soil_surface_water__vertical_saturated_hydraulic_conductivity` variable\n", "wflow_variable = \"soil_surface_water__vertical_saturated_hydraulic_conductivity\"\n", "config_opt = wflow.config.get_value(f\"input.static.{wflow_variable}\")\n", "print(f\"Before setup_grid_from_raster: {config_opt}\")\n", "\n", "# Path to the (updated) `soil_ksat_vertical_updated.tif`\n", "updated_map = join(updated_staticmaps, \"soil_ksat_vertical_updated.tif\")\n", "\n", "# Update the model KsatVer map with setup_grid_from_raster\n", "wflow.setup_grid_from_raster(\n", " raster_fn=updated_map, \n", " reproject_method='nearest',\n", " variables=[\"soil_ksat_vertical_updated\"], \n", " wflow_variables=[wflow_variable],\n", " \n", ")\n", "config_opt = wflow.config.get_value(f\"input.static.{wflow_variable}\")\n", "print(f\"After setup_grid_from_raster: {config_opt}\")\n", "\n", "# Write the updated grid and config files to new files\n", "wflow.staticmaps.write(filename=\"staticmaps_updated.nc\")\n", "wflow.config.write(filename=\"wflow_sbm_updated.toml\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "docs", "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }