for stat, nam, tit in zip(all_stats, stat_nams, titles): fig = plt.figure(figsize = (16, 12)) #yrcho = [5, 10, 15, 20, 25, 30, 40, 50, 60] yrcho = [5, 10, 20, 30, 40, 50, 60] colors = ctl.color_set(len(yrcho)) ind = 0 axes = [] for reg in range(4): ind += 1 ax = plt.subplot(2, 2, ind) for n_choice, col in zip(yrcho, colors): allvals = np.array(stat[(n_choice, reg)]) for perc in [1, 5, 10, 100/3., 50, 200/3., 90, 95, 99]: ERA_ref_thresholds[(nam, n_choice, reg, perc)] = np.percentile(allvals, perc) kpdf = ctl.calc_pdf(allvals) #xvec = np.linspace(0., 0.4, 500) if 'KS' in nam: xvec = np.linspace(0., 0.5, 500) ax.axvline(0.06, color = 'grey', alpha = 0.6) elif nam == 'Clus radius': xvec = np.linspace(1000, 2200, 500) ax.axvline(ref_cent_dist[reg], color = 'grey', alpha = 0.6) elif nam == 'Clus std': xvec = np.linspace(300, 800, 500) ax.axvline(ref_cent_std[reg], color = 'grey', alpha = 0.6) elif nam == 'dist centocen': xvec = np.linspace(0, 2300, 500) else:
new_pcs1 = results1['pcs'] new_pcs2 = results2['pcs'] nulabs_1 = results1['labels'] nulabs_2 = results2['labels'] fig = plt.figure(figsize=(24, 6), dpi=150) for nucou, cou in enumerate(coups): regime_pdf_2D = [] regime_pdf_2D_func = [] xi, yi = np.meshgrid(cordss[cou[0]], cordss[cou[1]]) ax = fig.add_subplot(1, 3, nucou + 1) okclu = nulabs_ref == reg okpc = new_pcs[okclu, :] kufu = ctl.calc_pdf(okpc[:, cou].T) zi = kufu(np.vstack([xi.flatten(), yi.flatten()])) ax.contour(xi, yi, zi.reshape(xi.shape), cmap=cm.get_cmap('Blues'), label='ref') okclu = nulabs_1 == reg okpc = new_pcs1[okclu, :] kufu = ctl.calc_pdf(okpc[:, cou].T) zi = kufu(np.vstack([xi.flatten(), yi.flatten()])) ax.contour(xi, yi, zi.reshape(xi.shape), cmap=cm.get_cmap('Greens'),
pcs = results['ERA_0']['pcs'] dates = results['ERA_0']['dates'] grid_i = [] for i in range(3): (x0, x1) = (np.percentile(pcs[:, i], 1), np.percentile(pcs[:, i], 99)) xss = np.linspace(x0, x1, 50) grid_i.append(xss) xi_grid, yi_grid, zi_grid = np.meshgrid(*grid_i) zi_ref = [] for reg in range(4): okclu = labels == reg okpc = pcs[okclu, :] kufu = ctl.calc_pdf(okpc[:, :3].T) zi = kufu( np.vstack([xi_grid.flatten(), yi_grid.flatten(), zi_grid.flatten()])) zi = zi / np.max(zi) zi_ref.append(zi) filt_labels = ctl.regime_filter_long(labels, dates, days_thres=5) ref_cen_filt = [] zi_ref_filt = [] for reg in range(4): okclu = filt_labels == reg okpc = pcs[okclu, :] kufu = ctl.calc_pdf(okpc[:, :3].T)
yic, zic = np.meshgrid(yss, zss) cordss = [xss, yss, zss] namz = ['x', 'y', 'z'] coups = [(0, 1), (0, 2), (1, 2)] for cou in coups: regime_pdf_2D = [] regime_pdf_2D_func = [] xi, yi = np.meshgrid(cordss[cou[0]], cordss[cou[1]]) for clus in range(2): print(clus) okclus = results_ref['labels'] == clus okpc = new_pcs[okclus, :] kufu = ctl.calc_pdf(okpc[:, cou].T) print('fatto kufu\n') zi = kufu(np.vstack([xi.flatten(), yi.flatten()])) regime_pdf_2D.append(zi) regime_pdf_2D_func.append(kufu) fig = plt.figure(figsize=(8, 6), dpi=150) for li in new_trans: li3 = li[:, cou] p0 = li3[0] dp = li3[1] - li3[0] plt.arrow(p0[0], p0[1], dp[0], dp[1], color='red',
def dopdf(var, xi, bnd_width): pdf = ctl.calc_pdf(var, bnd_width = bnd_width) pdfok = pdf(xi) pdfok /= np.sum(pdfok) return pdfok
continue #raise ValueError('OPS!') linewidth = 1.5 if mod == 'ERA': linewidth = 2.5 for reg in range(4): okin = regok == reg okjet = jetind[okin] if tag == '_v7_optmatch': if mod == 'CMCC-CM2-HR4' or mod == 'EC-Earth3P-HR': okin = regok == reg_change[mod][reg] okjet = jetind[okin] jetlat_comp[(mod, reg, tag)] = okjet pdf = ctl.calc_pdf(okjet) if vv in ['LR', 'OBS']: axes[reg].plot(latgri, pdf(latgri), color = col, label = mod, linewidth = linewidth, linestyle = sty) if vv in ['HR', 'OBS']: axes_HR[reg].plot(latgri, pdf(latgri), color = col, label = mod, linewidth = linewidth, linestyle = sty) pdf = ctl.calc_pdf(jetind) if vv in ['LR', 'OBS']: ax_all.plot(latgri, pdf(latgri), color = col, label = mod, linewidth = linewidth, linestyle = sty) if vv in ['HR', 'OBS']: ax_all_HR.plot(latgri, pdf(latgri), color = col, label = mod, linewidth = linewidth, linestyle = sty) ctl.adjust_ax_scale(axes+axes_HR) # ctl.custom_legend(fig, colors_wERA, model_names_all) ctl.custom_legend(fig, colors_wERA2[0::2], model_coups + ['OBS']) ctl.custom_legend(fig_HR, colors_wERA2[0::2], model_coups + ['OBS'])
ax.set_xticklabels(pattnames, size='small') ax.set_ylabel('Frequency') ax.set_ylim(0, 35) fig.savefig( cart_out + 'Regime_frequency_{}_{}yr_{}.pdf'.format(mod, n_choice, n_bootstrap)) all_figures.append(fig) fig = plt.figure() axes = [] for j in range(kwar['numclus']): ax = fig.add_subplot(2, 2, j + 1) all_freqs = np.array([coso['freq_clus'][j] for coso in all_res[mod]]) cof = ctl.calc_pdf(all_freqs) cofvals2 = np.array([cof(i) for i in np.linspace(10, 40, 1000)]) ax.plot(np.linspace(10, 40, 1000), n_bootstrap * cofvals2) binzz = np.arange(10, 40, 1) ax.hist(all_freqs, bins=binzz) ax.set_xlim(10, 40) ax.set_ylim(0, 250) ax.set_xlabel('Frequency') ax.set_title(pattnames[j]) axes.append(ax) ctl.adjust_ax_scale(axes) fig.tight_layout()
vsel = np.linspace(vmin, vmax, 100) def dopdf(var, xi, bnd_width): pdf = ctl.calc_pdf(var, bnd_width = bnd_width) pdfok = pdf(xi) pdfok /= np.sum(pdfok) return pdfok for ru, col in zip(aruok, colok): jli = resdict[(ru, 'jli', area, season)] jspeed = resdict[(ru, 'jspeed', area, season)] jliserie = ctl.bootstrap(jli, resdict[(ru, 'dates', season)], None, apply_func = dopdf, func_args = [latsel, 0.22], n_choice = 50, n_bootstrap = 1000) jspedserie = ctl.bootstrap(jspeed, resdict[(ru, 'dates', season)], None, apply_func = dopdf, func_args = [vsel, None], n_choice = 50, n_bootstrap = 1000) pdf = ctl.calc_pdf(jli, bnd_width = 0.22) pdfok = pdf(latsel) pdfok /= np.sum(pdfok) jlimin = np.percentile(jliserie, 10, axis = 0) jlimax = np.percentile(jliserie, 90, axis = 0) ax1.fill_between(latsel, jlimin, jlimax, color = col, alpha = 0.3) ax1.plot(latsel, pdfok, label = ru, color = col, linewidth = 3) if axlu is not None: axlu.fill_between(latsel, jlimin, jlimax, color = col, alpha = 0.3) axlu.plot(latsel, pdfok, label = ru, color = col, linewidth = 3) pdf = ctl.calc_pdf(jspeed) pdfok = pdf(vsel) pdfok /= np.sum(pdfok)
wind_lo, dates = dothing(wind, coords) winds.append(wind_lo) alldates.append(dates) cart_in = '/nas/reference/ERAInterim/daily/u/' file_in = 'u_Aday_ERAInterim_{}01_{}12.nc' for yea in range(1979, 2015): wind, coords, aux_info = ctl.readxDncfield(cart_in + file_in.format(yea, yea), extract_level=850) wind_lo, dates = dothing(wind, coords) winds.append(wind_lo) alldates.append(dates) winds = np.concatenate(winds, axis=0) dates = np.concatenate(alldates, axis=0) for co in range(winds.shape[0]): winds[co][orogarea] = np.nan wind_zon = np.nanmean(winds, axis=2) jli_speed = np.max(wind_zon, axis=1) jli = latsel[np.argmax(wind_zon, axis=1)] pickle.dump([jli, jli_speed], open(cart_out + 'jli_ERA.p', 'wb')) plt.ion() pdf = ctl.calc_pdf(jli) plt.plot(latsel, pdf(latsel))
yic, zic = np.meshgrid(yss, zss) cordss = [xss, yss, zss] namz = ['x', 'y', 'z'] coups = [(0, 1), (0, 2), (1, 2)] for cou in coups: regime_pdf_2D = [] regime_pdf_2D_func = [] xi, yi = np.meshgrid(cordss[cou[0]], cordss[cou[1]]) for clus in range(4): print(clus) okclus = resu1['labels'] == clus okpc = resu1['pcs'][okclus, :] kufu = ctl.calc_pdf(okpc[:, cou].T) print('fatto kufu\n') zi = kufu(np.vstack([xi.flatten(), yi.flatten()])) regime_pdf_2D.append(zi) regime_pdf_2D_func.append(kufu) fig = plt.figure(figsize=(8, 6), dpi=150) for li in trans_pcs[0, 1]: li3 = li[:, cou].T plt.plot(li3[0], li3[1], color='grey') for clus, namcm in enumerate(['Purples', 'Blues', 'Greens', 'Oranges']): plt.contour(xi, yi, regime_pdf_2D[clus].reshape(xi.shape), cmap=cm.get_cmap(namcm))
else: gigi = areadist[(varna, pio, seas, anam, cos, ru)][-gipio:]-np.mean(areadist[(varna, pio, seas, anam, cos, ru)][-gipio:]) elif typet == 'abs': if ru == 'pi': gigi = areadist[(varna, pio, seas, anam, cos, ru)] else: gigi = areadist[(varna, pio, seas, anam, cos, ru)][-gipio:] rugi.append(gigi) rucaz = np.concatenate(rugi) rumin, rumax = (np.min(rucaz), np.max(rucaz)) ruvec = np.linspace(rumin-0.2*abs(rumin), rumax+0.2*abs(rumax), 100) for ru, gi, col in zip(allru, rugi, colors): pdf = ctl.calc_pdf(gi) pdfvec = pdf(ruvec) pdfvec = pdfvec/np.sum(pdfvec) ax.plot(ruvec, pdfvec, color = col) ax.grid() ax.set_title('{} - {}'.format(anam, cos)) fig.suptitle('{} - {}'.format(varna, seas)) ctl.custom_legend(fig, colors, allru, ncol = 4, add_space_below = 0.06) fig.savefig(cart_out + '{}_{}_{}dist_{}.pdf'.format(varna, seas, pio, typet)) #### Now with smaller regions for varna in allvars_2D: for seas in seasons:
frgri = np.arange(0, 80) for nye in [20, 50]: fig = plt.figure(figsize=(16, 12)) axes = [] calcpdfs = dict() for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) distrib = dict() for sim, col in zip(allsims, colsim): distrib[sim] = np.concatenate( [seasfreq[(sim, ke, reg)][-nye:] for ke in okmods]) pdfhi = ctl.calc_pdf(distrib[sim]) calcpd = pdfhi(frgri) calcpd = calcpd / np.sum(calcpd) calcpdfs[(sim, reg)] = calcpd ax.plot(frgri, calcpd, color=col, linewidth=2) axes.append(ax) ctl.adjust_ax_scale(axes) ctl.custom_legend(fig, colsim, allsims, ncol=3) fig.savefig(cart_out + 'Freq_pdf_{}_{}y.pdf'.format(area, nye)) fig = plt.figure(figsize=(16, 12)) axes = [] for reg in range(4): ax = fig.add_subplot(2, 2, reg + 1) for sim, col in zip(allsims[1:], colsim[1:]):
file_hist = cart_in + 'out_NEW_cmip6_hist_NDJFM_{}_4clus_4pcs_1964-2014_refCLUS_dtr_light.p' gen_file_ssp = cart_in + 'out_NEW_cmip6_{}_NDJFM_{}_4clus_4pcs_2015-2100_refCLUS_dtr_histrebase.p' xss3 = np.linspace(-3000., 3000., 301) xi_grid3, yi_grid3 = np.meshgrid(xss3, xss3) ref_pdf = dict() for area in ['EAT', 'PNA']: results_hist, results_ref = ctl.load_wrtool(file_hist_refEOF.format(area)) dat1 = pd.Timestamp('09-01-1995').to_pydatetime() dat2 = pd.Timestamp('04-01-2014').to_pydatetime() okpc = results_ref['pcs'][:, :2] okpcok, dats = ctl.sel_time_range(okpc, results_ref['dates'], (dat1, dat2)) kufu = ctl.calc_pdf(okpcok.T) zi = kufu(np.vstack([xi_grid3.flatten(), yi_grid3.flatten()])) zi = zi / np.max(zi) ref_pdf[(area, 'last20')] = zi kufu = ctl.calc_pdf(okpc.T) zi = kufu(np.vstack([xi_grid3.flatten(), yi_grid3.flatten()])) zi = zi / np.max(zi) ref_pdf[(area, 'tot50')] = zi pickle.dump(ref_pdf, open(cart_out_orig + 'refpdf_ERA.p', 'wb')) area = 'EAT'