Tip

For an interactive online version click here: Binder badge

Plot Wflow results#

HydroMT provides a simple interface to model results from which we can make beautiful plots:

  • Results layers are saved to the model results component as a dictionnary of xarray.Dataset or xarray.DataArray.

These plots can be useful to analyze the model results or also compare model runs with different settings (different precipitation source or different parameters values).

Load dependencies#

[1]:
import matplotlib.pyplot as plt
import hydromt
from hydromt_wflow import WflowModel

Read the model run(s) results#

The wflow_piave_subbasin model was run using the default global data sources of the hydromt_wflow plugin. The different variables to save after the wflow were set in a separate wflow configuration file: wflow_sbm_results.toml.

A second run of the model was also done, where the KsatHorFrac parameter of wflow was set to 10 (instead of the default 100 value) using an alternative configuration file: wflow_sbm_results2.toml.

We will use the below runs dictionnary to define the model run(s) we want to read and some settings for plotting. If you want to plot and compare several runs together, you can simply add them to the runs dictionnary.

[2]:
# Dictionnary listing the different wflow models and runs to compare, including plotting options
runs = {
    "run1": {
        "longname": "default",
        "color": "blue",
        "root": "wflow_piave_subbasin",
        "config_fn": "wflow_sbm_results.toml",
    },
    "run2": {
        "longname": "KsatHorFrac10",
        "color": "green",
        "root": "wflow_piave_subbasin",
        "config_fn": "wflow_sbm_results2.toml",
    },
}
mainrun = "run1"
[3]:
# Initialize the different model run(s)
for r in runs:
    run = runs[r]
    mod = WflowModel(root=run["root"], mode="r+", config_fn=run["config_fn"])
    runs[r].update({"mod": mod})
Model dir already exists and files might be overwritten: /home/runner/work/hydromt_wflow/hydromt_wflow/docs/_examples/wflow_piave_subbasin/staticgeoms.

Wflow can save different types of outputs (netcdf gridded output, netcdf scalar netcdf, csv scalar timeseries) that are also reflected in the organisation of the HydroMT results component:

  • a “output” xarray.Dataset for the gridded netcdf file (output section of the TOML)

  • a “netcdf” xarray.Dataset for the netcdf point timeseries file (netcdf section of the TOML)

  • different hydromt.GeoDataArrays for the csv file , one per column (csv section and csv.column sections of the TOML). The xy coordinates are the coordinates of the station or of the representative point of the subcatch/area. The variable name in the GeoDataArray corresponds to the csv header attribute or header_map when available.

Below you can see how to access to the results of run1 and its contents:

