Skip to content
67 changes: 67 additions & 0 deletions CMIP7/esm1p6/atmosphere/ozone/cmip7_PI_mean_ozone_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from argparse import ArgumentParser
from pathlib import Path

import iris.coord_categorisation
from cmip7_ancil_argparse import (
grid_parser,
path_parser,
)
from cmip7_ancil_common import save_ancil
from cmip7_ancil_constants import ANCIL_TODAY
from ozone.cmip7_ozone import (
fix_cmip7_ozone,
load_cmip7_ozone,
ozone_parser,
)


def parse_args():
parser = ArgumentParser(
parents=[path_parser(), grid_parser(), ozone_parser()],
prog="cmip7_PI_mean_ozone_generate",
description=(
"Generate input files from 21 year mean of UK CMIP7 historical"
"ozone forcings"
),
)
return parser.parse_args()


def esm_pi_ozone_save_dirpath(args):
return (
Path(args.ancil_target_dirname)
/ "modern"
/ "pre-industrial-mean"
/ "forcing"
/ args.esm_grid_rel_dirname
/ ANCIL_TODAY
)


def save_cmip7_pi_mean_ozone(args, cube):
# Add a "month_number" variable.
iris.coord_categorisation.add_month_number(
cube, "time", name="month_number"
)
# Aggregate by "month_number" to obtain a mean for each month.
cube = cube.aggregated_by("month_number", iris.analysis.MEAN)
# Re-create the time bounds to ensure that time is contiguous.
time = cube.coord("time")
time.bounds = None
time.guess_bounds(bound_position=0.5)
# Remove the added "month_number" coordinate before saving.
cube.remove_coord("month_number")
# Save as an ancillary file
save_dirpath = esm_pi_ozone_save_dirpath(args)
save_ancil(cube, save_dirpath, args.save_filename, gregorian=False)


if __name__ == "__main__":
args = parse_args()

# Load the CMIP7 datasets
ozone_cube = load_cmip7_ozone(args)
# Match the ESM1.5 mask
esm_cube = fix_cmip7_ozone(args, ozone_cube)
# Save the ancillary
save_cmip7_pi_mean_ozone(args, esm_cube)
Loading