Beispiel #1
0
def compute(ifile):
    ## PRECOMPUTE
    print(ifile)
    print('Running precompute\n')
    var, level, lat, lon, dates, time_units, var_units, time_cal = ctl.read4Dncfield(
        ifile, extract_level=50000.)

    var_season, dates_season = ctl.sel_season(var, dates, season)

    climate_mean, dates_climat, climat_std = ctl.daily_climatology(
        var_season, dates_season, wnd)

    var_anom = ctl.anomalies_daily(var_season,
                                   dates_season,
                                   climate_mean=climate_mean,
                                   dates_climate_mean=dates_climat)

    var_area, lat_area, lon_area = ctl.sel_area(lat, lon, var_anom, area)
    print(var_area.shape)

    print('Running compute\n')
    #### EOF COMPUTATION
    eof_solver = ctl.eof_computation(var_area, lat_area)
    PCs = eof_solver.pcs()[:, :numpcs]

    print('Running clustering\n')
    #### CLUSTERING
    centroids, labels = ctl.Kmeans_clustering(PCs,
                                              numclus,
                                              algorithm='molteni')

    cluspattern = ctl.compute_clusterpatterns(var_anom, labels)
    cluspatt_area = []
    for clu in cluspattern:
        cluarea, _, _ = ctl.sel_area(lat, lon, clu, area)
        cluspatt_area.append(cluarea)
    cluspatt_area = np.stack(cluspatt_area)

    varopt = ctl.calc_varopt_molt(PCs, centroids, labels)
    print('varopt: {:8.4f}\n'.format(varopt))
    freq_clus = ctl.calc_clus_freq(labels)

    print('Running clus sig\n')
    significance = ctl.clusters_sig(PCs,
                                    centroids,
                                    labels,
                                    dates_season,
                                    nrsamp=5000)
    # significance_2 = ctl.clusters_sig(PCs, centroids, labels, dates_season, nrsamp = 5000)
    # print('Significances: {:7.3f} vs {:7.3f}\n'.format(significance, significance_2))

    return lat, lon, var_anom, eof_solver, centroids, labels, cluspattern, cluspatt_area, freq_clus, significance
            for ke in ['lat_area', 'lon_area', 'model_eofs']:
                koze[ke] = koze_ref[ke]

            # salvo le serie temporali filtrate e non (_all)
            for ke in ['labels', 'dist_centroid', 'pcs', 'dates']:
                koze[ke] = koze_ref[ke][mask == 1]

            # ricostruisco il pattern degli stati selezionati
            patts = ctl.reconstruct_from_pcs(koze['pcs'], koze['model_eofs'])
            cluspatt_ok = []
            for reg in range(4):
                okpo = koze['labels'] == reg
                cluspatt_ok.append(np.mean(patts[okpo, ...], axis=0))

            koze['cluspattern_area'] = np.stack(cluspatt_ok)
            koze['freq_clus'] = ctl.calc_clus_freq(koze['labels'], 4)
            koze['centroids'] = ctl.calc_effective_centroids(
                koze['pcs'], koze['labels'], 4)

            cd.plot_regimes(
                koze['lat_area'],
                koze['lon_area'],
                koze['cluspattern_area'],
                cart + 'regpatt_{}_{}_{}_v90.pdf'.format(season, area, tip),
                clatlo=clatlo,
                cbar_range=cbar_range,
                draw_contour_lines=False,
                cmappa='RdBu_r',
                n_color_levels=21)
            print(koze['lon_area'])
Beispiel #3
0
    fig.savefig(cart_out + 'WR_freq_change_dtr_{}_{}_{}_{}.pdf'.format(*ke))

    # Same but only for long-lasting (> 5 days) regimes
    for mod in modoks:
        results_ssp585[mod]['av_res'] = np.array([
            np.mean(results_ssp585[mod]['resid_times'][reg])
            for reg in range(4)
        ])
        results_hist[mod]['av_res'] = np.array([
            np.mean(results_hist[mod]['resid_times'][reg]) for reg in range(4)
        ])

        labs = ctl.regime_filter_long(results_ssp585[mod]['labels'],
                                      results_ssp585[mod]['dates'],
                                      days_thres=5)
        results_ssp585[mod]['freq_clus_long5'] = ctl.calc_clus_freq(
            labs[labs >= 0], numclus)
        labs = ctl.regime_filter_long(results_hist[mod]['labels'],
                                      results_hist[mod]['dates'],
                                      days_thres=5)
        results_hist[mod]['freq_clus_long5'] = ctl.calc_clus_freq(
            labs[labs >= 0], numclus)

    fig = plt.figure(figsize=(16, 12))
    axes = []
    for reg in range(4):
        ax = fig.add_subplot(2, 2, reg + 1)
        axes.append(ax)
        allfreqs[(ssp, area, season, 'refCLUS_long5', reg)] = []

        i = 0
        for mod, col in zip(modoks, coloks):
