Skip to content

Commit

Permalink
fix(index): change code to use leapyear correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Oct 9, 2024
1 parent 830bd83 commit 1720551
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
41 changes: 24 additions & 17 deletions antarest/study/storage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,36 @@ 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), 1, 1)
year_to_check_for_leap = target_year + (starting_month_index != 1)
if leapyear == calendar.isleap(year_to_check_for_leap):
first_day = datetime(year_to_check_for_leap, 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(2023, 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
20 changes: 10 additions & 10 deletions tests/storage/business/test_study_service_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 5)),
start_date=str(datetime.datetime(2028, 7, 5)),
steps=48,
first_week_size=7,
level=StudyDownloadLevelDTO.WEEKLY,
Expand All @@ -150,7 +150,7 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=7,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
Expand All @@ -167,7 +167,7 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=4,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
Expand All @@ -184,7 +184,7 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.HOURLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 3, 5)),
start_date=str(datetime.datetime(2028, 3, 5)),
steps=2304,
first_week_size=1,
level=StudyDownloadLevelDTO.HOURLY,
Expand All @@ -201,7 +201,7 @@ def test_output_downloads_export(tmp_path: Path):
},
StudyDownloadLevelDTO.ANNUAL,
MatrixIndex(
start_date=str(datetime.datetime(2023, 3, 5)),
start_date=str(datetime.datetime(2028, 3, 5)),
steps=1,
first_week_size=1,
level=StudyDownloadLevelDTO.ANNUAL,
Expand Down Expand Up @@ -286,7 +286,7 @@ def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownload
},
StudyDownloadLevelDTO.WEEKLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=53,
first_week_size=4,
level=StudyDownloadLevelDTO.WEEKLY,
Expand All @@ -303,7 +303,7 @@ def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownload
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=12,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
Expand All @@ -320,7 +320,7 @@ def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownload
},
StudyDownloadLevelDTO.MONTHLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 7, 1)),
start_date=str(datetime.datetime(2028, 7, 1)),
steps=12,
first_week_size=2,
level=StudyDownloadLevelDTO.MONTHLY,
Expand All @@ -337,7 +337,7 @@ def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownload
},
StudyDownloadLevelDTO.HOURLY,
MatrixIndex(
start_date=str(datetime.datetime(2023, 3, 1)),
start_date=str(datetime.datetime(2028, 3, 1)),
steps=8760,
first_week_size=5,
level=StudyDownloadLevelDTO.HOURLY,
Expand All @@ -354,7 +354,7 @@ def test_create_matrix_index_output(config: Dict[str, Any], level: StudyDownload
},
StudyDownloadLevelDTO.ANNUAL,
MatrixIndex(
start_date=str(datetime.datetime(2023, 3, 1)),
start_date=str(datetime.datetime(2028, 3, 1)),
steps=1,
first_week_size=5,
level=StudyDownloadLevelDTO.ANNUAL,
Expand Down

0 comments on commit 1720551

Please sign in to comment.