Skip to content

Commit

Permalink
Merge pull request #73 from IMMM-SFA/feature/remove-epiweeks-dependency
Browse files Browse the repository at this point in the history
remove epiweeks dependency in an attempt to fix #71
  • Loading branch information
thurber authored Dec 15, 2021
2 parents 9da0240 + 7eec282 commit 4c8af31
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ plot_reservoir(
Plot a particular output variable (as defined in `config.yaml`) over time:
```python
from mosartwmpy import Model
from mosartwmpy.plotting.plot import plot_reservoir
from mosartwmpy.plotting.plot import plot_variable
config_file = 'config.yaml'
mosart_wm = Model()
mosart_wm.initialize(config_file)
Expand Down
4 changes: 2 additions & 2 deletions mosartwmpy/plotting/plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextily as ctx
from epiweeks import Week
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -12,6 +11,7 @@
import warnings

from mosartwmpy import Model
from mosartwmpy.utilities.epiweek import get_epiweek_from_datetime

warnings.filterwarnings("ignore", category=ShapelyDeprecationWarning)

Expand Down Expand Up @@ -186,7 +186,7 @@ def plot_reservoir(

params = xr.open_dataset(model.config.get('water_management.reservoirs.parameters.path')).to_dataframe()
dam = params[params[model.config.get('water_management.reservoirs.parameters.variables.reservoir_id')] == grand_id]
weeks = df.index.to_series().apply(lambda time: Week.fromdate(pd.to_datetime(time)).week)
weeks = df.index.to_series().apply(lambda time: get_epiweek_from_datetime(pd.to_datetime(time)))
weeks[weeks > 52] = 52

meanflow = pd.read_parquet(model.config.get('water_management.reservoirs.streamflow.path'))
Expand Down
4 changes: 2 additions & 2 deletions mosartwmpy/reservoirs/istarf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
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
from mosartwmpy.utilities.epiweek import get_epiweek_from_datetime


def istarf_release(state: State, grid: Grid, config: Benedict, parameters: Parameters, current_time: datetime):
# estimate reservoir release using ISTARF which is based on harmonic functions

# restrict epiweek to [1, 52]
epiweek = np.minimum(float(Week.fromdate(current_time).week), 52.0)
epiweek = np.minimum(float(get_epiweek_from_datetime(current_time)), 52.0)

daily_release = np.zeros(len(grid.reservoir_id))

Expand Down
18 changes: 18 additions & 0 deletions mosartwmpy/tests/test_epiweek.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime
import unittest

from mosartwmpy.utilities.epiweek import get_epiweek_from_datetime


class EpiweekTest(unittest.TestCase):

def test_epiweek(self):
self.assertEqual(get_epiweek_from_datetime(datetime(2017, 1, 1)), 1)
self.assertEqual(get_epiweek_from_datetime(datetime(2015, 1, 1)), 53)
self.assertEqual(get_epiweek_from_datetime(datetime(2016, 1, 1)), 52)
self.assertEqual(get_epiweek_from_datetime(datetime(2017, 3, 15)), 11)
self.assertEqual(get_epiweek_from_datetime(datetime(2017, 3, 31)), 13)


if __name__ == '__main__':
unittest.main()
4 changes: 2 additions & 2 deletions mosartwmpy/tests/test_istarf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import unittest

from datetime import date
from epiweeks import Week

from mosartwmpy.reservoirs.istarf import compute_istarf_release
from mosartwmpy.utilities.epiweek import get_epiweek_from_datetime


class IstarfTest(unittest.TestCase):

def test_is_correct(self):

epiweek = np.minimum(float(Week.fromdate(date(1994, 10, 1)).week), 52.0)
epiweek = np.minimum(float(get_epiweek_from_datetime(date(1994, 10, 1))), 52.0)
reservoir_id = np.array([0.0])
upper_min = np.array([-np.Inf])
upper_max = np.array([np.Inf])
Expand Down
37 changes: 37 additions & 0 deletions mosartwmpy/utilities/epiweek.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from datetime import date, datetime


def get_epiweek_from_datetime(dt: datetime) -> int:
"""Calculate the epiweek number from a datetime
Args:
dt (datetime): the datetime object to calculate the epiweek for
Returns:
int: the epiweek number
"""
year = dt.year
dt_ordinal = dt.toordinal()
year_start_ordinal = _get_year_start_ordinal(year)
week = (dt_ordinal - year_start_ordinal) // 7
if week < 0:
year = year - 1
year_start_ordinal = _get_year_start_ordinal(year)
week = (dt_ordinal - year_start_ordinal) // 7
elif week >= 52:
year_start_ordinal = _get_year_start_ordinal(year + 1)
if date_ordinal > year_start_ordinal:
year = year + 1
week = 0
week = week + 1
return week


def _get_year_start_ordinal(y):
year_start = date(y, 1, 1)
year_start_weekday = year_start.weekday()
year_start_ordinal = year_start.toordinal() - year_start_weekday - 1
if year_start_weekday > 2:
return year_start_ordinal + 7
return year_start_ordinal
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def readme():
'click==8.0.1',
'contextily==1.2.0',
'dask[complete]==2021.10.0',
'epiweeks==2.1.3',
'geopandas==0.10.2',
'h5netcdf==0.11.0',
'hvplot==0.7.3',
Expand Down

0 comments on commit 4c8af31

Please sign in to comment.