Beispiel #4
0
    mod_hist = [mod for mod in mod_hist if np.any([mod in mod_ssp[ssp] for ssp in allssps])]
    print(mod_hist)

    ### Voglio: freqs, resid_time, resid_time_90, eff_centroids_ssp, centroids_hist (e patcor, rms)
    freqs = dict() # tot50 e last20
    residtimes = dict() # mean e p90
    eff_centroids = dict()
    num_event = dict()

    patterns_refEOF = dict()

    for mod in mod_hist:
        allmems = [cos for cos in results_hist.keys() if cos.split('_')[0] == mod]
        ## Attach all members labels
        alllabs = np.concatenate([results_hist[mem]['labels'] for mem in allmems])
        freqs[('hist', mod, 'tot50')] = ctl.calc_clus_freq(alllabs, numclus)

        for reg in range(numclus):
            alltimes = np.concatenate([results_hist[mem]['resid_times'][reg] for mem in allmems])
            residtimes[('hist', mod, 'mean', reg)] = np.mean(alltimes)
            residtimes[('hist', mod, 'p90', reg)] = np.percentile(alltimes, 90)
            num_event[('hist', mod, reg)] = freqs[(ssp, mod, 'tot50')][reg]/residtimes[(ssp, mod, 'mean', reg)]

        alllabs_20 = []
        alltimes_20 = []
        for mem in allmems:
            dat1 = pd.Timestamp('09-01-1995').to_pydatetime()
            dat2 = pd.Timestamp('04-01-2014').to_pydatetime()
            labs, dats = ctl.sel_time_range(results_hist[mem]['labels'], results_hist[mem]['dates'], (dat1, dat2))
            alllabs_20.append(labs)
            print(mem, len(labs))
Beispiel #5
0
                okla = labels == iclu
                centroids.append(np.mean(pcs[okla], axis=0))
            centroids = np.stack(centroids)

            sig = ctl.clusters_sig(pcs, centroids, labels, dates, nrsamp=200)
            # if sig < 10:
            #     sig2 = ctl.clusters_sig(pcs, centroids, labels, dates, nrsamp = 1000)
            #     print('RECHECK ', sig, sig2, sig3)

            varopt = ctl.calc_varopt_molt(pcs, centroids, labels)
            autocorr = ctl.calc_autocorr_wlag(pcs, dates, out_lag1=True)
            bootstraps['significance'].append(sig)
            bootstraps['varopt'].append(varopt)
            bootstraps['autocorr'].append(autocorr)

            bootstraps['freq'].append(ctl.calc_clus_freq(labels, 4))

            centdist = np.array([
                ctl.distance(centroids[iclu], ref_cen[iclu])
                for iclu in range(4)
            ])
            bootstraps['dist_cen'].append(centdist)
            bootstraps['centroids'].append(centroids)

            resid_times = ctl.calc_regime_residtimes(labels, dates=dates)[0]
            av_res = np.array([np.mean(resid_times[reg]) for reg in range(4)])
            av_res_90 = np.array(
                [np.percentile(resid_times[reg], 90) for reg in range(4)])
            bootstraps['resid_times_av'].append(av_res)
            bootstraps['resid_times_90'].append(av_res_90)
Beispiel #6
0
            trend_ssp[(tip, reg)] = np.array(
                [trend_ssp[(mem, tip, reg)] for mem in okmods])
            trend_ssp[(tip, reg, 'err')] = np.array(
                [trend_ssp[(mem, tip, reg, 'err')] for mem in okmods])

    freqs = dict()
    for tip in alltips:
        okmods = resdict[tip].keys()
        for mem in okmods:
            dat1 = pd.Timestamp('09-01-1964').to_pydatetime()
            dat2 = pd.Timestamp('04-01-2014').to_pydatetime()
            labs, dats = ctl.sel_time_range(resdict[tip][mem]['labels'],
                                            resdict[tip][mem]['dates'],
                                            (dat1, dat2))
            freqs[('hist', mem, tip)] = ctl.calc_clus_freq(labs, numclus)

            dat1 = pd.Timestamp('09-01-2050').to_pydatetime()
            dat2 = pd.Timestamp('04-01-2100').to_pydatetime()
            labs, dats = ctl.sel_time_range(resdict[tip][mem]['labels'],
                                            resdict[tip][mem]['dates'],
                                            (dat1, dat2))

            # restim, _, _ = ctl.calc_regime_residtimes(labs, dats)
            freqs[(ssp, mem, tip)] = ctl.calc_clus_freq(labs, numclus)

        freqs[('hist',
               tip)] = np.stack([freqs[('hist', mem, tip)] for mem in okmods])
        freqs[(ssp,
               tip)] = np.stack([freqs[(ssp, mem, tip)] for mem in okmods])
