Running a multi-member hydrological ensemble on the Raven Server

Here we use birdy’s WPS client to launch the GR4JCN and HMETS hydrological models on the server and analyze the outputs.

[1]:
from birdy import WPSClient

from example_data import TESTDATA
import datetime as dt
from urllib.request import urlretrieve
import xarray as xr
import numpy as np
from matplotlib import pyplot as plt
import os

# Set environment variable WPS_URL to "http://localhost:9099" to run on the default local server
url = os.environ.get("WPS_URL", "https://pavics.ouranos.ca/twitcher/ows/proxy/raven/wps")
wps = WPSClient(url)
[2]:
# The model parameters for gr4jcn and hmets. Can either be a string of comma separated values, a list, an array or a named tuple.
gr4jcn ='0.529, -3.396, 407.29, 1.072, 16.9, 0.947'
hmets = '9.5019, 0.2774, 6.3942, 0.6884, 1.2875, 5.4134, 2.3641, 0.0973, 0.0464, 0.1998, 0.0222, -1.0919, ' \
         '2.6851, 0.3740, 1.0000, 0.4739, 0.0114, 0.0243, 0.0069, 310.7211, 916.1947'

# Forcing files. Raven uses the same forcing files for all and extracts the information it requires for each model.
ts=TESTDATA['raven-gr4j-cemaneige-nc-ts']

# Model configuration parameters.
config = dict(
    start_date=dt.datetime(2000, 1, 1),
    end_date=dt.datetime(2002, 1, 1),
    area=4250.6,
    elevation=843.0,
    latitude=54.4848,
    longitude=-123.3659,
    )

# Launch the WPS to get the multi-model results.  Note the "gr4jcn" and "hmets" keys.
resp=wps.raven_multi_model(ts=str(ts),gr4jcn=gr4jcn,hmets=hmets, **config)

Let’s see the diagnostics and the output hydrograph.

[3]:
# And get the response
# With `asobj` set to False, only the reference to the output is returned in the response.
# Setting `asobj` to True will retrieve the actual files and copy the locally.
[hydrograph, storage, solution, diagnostics, rv] = resp.get(asobj=True)
print(diagnostics)
print(hydrograph)
['observed data series,filename,DIAG_NASH_SUTCLIFFE,DIAG_RMSE,\nHYDROGRAPH,/tmp/pywps_process_1m9rxaij/Salmon-River-Near-Prince-George_meteo_daily.nc,-0.117301,37.9493,\n', 'observed data series,filename,DIAG_NASH_SUTCLIFFE,DIAG_RMSE,\nHYDROGRAPH,/tmp/pywps_process_1m9rxaij/Salmon-River-Near-Prince-George_meteo_daily.nc,-3.0132,71.9223,\n']
[<xarray.Dataset>
Dimensions:     (nbasins: 1, time: 732)
Coordinates:
  * time        (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2002-01-01
    basin_name  (nbasins) object ...
Dimensions without coordinates: nbasins
Data variables:
    precip      (time) float64 ...
    q_sim       (time, nbasins) float64 ...
    q_obs       (time, nbasins) float64 ...
    q_in        (time, nbasins) float64 ...
Attributes:
    Conventions:          CF-1.6
    featureType:          timeSeries
    history:              Created on 2020-04-30 15:25:50 by Raven
    description:          Standard Output
    title:                Simulated river discharge
    references:           Craig J.R. and the Raven Development Team Raven use...
    comment:              Raven Hydrological Framework version 2.9 rev#254
    model_id:             gr4jcn
    time_frequency:       day
    time_coverage_start:  2000-01-01 00:00:00
    time_coverage_end:    2002-01-01 00:00:00, <xarray.Dataset>
Dimensions:     (nbasins: 1, time: 732)
Coordinates:
  * time        (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2002-01-01
    basin_name  (nbasins) object ...
Dimensions without coordinates: nbasins
Data variables:
    precip      (time) float64 ...
    q_sim       (time, nbasins) float64 ...
    q_obs       (time, nbasins) float64 ...
    q_in        (time, nbasins) float64 ...
Attributes:
    Conventions:          CF-1.6
    featureType:          timeSeries
    history:              Created on 2020-04-30 15:25:50 by Raven
    description:          Standard Output
    title:                Simulated river discharge
    references:           Craig J.R. and the Raven Development Team Raven use...
    comment:              Raven Hydrological Framework version 2.9 rev#254
    model_id:             hmets
    time_frequency:       day
    time_coverage_start:  2000-01-01 00:00:00
    time_coverage_end:    2002-01-01 00:00:00]

The hydrograph and storage outputs are netCDF files storing the time series. These files are opened by default using xarray, which provides convenient and powerful time series analysis and plotting tools.

[4]:
hydrograph[0].q_sim
[4]:
<xarray.DataArray 'q_sim' (time: 732, nbasins: 1)>
array([[ 0.      ],
       [ 0.165788],
       [ 0.559366],
       ...,
       [13.407794],
       [13.330653],
       [13.25446 ]])
Coordinates:
  * time        (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2002-01-01
    basin_name  (nbasins) object ...
Dimensions without coordinates: nbasins
Attributes:
    units:      m**3 s**-1
    long_name:  Simulated outflows
[5]:
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

hydrograph[0].q_sim.plot()
hydrograph[1].q_sim.plot()
[5]:
[<matplotlib.lines.Line2D at 0x7faee45f9940>]
../_images/notebooks_multi_model_simulation_7_1.png
[6]:
print("Max: ", hydrograph[0].q_sim.max())
print("Mean: ", hydrograph[0].q_sim.mean())
print("Monthly means: ", hydrograph[0].q_sim.groupby('time.month').mean(dim='time'))
Max:  <xarray.DataArray 'q_sim' ()>
array(89.92282654)
Mean:  <xarray.DataArray 'q_sim' ()>
array(24.68315955)
Monthly means:  <xarray.DataArray 'q_sim' (month: 12, nbasins: 1)>
array([[14.51757387],
       [11.47384701],
       [10.07247867],
       [20.42010176],
       [24.37541489],
       [22.55812621],
       [23.56102364],
       [25.3008606 ],
       [28.72388591],
       [34.377813  ],
       [46.44695716],
       [34.09486238]])
Coordinates:
    basin_name  (nbasins) object ...
  * month       (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Dimensions without coordinates: nbasins