From 6c832e2dd1d1da52ec82644d7275ddb3dbe501c2 Mon Sep 17 00:00:00 2001 From: travis Date: Tue, 8 Jun 2021 08:01:01 -0700 Subject: [PATCH] remove references to epiweeks for now --- mosartwmpy/config_defaults.yaml | 4 ++-- mosartwmpy/model.py | 4 ---- mosartwmpy/reservoirs/grid.py | 9 +++----- mosartwmpy/reservoirs/reservoirs.py | 33 +++-------------------------- 4 files changed, 8 insertions(+), 42 deletions(-) diff --git a/mosartwmpy/config_defaults.yaml b/mosartwmpy/config_defaults.yaml index be6ca37..f51f5aa 100644 --- a/mosartwmpy/config_defaults.yaml +++ b/mosartwmpy/config_defaults.yaml @@ -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 diff --git a/mosartwmpy/model.py b/mosartwmpy/model.py index 967a326..407223e 100644 --- a/mosartwmpy/model.py +++ b/mosartwmpy/model.py @@ -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 @@ -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 @@ -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 diff --git a/mosartwmpy/reservoirs/grid.py b/mosartwmpy/reservoirs/grid.py index 4af8bd9..8dc39f6 100644 --- a/mosartwmpy/reservoirs/grid.py +++ b/mosartwmpy/reservoirs/grid.py @@ -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() @@ -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')] @@ -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 @@ -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) diff --git a/mosartwmpy/reservoirs/reservoirs.py b/mosartwmpy/reservoirs/reservoirs.py index dfa3374..4c0186c 100644 --- a/mosartwmpy/reservoirs/reservoirs.py +++ b/mosartwmpy/reservoirs/reservoirs.py @@ -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 @@ -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') @@ -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') @@ -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( @@ -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,