alltips = tuple(resdict.keys()) colorz = ctl.color_set(len(alltips)) 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.set_title(reg_names[reg]) ctl.adjust_ax_scale(axes) ctl.custom_legend(fig, coloks, modoks, ncol=4) fig.suptitle( 'Change of WR freq. in 2050-2100 wrt 1964-2014 - {}'.format(sea)) fig.savefig( cart_out + 'WR_freq_change_dtr_{}_{}_{}_{}_subsea{}.pdf'.format(*ke, sea)) #### Prima posso guardare la cosa con le seasonal frequencies. O monthly? No seasonal. ## Devo. calcolare le seasonal freq. calcolare i percentili per hist e per ssp. Fare differenze di ogni percentile. Plottare. Ok vado. for mod in modoks: seasfr, yr = ctl.calc_seasonal_clus_freq(results_hist[mod]['labels'], results_hist[mod]['dates'], numclus) results_hist[mod]['freq_clus_seasonal'] = seasfr seasfr, yr = ctl.calc_seasonal_clus_freq(results_ssp585[mod]['labels'], results_ssp585[mod]['dates'], numclus) results_ssp585[mod]['freq_clus_seasonal'] = seasfr allpercs_regs = [] for reg in range(numclus): allpercs = dict() allpercs['mean'] = [] for pp in [10, 25, 50, 75, 90]: allpercs['p{}'.format(pp)] = [] for mod in modoks:
print(okmods) print(len(okmods)) 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))
mod_ssp[ssp] = np.unique([cos.split('_')[0] for cos in results_ssp[ssp].keys()]) print(ssp, mod_ssp[ssp]) 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:
detrended_anom_for_clustering=True, heavy_output=False) resu2 = cd.WRtool_core(var_season, lat, lon, dates_season, area, run_significance_calc=False, 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)
dates_pdh = pd.to_datetime(dates) pi = (dates_pdh.month == 12) | (dates_pdh.month == 1) | (dates_pdh.month == 2) pi2 = (pi) & (dates_pdh > dates_pdh[0]+timed) & (dates_pdh < dates_pdh[-1]-timed) datespi2 = dates[pi2] labs_fullp = [] dist_fullp = [] freqs_yr_all = dict() for ens in ensmem: labs_fullp.append(results_fullp[(ens, 'EAT', (1850,2100))]['labels']) dist_fullp.append(results_fullp[(ens, 'EAT', (1850,2100))]['dist_centroid']) if ens == 'lcb1': dates_ok = datespi2[90:] else: dates_ok = datespi2 freqs_yr_all[ens] = ctl.calc_seasonal_clus_freq(labs_fullp[-1], dates_ok) cartfr = cart+'freq_fullp/' if not os.path.exists(cartfr): os.mkdir(cartfr) plt.close('all') yearsall = np.arange(1851, 2101) for ens in ensmem: fig = plt.figure() if ens == 'lcb1': years = yearsall[1:] else: years = yearsall
timeseries = dict() for ke in resu.keys(): timeseries[ke] = xr.DataArray(resu[ke]['freq_clus_seasonal'], dims=('reg', 'time'), coords={ 'reg': [0, 1, 2, 3], 'time': resu[ke]['freq_clus_seasonal_years'] }) # resu2, resu_ref2 = ctl.load_wrtool(cart_in + 'out_tipes_nnetau_proj_NDJFM_EAT_4clus_4pcs_allyrs_{}.p'.format(tip)) # timeseries['eta'] = xr.DataArray(resu2['eta']['freq_clus_seasonal'], dims = ('reg', 'time'), coords = {'reg': [0, 1, 2, 3], 'time': resu2['eta']['freq_clus_seasonal_years']}) 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,