Skip to content

Commit 0951cd3

Browse files
committed
Add code for saving AMIP ancils
1 parent 57a0305 commit 0951cd3

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from argparse import ArgumentParser
2+
from pathlib import Path
3+
4+
from cmip7_ancil_argparse import (
5+
grid_parser,
6+
path_parser,
7+
)
8+
from cmip7_ancil_common import save_ancil
9+
from cmip7_ancil_constants import ANCIL_TODAY
10+
from cmip7_ancil_ukesm import (
11+
fix_cmip7_ukesm,
12+
load_cmip7_ukesm,
13+
ukesm_parser,
14+
)
15+
16+
17+
def parse_args():
18+
parser = ArgumentParser(
19+
parents=[path_parser(), grid_parser(), ukesm_parser()],
20+
prog="cmip7_AM_amip_generate",
21+
description=(
22+
"Generate input files from UK CMIP7 AMIP forcings"
23+
),
24+
)
25+
return parser.parse_args()
26+
27+
28+
def esm_am_amip_save_dirpath(args):
29+
return (
30+
Path(args.ancil_target_dirname)
31+
/ "modern"
32+
/ "amip"
33+
/ "atmosphere"
34+
/ "boundary_conditions"
35+
/ args.esm_grid_rel_dirname
36+
/ ANCIL_TODAY
37+
)
38+
39+
40+
def save_cmip7_am_amip(args, cube):
41+
# Save as an ancillary file
42+
save_dirpath = esm_am_amip_save_dirpath(args)
43+
save_ancil(cube, save_dirpath, args.save_filename, replace_bounds=True)
44+
45+
46+
if __name__ == "__main__":
47+
args = parse_args()
48+
49+
# Load the CMIP7 datasets
50+
ukesm_cube = load_cmip7_ukesm(args)
51+
# Match the ESM1.5 mask
52+
esm_cube = fix_cmip7_ukesm(args, ukesm_cube)
53+
# Save the ancillary
54+
save_cmip7_am_amip(args, esm_cube)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from argparse import ArgumentParser
2+
from pathlib import Path
3+
4+
import iris
5+
from cmip7_ancil_common import fix_coords
6+
7+
8+
def ukesm_parser():
9+
parser = ArgumentParser(add_help=False)
10+
parser.add_argument("--ukesm-ancil-dirpath")
11+
parser.add_argument("--ukesm-netcdf-filename")
12+
parser.add_argument("--save-filename")
13+
return parser
14+
15+
16+
def cmip7_ukesm_filepath(args):
17+
dirpath = Path(args.ukesm_ancil_dirpath)
18+
filename = args.ukesm_netcdf_filename
19+
return dirpath / filename
20+
21+
22+
def load_cmip7_ukesm(args):
23+
filepath = cmip7_ukesm_filepath(args)
24+
return iris.load_cube(filepath)
25+
26+
27+
def fix_cmip7_ukesm(args, cube):
28+
# Make the coordinates compatible with the ESM1.5 grid mask
29+
fix_coords(args, cube)
30+
cube.data = cube.data.filled(0.0)
31+
return cube

0 commit comments

Comments
 (0)