diff --git a/payu/models/cable.py b/payu/models/cable.py index 09252937..2251d171 100644 --- a/payu/models/cable.py +++ b/payu/models/cable.py @@ -8,6 +8,7 @@ """ # Standard Library +import glob import os import shutil @@ -36,7 +37,39 @@ def __init__(self, expt, name, config): 'pft_params.nml', ] - self.restart_info = {} + self.forcing_year_config = 'cable.forcing_year.yaml' + self.optional_config_files = [self.forcing_year_config] + + self.met_forcing_vars = [ + "Rainf", + "Snowf", + "LWdown", + "SWdown", + "PSurf", + "Qair", + "Tair", + "Wind", + ] + + def _get_forcing_path(self, variable, year): + """Return the met forcing file path for a given variable and year.""" + pattern = os.path.join(self.work_input_path, f"*{variable}*{year}*.nc") + for path in glob.glob(pattern): + return path + msg = f"Unable to infer met forcing path for variable {variable} for year {year}." + raise FileNotFoundError(msg) + + def _update_forcing(self, year, offset=None, repeat=None): + """Update the CABLE namelist file to use the correct met forcing.""" + if offset: + year += offset[1] - offset[0] + if repeat: + year = repeat[0] + ((year - repeat[0]) % (repeat[1] - repeat[0] + 1)) + for var in self.met_forcing_vars: + path = self._get_forcing_path(var, year) + self.cable_nml["cable"]["gswpfile"][var] = ( + os.path.relpath(path, start=self.work_path) + ) def set_model_pathnames(self): super(Cable, self).set_model_pathnames() @@ -61,9 +94,16 @@ def setup(self): if self.prior_restart_path: with open(self.restart_calendar_path, 'r') as restart_file: self.restart_info = yaml.safe_load(restart_file) - self.cable_nml['cable']['ncciy'] = self.restart_info['year'] else: - self.restart_info['year'] = self.cable_nml['cable']['ncciy'] + self.restart_info = {'year': self.cable_nml['cable']['ncciy']} + + year = self.cable_nml['cable']['ncciy'] = self.restart_info['year'] + + forcing_year_config_path = os.path.join(self.work_path, self.forcing_year_config) + if os.path.exists(forcing_year_config_path): + with open(forcing_year_config_path, 'r') as file: + forcing_year_config = yaml.safe_load(file) + self._update_forcing(year, **forcing_year_config) # Write modified namelist file to work dir self.cable_nml.write(