{ "cells": [ { "cell_type": "markdown", "id": "6ea7a8ea", "metadata": {}, "source": [ "## Convert wflow staticmaps netcdf to raster files" ] }, { "cell_type": "markdown", "id": "09ab27a8", "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": "163edfe3", "metadata": {}, "source": [ "### Load dependencies" ] }, { "cell_type": "code", "execution_count": null, "id": "d480af50", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import xarray as xr\n", "import numpy as np\n", "from os.path import join, dirname\n", "import os\n", "import hydromt" ] }, { "cell_type": "markdown", "id": "1c078a08", "metadata": {}, "source": [ "### Read wflow staticmaps" ] }, { "cell_type": "markdown", "id": "fb790d49", "metadata": {}, "source": [ "hydroMT provides an easy method to read the model schematization trought the Model API." ] }, { "cell_type": "code", "execution_count": null, "id": "15044d4b", "metadata": {}, "outputs": [], "source": [ "root = 'wflow_piave_subbasin' \n", "mod = hydromt.WflowModel(root, mode='r')\n", "ds = mod.staticmaps # here the staticmaps netcdf is loaded \n", "print(ds)" ] }, { "cell_type": "markdown", "id": "1acdee64", "metadata": {}, "source": [ "### Write netcdf to mapstack" ] }, { "cell_type": "markdown", "id": "36073d9b", "metadata": {}, "source": [ "The raster module provides many raster GIS methods throught the **raster** Dataset accessor. To write a Dataset to a mapstack one line with code is sufficient. 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. To write to PCRaster map-files it is recommended to have PCRaster python installed." ] }, { "cell_type": "code", "execution_count": null, "id": "07e4d5b7", "metadata": {}, "outputs": [], "source": [ "outdir =join(root, 'staticmaps')\n", "ds.raster.to_mapstack(outdir)" ] }, { "cell_type": "markdown", "id": "b848f0e9", "metadata": {}, "source": [ "Now the model files can easily be inspected and modified e.g. QGIS.\n", "\n", "NOTE: in QGIS, you can also visualize but direct modification is not (yet) possible." ] }, { "cell_type": "markdown", "id": "e1f84114", "metadata": {}, "source": [ "### Create staticmaps netcdf files based on mapstack" ] }, { "cell_type": "markdown", "id": "bbe69373", "metadata": {}, "source": [ "If you want to update the staticmaps after modification the maps can be read into a Dataset by hydromt. We recommend the following workflow:\n", "\n", "* read the original model\n", "* read the updated mapstack\n", "* change the model root to write the updated model to a new directory\n", "* update the staticmaps of the model\n", "* write the model\n", "\n", "NOTE: We do not read the forcing as it is probably faster to just copy the file instead of loading it into python and writing it back to netcdf.\n", "\n", "NOTE: The staticgeoms might be changed because of manual changes in the wflow_river, lakes, reservoir or glacier staticmaps and are therefore not read here. To change these maps we recommend using the hydromt update method to keep the staticgeoms and maps aligned." ] }, { "cell_type": "code", "execution_count": null, "id": "42c8a6f3", "metadata": {}, "outputs": [], "source": [ "# read the original model\n", "root = 'wflow_piave_subbasin' \n", "mod = hydromt.WflowModel(root, mode='r')\n", "mod.read_staticmaps()\n", "mod.read_config()" ] }, { "cell_type": "code", "execution_count": null, "id": "438a1d35", "metadata": {}, "outputs": [], "source": [ "# read the updated mapstack\n", "# NOTE: The mapstack does not have to include all staticmaps, only the once that are found will be updated.\n", "# The name of the staticmap should however have to be unchanged. \n", "ds_updated = hydromt.open_mfraster(join(root, 'staticmaps', '*.tif'))" ] }, { "cell_type": "code", "execution_count": null, "id": "98ba4297", "metadata": {}, "outputs": [], "source": [ "# change root to a new directory\n", "root_updated = 'wflow_piave_subbasin_updated'\n", "mod.set_root(root_updated, mode='w') " ] }, { "cell_type": "code", "execution_count": null, "id": "1f9fd886", "metadata": {}, "outputs": [], "source": [ "# update the model staticmaps\n", "mod.set_staticmaps(ds_updated)" ] }, { "cell_type": "code", "execution_count": null, "id": "2fef2a90", "metadata": {}, "outputs": [], "source": [ "# write the model to the new directory\n", "mod.write()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }