Skip to content

Commit

Permalink
remove references to epiweeks for now
Browse files Browse the repository at this point in the history
  • Loading branch information
thurber committed Jun 8, 2021
1 parent 7fc56f1 commit 6c832e2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 42 deletions.
4 changes: 2 additions & 2 deletions mosartwmpy/config_defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ water_management:
latitude: lat
# streamflow field name [m3/s]
streamflow: Qmon
# streamflow time resolution - 'month' or 'epiweek'
# streamflow time resolution - only 'month' supported so far
streamflow_time_resolution: month
# demand field name [m3/s]
demand: demand
# demand time resolution - 'month' or 'epiweek'
# demand time resolution - only 'month' supported so far
demand_time_resolution: month
# grid id to reservoir id mapping field
grid_to_reservoir: gridID_from_Dam
Expand Down
4 changes: 0 additions & 4 deletions mosartwmpy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from benedict import benedict
from bmipy import Bmi
from datetime import datetime, time, timedelta
# from epiweeks import Week
from pathlib import Path
from pathvalidate import sanitize_filename
from timeit import default_timer as timer
Expand Down Expand Up @@ -182,7 +181,6 @@ def update(self) -> None:
if self.config.get('water_management.enabled', False):
if self.config.get('water_management.demand.read_from_file', False):
# only read new demand and compute new release if it's the very start of simulation or new time period
# TODO this currently assumes monthly demand input
if self.current_time == datetime.combine(self.config.get('simulation.start_date'), time.min) or self.current_time == datetime(self.current_time.year, self.current_time.month, 1):
logging.debug(f'Reading demand rate input from file.')
# load the demand from file
Expand All @@ -193,8 +191,6 @@ def update(self) -> None:
self.state.grid_cell_supply[:] = 0
self.state.grid_cell_unmet_demand[:] = 0
# get streamflow for this time period
# TODO this is still written assuming monthly, but here's the epiweek for when that is relevant
# epiweek = Week.fromdate(self.current_time).week
month = self.current_time.month
streamflow_time_name = self.config.get('water_management.reservoirs.streamflow_time_resolution')
self.state.reservoir_streamflow[:] = self.grid.reservoir_streamflow_schedule.sel({streamflow_time_name: month}).values
Expand Down
9 changes: 3 additions & 6 deletions mosartwmpy/reservoirs/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def load_reservoirs(self, config: Benedict, parameters: Parameters) -> None:
# index by grid cell
self.reservoir_to_grid_mapping = self.reservoir_to_grid_mapping.set_index('grid_cell_id')

# prepare the month or epiweek based reservoir schedules mapped to the domain
# prepare the month based reservoir schedules mapped to the domain
prepare_reservoir_schedule(self, config, parameters, reservoirs)

reservoirs.close()
Expand All @@ -75,8 +75,6 @@ def prepare_reservoir_schedule(self, config: Benedict, parameters: Parameters, r
# the reservoir streamflow and demand are specified by the time resolution and reservoir id
# so let's remap those to the actual mosart domain for ease of use

# TODO i had wanted to convert these all to epiweeks no matter what format provided, but we don't know what year all the data came from

# streamflow flux
streamflow_time_name = config.get('water_management.reservoirs.streamflow_time_resolution')
streamflow = reservoirs[config.get('water_management.reservoirs.streamflow')]
Expand All @@ -92,7 +90,7 @@ def prepare_reservoir_schedule(self, config: Benedict, parameters: Parameters, r
else:
schedule = concat([schedule, sched], dim=streamflow_time_name)
self.reservoir_streamflow_schedule = schedule.assign_coords(
# if monthly, convert to 1 based month index (instead of starting from 0)
# convert to 1 based month index (instead of starting from 0)
{streamflow_time_name: (streamflow_time_name, schedule[streamflow_time_name].values + (1 if streamflow_time_name == 'month' else 0))}
).streamflow

Expand All @@ -110,12 +108,11 @@ def prepare_reservoir_schedule(self, config: Benedict, parameters: Parameters, r
else:
schedule = concat([schedule, sched], dim=demand_time_name)
self.reservoir_demand_schedule = schedule.assign_coords(
# if monthly, convert to 1 based month index (instead of starting from 0)
# convert to 1 based month index (instead of starting from 0)
{demand_time_name: (demand_time_name, schedule[demand_time_name].values + (1 if demand_time_name == 'month' else 0))}
).demand

# initialize prerelease based on long term mean flow and demand (Biemans 2011)
# TODO this assumes demand and flow use the same timescale :(
flow_avg = self.reservoir_streamflow_schedule.mean(dim=streamflow_time_name)
demand_avg = self.reservoir_demand_schedule.mean(dim=demand_time_name)
prerelease = (1.0 * self.reservoir_streamflow_schedule)
Expand Down
33 changes: 3 additions & 30 deletions mosartwmpy/reservoirs/reservoirs.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import numpy as np

from datetime import datetime
# from epiweeks import Week
from benedict.dicts import benedict as Benedict

from mosartwmpy.config.parameters import Parameters
from mosartwmpy.state.state import State
from mosartwmpy.grid.grid import Grid

# TODO in fortran mosart there is a StorCalibFlag that affects how storage targets are calculated -- code so far is written assuming that it == 0

def reservoir_release(state, grid, config, parameters, current_time):
# compute release from reservoirs

# TODO so much logic was dependent on monthly, so still assuming monthly for now, but here's the epiweek for when that is relevant
# epiweek = Week.fromdate(current_time).week

month = current_time.month

# if it's the start of the operational year for the reservoir, set it's start of op year storage to the current storage
Expand All @@ -30,9 +26,7 @@ def reservoir_release(state, grid, config, parameters, current_time):

def regulation_release(state, grid, config, parameters, current_time):
# compute the expected monthly release based on Biemans (2011)

# TODO this is still written assuming monthly, but here's the epiweek for when that is relevant
# epiweek = Week.fromdate(current_time).week

month = current_time.month
streamflow_time_name = config.get('water_management.reservoirs.streamflow_time_resolution')

Expand Down Expand Up @@ -77,8 +71,6 @@ def storage_targets(state: State, grid: Grid, config: Benedict, parameters: Para

# TODO the logic here is really hard to follow... can it be simplified or made more readable?

# TODO this is still written assuming monthly, but here's the epiweek for when that is relevant
# epiweek = Week.fromdate(current_time).week
month = current_time.month
streamflow_time_name = config.get('water_management.reservoirs.streamflow_time_resolution')

Expand All @@ -96,7 +88,7 @@ def storage_targets(state: State, grid: Grid, config: Benedict, parameters: Para
)
drop = 0 * state.reservoir_month_flood_control_start
n_month = 0 * drop
for m in np.arange(1,13): # TODO assumes monthly
for m in np.arange(1,13):
m_and_condition = (m >= state.reservoir_month_flood_control_start) & (m < state.reservoir_month_flood_control_end)
m_or_condition = (m >= state.reservoir_month_flood_control_start) | (m < state.reservoir_month_flood_control_end)
drop = np.where(
Expand Down Expand Up @@ -128,25 +120,6 @@ def storage_targets(state: State, grid: Grid, config: Benedict, parameters: Para
(month >= state.reservoir_month_flood_control_end) |
(month < state.reservoir_month_start_operations)
)
# TODO this logic exists in fortran mosart but isn't used...
# fill = 0 * drop
# n_month = 0 * drop
# for m in np.arange(1,13): # TODO assumes monthly
# m_condition = (m >= self.state.reservoir_month_flood_control_end.values) &
# (self.reservoir_streamflow_schedule.sel({streamflow_time_name: m}).values > self.reservoir_streamflow_schedule.mean(dim=streamflow_time_name).values) & (
# (first_condition & (m <= self.state.reservoir_month_start_operations)) |
# (second_condition & (m <= 12))
# )
# fill = np.where(
# m_condition,
# fill + np.abs(self.reservoir_streamflow_schedule.mean(dim=streamflow_time_name).values - self.reservoir_streamflow_schedule.sel({streamflow_time_name: m}).values),
# fill
# )
# n_month = np.where(
# m_condition,
# n_month + 1,
# n_month
# )
state.reservoir_release = np.where(
(state.reservoir_release> grid.reservoir_streamflow_schedule.mean(dim=streamflow_time_name).values) & (first_condition | second_condition),
grid.reservoir_streamflow_schedule.mean(dim=streamflow_time_name).values,
Expand Down

0 comments on commit 6c832e2

Please sign in to comment.