Export wflow staticmaps netcdf to raster files

In order to inspect or (manually) modify wflow staticmaps it is convienent 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.

Load dependencies

[1]:
import pandas as pd
import xarray as xr
import numpy as  np
from os.path import join, dirname
import os
import hydromt

Read wflow staticmaps

hydroMT provides an easy method to read the model schematization trought the Model API.

[2]:
root = 'wflow_piave_subbasin'
mod = hydromt.WflowModel(root, mode='r')
ds = mod.staticmaps  # here the staticmaps netcdf is loaded
print(ds)
<xarray.Dataset>
Dimensions:               (layer: 4, time: 12, x: 58, y: 53)
Coordinates:
  * x                     (x) float64 11.78 11.8 11.82 ... 12.7 12.72 12.73
  * y                     (y) float64 46.68 46.67 46.65 ... 45.85 45.83 45.82
    idx_out               (y, x) int32 24029 24049 28860 ... 1281540 1280360
    spatial_ref           int32 1
    mask                  (y, x) uint8 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0
  * time                  (time) int32 1 2 3 4 5 6 7 8 9 10 11 12
  * layer                 (layer) int64 0 1 2 3
Data variables: (12/83)
    x_out                 (y, x) float64 nan nan nan nan nan ... nan nan nan nan
    y_out                 (y, x) float64 nan nan nan nan nan ... nan nan nan nan
    wflow_ldd             (y, x) uint8 255 255 255 255 255 ... 255 255 255 255
    wflow_subcatch        (y, x) int32 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0
    wflow_uparea          (y, x) float64 -9.999e+03 -9.999e+03 ... -9.999e+03
    subare                (y, x) float64 -9.999e+03 -9.999e+03 ... -9.999e+03
    ...                    ...
    TTI                   (y, x) int32 -999 -999 -999 -999 ... -999 -999 -999
    TTM                   (y, x) int32 -999 -999 -999 -999 ... -999 -999 -999
    WHC                   (y, x) float64 -999.0 -999.0 -999.0 ... -999.0 -999.0
    G_Cfmax               (y, x) float64 -999.0 -999.0 -999.0 ... -999.0 -999.0
    G_SIfrac              (y, x) float64 -999.0 -999.0 -999.0 ... -999.0 -999.0
    G_TT                  (y, x) float64 -999.0 -999.0 -999.0 ... -999.0 -999.0

Write netcdf to mapstack

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.

[3]:
outdir =join(root, 'staticmaps')
ds.raster.to_mapstack(outdir)

Now the model files can easily be inspected and modified e.g. QGIS.

Create staticmaps netcdf files based on mapstack

If you want to update the staticmaps after modification the maps can be read into a Dataset by hydromt. We recommend the following workflow:

  • read the original model

  • read the updated mapstack

  • change the model root to write the updated model to a new directory

  • update the staticmaps of the model

  • write the model

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.

NOTE: The staticgeoms might be changed because of manaul 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.

[4]:
# read the original model
root = 'wflow_piave_subbasin'
mod = hydromt.WflowModel(root, mode='r')
mod.read_staticmaps()
mod.read_config()
[5]:
# read the updated mapstack
# NOTE: The mapstack does not have to include all staticmaps, only the once that are found will be updated.
# The name of the staticmap should however have to be unchanged.
ds_updated = hydromt.open_mfraster(join(root, 'staticmaps', '*.tif'))
[6]:
# change root to a new directory
root_updated = 'wflow_piave_subbasin_updated'
mod.set_root(root_updated, mode='w')
[7]:
# update the model staticmaps
mod.set_staticmaps(ds_updated)
[8]:
# write the model to the new directory
mod.write()