diff --git a/payu/experiment.py b/payu/experiment.py index 15f5f8cb..07ca3355 100644 --- a/payu/experiment.py +++ b/payu/experiment.py @@ -1050,6 +1050,10 @@ def get_restarts_to_prune(self, 'model. To use integer based restart pruning, ' 'set restart_freq to an integer value.') raise + except FileNotFoundError as e: + print(f'payu: warning: Ignoring {restart} from date-based ' + f'restart pruning. Error: {e}') + continue except Exception: print('payu: error: Error parsing restart directory ', f'{restart} for a datetime to prune restarts.') diff --git a/payu/models/fms.py b/payu/models/fms.py index 5da78635..f2f3e743 100644 --- a/payu/models/fms.py +++ b/payu/models/fms.py @@ -17,8 +17,6 @@ from itertools import count import fnmatch -import cftime - from payu.models.model import Model from payu import envmod from payu.fsops import required_libs @@ -255,31 +253,3 @@ def archive(self, **kwargs): def collate(self): fms_collate(self) - - def get_restart_datetime(self, restart_path): - """Given a restart path, parse the restart files and - return a cftime datetime (for date-based restart pruning)""" - # Check for ocean_solo.res file - ocean_solo_path = os.path.join(restart_path, 'ocean_solo.res') - if not os.path.exists(ocean_solo_path): - raise NotImplementedError( - 'Cannot find ocean_solo.res file, which is required for ' - 'date-based restart pruning') - - with open(ocean_solo_path, 'r') as ocean_solo: - lines = ocean_solo.readlines() - - calendar_int = int(lines[0].split()[0]) - cftime_calendars = { - 1: "360_day", - 2: "julian", - 3: "proleptic_gregorian", - 4: "noleap" - } - calendar = cftime_calendars[calendar_int] - - last_date_line = lines[-1].split() - date_values = [int(i) for i in last_date_line[:6]] - year, month, day, hour, minute, second = date_values - return cftime.datetime(year, month, day, hour, minute, second, - calendar=calendar) diff --git a/payu/models/mom.py b/payu/models/mom.py index 6a28f2b2..6084407b 100644 --- a/payu/models/mom.py +++ b/payu/models/mom.py @@ -11,10 +11,11 @@ import f90nml from payu.models.fms import Fms +from payu.models.mom_mixin import MomMixin from payu.fsops import mkdir_p, make_symlink -class Mom(Fms): +class Mom(MomMixin, Fms): def __init__(self, expt, name, config): diff --git a/payu/models/mom6.py b/payu/models/mom6.py index 188153e8..6b518383 100644 --- a/payu/models/mom6.py +++ b/payu/models/mom6.py @@ -17,6 +17,7 @@ # Local from payu.models.fms import Fms +from payu.models.mom_mixin import MomMixin def mom6_add_parameter_files(model): @@ -42,7 +43,7 @@ def mom6_add_parameter_files(model): model.config_files.extend(filenames) -class Mom6(Fms): +class Mom6(MomMixin, Fms): """Interface to GFDL's MOM6 ocean model.""" def __init__(self, expt, name, config): diff --git a/payu/models/mom_mixin.py b/payu/models/mom_mixin.py new file mode 100644 index 00000000..e6194483 --- /dev/null +++ b/payu/models/mom_mixin.py @@ -0,0 +1,40 @@ +"""Mixin class for MOM and MOM6 drivers + +:copyright: Copyright 2011 Marshall Ward, see AUTHORS for details +:license: Apache License, Version 2.0, see LICENSE for details +""" + +import os + +import cftime + + +class MomMixin: + + def get_restart_datetime(self, restart_path): + """Given a restart path, parse the restart files and + return a cftime datetime (for date-based restart pruning)""" + # Check for ocean_solo.res file + ocean_solo_path = os.path.join(restart_path, 'ocean_solo.res') + if not os.path.exists(ocean_solo_path): + raise FileNotFoundError( + 'Cannot find ocean_solo.res file, which is required for ' + 'date-based restart pruning') + + with open(ocean_solo_path, 'r') as ocean_solo: + lines = ocean_solo.readlines() + + calendar_int = int(lines[0].split()[0]) + cftime_calendars = { + 1: "360_day", + 2: "julian", + 3: "proleptic_gregorian", + 4: "noleap" + } + calendar = cftime_calendars[calendar_int] + + last_date_line = lines[-1].split() + date_values = [int(i) for i in last_date_line[:6]] + year, month, day, hour, minute, second = date_values + return cftime.datetime(year, month, day, hour, minute, second, + calendar=calendar) diff --git a/test/models/test_mom.py b/test/models/test_mom_mixin.py similarity index 100% rename from test/models/test_mom.py rename to test/models/test_mom_mixin.py diff --git a/test/test_prune_restarts.py b/test/test_prune_restarts.py index 253e02b1..ac2658bf 100644 --- a/test/test_prune_restarts.py +++ b/test/test_prune_restarts.py @@ -11,7 +11,7 @@ from test.common import write_config from test.common import make_all_files from test.common import remove_expt_archive_dirs -from test.models.test_mom import make_ocean_restart_dir +from test.models.test_mom_mixin import make_ocean_restart_dir verbose = True