{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Model API Example\n", "\n", "In this notebook, we'll explore some functionality of the models of this package. We'll work with the coupled CemaneigeGR4j model that is implemented in `rrmpg.models` module. The data we'll use, comes from the CAMELS [1] data set. For some basins, the data is provided within this Python library and can be easily imported using the `CAMELSLoader` class implemented in the `rrmpg.data` module.\n", "\n", "In summary we'll look at:\n", "- How you can create a model instance.\n", "- How we can use the CAMELSLoader.\n", "- How you can fit the model parameters to observed discharge by:\n", " - Using one of SciPy's global optimizer\n", " - Monte-Carlo-Simulation\n", "- How you can use a fitted model to calculate the simulated discharge.\n", "\n", "\n", "[1] Addor, N., A.J. Newman, N. Mizukami, and M.P. Clark, 2017: The CAMELS data set: catchment attributes and meteorology for large-sample studies. version 2.0. Boulder, CO: UCAR/NCAR. doi:10.5065/D6G73C3Q" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Imports and Notebook setup\n", "from timeit import timeit\n", "\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "from rrmpg.models import CemaneigeGR4J\n", "from rrmpg.data import CAMELSLoader\n", "from rrmpg.tools.monte_carlo import monte_carlo\n", "from rrmpg.utils.metrics import calc_nse" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a model\n", "\n", "As a first step let us have a look how we can create one of the models implemented in `rrmpg.models`. Basically, for all models we have two different options:\n", "1. Initialize a model **without** specific model parameters.\n", "2. Initialize a model **with** specific model parameters.\n", "\n", "The [documentation](http://rrmpg.readthedocs.io) provides a list of all model parameters. Alternatively we can look at `help()` for the model (e.g. `help(CemaneigeGR4J)`).\n", "\n", "If no specific model parameters are provided upon intialization, random parameters will be generated that are in between the default parameter bounds. We can look at these bounds by calling `.get_param_bounds()` method on the model object and check the current parameter values by calling `.get_params()` method.\n", "\n", "For now we don't know any specific parameter values, so we'll create one with random parameters." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'CTG': 0.3399735717656279,\n", " 'Kf': 0.8724652383290821,\n", " 'x1': 427.9652389107806,\n", " 'x2': 0.9927197563086638,\n", " 'x3': 288.20205223188475,\n", " 'x4': 1.4185137324914372}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = CemaneigeGR4J()\n", "model.get_params()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we can see the six model parameters of CemaneigeGR4J model and their current values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the CAMELSLoader\n", "To have data to start with, we can use the `CAMELSLoader` class to load data of provided basins from the CAMELS dataset. To get a list of all available basins that are provided within this library, we can use the `.get_basin_numbers()` method. For now we will use the provided basin number `01031500`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", " | dayl(s) | \n", "prcp(mm/day) | \n", "srad(W/m2) | \n", "swe(mm) | \n", "tmax(C) | \n", "tmin(C) | \n", "vp(Pa) | \n", "PET | \n", "QObs(mm/d) | \n", "
---|---|---|---|---|---|---|---|---|---|
1980-10-01 | \n", "41050.80 | \n", "0.00 | \n", "286.90 | \n", "0.0 | \n", "16.19 | \n", "4.31 | \n", "825.78 | \n", "1.5713 | \n", "0.5550 | \n", "
1980-10-02 | \n", "40780.81 | \n", "2.08 | \n", "195.94 | \n", "0.0 | \n", "13.46 | \n", "5.72 | \n", "920.18 | \n", "1.2619 | \n", "0.4979 | \n", "
1980-10-03 | \n", "40435.21 | \n", "5.57 | \n", "172.60 | \n", "0.0 | \n", "17.84 | \n", "8.61 | \n", "1128.70 | \n", "1.2979 | \n", "0.5169 | \n", "
1980-10-04 | \n", "40435.21 | \n", "23.68 | \n", "170.45 | \n", "0.0 | \n", "16.28 | \n", "7.32 | \n", "1027.91 | \n", "1.2251 | \n", "1.5634 | \n", "
1980-10-05 | \n", "40089.58 | \n", "3.00 | \n", "113.83 | \n", "0.0 | \n", "10.51 | \n", "5.01 | \n", "881.61 | \n", "0.9116 | \n", "2.8541 | \n", "