{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Hydrological modelling - Raven (distributed)\n", "\n", "`xHydro` provides a collection of functions designed to facilitate hydrological modelling, focusing on two key models: [HYDROTEL](https://github.com/INRS-Modelisation-hydrologique/hydrotel) and a suite of models emulated by the [Raven Hydrological Framework](https://raven.uwaterloo.ca/). It is important to note that Raven already possesses an extensive Python library, [RavenPy](https://github.com/CSHS-CWRA/RavenPy), which enables users to build, calibrate, and execute models. `xHydro` wraps some of these functions to support multi-model assessments with HYDROTEL, though users seeking advanced functionalities may prefer to use `RavenPy` directly. \n", "\n", "The primary contribution of `xHydro` to hydrological modelling is thus its support for HYDROTEL, a model that previously lacked a dedicated Python library. This Notebook covers `RavenPy` models, but a similar notebook for `HYDROTEL` is available [here](../hydrological_modelling_hydrotel.ipynb).\n", "\n", "## Basic information" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "from IPython.display import clear_output\n", "\n", "import xhydro as xh\n", "import xhydro.modelling as xhm\n", "\n", "clear_output(wait=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": { "editable": true, "nbsphinx": "hidden", "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import logging\n", "\n", "logger = logging.getLogger()\n", "logger.setLevel(logging.CRITICAL)" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "The `xHydro` modelling framework is based on a `model_config` dictionary, which is meant to contain all necessary information to execute a given hydrological model. For example, depending on the model, it can store meteorological datasets directly, paths to datasets (netCDF files or other), csv configuration files, parameters, and basically anything that is required to configure and execute an hydrological model.\n", "\n", "The list of required inputs for the dictionary can be obtained one of two ways. The first is to look at the hydrological model's class, such as `xhydro.modelling.RavenpyModel`. The second is to use the `xh.modelling.get_hydrological_model_inputs` function to get a list of the required keys for a given model, as well as the documentation." ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "help(xhm.get_hydrological_model_inputs)" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "# This function can be called to get a list of the keys for a given model, as well as its documentation.\n", "inputs, docs = xhm.get_hydrological_model_inputs(\"HBVEC\", required_only=False)\n", "inputs" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "print(docs)" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "HYDROTEL and Raven vary in terms of required inputs and available functions, but an effort will be made to standardize the outputs as much as possible. Currently, all models include the following three functions:\n", "\n", "- `.run()`: Executes the model, reformats the outputs to be compatible with analysis tools in `xHydro`, and returns the simulated streamflow as a `xarray.Dataset`.\n", " - The streamflow variable will be named `q` and will have units of `m3 s-1`.\n", " - For 1D data (such as hydrometric stations), the corresponding dimension in the dataset will be identified by the `cf_role: timeseries_id` attribute.\n", " \n", "- `.get_inputs()`: Retrieves the meteorological inputs used by the model.\n", "\n", "- `.get_outputs()`: Retrieves the simulated outputs from the model.\n", " - Use `.get_outputs(\"q\")` to obtain the simulated streamflow as a `xarray.Dataset`.\n", "\n", "- `.standardize_outputs()`: Standardizes the outputs to ensure consistency across different models, facilitating comparison and analysis. This function is used by default in the `.run()` method, but can also be called separately if needed." ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Initializing and running a calibrated model\n", "Raven requires several `.rv*` files to control various aspects such as meteorological inputs, watershed characteristics, and more. If the project directory already exists and contains data, `xHydro` will prepare the model for execution without overwriting existing `.rv*` files—unless the `overwrite` argument is explicitly set to `True`. To force overwriting of these files, you can thus either:\n", "\n", "- Set `overwrite=True` in the `model_config` when instantiating the model\n", "- Use the `.create_rv(overwrite=True)` method on the instantiated model.\n", "\n", "This Notebook will focus on distributed RavenPy models. For lumped models, refer to the [Raven lumped modelling notebook](../hydrological_modelling_raven.ipynb).\n", "\n", "### Formatting HRU Data for distributed models\n", "\n", "Raven relies on Hydrological Response Units (HRUs) for its hydrological simulations, which are typically generated using the [BasinMaker](https://hydrology.uwaterloo.ca/basinmaker/) tool, and specifically its `.Generate_HRUs` function. Thus, `xHydro`'s distributed modelling is currently based on the BasinMaker HRU structure and nomenclature, and uses the `BasinMakerExtractor` class of `RavenPy` to extract the necessary HRU attributes from a shapefile.\n", "\n", "Additionally, while BasinMaker will produce attributes such as `Landuse_ID`, these will not be passed on to the `RavenPy` model. Instead, the HRU should contain relevant land use attributes that can be directly mapped to the hydrological model's arguments. For example, for the HBV-EC model, which is currently the only distributed model available in `Raven`, the following attributes are used instead: `LAND_USE_C`, `VEG_C`, and `SOIL_PROF`, which represent land use, vegetation, and soil profile respectively.\n", "\n", "