ResORR
  • Home

How to run ResORR

  • Network generation
  • Run ResORR model
ResORR
  • How to run ResORR
  • Run ResORR model
  • Edit on GitHub

In [5]:
Copied!
import geopandas as gpd
from pathlib import Path
import warnings
import hvplot.xarray
import pandas as pd
import xarray as xr
import rioxarray as rxr
import numpy as np

import networkx as nx
import geonetworkx as gnx
import geopandas as gpd from pathlib import Path import warnings import hvplot.xarray import pandas as pd import xarray as xr import rioxarray as rxr import numpy as np import networkx as nx import geonetworkx as gnx

Read in the flow direction, reservoir locations, and path to a directory where the outputs will be saved.

In [7]:
Copied!
# read in the flow direction file
flow_dir_fn = Path("../../data/cumberland/flow-directions.tif")
res_location_fn = Path("../../data/cumberland/cumberland-stations.csv")
save_dir = Path("../../data/cumberland/regulation_data")
network_dir = Path("../../data/cumberland/regulation_data/")
# read in the flow direction file flow_dir_fn = Path("../../data/cumberland/flow-directions.tif") res_location_fn = Path("../../data/cumberland/cumberland-stations.csv") save_dir = Path("../../data/cumberland/regulation_data") network_dir = Path("../../data/cumberland/regulation_data/")

We will be usign the generate_forcings_from_rat function to generate the forcings for the reservoirs.

In [8]:
Copied!
from resorr.data_prep import generate_forcings_from_rat, generate_network
from resorr.network import ReservoirNetwork
from tqdm.notebook import tqdm

help(generate_forcings_from_rat)
from resorr.data_prep import generate_forcings_from_rat, generate_network from resorr.network import ReservoirNetwork from tqdm.notebook import tqdm help(generate_forcings_from_rat)
Help on function generate_forcings_from_rat in module resorr.data_prep:

generate_forcings_from_rat(reservoir_network, inflow_dir, storage_change_dir, save_dir, aggregate_freq='daily', rat_output_level='final_outputs')
    Args:
        reservoir_network (gnx.GeoDiGraph or str or pathlib.Path): reservoir network as GeoDiGraph or path to directory containing files generated by generate_network
        inflow_dir (str or Path): path to directory containing inflow files
        storage_change_dir (str or Path): path to directory containing storage change files
        save_dir (str or Path): directory to save regulation data to

This function requires the reservoir network as an argument. We can use the network we created in the previous notebook.

In [9]:
Copied!
edges_fn = network_dir / "rivreg_network.shp"
nodes_fn = network_dir / "rivreg_network_pts.shp"
G = gnx.read_geofiles(nodes_fn, edges_fn, directed=True)
G
edges_fn = network_dir / "rivreg_network.shp" nodes_fn = network_dir / "rivreg_network_pts.shp" G = gnx.read_geofiles(nodes_fn, edges_fn, directed=True) G
Out[9]:
<geonetworkx.geodigraph.GeoDiGraph at 0x17f8df3d0>
In [10]:
Copied!
forcings = generate_forcings_from_rat(
    G,
    '../../data/cumberland/unregulated-inflow',
    '../../data/cumberland/satellite-dels',
    '../../data/cumberland/regulation_data',
    rat_output_level='rat_outputs',
    aggregate_freq='daily'
)
forcings
forcings = generate_forcings_from_rat( G, '../../data/cumberland/unregulated-inflow', '../../data/cumberland/satellite-dels', '../../data/cumberland/regulation_data', rat_output_level='rat_outputs', aggregate_freq='daily' ) forcings
Out[10]:
<xarray.Dataset>
Dimensions:                     (time: 3189, node: 8)
Coordinates:
  * time                        (time) datetime64[ns] 2013-01-21 ... 2021-10-14
  * node                        (node) int64 0 1 2 3 4 5 6 7
Data variables:
    theoretical_natural_runoff  (time, node) float64 27.2 6.046 ... nan nan
    storage_change              (time, node) float64 nan nan nan ... nan nan nan
    dt                          (time) int64 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1
