Skip to content

Commit

Permalink
add mixin mom class for shared restart_parsing function between mom a…
Browse files Browse the repository at this point in the history
…nd mom6 drivers
  • Loading branch information
Jo Basevi committed Oct 23, 2023
1 parent a336968 commit 39db145
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 33 deletions.
4 changes: 4 additions & 0 deletions payu/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
30 changes: 0 additions & 30 deletions payu/models/fms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion payu/models/mom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
3 changes: 2 additions & 1 deletion payu/models/mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Local
from payu.models.fms import Fms
from payu.models.mom_mixin import MomMixin


def mom6_add_parameter_files(model):
Expand All @@ -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):
Expand Down
40 changes: 40 additions & 0 deletions payu/models/mom_mixin.py
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
2 changes: 1 addition & 1 deletion test/test_prune_restarts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 39db145

Please sign in to comment.