Calling HBV-EC on the Raven server¶
Here we use birdy’s WPS client to launch the HBV-EC hydrological model on the server and analyze the output.
[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. Can either be a string of comma separated values, a list, an array or a named tuple.
params = '0.05984519, 4.072232, 2.001574, 0.03473693, 0.09985144, 0.5060520, 3.438486, 38.32455, ' \
'0.4606565, 0.06303738, 2.277781, 4.873686, 0.5718813, 0.04505643, 0.877607, 18.94145, ' \
'2.036937, 0.4452843, 0.6771759, 1.141608, 1.024278'
# Forcing files
ts=TESTDATA['raven-hbv-ec-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,
)
# Let's call the model with the timeseries, model parameters and other configuration parameters
resp = wps.raven_hbv_ec(ts=str(ts), params=params, **config)
# 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)
Since we requested output objects, we can simply access the output objects. The dianostics is just a CSV file:
[3]:
print(diagnostics)
observed data series,filename,DIAG_NASH_SUTCLIFFE,DIAG_RMSE,
HYDROGRAPH,/tmp/pywps_process_55nmc3lt/input.nc,-0.075407,37.231,
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.q_sim
[4]:
<xarray.DataArray 'q_sim' (time: 732, nbasins: 1)>
array([[1. ],
[0.768899],
[0.928763],
...,
[0. ],
[0. ],
[0. ]])
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.q_sim.plot()
[5]:
[<matplotlib.lines.Line2D at 0x7fa0919bc710>]
[6]:
print("Max: ", hydrograph.q_sim.max())
print("Mean: ", hydrograph.q_sim.mean())
print("Monthly means: ", hydrograph.q_sim.groupby('time.month').mean(dim='time'))
Max: <xarray.DataArray 'q_sim' ()>
array(124.86333084)
Mean: <xarray.DataArray 'q_sim' ()>
array(6.54508271)
Monthly means: <xarray.DataArray 'q_sim' (month: 12, nbasins: 1)>
array([[ 1.19363324],
[ 0.05415771],
[ 4.18564509],
[28.76080213],
[20.22635228],
[ 2.7529817 ],
[ 0.36849142],
[ 0.03634777],
[ 0. ],
[ 1.09206023],
[16.62483913],
[ 3.51686589]])
Coordinates:
basin_name (nbasins) object ...
* month (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Dimensions without coordinates: nbasins