コード例 #1
0
def ann_cycle_obs_5var_aridity_update():
    "seasonal cycle climatology"
    keys = ['pan', 'rn', 'vpd', 'ea', 'tavg', 'wind']  # 'rh',
    keynames = ['Epan', r'$R_n$', 'VPD', r'$e_a$', r'$T_a$', r'$u_2$']  # 'RH',
    cols = [
        'black', 'Orange', 'Purple', 'springgreen', 'Crimson', 'DeepSkyblue'
    ]  # 'olive',
    numbers = ['d', 'b', 'c', 'e', 'f']
    arid_class = [
        'average', r'humid (0<$\phi$<2)', r'sub-humid (2<$\phi$<4)',
        r'semi-arid (4<$\phi$<8)', r'arid ($\phi$>8)'
    ]

    def annual_all_records(df):
        "This is a new function to calculate mean of each hours across all sites, not independently"
        ann = [np.nanmean(g) for _, g, in df.groupby(df.index.month)]
        return array(ann)

    def zscore(ts):
        return (ts - np.nanmean(ts)) / np.nanstd(ts)

    ist = 0
    met_station = []
    arid = []
    pan_obs_gapfill = load('%s/pan_obs_gapfill_good_stations' % workspace)
    rn = load('%s/Rn_0.2_0.5_good_stations' % (workspace))

    for ibasin in xrange(0, 10):
        data = scipy.io.loadmat('%s/%s_AP.mat' % (datadir, ibasin + 1))
        arid.append(
            load('%s/aridity_station_%s' % (workspace, basinlongs[ibasin])))

        for istation in good_stations[ibasin]:
            # the PowerSpectrum method take the matrix as different segment, so shoule be a 1d array
            dataplot = [
                Gapfill(data[v][0, istation][0:tstep].flatten()).flatten()
                for v in keys[2:6]
            ]
            dataplot.insert(0, pan_obs_gapfill[ist, :].flatten())
            dataplot.insert(1, rn[ist, :].flatten())
            # met_station.append(array([zscore(annual_all_records(pd.Series(dataplot[i], index=dates))) for i in xrange(6)]))
            met_station.append(
                array([
                    annual_all_records((pd.Series(zscore(dataplot[i]),
                                                  index=dates)))
                    for i in xrange(6)
                ]))
            ist = ist + 1

    met_station = array(met_station)
    arid = array(list(itertools.chain(*arid)))
    index_DI = msc_groupby_DI(arid)
    met_avg = [mean(met_station[index, :, :], axis=0) for index in index_DI]
    met_avg.insert(0, mean(met_station, axis=0))

    fig = plt.figure(figsize=(12, 7.5))

    lons = load('%s/lons_good_stations' % workspace)
    lats = load('%s/lats_good_stations' % workspace)

    colors = r_[linspace(0, 1, 4), linspace(0, 1, 4)]
    mymap = plt.get_cmap("coolwarm")
    my_colors = mymap(colors)
    arid_labels = ['humid', 'sub-humid', 'semi-arid', 'arid']
    ax = fig.add_subplot(2, 3, 1)
    [
        Plotting.Mapshow_Aridity_subplot(ax, lons[index_DI[i]],
                                         lats[index_DI[i]], 30, 0.8, 0, 1000,
                                         my_colors[i], arid_labels[i])
        for i in xrange(0, 4)
    ]

    plot_loc = (4, 2, 3, 5, 6)
    for i, mets in enumerate(met_avg):
        ax = fig.add_subplot(2, 3, plot_loc[i])
        lines = [
            ax.plot(met,
                    '--',
                    dashes=(12, 5),
                    color=cols[im],
                    label=keynames[im],
                    linewidth=3,
                    alpha=0.9,
                    rasterized=True) for im, met in enumerate(mets)
        ]  #  '-o', ms=10, mew=0,

        ax.set_xlim(0, 11)
        ax.set_xticks(range(12))
        ax.set_xticklabels(range(1, 13))
        ax.set_ylim(-2, 2)
        ymin, ymax = ax.get_ylim()
        ax.text(10.8,
                ymin + 0.03 * (ymax - ymin),
                arid_class[i],
                ha='right',
                fontsize=16)
        ax.text(0.8,
                ymax - 0.13 * (ymax - ymin),
                '(%s)' % numbers[i],
                fontsize=16)
        ax.tick_params(axis='y', labelsize=14)
        ax.tick_params(axis='x', labelsize=14)
        ax.grid('on')

    lines = list(itertools.chain(*lines))
    plt.figlegend(
        lines, keynames, loc='lower center', ncol=6, fontsize=16
    )  # doesn't work bbox_to_anchor=(0, -0.1, 1, 1),, bbox_transform=plt.gcf().transFigure
    # plt.subplots_adjust(bottom=0.2)
    plt.tight_layout(rect=[0, 0.07, 1, 1])
    # savefig('%s/fig1_ann_cycle_5var_aridity_good_station.tif' %figdir, dpi=300)
    plt.show()

    return