xarray.Dataset
    • time: 3189
    • node: 8
    • time
      (time)
      datetime64[ns]
      2013-01-21 ... 2021-10-14
      array(['2013-01-21T00:00:00.000000000', '2013-01-22T00:00:00.000000000',
             '2013-01-23T00:00:00.000000000', ..., '2021-10-12T00:00:00.000000000',
             '2021-10-13T00:00:00.000000000', '2021-10-14T00:00:00.000000000'],
            dtype='datetime64[ns]')
    • node
      (node)
      int64
      0 1 2 3 4 5 6 7
      array([0, 1, 2, 3, 4, 5, 6, 7])
    • theoretical_natural_runoff
      (time, node)
      float64
      27.2 6.046 78.87 ... nan nan nan
      units :
      m3
      long_name :
      Volume of unregulated_inflow
      description :
      Volume of unregulated_inflow in m3
      array([[2.71992555e+01, 6.04631640e+00, 7.88654556e+01, ...,
              1.99280966e+00, 1.57005803e+02, 2.05498073e+02],
             [8.38652182e+02, 1.52481303e+02, 2.31205692e+03, ...,
              6.49593583e+01, 4.25556779e+03, 5.92504474e+03],
             [6.44065731e+03, 3.83798194e+02, 1.32041400e+04, ...,
              5.11122533e+02, 1.76185472e+04, 3.24521577e+04],
             ...,
             [           nan,            nan,            nan, ...,
                         nan,            nan,            nan],
             [           nan,            nan,            nan, ...,
                         nan,            nan,            nan],
             [           nan,            nan,            nan, ...,
                         nan,            nan,            nan]])
    • storage_change
      (time, node)
      float64
      nan nan nan nan ... nan nan nan nan
      units :
      m3
      long_name :
      Volume of storage_change
      description :
      Volume of storage_change in m3
      array([[               nan,                nan,                nan, ...,
                             nan,                nan,                nan],
             [               nan,                nan,                nan, ...,
                             nan,                nan,                nan],
             [               nan,                nan,                nan, ...,
                             nan,                nan,                nan],
             ...,
             [        0.        ,  -3208294.69317595, -89106851.67957063, ...,
                             nan,                nan,                nan],
             [               nan,  -3208294.69317595, -89106851.67957063, ...,
                             nan,                nan,                nan],
             [               nan,  -3208294.69317595, -89106851.67957063, ...,
                             nan,                nan,                nan]])
    • dt
      (time)
      int64
      1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1
      array([1, 1, 1, ..., 1, 1, 1])
    • time
      PandasIndex
      PandasIndex(DatetimeIndex(['2013-01-21', '2013-01-22', '2013-01-23', '2013-01-24',
                     '2013-01-25', '2013-01-26', '2013-01-27', '2013-01-28',
                     '2013-01-29', '2013-01-30',
                     ...
                     '2021-10-05', '2021-10-06', '2021-10-07', '2021-10-08',
                     '2021-10-09', '2021-10-10', '2021-10-11', '2021-10-12',
                     '2021-10-13', '2021-10-14'],
                    dtype='datetime64[ns]', name='time', length=3189, freq='D'))
    • node
      PandasIndex
      PandasIndex(Index([0, 1, 2, 3, 4, 5, 6, 7], dtype='int64', name='node'))
In [11]:
Copied!
start_time = pd.to_datetime('2017-01-01')
end_time = pd.to_datetime('2019-01-01')
forcings = forcings.sel(time=slice(start_time, end_time))

reservoir_network = ReservoirNetwork(G, start_time)

for timestep in tqdm(forcings.time.values):
    dt = forcings['dt'].sel(time=timestep).values.item()
    reservoir_network.update(forcings, dt, 'wb')

reservoir_network.data
start_time = pd.to_datetime('2017-01-01') end_time = pd.to_datetime('2019-01-01') forcings = forcings.sel(time=slice(start_time, end_time)) reservoir_network = ReservoirNetwork(G, start_time) for timestep in tqdm(forcings.time.values): dt = forcings['dt'].sel(time=timestep).values.item() reservoir_network.update(forcings, dt, 'wb') reservoir_network.data
  0%|          | 0/731 [00:00<?, ?it/s]
Out[11]:
<xarray.Dataset>
Dimensions:                     (node: 8, time: 731)
Coordinates:
  * node                        (node) int64 0 1 2 3 4 5 6 7
  * time                        (time) datetime64[ns] 2017-01-01 ... 2019-01-01
Data variables:
    inflow                      (node, time) float64 2.396e+07 ... 1.519e+08
    outflow                     (node, time) float64 0.0 0.0 ... 1.526e+08
    regulated_runoff            (node, time) float64 0.0 0.0 ... 1.193e+08
    natural_runoff              (node, time) float64 2.396e+07 ... 3.268e+07
    theoretical_natural_runoff  (time, node) float64 6.858e+07 ... 1.833e+08
    storage                     (node, time) float64 nan nan nan ... nan nan nan
    storage_change              (time, node) float64 nan nan ... -6.471e+05
    regulation                  (node, time) float64 4.461e+07 ... 3.133e+07
    dt                          (time) int64 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1
