Calling MOHYSE on the Raven server¶
Here we use birdy’s WPS client to launch the MOHYSE 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.
# Mohyse also requires HRU parameters
params = '1.00, 0.0468, 4.2952, 2.6580, 0.4038, 0.0621, 0.0273, 0.0453'
hrus = '0.9039, 5.6179775'
# Forcing files
ts=TESTDATA['raven-mohyse-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_mohyse(ts=str(ts), params = params, hrus=hrus, **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_6uslcp5c/input.nc,0.194612,32.2196,
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. ],
[ 0. ],
...,
[15.83282 ],
[15.143114],
[14.480141]])
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 0x7f9868a9cf98>]
[6]:
print("Max: ", hydrograph.q_sim.max())
print("Mean: ", hydrograph.q_sim.mean())
print("Monthly means: ", hydrograph.q_sim.groupby(hydrograph.time.dt.month).mean(dim='time'))
Max: <xarray.DataArray 'q_sim' ()>
array(130.83915822)
Mean: <xarray.DataArray 'q_sim' ()>
array(48.38849311)
Monthly means: <xarray.DataArray 'q_sim' (month: 12, nbasins: 1)>
array([[ 6.24447576],
[ 2.55609967],
[ 9.11604814],
[62.74908332],
[92.79608734],
[60.44819211],
[59.04811531],
[57.81612411],
[63.59451548],
[53.42205803],
[80.20394387],
[32.01984468]])
Coordinates:
basin_name (nbasins) object ...
* month (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Dimensions without coordinates: nbasins