def calc_SNR(): SNRs = [] N = 20 for endyear in range(2021, 2096): print(endyear) members = [] for i in range(1, N + 1): ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0." + varcode + ".202001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2020, tim2=endyear, varcode=varcode) trend = vartimeobj.trend_lat_lon(season) members.append(trend) ensmean, ensstd = ensemble_defs.stats(members) SNR = abs(ensmean) / (ensstd / np.sqrt(N - 1)) SNRs.append(SNR) dim = 'endyear' new_coord = range(2021, 2096) func = lambda *x: np.stack(x, axis=-1) xSNR = xr.apply_ufunc(func, *SNRs, output_core_dims=[[dim]], join='outer', dataset_fill_value=np.nan) xSNR[dim] = new_coord xSNR.to_netcdf('SNR_' + varcode + '_trend.nc') return
import ensemble_functions import plot_functions import vartimeproc #******************************************************************************************************** season = 'DJF' outdir="/Users/abanerjee/scripts/glens/output/" varcode = 'TREFHT' #******************************************************************************************************** # 1) Plot interannual sigma from Base variability # annual, seasonal mean members_control = [] for i in range(1,21): ncpath = glob.glob("/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0"+str(i).zfill(2)+"/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0"+str(i).zfill(2)+".cam.h0."+varcode+".201001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2010, tim2=2030, varcode=varcode) ann = vartimeobj.annual_mean(season) members_control.append(ann) # adding coordinate dim = 'member' new_coord = range(len(members_control)) func = lambda *x: np.stack(x, axis=-1) stack = xr.apply_ufunc(func, *members_control, output_core_dims=[[dim]], join='outer', dataset_fill_value=np.nan) stack[dim] = new_coord # Interannual sigma from residuals of annual means from member average gives similar results
def clim_lat_hgt(run, season, varcode): members = [] # Control Climatology if run == 'control': print("Calculating climatology for control") for i in range(1, 21): ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/p.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0zm." + varcode + ".201001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2010, tim2=2030, varcode=varcode, zm=True) clim = vartimeobj.clim_mean(season) members.append(clim) # RCP8.5 if run == 'rcp85': print("Calculating trend for RCP8.5") for i in [1, 2, 3]: ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/p.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0zm." + varcode + ".201001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2075, tim2=2095, varcode=varcode, zm=True) clim = vartimeobj.clim_mean(season) members.append(clim) # Feedback elif run == 'feedback': print("Calculating trend for Feedback") for i in range(1, 21): ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/p.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0zm." + varcode + ".202001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2075, tim2=2095, varcode=varcode, zm=True) clim = vartimeobj.clim_mean(season) members.append(clim) # GEOHEAT_S elif run == 'geoheats': print("Calculating trend for GEOHEAT_S") for i in range(1, 5): yrvals = [] for yr in range(2011, 2031): ncpath = glob.glob( "/Volumes/CESM-GLENS/SUE/" + str(i).zfill(3) + "/b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + "/Combined/" + varcode + ".b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + ".zm.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=yr, tim2=yr + 1, varcode=varcode, zm=True) clim = vartimeobj.clim_mean(season) yrvals.append(clim) yrvals_mean, yrvals_std = ensemble_defs.stats(yrvals) members.append(yrvals_mean) return members
def clim_lat_lon(run, season, varcode): members = [] # Control climatology if run == 'control': print("Calculating climatology for Control") for i in range(1, 21): if varcode == 'precip': ncpath1 = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0.PRECC.201001-*.nc")[0] ncpath2 = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0.PRECL.201001-*.nc")[0] vartimeobj = vartimeproc.PrecipTimeProc(ncpath1, ncpath2, tim1=2010, tim2=2030, ppt1='PRECC', ppt2='PRECL') else: ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0." + varcode + ".201001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2010, tim2=2030, varcode=varcode) clim = vartimeobj.clim_mean(season) members.append(clim) # RCP8.5 if run == 'rcp85': print("Calculating climatology for RCP8.5") for i in [1, 2, 3]: # Precip if varcode == 'precip': ncpath1 = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0.PRECC.201001-*.nc")[0] ncpath2 = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0.PRECL.201001-*.nc")[0] vartimeobj = vartimeproc.PrecipTimeProc(ncpath1, ncpath2, tim1=2075, tim2=2095, ppt1='PRECC', ppt2='PRECL') # All other variables else: ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.control.0" + str(i).zfill(2) + ".cam.h0." + varcode + ".201001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2075, tim2=2095, varcode=varcode) clim = vartimeobj.clim_mean(season) members.append(clim) # Feedback elif run == 'feedback': print("Calculating climatology for Feedback") for i in range(1, 21): # Precip if varcode == 'precip': ncpath1 = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0.PRECC.202001-*.nc")[0] ncpath2 = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0.PRECL.202001-*.nc")[0] vartimeobj = vartimeproc.PrecipTimeProc(ncpath1, ncpath2, tim1=2075, tim2=2095, ppt1='PRECC', ppt2='PRECL') # All other variables else: ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0." + varcode + ".202001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2075, tim2=2095, varcode=varcode) clim = vartimeobj.clim_mean(season) members.append(clim) # GEOHEAT_S elif run == 'geoheats': print("Calculating climatology for GEOHEAT_S") for i in range(1, 5): yrvals = [] for yr in range(2011, 2031): # Precip if varcode == 'precip': ncpath1 = glob.glob( "/Volumes/CESM-GLENS/SUE/" + str(i).zfill(3) + "/b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + "/Combined/PRECC.b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + ".nc")[0] ncpath2 = glob.glob( "/Volumes/CESM-GLENS/SUE/" + str(i).zfill(3) + "/b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + "/Combined/PRECL.b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + ".nc")[0] vartimeobj = vartimeproc.PrecipTimeProc(ncpath1, ncpath2, tim1=yr, tim2=yr + 1, ppt1='PRECC', ppt2='PRECL') # All other variables else: ncpath = glob.glob( "/Volumes/CESM-GLENS/SUE/" + str(i).zfill(3) + "/b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + "/Combined/" + varcode + ".b.e15.B5505C5WCCML45BGCR.f09_g16.GEOHEATSUE." + str(i).zfill(3) + "_" + str(yr) + ".nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=yr, tim2=yr + 1, varcode=varcode) clim = vartimeobj.clim_mean(season) yrvals.append(clim) yrvals_mean, yrvals_std = ensemble_defs.stats(yrvals) members.append(yrvals_mean) # Convert to hPa for PSL if varcode == 'PSL': members = [x * 0.01 for x in members] return members
"/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0.PRECL.202001-*.nc")[0] vartimeobj = vartimeproc.PrecipTimeProc(ncpath1, ncpath2, tim1=2020, tim2=2095, ppt1='PRECC', ppt2='PRECL') else: ncpath = glob.glob( "/Volumes/CESM-GLENS/GLENS/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + "/atm/proc/tseries/month_1/Combined/b.e15.B5505C5WCCML45BGCR.f09_g16.feedback.0" + str(i).zfill(2) + ".cam.h0." + varcode + ".202001-*.nc")[0] vartimeobj = vartimeproc.VarTimeProc(ncpath, tim1=2020, tim2=2095, varcode=varcode) tseries_surf = vartimeobj.annual_mean(season) * scaler[varcode] trend_surf = vartimeobj.trend_lat_lon(season) * scaler[varcode] # NAM index at 50hPa: timeseries and trend ilev = np.where(nplevel == 50)[0][0] tseries_NAM = np.load(npydir + 'NAM-Z3_PC_feedback_' + str(i) + '_DJF.npy')[:, ilev] nyrs = tseries_NAM.shape[0] trend_NAM = ss.linregress(range(nyrs), tseries_NAM)[0] * 30 # congruent trend regress = lambda y: ss.linregress(tseries_NAM, y)[0] * trend_NAM tseries_surf_flat = tseries_surf.values.reshape( [tseries_surf.shape[0], tseries_surf.shape[1] * tseries_surf.shape[2]])