diff --git a/CMIP7/esm1p6/atmosphere/amip/cmip7_AM_amip_generate.py b/CMIP7/esm1p6/atmosphere/amip/cmip7_AM_amip_generate.py new file mode 100644 index 0000000..62aa13b --- /dev/null +++ b/CMIP7/esm1p6/atmosphere/amip/cmip7_AM_amip_generate.py @@ -0,0 +1,58 @@ +from argparse import ArgumentParser +from pathlib import Path + +from cmip7_ancil_argparse import ( + grid_parser, + path_parser, +) +from cmip7_ancil_common import save_ancil +from cmip7_ancil_constants import ANCIL_TODAY +from cmip7_ancil_ukesm import ( + fix_cmip7_ukesm, + load_cmip7_ukesm, + ukesm_parser, +) + + +def parse_args(): + parser = ArgumentParser( + parents=[path_parser(), grid_parser(), ukesm_parser()], + prog="cmip7_AM_amip_generate", + description="Generate input files from UK CMIP7 AMIP forcings", + ) + return parser.parse_args() + + +def esm_am_amip_save_dirpath(args): + return ( + Path(args.ancil_target_dirname) + / "modern" + / "amip" + / "atmosphere" + / "boundary_conditions" + / args.esm_grid_rel_dirname + / ANCIL_TODAY + ) + + +def save_cmip7_am_amip(args, cube): + # Save as an ancillary file + save_dirpath = esm_am_amip_save_dirpath(args) + save_ancil( + cube, + save_dirpath, + args.save_filename, + gregorian=False, + replace_bounds=True, + ) + + +if __name__ == "__main__": + args = parse_args() + + # Load the CMIP7 datasets + ukesm_cube = load_cmip7_ukesm(args) + # Match the ESM1.5 mask + esm_cube = fix_cmip7_ukesm(args, ukesm_cube) + # Save the ancillary + save_cmip7_am_amip(args, esm_cube) diff --git a/CMIP7/esm1p6/atmosphere/cmip7_ancil_ukesm.py b/CMIP7/esm1p6/atmosphere/cmip7_ancil_ukesm.py new file mode 100644 index 0000000..3c5f970 --- /dev/null +++ b/CMIP7/esm1p6/atmosphere/cmip7_ancil_ukesm.py @@ -0,0 +1,31 @@ +from argparse import ArgumentParser +from pathlib import Path + +import iris +from cmip7_ancil_common import fix_coords + + +def ukesm_parser(): + parser = ArgumentParser(add_help=False) + parser.add_argument("--ukesm-ancil-dirpath") + parser.add_argument("--ukesm-netcdf-filename") + parser.add_argument("--save-filename") + return parser + + +def cmip7_ukesm_filepath(args): + dirpath = Path(args.ukesm_ancil_dirpath) + filename = args.ukesm_netcdf_filename + return dirpath / filename + + +def load_cmip7_ukesm(args): + filepath = cmip7_ukesm_filepath(args) + return iris.load_cube(filepath) + + +def fix_cmip7_ukesm(args, cube): + # Make the coordinates compatible with the ESM1.5 grid mask + fix_coords(args, cube) + cube.data = cube.data.filled(0.0) + return cube