11from argparse import ArgumentParser
22
33import iris
4+ import numpy as np
45from cmip7_ancil_argparse import common_parser
6+ from cmip7_ancil_constants import REAL_MISSING_DATA_INDICATOR
57from cmip7_HI import (
68 CMIP7_HI_BEG_YEAR ,
79 CMIP7_HI_END_YEAR ,
810 esm_hi_forcing_save_dirpath ,
911)
12+ from cmip7_PI import CMIP7_PI_YEAR
1013from solar .cmip7_solar import cmip7_solar_dirpath , load_cmip7_solar_cube
1114
15+ SOLAR_ARRAY_BEG_YEAR = 1700
16+ SOLAR_ARRAY_END_YEAR = 2300
17+
1218
1319def parse_args ():
1420 parser = ArgumentParser (
@@ -25,19 +31,52 @@ def parse_args():
2531 return parser .parse_args ()
2632
2733
34+ def cmip7_hi_solar_year_mean (cube ):
35+ """
36+ Calculate mean TSI values for each year and save them into an array.
37+ """
38+ NBR_YEARS = SOLAR_ARRAY_END_YEAR - SOLAR_ARRAY_BEG_YEAR + 1
39+ solar_array = np .zeros (NBR_YEARS )
40+ # Calculate and save the mean annual TSI for each CMIP7 historical year.
41+ historical_year_range = range (CMIP7_HI_BEG_YEAR , CMIP7_HI_END_YEAR + 1 )
42+ assert CMIP7_PI_YEAR in historical_year_range
43+ for year in historical_year_range :
44+ year_cons = iris .Constraint (time = lambda cell : cell .point .year == year )
45+ year_cube = cube .extract (year_cons )
46+ year_mean = year_cube .collapsed ("time" , iris .analysis .MEAN ).data
47+ solar_array [year - SOLAR_ARRAY_BEG_YEAR ] = year_mean
48+ # Save the year mean for the pre-industrial year.
49+ if year == CMIP7_PI_YEAR :
50+ pi_year_mean = year_mean
51+
52+ # For the years from SOLAR_ARRAY_BEG_YEAR to CMIP7_HI_BEG_YEAR - 1,
53+ # set the saved TSI value to the pre-industrial year mean TSI.
54+ for year in range (SOLAR_ARRAY_BEG_YEAR , CMIP7_HI_BEG_YEAR ):
55+ solar_array [year - SOLAR_ARRAY_BEG_YEAR ] = pi_year_mean
56+
57+ # For the years from CMIP7_HI_END_YEAR + 1 to SOLAR_ARRAY_END_YEAR,
58+ # set the saved TSI value to the real missing data indicator
59+ for year in range (CMIP7_HI_END_YEAR + 1 , SOLAR_ARRAY_END_YEAR + 1 ):
60+ solar_array [year - SOLAR_ARRAY_BEG_YEAR ] = REAL_MISSING_DATA_INDICATOR
61+ return solar_array
62+
63+
2864def cmip7_hi_solar_save (args , cube ):
65+ """
66+ Save the TSI values for each year into a text file.
67+ """
68+ solar_array = cmip7_hi_solar_year_mean (cube )
2969 save_dirpath = esm_hi_forcing_save_dirpath (args )
3070 # Ensure that the save directory exists.
3171 save_dirpath .mkdir (mode = 0o755 , parents = True , exist_ok = True )
3272 save_filepath = save_dirpath / args .save_filename
3373 with open (save_filepath , "w" ) as save_file :
34- for year in range (CMIP7_HI_BEG_YEAR , CMIP7_HI_END_YEAR + 1 ):
35- year_cons = iris .Constraint (
36- time = lambda cell : cell .point .year == year
37- )
38- year_cube = cube .extract (year_cons )
39- year_mean = year_cube .collapsed ("time" , iris .analysis .MEAN ).data
40- print (year , f"{ year_mean :.3f} " , file = save_file )
74+ for year in range (SOLAR_ARRAY_BEG_YEAR , SOLAR_ARRAY_END_YEAR + 1 ):
75+ year_mean = solar_array [year - SOLAR_ARRAY_BEG_YEAR ]
76+ if year_mean == REAL_MISSING_DATA_INDICATOR :
77+ print (year , f"{ year_mean :.1f} " , file = save_file )
78+ else :
79+ print (year , f"{ year_mean :.3f} " , file = save_file )
4180
4281
4382if __name__ == "__main__" :
0 commit comments