print('BAUUUUUUUUUUUUUUUUUUUUUUUUU') ## plots runfreq = dict() seasfreq = dict() fig = plt.figure(figsize=(16, 12)) for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) cosi = [] for mem in okmods: labok, datok = ctl.sel_time_range(results_hist[mem]['labels'], results_hist[mem]['dates'], ctl.range_years(yr0, yr1)) seasfr, yr = ctl.calc_seasonal_clus_freq(labok, datok, numclus) seasfreq[('hist_cmip5', mem, reg)] = seasfr[reg, :] seas20 = np.array(ctl.running_mean(seasfr[reg, :], 20)) print(mem, len(seas20)) if len(seas20) == len(yr): ax.plot(yr, seas20) cosi.append(seas20) else: print(mem, len(seas20), 'too short') coso = np.mean(cosi, axis=0) runfreq[('hist_cmip5', reg)] = coso ax.plot(yr, coso, color='black', linewidth=3) ax.set_title(reg_names_area[area][reg]) fig.savefig(cart_out + 'models_run20_{}_hist.pdf'.format(area)) reg_names = reg_names_area[area]
runfreq = dict() seasfreq = dict() figs = [] for tip, col in zip(alltips, colorz): okmods = resdict[tip].keys() fig = plt.figure(figsize=(16, 12)) for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) cosi = [] for mem in okmods: seasfr, yr = ctl.calc_seasonal_clus_freq( resdict[tip][mem]['labels'], resdict[tip][mem]['dates'], numclus) seasfreq[(tip, mem, reg)] = seasfr[reg, :] seas1 = np.array(ctl.running_mean(seasfr[reg, :], 20)) runfreq[(tip, mem, reg)] = seas1 ax.plot(yr, seas1, label=tip, color=col) ax.set_title(reg_names_area[area][reg]) #ax.legend() fig.suptitle(tip) figs.append(fig) ctl.plot_pdfpages(cart_out + 'check_single_members_{}.pdf'.format(area), figs) fig = plt.figure(figsize=(16, 12)) for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) for tip, col in zip(alltips, colorz): okmods = resdict[tip].keys()
print(okmods) print(len(okmods)) runfreq = dict() seasfreq = dict() fig = plt.figure(figsize=(16, 12)) for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) cosi = [] for mem in okmods: seasfr, yr = ctl.calc_seasonal_clus_freq( results_hist[mem]['labels'], results_hist[mem]['dates'], numclus) seasfreq[('hist', mem, reg)] = seasfr[reg, :] seas20 = np.array(ctl.running_mean(seasfr[reg, :], 20)) ax.plot(yr, seas20) cosi.append(seas20) coso = np.mean(cosi, axis=0) runfreq[('hist', reg)] = coso ax.plot(yr, coso, color='black', linewidth=3) ax.set_title(reg_names_area[area][reg]) fig.savefig(cart_out + 'models_run20_{}_hist.pdf'.format(area)) reg_names = reg_names_area[area] seasfr, yr_ref = ctl.calc_seasonal_clus_freq(results_ref['labels'], results_ref['dates'], numclus) for reg in range(4): seasfreq[('hist', 'ref', reg)] = seasfr[reg, :]
okmods_hist = list(results_hist.keys()) okmods_ssp = list(results_ssp[ssp].keys()) okmods = okmods_ssp runfreq = dict() seasfreq = dict() fig = plt.figure(figsize = (16,12)) for reg in range(4): ax = fig.add_subplot(2, 2, reg+1) cosi = [] for mem in okmods_hist: seasfr, yr = ctl.calc_seasonal_clus_freq(results_hist[mem]['labels'], results_hist[mem]['dates'], numclus) seasfreq[('hist', mem, reg)] = seasfr[reg, :] seas10 = np.array(ctl.running_mean(seasfr[reg, :], yr10)) ax.plot(yr, seas10) cosi.append(seas10) coso = np.mean(cosi, axis = 0) runfreq[('hist', reg)] = coso ax.plot(yr, coso, color = 'black', linewidth = 3) ax.set_title(reg_names_area[area][reg]) fig.savefig(cart_out + 'models_freq10_{}_hist.pdf'.format(area, ssp)) trend_ssp = dict() residtime_ssp = dict() for ssp in allssps: fig = plt.figure(figsize = (16,12)) for reg in range(4):
if 'freq_clus_seasonal' not in resu_ref: resu_ref['freq_clus_seasonal'], resu_ref[ 'freq_clus_seasonal_years'] = ctl.calc_seasonal_clus_freq( resu_ref['labels'], resu_ref['dates'], 4) #gigi_obs = xr.DataArray(resu_ref['freq_clus_seasonal'], dims = ('reg', 'time'), coords = {'reg': [0, 1, 2, 3], 'time': resu_ref['freq_clus_seasonal_years']}) fig = plt.figure(figsize=(16, 12)) axes = [] for num, patt in enumerate(patnames[seas]): ax = plt.subplot(2, 2, num + 1) for ke, col in zip(keall, colorz): timeseries[ke].sel(reg=num).plot(ax=ax, color=col, linewidth=0.2) rupi = ctl.running_mean(timeseries[ke].sel(reg=num).values, 10) ax.plot(timeseries[ke].time, rupi, color=col, linewidth=3, label=ke) ax.set_title(patt, fontsize=16) axes.append(ax) ax.grid() if num in [2, 3]: ax.set_xlabel('year') else: ax.set_xlabel(None) if num == 1: ax.legend()
# plot resid times w std dev for mod in all_res: axes = [] fig = plt.figure() for j in range(kwar['numclus']): ax = fig.add_subplot(2, 2, j + 1) ax.set_title(pattnames[j]) histomea = np.mean( [coso['histo_resid_times'][j] for coso in all_res[mod]], axis=0) histostd = np.std( [coso['histo_resid_times'][j] for coso in all_res[mod]], axis=0) ax.bar(numarr, histomea, alpha=0.5, label='mean', color='indianred') cosomea = ctl.running_mean(histomea[:-1], 3) ax.bar(numarr, histostd, alpha=0.5, label='stddev', color='steelblue') cosostd = ctl.running_mean(histostd[:-1], 3) ax.plot(numarr[:-1], cosomea, color='indianred') ax.plot(numarr[:-1], cosostd, color='steelblue') ax.legend() ax.set_xlim(0, max_days + 2) tics = np.arange(0, max_days + 2, 5) labs = ['{}'.format(ti) for ti in tics[:-1]] labs.append('>{}'.format(max_days)) ax.set_xticks(tics, minor=False) ax.set_xticklabels(labs, size='small') ax.set_xlabel('Days') ax.set_ylabel('Frequency') ax.set_ylim(0, 0.2)
spect_mean = np.mean(allcose, axis=0) spect_q1 = np.percentile(allcose, 25, axis=0) spect_q3 = np.percentile(allcose, 75, axis=0) if ind == 'enso': zuc = np.sum(spect_mean) spect_mean /= zuc spect_q1 /= zuc spect_q3 /= zuc if ind == 'enso': ruwi = 3 else: ruwi = 5 spect_mean = ctl.running_mean(spect_mean, ruwi) spect_q1 = ctl.running_mean(spect_q1, ruwi) spect_q3 = ctl.running_mean(spect_q3, ruwi) #due giri di smoothing spect_mean = ctl.running_mean(spect_mean, ruwi) spect_q1 = ctl.running_mean(spect_q1, ruwi) spect_q3 = ctl.running_mean(spect_q3, ruwi) # if ind == 'enso': # thres = 10 # else: # thres = 50 # ax.fill_between(frme, spect_q1, spect_q3, color = col, alpha = 0.3) # ax.plot(frme, spect_mean, color = col, lw = 2)
cb.set_label('Global net TOA (W/m2)') fig.savefig(cart_out + 'amvperc_vs_tasytoa_50.pdf') fig, axs = plt.subplots(2, 2, figsize=(16, 12)) for ru, ax in zip(allru, axs.flatten()): piuz = enso[ru]['tos'].groupby('time.year').mean() data = piuz.values.flatten() ps = np.abs(np.fft.fft(data))**2 freqs = np.fft.fftfreq(data.size, 1) idx = freqs > 0 ax.plot(1 / freqs[idx], ps[idx], linewidth=0.5) #ps_low = ctl.butter_filter(ps, 20) ps_low = ctl.running_mean(ps, 20) ax.plot(1 / freqs[idx], ps_low[idx], linewidth=2) #ps_low = ctl.butter_filter(ps, 50) ps_low = ctl.running_mean(ps, 50) ax.plot(1 / freqs[idx], ps_low[idx], linewidth=2) ax.set_title(ru) if ax in axs[1, :]: ax.set_xlabel('period (yr)') #ax.set_ylabel('amplitude') ax.set_xlim(2, 50) #ctl.adjust_ax_scale(axs.flatten()) fig.savefig(cart_out + 'amv_spectra_all500.pdf') #frbins = [0, 5, 10, 20, 30, 50, 100, 200]
fig = plt.figure() for ens in stoc_ens: sig = np.array([results[(ens,area,ran)]['significance'] for ran in yr_ranges]) plt.plot(cyea, sig, label = ens) # plt.scatter(cyea, sig, color = col, marker = sym, label = ens) plt.legend() plt.ylabel('Significance') plt.xlabel('central year of 30yr period') plt.title(area+' - stoc runs') fig.savefig(cartsig+'Sig_{}_stoc_5yr.pdf'.format(area)) fig = plt.figure() for ens in base_ens: sig = np.array([results[(ens,area,ran)]['significance'] for ran in yr_ranges]) sig = ctl.running_mean(sig, 5) plt.plot(cyea, sig, label = ens) # plt.scatter(cyea, sig, color = col, marker = sym, label = ens) plt.legend() plt.ylabel('Significance') plt.xlabel('central year of 30yr period') plt.title(area+' - base runs (25 yr smooth)') fig.savefig(cartsig+'Sig_{}_base_20yr_smooth.pdf'.format(area)) fig = plt.figure() for ens in stoc_ens: sig = np.array([results[(ens,area,ran)]['significance'] for ran in yr_ranges]) sig = ctl.running_mean(sig, 5) plt.plot(cyea, sig, label = ens) # plt.scatter(cyea, sig, color = col, marker = sym, label = ens) plt.legend()
ref_solver=ref_solver, ref_patterns_area=ref_patterns_area, detrended_eof_calculation=True, detrended_anom_for_clustering=False, heavy_output=False) freq1 = ctl.calc_seasonal_clus_freq(resu1['labels'], resu1['dates']) freq2 = ctl.calc_seasonal_clus_freq(resu2['labels'], resu2['dates']) years = np.unique(pd.to_datetime(resu1['dates']).year)[:-1] patnames = ['NAO +', 'Blocking', 'NAO -', 'Atl. Ridge'] fig = plt.figure() plt.ylim(15., 35.) plt.title('detr.EOFs + detrended anomalies') for clu, clunam in enumerate(patnames): smut = ctl.running_mean(freq1[:, clu], wnd=30) plt.plot(years, smut, label=clunam) fig = plt.figure() plt.ylim(20., 30.) plt.title('detr.EOFs + non-detrended anomalies') for clu, clunam in enumerate(patnames): smut = ctl.running_mean(freq2[:, clu], wnd=30) plt.plot(years, smut, label=clunam) trans_matrix = ctl.calc_regime_transmatrix(1, resu1['labels'], dates_season) print(trans_matrix) filter = resu1['dist_centroid'] < np.percentile(resu1['dist_centroid'], 70) trans_pcs = ctl.find_transition_pcs(1, resu1['labels'][filter], dates_season[filter],
cose['fdNAO50'] = freqs[(ssp, mod, 'tot50')][0] - freqs[ ('hist', mod, 'tot50')][0] cose['fdSBL50'] = freqs[(ssp, mod, 'tot50')][1] - freqs[ ('hist', mod, 'tot50')][1] cose['fdAR50'] = freqs[(ssp, mod, 'tot50')][2] - freqs[ ('hist', mod, 'tot50')][2] cose['fdNAOneg50'] = freqs[(ssp, mod, 'tot50')][3] - freqs[ ('hist', mod, 'tot50')][3] cose['fdNAO50_divDT'] = cose['fdNAO50'] / cose['deltaT'] cose['fdSBL50_divDT'] = cose['fdSBL50'] / cose['deltaT'] cose['fdAR50_divDT'] = cose['fdAR50'] / cose['deltaT'] cose['fdNAOneg50_divDT'] = cose['fdNAOneg50'] / cose['deltaT'] gigi = ctl.running_mean(seasfreq[(ssp, mod, 0)], yr10, remove_nans=True) pio = mk.original_test(gigi) cose['trendNAO_pval'] = pio.p if pio.h: trendo = pio.trend nutrend = pio.trend ima = -1 while nutrend == trendo: gigi = ctl.running_mean(seasfreq[(ssp, mod, 0)][:ima], yr10, remove_nans=True) pio = mk.original_test(gigi) nutrend = pio.trend
linewidth=1.5, color=col[exp][1]) axtot2.scatter(x[50:], y[50:], label=exp, color=col[exp][0], s=2) axtot2.scatter(x[:50], y[:50], color=col[exp][0], s=8, marker='*') axtot2.plot(xlin, xlin * linefirst[0] + linefirst[1], label='slope = {:8.3f} +/- {:5.2f}'.format( linefirst[0], linefirst[2]), linewidth=1.5, color=col[exp][1]) ax1 = fig2.add_subplot(1, 2, i + 1) print(i) ax1.scatter(time, x, label='tas', color='lightsteelblue', s=3) coso = ctl.running_mean(x, 10) ax1.plot(time, coso, color='steelblue') axes1.append(ax1) ax1.set_title(exp) ax2 = ax1.twinx() ax2.scatter(time, y, label=var2, color='lightcoral', s=3) coso = ctl.running_mean(y, 10) ax2.plot(time, coso, color='indianred') axes2.append(ax2) #for i, (xli, line) in enumerate(zip(xlins, lines)): # ax.plot(xli, xli*line[0]+line[1], label='y{} = {:9.2e} x + {:9.2e}'.format(i, line[0], line[1]), linewidth = 1.5) ax1.legend(fontsize='small', loc=3) ax2.legend(fontsize='small', loc=4) #axes.append(ax)
oksiarea = oksi * areaok[np.newaxis, :] seaicearea = np.nansum(oksiarea, axis=1) dates = np.array(gigi.time.data) okmarch = np.array([da.month == 3 for da in dates]) oksept = np.array([da.month == 9 for da in dates]) resdict[(ru, varnam, 'glomean', 'mar')] = seaicearea[okmarch] resdict[(ru, varnam, 'glomean', 'sep')] = seaicearea[oksept] if ru != 'pi': yeaok = np.array([da.year for da in dates]) else: yeaok = np.array([da.year - 2256 + 2015 for da in dates]) sima10 = ctl.running_mean(seaicearea[okmarch], 10) sise10 = ctl.running_mean(seaicearea[oksept], 10) axs[ii, 0].plot(yeaok[okmarch], sima10, linestyle='solid', marker='None', color=col, label=ru, linewidth=2) axs[ii, 1].plot(yeaok[oksept], sise10, linestyle='solid', marker='None', color=col, label=ru,
for indexname, file_ref, file_mod, filogen, reg_names, n_yr in zip( indexes, file_refs, file_mods, filogens, reg_names_all, n_yrs): cart_out_ind = cart_out_all + indexname + '/' if not os.path.exists(cart_out_ind): os.mkdir(cart_out_ind) results, results_ref = pickle.load(open(filogen, 'rb')) amv_ref, dates = read_file_index(file_ref) amv_ref, dates = ctl.sel_time_range(amv_ref, dates, ctl.range_years(1957, 2013)) # Yearly amv index amv_ref_yr, yrdates = ctl.yearly_average(amv_ref, dates) amv_ref_yr = np.squeeze(amv_ref_yr) # amv_ref_djf, dates_djf = ctl.sel_season(amv_ref, dates, seas) # amv_ref_djf = np.squeeze(amv_ref_djf) amv_nyr = np.array(ctl.running_mean(amv_ref_yr, n_yr)) amvc = amv_nyr[~np.isnan(amv_nyr)] fig_all = plt.figure(figsize=(16, 12)) ax_all = [] for i in range(4): ax_all.append(fig_all.add_subplot(2, 2, i + 1)) fig_all_ul = plt.figure(figsize=(16, 12)) ax_all_ul = [] for i in range(4): ax_all_ul.append(fig_all_ul.add_subplot(2, 2, i + 1)) for ise, seas in enumerate(seasons): y1 = 1958 if seas == 'FM': y1 = 1959
lats_N = np.stack(lats_N) lats_S = np.stack(lats_S) percs = [10, 25, 50, 75, 90] lindict = dict() for prc in percs: lindict[('N', prc)] = np.percentile(lats_N, prc, axis=1).squeeze() lindict[('S', prc)] = np.percentile(lats_S, prc, axis=1).squeeze() lons = mapa['lon'].values for pol in ['N', 'S']: for prc in percs: lindict[(pol, prc)] = ctl.running_mean(lindict[(pol, prc)], 5, cyclic=True) lindict[(pol, prc)], lonu = cutil.add_cyclic_point(lindict[(pol, prc)], coord=lons) # spd_N = np.array(spd_N) # spd_S = np.array(spd_S) # lats_N_smo[spd_N < 5] = np.nan # lats_S_smo[spd_S < 5] = np.nan for pol in ['N', 'S']: ax.fill_between(lonu, lindict[(pol, 10)], lindict[(pol, 90)],
n_color_levels=21, draw_contour_lines=False, n_lines=5, color_percentiles=(0, 100), bounding_lat=30, plot_margins='EAT', add_rectangles=None, draw_grid=True, plot_type='filled_contour', verbose=False, lw_contour=0.5) figs.append(fig) ctl.plot_pdfpages(cart_out_orig + 'check_climate_mean_allmem.pdf', figs) fig = plt.figure() noreb = ctl.running_mean(resssp_noreb['EC-Earth3_r1i1p1f1']['pcs'][:, 0], 10) reb = ctl.running_mean(resssp['EC-Earth3_r1i1p1f1']['pcs'][:, 0], 10) plt.plot(noreb, label='rebase_ssp') plt.plot(reb, label='rebase_hist') plt.plot(reb - noreb, label='diff') fig.savefig(cart_out_orig + 'check_first_eof_vs_rebase.pdf') fig = plt.figure() noreb = ctl.running_mean(resssp_noreb['EC-Earth3_r1i1p1f1']['pcs'][:300, 0], 10) reb = ctl.running_mean(resssp['EC-Earth3_r1i1p1f1']['pcs'][:300, 0], 10) plt.plot(noreb, label='rebase_ssp') plt.plot(reb, label='rebase_hist') plt.plot(reb - noreb, label='diff') fig.savefig(cart_out_orig + 'check_first_eof_vs_rebase_zoom.pdf')
print(okmods) print(len(okmods)) runfreq = dict() seasfreq = dict() fig = plt.figure(figsize=(16, 12)) for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) cosi = [] for mem in okmods: seasfr, yr = ctl.calc_seasonal_clus_freq( results_hist[mem]['labels'], results_hist[mem]['dates'], numclus) seasfreq[('hist', mem, reg)] = seasfr[reg, :] seas10 = np.array(ctl.running_mean(seasfr[reg, :], yr10)) ax.plot(yr, seas10) cosi.append(seas10) coso = np.mean(cosi, axis=0) runfreq[('hist', reg)] = coso ax.plot(yr, coso, color='black', linewidth=3) ax.set_title(reg_names_area[area][reg]) fig.savefig(cart_out + 'models_freq10_{}_hist.pdf'.format(area, ssp)) trend_ssp = dict() residtime_ssp = dict() for ssp in allssps: fig = plt.figure(figsize=(16, 12)) for reg in range(4):
ctl.plot_pdfpages(figure_file, figures) plt.close('all') figure_file = cart_out + 'global_diff_net_fluxes.pdf' figures = [] axes = [] fig = plt.figure() ax = plt.subplot(1, 1, 1) plt.title('Net flux at TOA') for lett, tim in zip(['a', 'f'], ['_hist', '_fut']): for cosone, ls in zip(['stoc', 'base'], [':', '--']): coso = radclim[('global', cosone + tim, 'toa_balance')] ax.plot(years[lett], coso, linewidth=0.5, color='grey', linestyle=ls) rollcoso = ctl.running_mean(coso, wnd=10) ax.plot(years[lett], rollcoso, label=cosone + tim, linewidth=2.0) plt.xlabel('Year') plt.ylabel('Rad. forcing (W/m^2)') plt.legend() plt.grid() figures.append(fig) axes.append(ax) # fig = plt.figure() # ax = plt.subplot(1,1,1) # cosone = 'diff s-b' # coso = radclim[('global', 'stoc', 'toa_balance')]-radclim[('global', 'base', 'toa_balance')] # plt.title('Net flux at TOA - ({})'.format(cosone)) # ax.plot(years, coso, label = 'base', linewidth = 0.5, color = 'grey') # rollcoso = ctl.running_mean(coso, wnd = 10)
cosopi = yeamean[('pi', var)] # cosohist = yeamean[('hist', var)] # cosossp = yeamean[('ssp585', var)] cosohist = yeamean[('historical_mean', var)] cosossp = yeamean[('ssp585_mean', var)] #cosohistssp = xr.concat([cosohist, cosossp], dim = 'year') cosohistssp = np.concatenate([cosohist, cosossp], axis=0) yeahissp = np.arange(1970, 2101) fact = 1 if varnam == 'pr': fact = 60 * 60 * 24 * 365 pimap = fact * cosopi.mean('year').values pirun = ctl.running_mean(cosopi, 50) pistd = (cosopi - pirun).std('year').values if varnam == 'pr': thres = pimap < 50. fig_patt_trans = [] fig_patt_stab = [] fig_abs_start = [] fig_abs_end = [] fig_ratio = [] fig_std = [] for ru in allru[1:]: coso = yeamean[(ru, var)]