Skip to content

Commit

Permalink
Merge pull request #4723 from jedwards4b/fix/leapdatefortests
Browse files Browse the repository at this point in the history
fix correction for no_leap calendar in tests
  • Loading branch information
jedwards4b authored Dec 20, 2024
2 parents eb274e1 + 42f8ce6 commit 3336d89
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions CIME/SystemTests/system_tests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,13 @@ def _set_restart_interval(
expect(False, f"stop_option {stop_option} not available for this test")

restdatetime = startdatetime + rtd
# We are working with python datatime and the model uses a NO_LEAP 365 day calendar
# so we need to correct for leap years
if cal == "NO_LEAP":
dayscorrected = 0
syr = startdatetime.year
smon = startdatetime.month
ryr = restdatetime.year
rmon = restdatetime.month
while ryr > syr:
if rmon > 2 and calendar.isleap(ryr):
dayscorrected += 1
ryr = ryr - 1
if rmon > 2 and smon <= 2:
if calendar.isleap(syr):
dayscorrected += 1
restdatetime = restdatetime + timedelta(days=dayscorrected)
restdatetime = restdatetime + self._leap_year_correction(
startdatetime, restdatetime
)

self._rest_time = (
f".{restdatetime.year:04d}-{restdatetime.month:02d}-{restdatetime.day:02d}-"
)
Expand All @@ -250,6 +243,35 @@ def _set_restart_interval(

return rest_n

@staticmethod
def _leap_year_correction(startdatetime, restdatetime):
"""
Compute correction needed for restdate time if model is using NO_LEAP calendar
>>> SystemTestsCommon._leap_year_correction(datetime.strptime("20000225","%Y%m%d"), datetime.strptime("20000301","%Y%m%d"))
datetime.timedelta(days=1)
>>> SystemTestsCommon._leap_year_correction(datetime.strptime("20010225","%Y%m%d"), datetime.strptime("20010301","%Y%m%d"))
datetime.timedelta(0)
>>> SystemTestsCommon._leap_year_correction(datetime.strptime("20010225","%Y%m%d"), datetime.strptime("20050301","%Y%m%d"))
datetime.timedelta(days=1)
>>> SystemTestsCommon._leap_year_correction(datetime.strptime("18500101","%Y%m%d"), datetime.strptime("20201231","%Y%m%d"))
datetime.timedelta(days=42)
"""
dayscorrected = 0
syr = startdatetime.year
smon = startdatetime.month
ryr = syr
rmon = restdatetime.month
while ryr < restdatetime.year:
if calendar.isleap(ryr):
dayscorrected += 1
ryr = ryr + 1
if rmon > 2 and smon <= 2 or restdatetime.year > syr:
if calendar.isleap(ryr):
dayscorrected += 1
logger.info("correcting calendar for no leap {}".format(dayscorrected))
return timedelta(days=dayscorrected)

def _init_environment(self, caseroot):
"""
Do initializations of environment variables that are needed in __init__
Expand Down

0 comments on commit 3336d89

Please sign in to comment.