Beispiel #7
0
        ax.set_title(reg_names_area[area][reg])

    fig.savefig(cart_out + 'long_run20_{}_rcp85.pdf'.format(area))

    print('BAUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU')

    numclus = 4
    print('freqs!')
    for mem in okmods:
        print(mem)
        dat1 = pd.Timestamp('09-01-1964').to_pydatetime()
        dat2 = pd.Timestamp('04-01-2005').to_pydatetime()
        labs, dats = ctl.sel_time_range(results_hist[mem]['labels'],
                                        results_hist[mem]['dates'],
                                        (dat1, dat2))
        freqs[('hist_cmip5', mem, 'tot50')] = ctl.calc_clus_freq(labs, numclus)

        restim, _, _ = ctl.calc_regime_residtimes(labs, dats)
        for reg in range(numclus):
            residtimes[('hist_cmip5', mem, 'mean', reg)] = np.mean(restim[reg])
            residtimes[('hist_cmip5', mem, 'p90',
                        reg)] = np.percentile(restim[reg], 90)
            num_event[(
                'hist_cmip5', mem,
                reg)] = freqs[('hist_cmip5', mem, 'tot50')][reg] / residtimes[
                    ('hist_cmip5', mem, 'mean', reg)]

    freqs[('hist_cmip5', 'all', 'tot50')] = np.array(
        [freqs[('hist_cmip5', ke, 'tot50')] for ke in okmods])

    print('freqs2!')
Beispiel #8
0
            ax.set_title(reg_names_area[area][reg])
            ax.axvline(2015, color = 'lightslategray', linewidth = 0.2)

        fig.savefig(cart_out + 'long_freq20_{}_{}_e_hist.pdf'.format(area, ssp))

    plt.close('all')

    pickle.dump([seasfreq, runfreq], open(cart_out + 'seasfreqs_{}_v4.p'.format(area), 'wb'))

    #################### la parte di v2
    freqs = dict() # tot50 e last20
    residtimes = dict() # mean e p90
    patterns = dict()

    for mem in okmods_hist:
        freqs[('hist', mem, 'tot50')] = ctl.calc_clus_freq(results_hist[mem]['labels'], numclus)

        for reg in range(numclus):
            alltimes = results_hist[mem]['resid_times'][reg]
            residtimes[('hist', mem, 'mean', reg)] = np.mean(alltimes)
            residtimes[('hist', mem, 'p90', reg)] = np.percentile(alltimes, 90)

        patterns[('hist', mem, 'tot50')] = results_hist[mem]['eff_centroids']

        dat1 = pd.Timestamp('09-01-1995').to_pydatetime()
        dat2 = pd.Timestamp('04-01-2014').to_pydatetime()
        alllabs_20, dats = ctl.sel_time_range(results_hist[mem]['labels'], results_hist[mem]['dates'], (dat1, dat2))
        pcs, dats = ctl.sel_time_range(results_hist[mem]['pcs'], results_hist[mem]['dates'], (dat1, dat2))

        alltimes_20, _, _ = ctl.calc_regime_residtimes(alllabs_20, dats)
        effcen = ctl.calc_effective_centroids(pcs, alllabs_20, numclus)
Beispiel #9
0
dists = results[('lcb0', 'EAT', (1850,2005))]['dist_centroid']
thres['EAT'] = np.percentile(dists, 80)
dists = results[('lcb0', 'PNA', (1850,2005))]['dist_centroid']
thres['PNA'] = np.percentile(dists, 80)

dates_long = np.concatenate([dates,dates,dates])
freq = dict()
resid_times = dict()
for area in ['EAT', 'PNA']:
    for ran in [(1850,2005), (2006,2100)]:
        base_labels = np.concatenate([results[('lcb{}'.format(i), 'EAT', ran)]['labels'] for i in range(3)])
        base_dists =  np.concatenate([results[('lcb{}'.format(i), 'EAT', ran)]['dist_centroid'] for i in range(3)])
        stoc_labels = np.concatenate([results[('lcs{}'.format(i), 'EAT', ran)]['labels'] for i in range(3)])
        stoc_dists =  np.concatenate([results[('lcs{}'.format(i), 'EAT', ran)]['dist_centroid'] for i in range(3)])

        freq[('base', area, ran)] = ctl.calc_clus_freq(base_labels)
        freq[('stoc', area, ran)] = ctl.calc_clus_freq(stoc_labels)

        greylabs = base_dists > thres[area]
        gigi = base_labels
        gigi[greylabs] = np.max(base_labels)+1
        freq[('base', area, ran, 'filt80')] = ctl.calc_clus_freq(gigi)

        greylabs = stoc_dists > thres[area]
        gigi = stoc_labels
        gigi[greylabs] = np.max(stoc_labels)+1
        freq[('stoc', area, ran, 'filt80')] = ctl.calc_clus_freq(gigi)

        rs_base, dates_init_b = ctl.calc_regime_residtimes(base_labels, dates = dates_long)
        resid_times[('base', area, ran)] = rs_base
        rs_stoc, dates_init_s = ctl.calc_regime_residtimes(stoc_labels, dates = dates_long)