{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Use of the wflow OpenStreams framework API to connect a reservoir model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This ipython notebook demonstrates how to load an openstreams python model, execute it step-by-step and investigate the (intermediate) results. It alsoe show how to re-route surface water through a reservoir model. The first steps is to load the model and framework:\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# First import the model. Here we use the HBV version\n", "from wflow.wflow_sbm import *\n", "import IPython\n", "from IPython.display import display, clear_output\n", "\n", "#clear_output = IPython.core.display.clear_output" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# Here we define a simple fictious reservoir\n", "reservoirstorage = 1800\n", "\n", "def simplereservoir(inputq,storage):\n", " K = 0.087\n", " storage = storage + inputq\n", " outflow = storage * K\n", " storage = storage - outflow\n", " \n", " return outflow, storage" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Set model run-time parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Set the:\n", " \n", " - start and time time\n", " - set the runid (this is where the results are stored, relative to the casename)\n", " - set the name of the configfile (stire in the case directory\n", " - set the clone mape (usually the wflow_subcatch.map)\n", " - set the casename. This is where all the model the model resides\n", "\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define start and stop time of the run\n", "startTime = 1\n", "stopTime = 200\n", "currentTime = 1\n", " \n", "# set runid, cl;onemap and casename. Also define the ini file\n", "runId = \"reservoirtest_1\"\n", "#configfile=\"wflow_hbv_mem.ini\"\n", "configfile=\"wflow_sbm.ini\"\n", "wflow_cloneMap = 'wflow_subcatch.map' \n", "\n", "# the casename points to the complete model setup with both static and dynamic input\n", "caseName=\"../../examples/wflow_rhine_sbm/\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "#make a usermodel object \n", "myModel = WflowModel(wflow_cloneMap, caseName,runId,configfile)\n", "\n", "# initialise the framework\n", "dynModelFw = wf_DynamicFramework(myModel, stopTime,startTime)\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "dynModelFw.createRunId(NoOverWrite=False,level=logging.DEBUG)\n", "dynModelFw.setQuiet(1)\n", "# Run the initial part of the model (reads parameters and sets initial values)\n", "dynModelFw._runInitial() # Runs initial part" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,565 - model - DEBUG - File logging to ../../examples/wflow_rhine_sbm//reservoirtest_1/wflow.log\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,566 - model - INFO - model no version Case: ../../examples/wflow_rhine_sbm/ Runid: reservoirtest_1\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,569 - model - INFO - Adding csv output at ../../examples/wflow_rhine_sbm//staticmaps/area.map\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,572 - model - INFO - Adding tss output at ../../examples/wflow_rhine_sbm//staticmaps/wflow_gauges.map\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,572 - model - INFO - running for 200 timesteps\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,582 - model - INFO - Linking parameters to landuse, catchment and soil...\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,582 - model - WARNING - tbl file not found (../../examples/wflow_rhine_sbm///intbl/RunoffGeneratingGWPerc.tbl) returning default value: 0.1\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,586 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/MaxCanopyStorage.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,589 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/EoverR.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,594 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/CanopyGapFraction.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,601 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/RootingDepth.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,604 - model - WARNING - tbl file not found (../../examples/wflow_rhine_sbm///intbl/rootdistpar.tbl) returning default value: -8000\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,608 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/InfiltCapSoil.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,608 - model - WARNING - tbl file not found (../../examples/wflow_rhine_sbm///intbl/CapScale.tbl) returning default value: 100.0\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,612 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/InfiltCapPath.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,617 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/MaxLeakage.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,621 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/PathFrac.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,628 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/FirstZoneCapacity.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,632 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/thetaR.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,637 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/thetaS.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,644 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/FirstZoneMinCapacity.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,650 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/FirstZoneKsatVer.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,654 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/M.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,660 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/N.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,664 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/N_River.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,667 - model - WARNING - tbl file not found (../../examples/wflow_rhine_sbm///intbl/WaterFrac.tbl) returning default value: 0.0\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,671 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/TTI.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,674 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/TT.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,678 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/Cfmax.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,684 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/WHC.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,686 - model - WARNING - tbl file not found (../../examples/wflow_rhine_sbm///intbl/w_soil.tbl) returning default value: 0.1125\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,690 - model - INFO - Creating map from table: ../../examples/wflow_rhine_sbm///intbl/cf_soil.tbl\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,756 - model - INFO - Initializing of model variables..\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,793 - model - INFO - Saving summary maps...\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,865 - model - INFO - End of initial section\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "dynModelFw._runResume() # gets the state variables from disk\n", "# Get list of variables supplied by the model\n", "#print dynModelFw.wf_supplyVariableNamesAndRoles()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:36,868 - model - INFO - Setting initial conditions from state files\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Here we make a pit in the middle of the main river. This will be the inflow to the reservoir" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A pit can be set in the ldd by specifying the direction 5 \n", "# (see pcraster.eu for the ldd direction conventions)\n", "ret = dynModelFw.wf_setValueLdd(\"TopoLdd\",5.0,8.40943,49.6682)\n", "report(myModel.TopoLdd,\"n_ldd.map\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:530: RuntimeWarning: invalid value encountered in absolute\n", " col_ = absolute(diffx) <= (XX[0,0] * tolerance) # cellsize\n", "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:530: RuntimeWarning: invalid value encountered in less_equal\n", " col_ = absolute(diffx) <= (XX[0,0] * tolerance) # cellsize\n", "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:531: RuntimeWarning: invalid value encountered in absolute\n", " row_ = absolute(diffy) <= (XX[0,0] * tolerance)# cellsize\n", "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:531: RuntimeWarning: invalid value encountered in less_equal\n", " row_ = absolute(diffy) <= (XX[0,0] * tolerance)# cellsize\n" ] } ], "prompt_number": "*" }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Run for a number of timesteps" ] }, { "cell_type": "code", "collapsed": false, "input": [ "f, ax = plt.subplots(1,3,figsize=(16, 6))\n", "\n", "plotar = []\n", "plotarstorage = []\n", "plotaroutflow = []\n", "\n", "for ts in range(1,3):\n", " \n", "\n", " # Add inflow to outflow downstream of the pit\n", " # See the API setion of the INI file\n", " \n", " \n", " \n", " # Get Q value at pit, the reservoir inflow\n", " inflowQ = dynModelFw.wf_supplyScalar(\"SurfaceRunoff\",8.40943,49.6682)\n", " # save for plotting\n", " plotar.append(inflowQ) \n", " # Feed to the reservoir model\n", " outflow, reservoirstorage = simplereservoir(inflowQ, reservoirstorage)\n", " # save for plotting\n", " plotarstorage.append(reservoirstorage)\n", " plotaroutflow.append(outflow)\n", " #dynModelFw._userModel().IF = cover(0.0)\n", " dynModelFw.wf_setValue(\"IF\", outflow ,8.40943,49.7085)\n", " # update runoff ONLY NEEDED IF YOU FIDDLE WITH THE KIN_WAVE RESERVOIR\n", " myModel.updateRunOff()\n", " \n", " \n", " \n", " dynModelFw._runDynamic(ts,ts) # runs for this timesteps\n", "\n", " \n", " # Now get some results for display\n", " run = dynModelFw.wf_supplyMapAsNumpy(\"SurfaceRunoff\")\n", " uz = dynModelFw.wf_supplyMapAsNumpy(\"FirstZoneCapacity\")\n", " sm = dynModelFw.wf_supplyMapAsNumpy(\"UStoreDepth\")\n", " \n", " sm[sm == -999] = np.nan\n", " uz[uz == -999] = np.nan\n", " run[run == -999] = np.nan\n", " ax[0].imshow(log(run))\n", " ax[1].plot(plotarstorage,'k')\n", " ax[1].set_title(\"Reservoir storage\")\n", " ax[2].plot(plotar,'b')\n", " ax[2].plot(plotaroutflow,'r')\n", " \n", " ax[2].set_title(\"Blue inflow, red outflow:\" + str(ts))\n", " #clear_output()\n", " #display(f)\n", " \n", "plt.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:558: RuntimeWarning: invalid value encountered in absolute\n", " col_ = absolute(diffx) <= (XX[0,0] * tolerance) # cellsize\n", "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:558: RuntimeWarning: invalid value encountered in less_equal\n", " col_ = absolute(diffx) <= (XX[0,0] * tolerance) # cellsize\n", "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:559: RuntimeWarning: invalid value encountered in absolute\n", " row_ = absolute(diffy) <= (XX[0,0] * tolerance)# cellsize\n", "C:\\Users\\schelle\\AppData\\Local\\Continuum\\Anaconda\\lib\\site-packages\\wflow\\wflow_lib.py:559: RuntimeWarning: invalid value encountered in less_equal\n", " row_ = absolute(diffy) <= (XX[0,0] * tolerance)# cellsize\n", "2014-05-06 08:57:37,026 - model - DEBUG - IF is not defined in the usermodel\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "." ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:37,030 - model - DEBUG - Step: 1/1\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:37,032 - model - WARNING - Variable: IFnot set by API, returning default\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "413.795681013\n", "[[ 1.00000000e+00 1.00000000e+00 1.00000000e+00 ..., 6.30736691e+09\n", " 1.00000000e+00 -1.12416583e+27]\n", " [ 1.00000000e+00 1.00000000e+00 1.00000000e+00 ..., 2.87500000e+00\n", " 1.00000000e+00 2.87500000e+00]\n", " [ 1.00000000e+00 1.00000000e+00 6.30740787e+09 ..., -1.12416583e+27\n", " 1.00000000e+00 1.00000000e+00]\n", " ..., \n", " [ 1.00000000e+00 1.00000000e+00 1.00000000e+00 ..., 1.00000000e+00\n", " 1.00000000e+00 3.36830131e+09]\n", " [ 1.00000000e+00 1.00000000e+00 1.00000000e+00 ..., 1.00000000e+00\n", " 1.00000000e+00 1.00000000e+00]\n", " [ 1.00000000e+00 1.00000000e+00 1.62345619e+01 ..., 1.00000000e+00\n", " 1.00000000e+00 1.00000000e+00]]\n", "." ] }, { "output_type": "stream", "stream": "stderr", "text": [ "2014-05-06 08:57:37,191 - model - DEBUG - Step: 2/2\n" ] } ], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "dynModelFw._runSuspend() # saves the state variables\n", "dynModelFw._wf_shutdown()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" } ], "metadata": {} } ] }