Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMIP7/esm1p6/atmosphere/cmip7_ancil_constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime

ANCIL_TODAY = datetime.now().strftime("%Y.%m.%d")
REAL_MISSING_DATA_INDICATOR = -32768 * 32768.0
UM_VERSION = "7.3"
53 changes: 46 additions & 7 deletions CMIP7/esm1p6/atmosphere/solar/cmip7_HI_solar_generate.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from argparse import ArgumentParser

import iris
import numpy as np
from cmip7_ancil_argparse import common_parser
from cmip7_ancil_constants import REAL_MISSING_DATA_INDICATOR
from cmip7_HI import (
CMIP7_HI_BEG_YEAR,
CMIP7_HI_END_YEAR,
esm_hi_forcing_save_dirpath,
)
from cmip7_PI import CMIP7_PI_YEAR
from solar.cmip7_solar import cmip7_solar_dirpath, load_cmip7_solar_cube

SOLAR_ARRAY_BEG_YEAR = 1700
SOLAR_ARRAY_END_YEAR = 2300


def parse_args():
parser = ArgumentParser(
Expand All @@ -25,19 +31,52 @@ def parse_args():
return parser.parse_args()


def cmip7_hi_solar_year_mean(cube):
"""
Calculate mean TSI values for each year and save them into an array.
"""
NBR_YEARS = SOLAR_ARRAY_END_YEAR - SOLAR_ARRAY_BEG_YEAR + 1
solar_array = np.zeros(NBR_YEARS)
# Calculate and save the mean annual TSI for each CMIP7 historical year.
historical_year_range = range(CMIP7_HI_BEG_YEAR, CMIP7_HI_END_YEAR + 1)
assert CMIP7_PI_YEAR in historical_year_range
for year in historical_year_range:
year_cons = iris.Constraint(time=lambda cell: cell.point.year == year)
year_cube = cube.extract(year_cons)
year_mean = year_cube.collapsed("time", iris.analysis.MEAN).data
solar_array[year - SOLAR_ARRAY_BEG_YEAR] = year_mean
# Save the year mean for the pre-industrial year.
if year == CMIP7_PI_YEAR:
pi_year_mean = year_mean

# For the years from SOLAR_ARRAY_BEG_YEAR to CMIP7_HI_BEG_YEAR - 1,
# set the saved TSI value to the pre-industrial year mean TSI.
for year in range(SOLAR_ARRAY_BEG_YEAR, CMIP7_HI_BEG_YEAR):
solar_array[year - SOLAR_ARRAY_BEG_YEAR] = pi_year_mean

# For the years from CMIP7_HI_END_YEAR + 1 to SOLAR_ARRAY_END_YEAR,
# set the saved TSI value to the real missing data indicator
for year in range(CMIP7_HI_END_YEAR + 1, SOLAR_ARRAY_END_YEAR + 1):
solar_array[year - SOLAR_ARRAY_BEG_YEAR] = REAL_MISSING_DATA_INDICATOR
return solar_array


def cmip7_hi_solar_save(args, cube):
"""
Save the TSI values for each year into a text file.
"""
solar_array = cmip7_hi_solar_year_mean(cube)
save_dirpath = esm_hi_forcing_save_dirpath(args)
# Ensure that the save directory exists.
save_dirpath.mkdir(mode=0o755, parents=True, exist_ok=True)
save_filepath = save_dirpath / args.save_filename
with open(save_filepath, "w") as save_file:
for year in range(CMIP7_HI_BEG_YEAR, CMIP7_HI_END_YEAR + 1):
year_cons = iris.Constraint(
time=lambda cell: cell.point.year == year
)
year_cube = cube.extract(year_cons)
year_mean = year_cube.collapsed("time", iris.analysis.MEAN).data
print(year, f"{year_mean:.3f}", file=save_file)
for year in range(SOLAR_ARRAY_BEG_YEAR, SOLAR_ARRAY_END_YEAR + 1):
year_mean = solar_array[year - SOLAR_ARRAY_BEG_YEAR]
if year_mean == REAL_MISSING_DATA_INDICATOR:
print(year, f"{year_mean:.1f}", file=save_file)
else:
print(year, f"{year_mean:.3f}", file=save_file)


if __name__ == "__main__":
Expand Down
Loading