xarray.Dataset
    • node: 8
    • time: 731
    • node
      (node)
      int64
      0 1 2 3 4 5 6 7
      array([0, 1, 2, 3, 4, 5, 6, 7])
    • time
      (time)
      datetime64[ns]
      2017-01-01 ... 2019-01-01
      array(['2017-01-01T00:00:00.000000000', '2017-01-02T00:00:00.000000000',
             '2017-01-03T00:00:00.000000000', ..., '2018-12-30T00:00:00.000000000',
             '2018-12-31T00:00:00.000000000', '2019-01-01T00:00:00.000000000'],
            dtype='datetime64[ns]')
    • inflow
      (node, time)
      float64
      2.396e+07 2.75e+07 ... 1.519e+08
      array([[2.39620492e+07, 2.74960709e+07, 2.94775076e+07, ...,
              1.68835688e+08, 1.79235778e+08, 2.11188723e+08],
             [8.37681482e+05, 8.17938270e+05, 8.30171148e+05, ...,
              6.03473013e+06, 4.46528894e+06, 4.49821945e+06],
             [2.05707675e+07, 1.73575104e+07, 1.74529257e+07, ...,
              9.30281854e+07, 1.00523384e+08, 9.14187304e+07],
             ...,
             [3.96313343e+04, 4.68511375e+04, 4.32890042e+04, ...,
              1.92788987e+05, 1.49741982e+05, 4.17031046e+05],
             [7.84186965e+06, 6.80888972e+06, 6.93460287e+06, ...,
              4.42991980e+07, 4.50274508e+07, 4.30045007e+07],
             [6.32450756e+06, 8.27468602e+06, 5.22480383e+06, ...,
              1.17223205e+08, 1.46441883e+08, 1.51933452e+08]])
    • outflow
      (node, time)
      float64
      0.0 0.0 0.0 ... 1.471e+08 1.526e+08
      array([[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              1.74731558e+08, 1.85131648e+08, 2.17084593e+08],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              5.50604686e+06, 3.93660566e+06, 3.96953618e+06],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              8.01273915e+07, 8.76225905e+07, 7.85179364e+07],
             ...,
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              2.78114192e+07, 2.85396720e+07, 2.65167220e+07],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              1.17870324e+08, 1.47089002e+08, 1.52580570e+08]])
    • regulated_runoff
      (node, time)
      float64
      0.0 0.0 0.0 ... 1.293e+08 1.193e+08
      array([[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              1.14259837e+08, 1.46802236e+08, 1.66626437e+08],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              5.50604686e+06, 3.93660566e+06, 3.96953618e+06],
             ...,
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
             [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
              1.21313437e+08, 1.29296180e+08, 1.19255518e+08]])
    • natural_runoff
      (node, time)
      float64
      2.396e+07 2.75e+07 ... 3.268e+07
      array([[ 2.39620492e+07,  2.74960709e+07,  2.94775076e+07, ...,
               5.45758505e+07,  3.24335414e+07,  4.45622863e+07],
             [ 8.37681482e+05,  8.17938270e+05,  8.30171148e+05, ...,
               6.03473013e+06,  4.46528894e+06,  4.49821945e+06],
             [ 2.05707675e+07,  1.73575104e+07,  1.74529257e+07, ...,
               8.75221385e+07,  9.65867788e+07,  8.74491942e+07],
             ...,
             [ 3.96313343e+04,  4.68511375e+04,  4.32890042e+04, ...,
               1.92788987e+05,  1.49741982e+05,  4.17031046e+05],
             [ 7.84186965e+06,  6.80888972e+06,  6.93460287e+06, ...,
               4.42991980e+07,  4.50274508e+07,  4.30045007e+07],
             [ 6.32450756e+06,  8.27468602e+06,  5.22480383e+06, ...,
              -4.09023122e+06,  1.71457032e+07,  3.26779340e+07]])
    • theoretical_natural_runoff
      (time, node)
      float64
      6.858e+07 8.377e+05 ... 1.833e+08
      units :
      m3
      long_name :
      Volume of unregulated_inflow
      description :
      Volume of unregulated_inflow in m3
      array([[6.85754254e+07, 8.37681482e+05, 2.14084490e+07, ...,
              3.96313343e+04, 7.84186965e+06, 3.80670817e+07],
             [6.99518201e+07, 8.17938270e+05, 1.81754487e+07, ...,
              4.68511375e+04, 6.80888972e+06, 3.55198286e+07],
             [6.98042848e+07, 8.30171148e+05, 1.82830968e+07, ...,
              4.32890042e+04, 6.93460287e+06, 3.25882529e+07],
             ...,
             [1.99414756e+08, 6.03473013e+06, 9.35568687e+07, ...,
              1.92788987e+05, 4.42991980e+07, 1.48557264e+08],
             [2.09771799e+08, 4.46528894e+06, 1.01052068e+08, ...,
              1.49741982e+05, 4.50274508e+07, 1.77775942e+08],
             [2.41992033e+08, 4.49821945e+06, 9.19474136e+07, ...,
              4.17031046e+05, 4.30045007e+07, 1.83267510e+08]])
    • storage
      (node, time)
      float64
      nan nan nan nan ... nan nan nan nan
      array([[nan, nan, nan, ..., nan, nan, nan],
             [nan, nan, nan, ..., nan, nan, nan],
             [nan, nan, nan, ..., nan, nan, nan],
             ...,
             [nan, nan, nan, ..., nan, nan, nan],
             [nan, nan, nan, ..., nan, nan, nan],
             [nan, nan, nan, ..., nan, nan, nan]])
    • storage_change
      (time, node)
      float64
      nan nan ... 1.649e+07 -6.471e+05
      units :
      m3
      long_name :
      Volume of storage_change
      description :
      Volume of storage_change in m3
      array([[              nan,               nan,               nan, ...,
                            nan,               nan,               nan],
             [              nan,               nan,               nan, ...,
                            nan,               nan,               nan],
             [              nan,               nan,               nan, ...,
                            nan,               nan,               nan],
             ...,
             [-5895870.18026576,   528683.27251331, 12900793.9402434 , ...,
                492957.33721251, 16487778.77497361,  -647118.62264429],
             [-5895870.18026576,   528683.27251331, 12900793.9402434 , ...,
                492957.33721251, 16487778.77497361,  -647118.62264429],
             [-5895870.18026576,   528683.27251331, 12900793.9402434 , ...,
                492957.33721251, 16487778.77497361,  -647118.62264429]])
    • regulation
      (node, time)
      float64
      4.461e+07 4.246e+07 ... 3.133e+07
      array([[44613376.16772307, 42455749.19536785, 40326777.12761419, ...,
              30579068.07979456, 30536021.07421207, 30803310.13803044],
             [       0.        ,        0.        ,        0.        , ...,
                     0.        ,        0.        ,        0.        ],
             [  837681.4815538 ,   817938.26985242,   830171.14775642, ...,
                528683.27251332,   528683.27251332,   528683.27251332],
             ...,
             [       0.        ,        0.        ,        0.        , ...,
                     0.        ,        0.        ,        0.        ],
             [       0.        ,        0.        ,        0.        , ...,
                     0.        ,        0.        ,        0.        ],
             [31742574.16840874, 27245142.55631958, 27363449.07702802, ...,
              31334058.5424273 , 31334058.5424273 , 31334058.5424273 ]])
    • dt
      (time)
      int64
      1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1
      array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1])
    • node
      PandasIndex
      PandasIndex(Index([0, 1, 2, 3, 4, 5, 6, 7], dtype='int64', name='node'))
    • time
      PandasIndex
      PandasIndex(DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
                     '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
                     '2017-01-09', '2017-01-10',
                     ...
                     '2018-12-23', '2018-12-24', '2018-12-25', '2018-12-26',
                     '2018-12-27', '2018-12-28', '2018-12-29', '2018-12-30',
                     '2018-12-31', '2019-01-01'],
                    dtype='datetime64[ns]', name='time', length=731, freq='D'))
In [12]:
Copied!
reservoir_network.data.to_netcdf('../../data/cumberland/regulation_data/resorr_outputs.nc')
reservoir_network.data.to_netcdf('../../data/cumberland/regulation_data/resorr_outputs.nc')
In [22]:
Copied!
ds = reservoir_network.data

modeled_inflow = ds['inflow'].sel(node=0).hvplot(label='With regulation: Modeled inflow')
tnr = ds['theoretical_natural_runoff'].sel(node=0).hvplot(label='Without regulation: TNR')

modeled_inflow * tnr
ds = reservoir_network.data modeled_inflow = ds['inflow'].sel(node=0).hvplot(label='With regulation: Modeled inflow') tnr = ds['theoretical_natural_runoff'].sel(node=0).hvplot(label='Without regulation: TNR') modeled_inflow * tnr
Out[22]:
Previous

Built with MkDocs using a theme provided by Read the Docs.