Skip to content

Commit e12fd95

Browse files
committed
Use np.interp to taper SAOD values
1 parent 00e3bbc commit e12fd95

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

CMIP7/esm1p6/atmosphere/volcanic/cmip7_HI_volcanic_generate.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,42 @@ def constrain_to_latitude_band(cube, band):
7171
return cube.extract(lat_constraint)
7272

7373

74-
def save_hi_year_interpolated_saod(
75-
year, saod_for_beg_year, saod_for_end_year, save_file
76-
):
74+
def taper_saod(saod_for_beg_year, saod_for_end_year):
7775
"""
7876
Interpolate between the saod values in saod_for_beg_year
79-
and saod_for_end_year and save one year's worth of values
80-
in save_file.
77+
and saod_for_end_year.
8178
"""
82-
ratio = min(
83-
(year - CMIP7_HI_VOLCANIC_END_YEAR) / float(NBR_TAPER_YEARS), 1.0
84-
)
79+
RATIO_ARRAY_LEN = CMIP7_HI_SAOD_ARRAY_END_YEAR - CMIP7_HI_VOLCANIC_END_YEAR
80+
saod_array = np.zeros(RATIO_ARRAY_LEN, MONTHS_IN_YEAR, NBR_OF_BANDS)
81+
ratio_array = np.zeros(RATIO_ARRAY_LEN)
82+
for index in range(RATIO_ARRAY_LEN):
83+
ratio_array[index] = (index + 1) / float(NBR_TAPER_YEARS)
84+
ratio_beg_end = np.array([0.0, 1.0])
8585
for month in range(1, MONTHS_IN_YEAR + 1):
86-
print(f"{year:4d} {month:4d}", end="", file=save_file)
87-
8886
# Divide into latitude bands.
8987
for lat_band_nbr in range(NBR_OF_BANDS):
9088
saod_beg = saod_for_beg_year[month - 1, lat_band_nbr]
9189
saod_end = saod_for_end_year[month - 1, lat_band_nbr]
92-
saod = saod_beg * ratio + saod_end * (1.0 - ratio)
90+
saod_beg_end = np.array([saod_beg, saod_end])
91+
saod_array[:, month, lat_band_nbr] = np.interp(
92+
ratio_array, ratio_beg_end, saod_beg_end
93+
)
94+
return saod_array
95+
96+
97+
def save_hi_year_tapered_saod(year, tapered_saod_array, save_file):
98+
"""
99+
Save one year's worth of interpolated saod values in save_file.
100+
"""
101+
index = year - (CMIP7_HI_VOLCANIC_END_YEAR + 1)
102+
for month in range(1, MONTHS_IN_YEAR + 1):
103+
print(f"{year:4d} {month:4d}", end="", file=save_file)
104+
105+
# Divide into latitude bands.
106+
for lat_band_nbr in range(NBR_OF_BANDS):
107+
saod = tapered_saod_array[index, month - 1, lat_band_nbr]
93108
print(
94-
f"{int(saod):5d}",
109+
f"{round(saod):5d}",
95110
end="",
96111
file=save_file,
97112
)
@@ -143,7 +158,7 @@ def save_hi_stratospheric_aerosol_optical_depth(args, dataset_path):
143158
lat_cube = sum_over_height_layers(lat_cube)
144159
saod = lat_cube.data * 10000.0
145160
print(
146-
f"{int(saod):5d}",
161+
f"{round(saod):5d}",
147162
end="",
148163
file=save_file,
149164
)
@@ -158,12 +173,11 @@ def save_hi_stratospheric_aerosol_optical_depth(args, dataset_path):
158173
# CMIP7_HI_SAOD_ARRAY_END_YEAR interpolate between the saod values
159174
# in saod_for_beg_year and saod_for_end_year and save values in
160175
# save_file.
176+
tapered_saod_array = taper_saod(saod_for_beg_year, saod_for_end_year)
161177
for year in range(
162178
CMIP7_HI_VOLCANIC_END_YEAR + 1, CMIP7_HI_SAOD_ARRAY_END_YEAR + 1
163179
):
164-
save_hi_year_interpolated_saod(
165-
year, saod_for_beg_year, saod_for_end_year, save_file
166-
)
180+
save_hi_year_tapered_saod(year, tapered_saod_array, save_file)
167181

168182

169183
if __name__ == "__main__":

0 commit comments

Comments
 (0)