Esempio n. 1
0
def doy_plot(dt_list, v, ylabel=None, title=None):
    samp_list = split_sample_doy(dt_list, v)
    #fig = plt.figure(figsize=(5,7.5))
    fig = plt.figure()
    doy_ax = fig.add_subplot(111)
    doy_ax.set_title(title)
    if ylabel is not None:
        doy_ax.set_ylabel(ylabel)
    for i in samp_list:
        c = cmap(i[3])
        doy_ax.plot(i[1],
                    i[2],
                    color=c,
                    label=i[0],
                    marker='o',
                    markersize=5,
                    linestyle='-',
                    linewidth=0.6)
    doy_ax.set_xlim(0, 365)
    month_ndays = np.array([0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
    month_ticks = np.cumsum(month_ndays)
    doy_ax.xaxis.set_ticks(month_ticks)
    month_ticklabels = doy_ax.xaxis.get_ticklabels()
    #for label in month_ticklabels[::2]:
    for label in month_ticklabels:
        label.set_visible(False)
    doy_ax.set_xlabel('Day of year')
    #month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    month_names = ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D']
    doy_ax.xaxis.set_ticks(month_ticks[:-1] + 15, minor=True)
    doy_ax.xaxis.set_ticklabels(month_names, minor=True)
    for tick in doy_ax.xaxis.get_minor_ticks():
        tick.tick1line.set_markersize(0)
        tick.tick2line.set_markersize(0)
        tick.label1.set_horizontalalignment('center')
    doy_ax.set_xlabel('Month')
    plt.legend(loc='upper right', prop={'size': 8})
    #doy_ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())
    doy_ax.xaxis.grid(True, 'major')
    pltlib.minorticks_on(doy_ax, x=False, y=True)
    if title is not None:
        mx, my = title.split(', ')
        fig_fn = '%s_doyplot_%s_%s_fig.png' % (os.path.splitext(
            s.stack_fn)[0], mx, my)
    else:
        fig_fn = '%s_doyplot_fig.png' % os.path.splitext(s.stack_fn)[0]
    #doy_ax.set_ylim(100,260)
    plt.tight_layout()
    #plt.savefig(fig_fn, dpi=300)
    plt.draw()
Esempio n. 2
0
def hist_plot(gf, outdir, bin_width=10.0):
    #print("Generating histograms")
    #Create bins for full range of input data and specified bin width

    #NOTE: these counts/areas are for valid pixels only
    #Not necessarily a true representation of actual glacier hypsometry
    #Need a void-filled DEM for this

    z_bin_edges, z_bin_centers = malib.get_bins(gf.z1, bin_width)
    z1_bin_counts, z1_bin_edges = np.histogram(gf.z1, bins=z_bin_edges)
    z1_bin_areas = z1_bin_counts * gf.res[0] * gf.res[1] / 1E6
    #RGI standard is integer thousandths of glaciers total area
    #Should check to make sure sum of bin areas equals total area
    z1_bin_areas_perc = 100. * z1_bin_areas / np.sum(z1_bin_areas)

    z2_bin_counts, z2_bin_edges = np.histogram(gf.z2, bins=z_bin_edges)
    z2_bin_areas = z2_bin_counts * gf.res[0] * gf.res[1] / 1E6
    z2_bin_areas_perc = 100. * z2_bin_areas / np.sum(z2_bin_areas)

    #Create arrays to store output
    mb_bin_med = np.ma.masked_all_like(z1_bin_areas)
    mb_bin_mad = np.ma.masked_all_like(z1_bin_areas)
    mb_bin_mean = np.ma.masked_all_like(z1_bin_areas)
    mb_bin_std = np.ma.masked_all_like(z1_bin_areas)
    dz_bin_med = np.ma.masked_all_like(z1_bin_areas)
    dz_bin_mad = np.ma.masked_all_like(z1_bin_areas)
    dz_bin_mean = np.ma.masked_all_like(z1_bin_areas)
    dz_bin_std = np.ma.masked_all_like(z1_bin_areas)
    if gf.debris_class is not None:
        perc_clean = np.ma.masked_all_like(z1_bin_areas)
        perc_debris = np.ma.masked_all_like(z1_bin_areas)
        perc_pond = np.ma.masked_all_like(z1_bin_areas)
        debris_thick_med = np.ma.masked_all_like(z1_bin_areas)
        debris_thick_mad = np.ma.masked_all_like(z1_bin_areas)

    #Loop through each bin and extract stats
    idx = np.digitize(gf.z1, z_bin_edges)
    for bin_n in range(z_bin_centers.size):
        mb_bin_samp = gf.mb[(idx == bin_n + 1)]
        if mb_bin_samp.count() > 0:
            mb_bin_med[bin_n] = malib.fast_median(mb_bin_samp)
            mb_bin_mad[bin_n] = malib.mad(mb_bin_samp)
            mb_bin_mean[bin_n] = mb_bin_samp.mean()
            mb_bin_std[bin_n] = mb_bin_samp.std()
        dz_bin_samp = gf.dhdt[(idx == bin_n + 1)]
        if dz_bin_samp.count() > 0:
            dz_bin_med[bin_n] = malib.fast_median(dz_bin_samp)
            dz_bin_mad[bin_n] = malib.mad(dz_bin_samp)
            dz_bin_mean[bin_n] = dz_bin_samp.mean()
            dz_bin_std[bin_n] = dz_bin_samp.std()
        if gf.debris_class is not None:
            debris_class_bin_samp = gf.debris_class[(idx == bin_n + 1)]
            if debris_class_bin_samp.count() > 0:
                perc_clean[bin_n] = 100. * (
                    debris_class_bin_samp
                    == 1).sum() / debris_class_bin_samp.count()
                perc_debris[bin_n] = 100. * (
                    debris_class_bin_samp
                    == 2).sum() / debris_class_bin_samp.count()
                perc_pond[bin_n] = 100. * (
                    debris_class_bin_samp
                    == 3).sum() / debris_class_bin_samp.count()

            debris_thick_bin_samp = gf.debris_thick[(idx == bin_n + 1)]
            debris_thick_med[bin_n] = malib.fast_median(debris_thick_bin_samp)
            debris_thick_mad[bin_n] = malib.mad(debris_thick_bin_samp)

    outbins_header = 'bin_center_elev_m, z1_bin_count_valid, z1_bin_area_valid_km2, z1_bin_area_perc, z2_bin_count_valid, z2_bin_area_valid_km2, z2_bin_area_perc, dhdt_bin_med_ma, dhdt_bin_mad_ma, dhdt_bin_mean_ma, dhdt_bin_std_ma, mb_bin_med_mwea, mb_bin_mad_mwea, mb_bin_mean_mwea, mb_bin_std_mwea'
    fmt = '%0.1f, %i, %0.3f, %0.2f, %i, %0.3f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f'
    outbins = [
        z_bin_centers, z1_bin_counts, z1_bin_areas, z1_bin_areas_perc,
        z2_bin_counts, z2_bin_areas, z2_bin_areas_perc, dz_bin_med, dz_bin_mad,
        dz_bin_mean, dz_bin_std, mb_bin_med, mb_bin_mad, mb_bin_mean,
        mb_bin_std
    ]

    if gf.debris_class is not None:
        outbins_header += ', debris_thick_med_m, debris_thick_mad_m, perc_debris, perc_pond, perc_clean'
        fmt += ', %0.2f, %0.2f, %0.2f, %0.2f, %0.2f'
        debris_thick_med[debris_thick_med == -(np.inf)] = 0.00
        debris_thick_mad[debris_thick_mad == -(np.inf)] = 0.00
        outbins.extend([
            debris_thick_med, debris_thick_mad, perc_debris, perc_pond,
            perc_clean
        ])

    #print(len(outbins), len(fmt.split(',')), len(outbins_header.split(',')))
    outbins = np.ma.array(outbins).T.astype('float32')
    np.ma.set_fill_value(outbins, -9999.0)
    outbins_fn = os.path.join(outdir, gf.feat_fn + '_mb_bins.csv')
    #print(outbins.shape)
    np.savetxt(outbins_fn,
               outbins,
               fmt=fmt,
               delimiter=',',
               header=outbins_header)

    #print("Generating aed plot")
    #f,axa = plt.subplots(1,2, figsize=(6, 6))
    f, axa = plt.subplots(1, 3, figsize=(10, 7.5))
    f.suptitle(gf.feat_fn)
    axa[0].plot(z1_bin_areas, z_bin_centers, label='%0.2f' % gf.t1)
    axa[0].plot(z2_bin_areas, z_bin_centers, label='%0.2f' % gf.t2)
    axa[0].axhline(gf.z1_ela, ls=':', c='C0')
    axa[0].axhline(gf.z2_ela, ls=':', c='C1')
    axa[0].legend(prop={'size': 8}, loc='upper right')
    axa[0].set_ylabel('Elevation (m WGS84)')
    axa[0].set_xlabel('Area $\mathregular{km^2}$')
    pltlib.minorticks_on(axa[0])
    axa[1].axvline(0, lw=1.0, c='k')
    axa[1].axvline(gf.mb_mean,
                   lw=0.5,
                   ls=':',
                   c='k',
                   label='%0.2f m w.e./yr' % gf.mb_mean)
    axa[1].legend(prop={'size': 8}, loc='upper right')
    axa[1].plot(mb_bin_med, z_bin_centers, color='k')
    axa[1].fill_betweenx(z_bin_centers,
                         mb_bin_med - mb_bin_mad,
                         mb_bin_med + mb_bin_mad,
                         color='k',
                         alpha=0.1)
    axa[1].fill_betweenx(z_bin_centers,
                         0,
                         mb_bin_med,
                         where=(mb_bin_med < 0),
                         color='r',
                         alpha=0.2)
    axa[1].fill_betweenx(z_bin_centers,
                         0,
                         mb_bin_med,
                         where=(mb_bin_med > 0),
                         color='b',
                         alpha=0.2)
    #axa[1].set_ylabel('Elevation (m WGS84)')
    #axa[1].set_xlabel('dh/dt (m/yr)')
    axa[1].set_xlabel('mb (m w.e./yr)')
    pltlib.minorticks_on(axa[1])
    #Hide y-axis labels
    axa[1].axes.yaxis.set_ticklabels([])
    #axa[1].set_xlim(-2.0, 2.0)
    #axa[1].set_xlim(-8.0, 8.0)
    axa[1].set_xlim(-3.0, 3.0)
    if gf.debris_class is not None:
        axa[2].errorbar(debris_thick_med * 100.,
                        z_bin_centers,
                        xerr=debris_thick_mad * 100,
                        color='k',
                        fmt='o',
                        ms=3,
                        label='Thickness',
                        alpha=0.6)
        axa[2].plot(perc_debris,
                    z_bin_centers,
                    color='sienna',
                    label='Debris Coverage')
        axa[2].plot(perc_pond,
                    z_bin_centers,
                    color='turquoise',
                    label='Pond Coverage')
        axa[2].set_xlim(0, 100)
        pltlib.minorticks_on(axa[2])
        axa[2].legend(prop={'size': 8}, loc='upper right')
        axa[2].set_xlabel('Debris thickness (cm), coverage (%)')
        axa[2].yaxis.tick_right()
        axa[2].yaxis.set_label_position("right")
    plt.tight_layout()
    #Make room for suptitle
    plt.subplots_adjust(top=0.95)
    #print("Saving aed plot")
    fig_fn = os.path.join(outdir, gf.feat_fn + '_mb_aed.png')
    plt.savefig(fig_fn, bbox_inches='tight', dpi=300)
    plt.close(f)
    return z_bin_edges