Skip to content

Commit

Permalink
add some doc tests for calculating leap year corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards4b committed Dec 20, 2024
1 parent 5013a2e commit 42f8ce6
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions CIME/SystemTests/system_tests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +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 and (ryr < restdatetime.year or rmon > 2):
if calendar.isleap(ryr):
dayscorrected += 1
ryr = ryr - 1
if rmon > 2 and smon <= 2 or restdatetime.year > syr:
if calendar.isleap(syr):
dayscorrected += 1
restdatetime = restdatetime + timedelta(days=dayscorrected)
logger.info("correcting calendar for no leap {}".format(dayscorrected))
restdatetime = restdatetime + self._leap_year_correction(
startdatetime, restdatetime
)

self._rest_time = (
f".{restdatetime.year:04d}-{restdatetime.month:02d}-{restdatetime.day:02d}-"
)
Expand All @@ -251,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 42f8ce6

Please sign in to comment.