[4]:
mod1 = runs["run1"]["mod"]
mod1.results
[4]:
{'output': <xarray.Dataset>
 Dimensions:  (lon: 58, lat: 53, layer: 4, time: 9)
 Coordinates:
   * lon      (lon) float64 11.78 11.8 11.82 11.83 ... 12.68 12.7 12.72 12.73
   * lat      (lat) float64 45.82 45.83 45.85 45.87 ... 46.63 46.65 46.67 46.68
   * layer    (layer) int64 1 2 3 4
   * time     (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
 Data variables:
     q_river  (time, lat, lon) float32 dask.array<chunksize=(9, 53, 58), meta=np.ndarray>
     h_land   (time, lat, lon) float32 dask.array<chunksize=(9, 53, 58), meta=np.ndarray>,
 'netcdf': <xarray.Dataset>
 Dimensions:       (time: 9, Q_gauges: 1, temp_bycoord: 1)
 Coordinates:
   * time          (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * Q_gauges      (Q_gauges) <U1 '1'
   * temp_bycoord  (temp_bycoord) <U12 'temp_bycoord'
 Data variables:
     Q             (time, Q_gauges) float32 dask.array<chunksize=(9, 1), meta=np.ndarray>
     temp_coord    (time, temp_bycoord) float32 dask.array<chunksize=(9, 1), meta=np.ndarray>,
 'Q-max': <xarray.DataArray 'Q-max' (time: 9, index: 1)>
 array([[ 92.29159319],
        [214.84545211],
        [278.7532426 ],
        [317.66886014],
        [338.05469058],
        [346.00525086],
        [337.03409614],
        [319.22097847],
        [304.51376705]])
 Coordinates:
   * time     (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * index    (index) int64 0
     x        (index) float64 12.26
     y        (index) float64 46.25,
 'res-volume': <xarray.DataArray 'res-volume' (time: 9, index: 1)>
 array([[42800000.],
        [42800000.],
        [42800000.],
        [42800000.],
        [42800000.],
        [42800000.],
        [42800000.],
        [42800000.],
        [42800000.]])
 Coordinates:
   * time     (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * index    (index) int64 0
     x        (index) float64 12.08
     y        (index) float64 46.17,
 'temp_bycoord': <xarray.DataArray 'temp_bycoord' (time: 9, index: 1)>
 array([[-0.07535708],
        [ 0.80580747],
        [ 1.95656431],
        [ 2.93230295],
        [ 4.55180979],
        [ 6.96596432],
        [ 5.75890207],
        [ 2.75527048],
        [ 2.07729197]])
 Coordinates:
   * time     (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * index    (index) int64 0
     x        (index) float64 11.95
     y        (index) float64 45.9,
 'temp_byindex': <xarray.DataArray 'temp_byindex' (time: 9, index: 1)>
 array([[-0.07535708],
        [ 0.80580747],
        [ 1.95656431],
        [ 2.93230295],
        [ 4.55180979],
        [ 6.96596432],
        [ 5.75890207],
        [ 2.75527048],
        [ 2.07729197]])
 Coordinates:
   * time     (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * index    (index) int64 0
     x        (index) float64 11.95
     y        (index) float64 45.9,
 'Q_gauges_grdc': <xarray.DataArray 'Q_gauges_grdc' (index: 3, time: 9)>
 array([[ 5.65726064, 10.23746886, 12.94857033, 13.4642965 , 12.93064957,
         12.23056571, 11.51420303, 10.88308009, 10.3122563 ],
        [24.14968045, 41.4367125 , 55.46918112, 60.2240498 , 60.21861451,
         58.43010672, 55.0491823 , 51.84542554, 49.09354881],
        [15.21117463, 21.63727294, 27.21875575, 30.66606831, 33.1589512 ,
         33.26008825, 32.09006663, 30.99882382, 30.10063939]])
 Coordinates:
   * time         (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * index        (index) int32 6349411 6349400 6349410
     geometry     (index) object POINT (12.116666666666362 46.600000000000094)...
     value        (index) float64 6.349e+06 6.349e+06 6.349e+06
     spatial_ref  int64 0,
 'P_subcatchment': <xarray.DataArray 'P_subcatchment' (index: 1, time: 9)>
 array([[ 0.12844024,  0.14628571,  0.08870213,  0.02100732, 13.49765681,
          6.09464082,  2.09232876,  0.06205084,  0.37978556]])
 Coordinates:
   * time         (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
   * index        (index) int32 1
     geometry     (index) object POINT (12.174999999999642 46.25000000000041)
     value        (index) float64 1.0
     spatial_ref  int64 0}

Read observations#

You can also use HydroMT to read observations data in order to analyze your model results. Here a fictional observations timeseries was prepared for the gauges_grdc locations.

[5]:
# Discharge data
timeseries_fn = "gauges_observed_flow.csv"  # observed discharge timeseries
name = "gauges_grdc"  # gauges locations in staticgeoms
stationID = "grdc_no"  # column name in staticgeoms containing the stations IDs

# Read the observations data
# read timeseries data and match with existing gdf
gdf = runs[mainrun]["mod"].geoms[name]
gdf.index = gdf[stationID]
da_ts = hydromt.io.open_timeseries_from_table(timeseries_fn, name=name, sep=";")
da = hydromt.vector.GeoDataArray.from_gdf(gdf, da_ts, index_dim="index")

obs = da
obs
[5]:
<xarray.DataArray 'gauges_grdc' (index: 3, time: 9)>
array([[14.21117463, 20.63727294, 26.21875575, 29.66606831, 32.1589512 ,
        32.26008825, 31.09006663, 29.99882382, 29.10063939],
       [23.14968045, 40.4367125 , 54.46918112, 59.2240498 , 59.21861451,
        57.43010672, 54.0491823 , 50.84542554, 48.09354881],
       [ 4.65726064,  9.23746886, 11.94857033, 12.4642965 , 11.93064957,
        11.23056571, 10.51420303,  9.88308009,  9.31225629]])
Coordinates: (12/26)
  * time           (time) datetime64[ns] 2010-02-02 2010-02-03 ... 2010-02-10
  * index          (index) int64 6349410 6349400 6349411
    grdc_no        (index) int64 6349410 6349400 6349411
    wmo_reg        (index) int64 6 6 6
    sub_reg        (index) int64 49 49 49
    river          (index) object 'BOITE, TORRENTE' ... 'BOITE, TORRENTE'
    ...             ...
    t_yrs          (index) int64 79 77 66
    lta_discharge  (index) float64 9.461 10.36 2.256
    r_volume_yr    (index) float64 0.2984 0.3268 0.07115
    r_height_yr    (index) float64 953.2 915.5 867.6
    geometry       (index) object POINT (12.21666666666668 46.43333333333333)...
    spatial_ref    int64 0

Plot model results#

Here we plot the different model results for the gauges_grdc locations.

[6]:
# Plotting options
# select the gauges_grdc results (name in csv column of wflow results to plot)
result_name = "Q_gauges_grdc"

# selection of runs to plot (all or a subset)
runs_subset = ["run1", "run2"]
[7]:
# Plots
from hydromt.stats import skills as skillstats

# from hydromt import stats as skillstats # for hydromt < v0.4.5

station_ids = list(runs[mainrun]["mod"].results[result_name].index.values)

for i, st in enumerate(station_ids):
    n = 2
    fig, axes = plt.subplots(n, 1, sharex=True, figsize=(15, n * 4))
    axes = [axes] if n == 1 else axes

    # Discharge
    obs_i = obs.sel(index=st)
    obs_i.plot.line(ax=axes[0], x="time", label="obs", color="black")

    for r in runs_subset:
        run = runs[r]
        run_i = run["mod"].results[result_name].sel(index=st)
        # Stats
        nse_i = skillstats.nashsutcliffe(run_i, obs_i).values.round(2)
        kge_i = skillstats.kge(run_i, obs_i)["kge"].values.round(2)
        labeltxt = f"{run['longname']}, NSE: {nse_i}, KGE: {kge_i}"
        run_i.plot.line(
            ax=axes[0],
            x="time",
            label=labeltxt,
            color=f"{run['color']}",
            linestyle="--",
        )

    axes[0].set_title(f"Simulated discharge at station {st}")
    axes[0].set_ylabel("Discharge [m3/s]")
    axes[0].legend()

    # Cumulative Discharge
    obs_i = obs.sel(index=st)
    obs_i.cumsum().plot.line(ax=axes[1], x="time", label="obs", color="black")

    for r in runs_subset:
        run = runs[r]
        run_i = run["mod"].results[result_name].sel(index=st)
        run_i.cumsum().plot.line(
            ax=axes[1],
            x="time",
            label=f"{run['longname']}",
            color=f"{run['color']}",
            linestyle="--",
        )

    axes[1].set_title(f"Cumulative discharge at station {st}")
    axes[1].set_ylabel("Cumulative Discharge [m3/s]")
    axes[1].legend()
../_images/_examples_plot_wflow_results_16_0.png
../_images/_examples_plot_wflow_results_16_1.png
../_images/_examples_plot_wflow_results_16_2.png

You can see on the discharge plots legends that some statistical criteria were computed using the fictional observations and the model runs results.

These statistics were computed using the stats module of HydroMT. You can find the available statisctics functions in the documentation.

And finally once the results are loaded, you can use them to derive more statistics or plots to further analyze your model.