/
create_HadISST_CMIP5_syn_SSTs.py
executable file
·533 lines (444 loc) · 24 KB
/
create_HadISST_CMIP5_syn_SSTs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#! /usr/bin/env python
#############################################################################
#
# Program : create_HadISST_CMIP5_syn_SSTs.py
# Author : Neil Massey
# Purpose : Create a single synthetic SST from everything that has been
# computed before hand
# Inputs : run_type : rcp4.5 | rc8.5 | histo
# ref_start : year to start reference period, 1850->2005
# ref_end : year to end reference period, 1850->2005
# run_type : historical | rcp45 | rcp85
# Notes : all reference values are calculated from the historical run_type
# CMIP5 ensemble members are only included if their historical run
# includes the reference period
# requires Andrew Dawsons eofs python libraries:
# http://ajdawson.github.io/eofs/
# Output : in the output/ directory filename is:
#
# Date : 18/02/15
#
#############################################################################
import os, sys, getopt
from calc_CMIP5_EOFs import get_cmip5_EOF_filename, get_cmip5_proj_PC_scale_filename
from create_CMIP5_syn_PCs import get_syn_SST_PCs_filename
from create_CMIP5_sst_anoms import get_concat_anom_sst_ens_mean_smooth_fname, get_start_end_periods
from create_HadISST_sst_anoms import get_HadISST_smooth_fname, get_HadISST_monthly_reference_fname, get_HadISST_month_smooth_filename, get_HadISST_monthly_residuals_fname
from cmip5_functions import load_data, reconstruct_field, calc_GMSST, load_sst_data
from calc_HadISST_residual_EOFs import get_HadISST_monthly_residual_EOFs_fname, get_HadISST_monthly_residual_PCs_fname
import numpy
from scipy.io.netcdf import *
from ARN import ARN
from eofs.standard import Eof
import pyximport
pyximport.install(setup_args={'include_dirs':[numpy.get_include()]})
from zonal_smoother import *
#############################################################################
def get_SST_output_directory(run_type, ref_start, ref_end, eof_year):
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
out_dir = "HadISST_"+str(histo_sy)+"_"+str(histo_ey)+"_"+\
run_type+"_"+str(rcp_sy)+"_"+str(rcp_ey)+\
"_r"+str(ref_start)+"_"+str(ref_end)+\
"_y"+str(eof_year)
if not os.path.exists("/Users/Neil/Coding/CREDIBLE_output/output/"+out_dir):
os.mkdir("/Users/Neil/Coding/CREDIBLE_output/output/"+out_dir)
return out_dir
#############################################################################
def get_syn_sst_filename(run_type, ref_start, ref_end, neofs, eof_year, percentile, intvarmode, monthly):
# build the filename for the synthetic SSTs
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
if intvarmode == 0:
intvarstr = "varnone"
elif intvarmode == 1:
intvarstr = "varyear"
elif intvarmode == 2:
intvarstr = "varmon"
out_dir = get_SST_output_directory(run_type, ref_start, ref_end, eof_year)
out_name = out_dir + "_n"+str(neofs)+"_a"+str(percentile)+"_"+intvarstr+"_ssts"
if monthly:
out_name += "_mon"
out_name += ".nc"
ppath = "/Users/Neil/Coding/CREDIBLE_output/output/" + out_dir + "/" + intvarstr
if not os.path.exists(ppath):
os.mkdir(ppath)
spath = ppath + "/sst/"
if not os.path.exists(spath):
os.mkdir(spath)
return spath + out_name
#############################################################################
def create_monthly_intvar(run_type, ref_start, ref_end, n_pcs=22, run_n=400):
# load in the PCs and EOFs
histo_sy = 1899
histo_ey = 2010
# monthly_pc_fname = get_HadISST_monthly_residual_PCs_fname(histo_sy, histo_ey, run_n)
# monthly_pcs = load_data(monthly_pc_fname)
# monthly_eof_fname = get_HadISST_monthly_residual_EOFs_fname(histo_sy, histo_ey, run_n)
# monthly_eofs = load_sst_data(monthly_eof_fname, "sst")
monthly_residuals_fname = get_HadISST_monthly_residuals_fname(histo_sy, histo_ey, run_n)
# open netcdf_file
fh = netcdf_file(monthly_residuals_fname, 'r')
attrs = fh.variables["sst"]._attributes
mv = attrs["_FillValue"]
var = fh.variables["sst"]
monthly_residuals = numpy.ma.masked_equal(var[:], mv)
# weights for reconstruction / projection
coslat = numpy.cos(numpy.deg2rad(numpy.arange(89.5, -90.5, -1)).clip(0., 1.))
wgts = numpy.sqrt(coslat)[..., numpy.newaxis]
eof_solver = Eof(monthly_residuals, center=False, weights=wgts)
monthly_pcs = eof_solver.pcs(npcs=n_pcs)
monthly_eofs = eof_solver.eofs(neofs=n_pcs)
# get the explanation of variance and calculate the scalar from it
M = 1.0 / numpy.sum(eof_solver.varianceFraction(neigs=n_pcs))
# get the number of months to predict the PCs for and create the storage
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
n_mnths = 12*(rcp_ey - histo_sy)
predicted_pcs = numpy.zeros([n_mnths+12, n_pcs], "f")
# fit an AR process to the first ~20 pcs
for pc in range(0, n_pcs):
# create the model
arn = ARN(monthly_pcs[:,pc].squeeze())
# fit the model to the data
res = arn.fit()
arp = res.k_ar
# create a timeseries of predicted values
predicted_pcs[:,pc] = M*arn.predict(res.params, noise='all', dynamic=True, start=arp, end=n_mnths+arp+11)
# reconstruct the field and return
# reconstruct the field
monthly_intvar = reconstruct_field(predicted_pcs, monthly_eofs[:n_pcs], n_pcs, wgts)
return monthly_intvar
#############################################################################
def save_3d_file(out_fname, out_data, out_lon_var, out_lat_var, out_attrs, t_data, t_attrs, out_vname="sst"):
# open the file
out_fh = netcdf_file(out_fname, "w")
# create latitude and longitude dimensions - copy from the ens_mean file
lon_data = numpy.array(out_lon_var[:])
lat_data = numpy.array(out_lat_var[:])
lon_out_dim = out_fh.createDimension("longitude", lon_data.shape[0])
lat_out_dim = out_fh.createDimension("latitude", lat_data.shape[0])
lon_out_var = out_fh.createVariable("longitude", lon_data.dtype, ("longitude",))
lat_out_var = out_fh.createVariable("latitude", lat_data.dtype, ("latitude",))
time_out_dim = out_fh.createDimension("time", t_data.shape[0])
time_out_var = out_fh.createVariable("time", t_data.dtype, ("time",))
lon_out_var[:] = lon_data
lat_out_var[:] = lat_data
time_out_var[:] = t_data
lon_out_var._attributes = out_lon_var._attributes
lat_out_var._attributes = out_lat_var._attributes
time_out_var._attributes = t_attrs
data_out_var = out_fh.createVariable(out_vname, out_data.dtype, ("time", "latitude", "longitude"))
data_out_var[:] = out_data[:]
data_out_var._attributes = out_attrs
out_fh.close()
#############################################################################
def fit_mean_to_likely(ens_mean, monthly=True):
# scale the ensemble mean so that the GMSST matches the AR5 likely range
# between 2016 and 2035.
# first need the scaling factors to convert GMT to GMSST. These are
# calculated from a linear regression between the GMT and GMSST of the
# CMIP5 models for the RCP4.5 and historical scenarios.
# monthly scaling factor
if monthly:
M = 12
else:
M = 1
# plot the AR5 fig 11.25 likely range
# first calc in GMT
grad_min = (0.3-0.16)/(2025.5-2009.0)
grad_max = (0.7-0.16)/(2025.5-2009.0)
# convert to gmsst using the values of slope and intercept computed
# by regressing the tos anomaly onto tas anomaly in CMIP5 ensemble members
slope = 0.669
intercept = 0.017
gmsst_min_2016 = (grad_min*(2016.0-2009.0)+0.16-1.0) * slope + intercept
gmsst_max_2016 = (grad_max*(2016.0-2009.0)+0.16+1.0) * slope + intercept
gmsst_min_2035 = (grad_min*(2035.0-2009.0)+0.16-1.0) * slope + intercept
gmsst_max_2035 = (grad_max*(2035.0-2009.0)+0.16+1.0) * slope + intercept
# calculate the middle of the likely range
likely_gmsst_mean_2016 = (gmsst_min_2016+gmsst_max_2016) * 0.5
likely_gmsst_mean_2035 = (gmsst_min_2035+gmsst_max_2035) * 0.5
# what is the ratio of the gmsst ens_mean to gmsst_mean at 2016 and 2035
ens_mean_2016 = ens_mean[(2016-1899)*M]
ens_mean_2016 = numpy.reshape(ens_mean_2016, [1, ens_mean_2016.shape[0], ens_mean_2016.shape[1]])
ens_mean_2035 = ens_mean[(2035-1899)*M]
ens_mean_2035 = numpy.reshape(ens_mean_2035, [1, ens_mean_2035.shape[0], ens_mean_2035.shape[1]])
gmsst_ratio_2016 = (likely_gmsst_mean_2016 / calc_GMSST(ens_mean_2016)).squeeze()
gmsst_ratio_2035 = (likely_gmsst_mean_2035 / calc_GMSST(ens_mean_2035)).squeeze()
# create a linear interpolation between 1.0 in 2005, the 2016 ratio and
# the 2035 ratio - 2035 ratio will then be applied for rest of timeseries
gmsst_scaling = numpy.ones([ens_mean.shape[0],1,1], 'f')
# calculate the interpolated sections
interp_section_2005_2016 = numpy.interp(numpy.arange(2005,2017,1.0/M), [2005,2016], [1.0, gmsst_ratio_2016])
interp_section_2016_2035 = numpy.interp(numpy.arange(2017,2035,1.0/M), [2016,2035], [gmsst_ratio_2016, gmsst_ratio_2035])
gmsst_scaling[(2005-1899)*M:(2017-1899)*M] = interp_section_2005_2016.reshape([interp_section_2005_2016.shape[0],1,1])
gmsst_scaling[(2017-1899)*M:(2035-1899)*M] = interp_section_2016_2035.reshape([interp_section_2016_2035.shape[0],1,1])
gmsst_scaling[(2035-1899)*M:] = gmsst_ratio_2035
ens_mean = ens_mean * gmsst_scaling
return ens_mean
#############################################################################
# The synthetic SSTs have two distinct time periods:
# 1. The HadISST2 period (1899->2010)
# 2. The CMIP5 period (2006->2100)
#
# To create the SSTs we create the SSTs for each individual time period and
# then interpolate between the two for the overlapping period (2006->2100)
#
# The methods used to generate each one can be decomposed into:
# (HadISST period) HadISST monthly smoothed data +
# HadISST internal variability (statistically generated)
# (CMIP5 period) CMIP5 monthly smoothed ensemble mean +
# CMIP5 monthly smoothed long term trend (anomalies from the ensemble mean) +
# HadISST 1986->2005 yearly reference pattern +
# HadISST internal variability +
# HadISST annual cycle
#
# To avoid discontinuity of the internal variability, we generate one long
# timeseries of this to add onto the results at the end
# functions follow to generate each individual component
#############################################################################
def create_hadisst_long_term_timeseries(monthly=True, run_n=400):
# get the dates
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
hadisst_ey = 2010
# create the long term trend timeseries from the monthly smoothed hadisst data
if monthly:
hadisst_smoothed_fname = get_HadISST_month_smooth_filename(histo_sy, hadisst_ey, run_n)
else:
hadisst_smoothed_fname = get_HadISST_smooth_fname(histo_sy, hadisst_ey, run_n)
hadisst_sst = load_sst_data(hadisst_smoothed_fname, "sst")
return hadisst_sst
#############################################################################
def correct_cmip5_long_term_mean_timeseries(cmip5_ts, monthly=True, run_n=400):
# correct the ensemble mean of the CMIP5 ensemble, this is achieved by subtracting the
# difference between the 5 year mean of HadISST and the 5 year mean of the CMIP5 ensemble
# mean for 2006->2010 (the overlap period) from the CMIP5 ensemble mean
# need to load in the hadisst values to enable the correction
# get the dates
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
hadisst_ey = 2010
if monthly:
hadisst_smoothed_fname = get_HadISST_month_smooth_filename(histo_sy, hadisst_ey, run_n)
else:
hadisst_smoothed_fname = get_HadISST_smooth_fname(histo_sy, hadisst_ey, run_n)
hadisst_sst = load_sst_data(hadisst_smoothed_fname, "sst")
# if monthly calculate the overlap indices in terms of months by multiplying by 12
if monthly:
# correct each month individually
ovl_idx = (hadisst_ey - histo_sy) * 12 # start offset
mon_correct = numpy.zeros([12, cmip5_ts.shape[1], cmip5_ts.shape[2]], 'f')
for m in range(0, 12):
cmip5_ens_mean_monmean = numpy.mean(cmip5_ts[ovl_idx-5+m:ovl_idx+m:12], axis=0)
hadisst_monmean = numpy.mean(hadisst_sst[ovl_idx-5+m::12], axis=0)
mon_correct[m] = hadisst_monmean - cmip5_ens_mean_monmean
# tile and subtract from cmip5 timeseries
n_repeats = cmip5_ts.shape[0] / 12
cmip5_ens_mean_correction = numpy.tile(mon_correct, [n_repeats,1,1])
else:
ovl_idx = hadisst_ey - histo_sy
cmip5_ens_mean_timmean = numpy.mean(cmip5_ts[ovl_idx-5:ovl_idx], axis=0)
hadisst_timmean = numpy.mean(hadisst_sst[ovl_idx-5:], axis=0)
cmip5_ens_mean_correction = hadisst_timmean - cmip5_ens_mean_timmean
return cmip5_ts + cmip5_ens_mean_correction
#############################################################################
def create_cmip5_long_term_mean_timeseries(run_type, ref_start, ref_end, monthly=True, run_n=400):
# create the cmip5 timeseries of the cmip5 ensemble mean
# This consists of the ens mean of the cmip5 anomalies from the reference
# plus the HadISST reference
# check which run type we should actually load.
# Likely is rcp45 + adjustment
if run_type == "likely":
load_run_type = "rcp45"
else:
load_run_type = run_type
# get the dates
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
hadisst_ey = 2010
# load the ensemble mean of the anomalies
cmip5_ens_mean_anoms_fname = get_concat_anom_sst_ens_mean_smooth_fname(load_run_type, ref_start, ref_end, monthly=monthly)
cmip5_ens_mean_anoms = load_sst_data(cmip5_ens_mean_anoms_fname, "sst")
# if we have the likely scenario then fit the period between 2016 and 2035 to the likely
# scenario from AR5 Ch11
if run_type == "likely":
cmip5_ens_mean_anoms = fit_mean_to_likely(cmip5_ens_mean_anoms, monthly)
# add it onto the ensemble mean anomalies
cmip5_ens_mean = cmip5_ens_mean_anoms
return cmip5_ens_mean
#############################################################################
def create_cmip5_rcp_anomalies(run_type, ref_start, ref_end, eof_year, percentile, monthly=True):
# create the time series of anomalies from the mean of the various
# samples in the CMIP5 ensemble
# This spans the uncertainty of the GMT response to GHG forcing in CMIP5
if run_type == "likely":
load_run_type = "rcp45"
else:
load_run_type = run_type
# load the eof patterns in the eof_year
eof_fname = get_cmip5_EOF_filename(load_run_type, ref_start, ref_end, eof_year, monthly=monthly)
eofs = load_sst_data(eof_fname, "sst")
# load the principle components for the eof_year
syn_pc_fname = get_syn_SST_PCs_filename(load_run_type, ref_start, ref_end, eof_year, monthly=monthly)
syn_pc_fname_new = syn_pc_fname[:-3] + "_new.nc"
syn_pc = load_data(syn_pc_fname_new, "sst")
# load the timeseries of scalings and offsets to the pcs over the CMIP5 period
proj_pc_scale_fname = get_cmip5_proj_PC_scale_filename(load_run_type, ref_start, ref_end, eof_year, monthly=monthly)
proj_pc_scale = load_data(proj_pc_scale_fname, "sst_scale")
proj_pc_offset = load_data(proj_pc_scale_fname, "sst_offset")
# corresponding weights that we supplied to the EOF function
coslat = numpy.cos(numpy.deg2rad(numpy.arange(89.5, -90.5,-1.0))).clip(0., 1.)
wgts = numpy.sqrt(coslat)[..., numpy.newaxis]
# create the timeseries of reconstructed SSTs for just this sample
# recreate the field - monthy by month if necessary
if monthly:
syn_sst_rcp = numpy.ma.zeros([proj_pc_scale.shape[0], eofs.shape[2], eofs.shape[3]], 'f')
for m in range(0, 12):
pc_ts = syn_pc[m,percentile,:neofs] * proj_pc_scale[m::12,:neofs] + proj_pc_offset[m::12,:neofs]
syn_sst_rcp[m::12] = reconstruct_field(pc_ts, eofs[m], neofs, wgts)
else:
pc_ts = syn_pc[0,percentile,:neofs] * proj_pc_scale[:,:neofs] + proj_pc_offset[:,:neofs]
syn_sst_rcp = reconstruct_field(pc_ts, eofs[0], neofs, wgts)
return syn_sst_rcp
#############################################################################
def create_hadisst_monthly_reference(run_type, ref_start, ref_end, n_repeats, run_n=400):
# create the annual cycle from hadisst, repeating it a number of times
# so as to add it onto the CMIP5 timeseries
# get the dates
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
hadisst_ey = 2010
# load in the monthly smoothed reference
mon_smooth_name = get_HadISST_monthly_reference_fname(histo_sy, hadisst_ey, ref_start, ref_end, run_n)
mon_smooth_ref = load_sst_data(mon_smooth_name, "sst")
mon_smooth_ref_tile = numpy.tile(mon_smooth_ref, [n_repeats,1,1])
return mon_smooth_ref_tile
#############################################################################
def create_hadisst_cmip5_long_term_timeseries(hadisst_ts, cmip5_ts, monthly=True):
# create a long timeseries of the long term trend which has the hadisst
# data for the first ~100 years and then the CMIP5 trends into the future
# create the output - this is just the shape of the cmip5 timeseries as the
# cmip5 timeseries also spans the historical timeperiod
out_data = numpy.ma.zeros(cmip5_ts.shape, 'f')
# get the dates
histo_sy, histo_ey, rcp_sy, rcp_ey = get_start_end_periods()
hadisst_ey = 2010
# get the monthly scalar for the indices
if monthly:
M = 12
else:
M = 1
# assign the output data from hadisst_ey+1 to rcp_ey to the reconstructed sst
out_data[(hadisst_ey-histo_sy)*M:] = cmip5_ts[(hadisst_ey-histo_sy)*M:]
ovl_yr = 5 # overlap years
# assign the output data from 0 to hadisst_ey-histo_sy-5 to the HadISST ssts
out_data[:(hadisst_ey-histo_sy-ovl_yr)*M] = hadisst_ts[:(hadisst_ey-histo_sy-ovl_yr)*M]
# now, over the 5 year period we want to interpolate between the HadISST
# data and the data generated from the CMIP5 ensemble
for y in range(0, ovl_yr):
# calculate the weights
w0 = float(ovl_yr-y)/ovl_yr # HadISST2 weight
w1 = float(y)/ovl_yr # CMIP5 weight
for mm in range(0, M):
idx = (hadisst_ey-histo_sy-ovl_yr+y)*M + mm
out_data[idx] = hadisst_ts[idx] * w0 + cmip5_ts[idx] * w1
return out_data.astype(numpy.float32)
#############################################################################
def save_syn_SSTs(out_data, run_type, ref_start, ref_end, neofs, eof_year, sample, intvarmode, monthly):
if run_type == "likely":
load_run_type = "rcp45"
else:
load_run_type = run_type
# we require the length of the time series - get this from the proj_pc_scale_fname
proj_pc_scale_fname = get_cmip5_proj_PC_scale_filename(load_run_type, ref_start, ref_end, eof_year, monthly=monthly)
fh = netcdf_file(proj_pc_scale_fname, 'r')
t_var = fh.variables["time"]
# we require the shape of the field - get this from the cmip5 ens mean file
ens_mean_fname = get_concat_anom_sst_ens_mean_smooth_fname(load_run_type, ref_start, ref_end, monthly=monthly)
fh2 = netcdf_file(ens_mean_fname, 'r')
lon_var = fh2.variables["longitude"]
lat_var = fh2.variables["latitude"]
attrs = fh2.variables["sst"]._attributes
# mask any nans
mv = attrs["_FillValue"]
out_data = numpy.ma.fix_invalid(out_data, fill_value=mv)
# fix the lsm
for t in range(1, out_data.shape[0]):
out_data.data[t][out_data.data[0] == mv] = mv
# get the output name - manipulate the sample
ptiles = [0.10, 0.25, 0.50, 0.75, 0.90]
out_ptile = int(ptiles[sample/20] * 100)
out_sample = sample % 20 # 20 samples per percentile
out_name = get_syn_sst_filename(run_type, ref_start, ref_end, neofs, eof_year, out_ptile, intvarmode, monthly)
out_name = out_name[:-3] + "_s" + str(out_sample) + ".nc"
save_3d_file(out_name, out_data, lon_var, lat_var, attrs, t_var[:], t_var._attributes)
fh.close()
fh2.close()
print out_name
#############################################################################
def create_syn_SSTs(run_type, ref_start, ref_end, neofs, eof_year, sample, intvarmode, var_eofs, monthly):
# determine which hadisst ensemble member to use
hadisst_ens_members = [1059, 115, 1169, 1194, 1346, 137, 1466, 396, 400, 69]
run_n = hadisst_ens_members[numpy.random.randint(0, len(hadisst_ens_members))]
# get the hadisst trend
hadisst_trend = create_hadisst_long_term_timeseries(monthly, run_n)
# get the cmip5 trend
cmip5_trend = create_cmip5_long_term_mean_timeseries(run_type, ref_start, ref_end, monthly, run_n)
# create the sample from the distribution of the CMIP5 SST response to GHG forcing
syn_sst_rcp = create_cmip5_rcp_anomalies(run_type, ref_start, ref_end, eof_year, sample, monthly)
# cmip5 ssts are the sum of the ensemble mean trend and the deviation from the ensemble mean
# monthly ssts have the hadisst annual cycle added onto them
# create the hadisst annual cycle to add to the cmip5 projected SSTs, if monthly data is
# required
if monthly:
n_repeats = cmip5_trend.shape[0] / 12 # number of repeats = number of years
hadisst_ac = create_hadisst_monthly_reference(run_type, ref_start, ref_end, n_repeats, run_n)
cmip5_sst = cmip5_trend + hadisst_ac
else:
cmip5_sst = cmip5_trend
# adjust the cmip5 data
cmip5_sst = correct_cmip5_long_term_mean_timeseries(cmip5_sst, monthly, run_n)
# add the synthetic warming
cmip5_sst += syn_sst_rcp
# create the interpolated / composite trend data
out_data = create_hadisst_cmip5_long_term_timeseries(hadisst_trend, cmip5_sst, monthly)
# need these for smoothing the data
ens_mean_fname = get_concat_anom_sst_ens_mean_smooth_fname(run_type, ref_start, ref_end, monthly=monthly)
fh2 = netcdf_file(ens_mean_fname, 'r')
lat_var = fh2.variables["latitude"]
attrs = fh2.variables["sst"]._attributes
mv = attrs["_FillValue"]
# smooth the data
lat_data = lat_var[:].byteswap().newbyteorder().astype(numpy.float32)
out_data = zonal_smoother(out_data, lat_data, 64, 90, mv)
fh2.close()
# create the synthetic internal variability
if monthly:
intvar = create_monthly_intvar(run_type, ref_start, ref_end, n_pcs=var_eofs, run_n=run_n)
out_data += intvar.astype(numpy.float32)
# save the synthetic ssts
save_syn_SSTs(out_data, run_type, ref_start, ref_end, neofs, eof_year, sample, intvarmode, monthly)
#############################################################################
if __name__ == "__main__":
# defaults arguments only
intvarmode = 0 # internal variability mode - 0 = none, 1 = yearly, 2 = monthly
monthly = False # use the monthly EOFs / PCs ?
opts, args = getopt.getopt(sys.argv[1:], 'r:s:e:n:f:a:i:v:m',
['run_type=', 'ref_start=', 'ref_end=', 'neofs=', 'eof_year=',
'sample=', 'intvarmode=',
'varneofs=', 'monthly'])
for opt, val in opts:
if opt in ['--run_type', '-r']:
run_type = val
if opt in ['--ref_start', '-s']:
ref_start = int(val)
if opt in ['--ref_end', '-e']:
ref_end = int(val)
if opt in ['--neofs', '-n']:
neofs = int(val)
if opt in ['--eof_year', '-f']:
eof_year = int(val)
if opt in ['--sample', '-a']:
sample = int(val)
if opt in ['--intvar', '-i']:
intvarmode = int(val)
if opt in ['--varneofs', '-v']:
var_eofs = int(val)
if opt in ['--monthly', '-m']:
monthly = True
create_syn_SSTs(run_type, ref_start, ref_end, neofs, eof_year, sample, intvarmode, var_eofs, monthly)