Calling GR4J-CemaNeige on the Raven server¶
Here we use birdy’s WPS client to launch the GR4JCN 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.529, -3.396, 407.29, 1.072, 16.9, 0.947'
# Forcing files
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,
)
# Let's call the model
resp = wps.raven_gr4j_cemaneige(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_q45enn8e/Salmon-River-Near-Prince-George_meteo_daily.nc,-0.0371048,36.562,
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([[ 0. ],
[ 0.165788],
[ 0.559366],
...,
[28.077935],
[27.835868],
[27.597955]])
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 0x7fd220882940>]
[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(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