Skip to content

Commit

Permalink
Merge branch 'dev' into build/bump-paramiko-version
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Oct 9, 2024
2 parents 56cabb5 + bb7c844 commit 53ed50b
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 32 deletions.
40 changes: 23 additions & 17 deletions antarest/study/storage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,35 @@ def get_start_date(
starting_day_index = DAY_NAMES.index(starting_day.title())
target_year = 2018
while True:
if leapyear == calendar.isleap(target_year):
first_day = datetime(target_year, starting_month_index, 1)
if leapyear == calendar.isleap(target_year + (starting_month_index > 2)):
first_day = datetime(target_year + (starting_month_index != 1), 1, 1)
if first_day.weekday() == starting_day_index:
break
target_year += 1

start_offset_days = timedelta(days=(0 if output_id is None else start_offset - 1))
start_date = datetime(target_year, starting_month_index, 1) + start_offset_days
# base case is DAILY
steps = MATRIX_INPUT_DAYS_COUNT if output_id is None else end - start_offset + 1
if level == StudyDownloadLevelDTO.HOURLY:
steps = steps * 24
elif level == StudyDownloadLevelDTO.ANNUAL:
steps = 1
elif level == StudyDownloadLevelDTO.WEEKLY:
steps = math.ceil(steps / 7)
elif level == StudyDownloadLevelDTO.MONTHLY:
end_date = start_date + timedelta(days=steps)
same_year = end_date.year == start_date.year
if same_year:
steps = 1 + end_date.month - start_date.month
else:
steps = (13 - start_date.month) + end_date.month

def _get_steps(
daily_steps: int, temporality: StudyDownloadLevelDTO, begin_date: datetime, is_output: t.Optional[str] = None
) -> int:
temporality_mapping = {
StudyDownloadLevelDTO.DAILY: daily_steps,
StudyDownloadLevelDTO.HOURLY: daily_steps * 24,
StudyDownloadLevelDTO.ANNUAL: 1,
StudyDownloadLevelDTO.WEEKLY: math.ceil(daily_steps / 7),
StudyDownloadLevelDTO.MONTHLY: 12,
}

if temporality == StudyDownloadLevelDTO.MONTHLY and is_output:
end_date = begin_date + timedelta(days=daily_steps)
same_year = end_date.year == begin_date.year
return 1 + end_date.month - begin_date.month if same_year else (13 - begin_date.month) + end_date.month

return temporality_mapping[temporality]

days_count = MATRIX_INPUT_DAYS_COUNT if output_id is None else end - start_offset + 1
steps = _get_steps(days_count, level, start_date, output_id)

first_week_day_index = DAY_NAMES.index(first_week_day)
first_week_offset = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_download_matrices(self, client: TestClient, user_access_token: str, int
variant_matrix_path = f"input/load/series/load_{area_id}"

raw_start_date = datetime.datetime(2018, 1, 1)
variant_start_date = datetime.datetime(2019, 7, 1)
variant_start_date = datetime.datetime(2028, 7, 1)

for uuid, path, start_date in [
(study_820_id, raw_matrix_path, raw_start_date),
Expand Down
182 changes: 168 additions & 14 deletions tests/storage/business/test_study_service_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
TimeSeriesData,
)
from antarest.study.storage.study_download_utils import StudyDownloader
from antarest.study.storage.utils import get_start_date
from antarest.study.storage.utils import DAY_NAMES, get_start_date


def test_output_downloads_export(tmp_path: Path):
Expand Down Expand Up @@ -133,9 +133,9 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2019, 7, 5)),
start_date=str(datetime.datetime(2028, 7, 5)),
steps=48,
first_week_size=5,
first_week_size=7,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
Expand All @@ -150,9 +150,9 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2019, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=7,
first_week_size=7,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
),
),
Expand All @@ -167,9 +167,9 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2019, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=4,
first_week_size=7,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
),
),
Expand All @@ -184,9 +184,9 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.HOURLY,
MatrixIndex(
start_date=str(datetime.datetime(2021, 3, 5)),
start_date=str(datetime.datetime(2028, 3, 5)),
steps=2304,
first_week_size=3,
first_week_size=1,
level=StudyDownloadLevelDTO.HOURLY,
),
),
Expand All @@ -201,9 +201,9 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.ANNUAL,
MatrixIndex(
start_date=str(datetime.datetime(2021, 3, 5)),
start_date=str(datetime.datetime(2028, 3, 5)),
steps=1,
first_week_size=3,
first_week_size=1,
level=StudyDownloadLevelDTO.ANNUAL,
),
),
Expand All @@ -218,15 +218,15 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.DAILY,
MatrixIndex(
start_date=str(datetime.datetime(2026, 3, 3)),
start_date=str(datetime.datetime(2022, 3, 3)),
steps=98,
first_week_size=3,
first_week_size=1,
level=StudyDownloadLevelDTO.DAILY,
),
),
],
)
def test_create_matrix_index(config: Dict[str, Any], level: StudyDownloadLevelDTO, expected: MatrixIndex):
def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownloadLevelDTO, expected: MatrixIndex):
config_mock = Mock()
config_mock.archived = False
output_id = "some output"
Expand All @@ -236,3 +236,157 @@ def test_create_matrix_index(config: Dict[str, Any], level: StudyDownloadLevelDT
file_study.config.outputs = {output_id: config_mock}

assert get_start_date(file_study, output_id, level) == expected


@pytest.mark.parametrize(
"config,level,expected",
[
(
{
"first-month-in-year": "january",
"january.1st": "Monday",
"leapyear": True,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 354,
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2024, 1, 1)),
steps=53,
first_week_size=7,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
(
{
"first-month-in-year": "january",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 354,
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2018, 1, 1)),
steps=53,
first_week_size=7,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
(
{
"first-month-in-year": "july",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Wednesday",
"simulation.start": 5,
"simulation.end": 340,
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2028, 7, 1)),
steps=53,
first_week_size=4,
level=StudyDownloadLevelDTO.WEEKLY,
),
),
(
{
"first-month-in-year": "july",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 200,
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2028, 7, 1)),
steps=12,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
),
),
(
{
"first-month-in-year": "july",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 1,
"simulation.end": 100,
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2028, 7, 1)),
steps=12,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
),
),
(
{
"first-month-in-year": "march",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 5,
"simulation.end": 100,
},
StudyDownloadLevelDTO.HOURLY,
MatrixIndex(
start_date=str(datetime.datetime(2028, 3, 1)),
steps=8760,
first_week_size=5,
level=StudyDownloadLevelDTO.HOURLY,
),
),
(
{
"first-month-in-year": "march",
"january.1st": "Monday",
"leapyear": False,
"first.weekday": "Monday",
"simulation.start": 5,
"simulation.end": 100,
},
StudyDownloadLevelDTO.ANNUAL,
MatrixIndex(
start_date=str(datetime.datetime(2028, 3, 1)),
steps=1,
first_week_size=5,
level=StudyDownloadLevelDTO.ANNUAL,
),
),
(
{
"first-month-in-year": "march",
"january.1st": "Sunday",
"leapyear": False,
"first.weekday": "Friday",
"simulation.start": 3,
"simulation.end": 100,
},
StudyDownloadLevelDTO.DAILY,
MatrixIndex(
start_date=str(datetime.datetime(2022, 3, 1)),
steps=365,
first_week_size=3,
level=StudyDownloadLevelDTO.DAILY,
),
),
],
)
def test_create_matrix_index_input(config: Dict[str, Any], level: StudyDownloadLevelDTO, expected: MatrixIndex):
file_study = Mock()
file_study.tree.get.return_value = {"general": config}
# Asserts the content are the same
actual = get_start_date(file_study, None, level)
assert actual == expected
# Asserts the returned 1st January corresponds to the chosen one
actual_datetime = datetime.datetime.strptime(actual.start_date, "%Y-%m-%d %H:%M:%S")
next_year = str(actual_datetime.year + (actual_datetime.month != 1))
first_january = datetime.datetime.strptime(next_year, "%Y").weekday()
assert first_january == DAY_NAMES.index(config["january.1st"])

0 comments on commit 53ed50b

Please sign in to comment.