コード例 #1
0
def makefig(dem, hs, anomaly, ds, title=None):
    f,axa = plt.subplots(2, figsize=(8,5))
    hs_clim = (1, 255)
    hs_im = axa[0].imshow(hs, vmin=hs_clim[0], vmax=hs_clim[1], cmap='gray')
    #dem_clim = (1600, 2100)
    dem_im = axa[0].imshow(dem, vmin=dem_clim[0], vmax=dem_clim[1], cmap='cpt_rainbow', alpha=0.5)
    anomaly_clim = (-15, 15)
    anomaly_im = axa[1].imshow(anomaly, vmin=anomaly_clim[0], vmax=anomaly_clim[1], cmap='RdBu')
    pltlib.add_cbar(axa[0], dem_im, label='Elevation (m WGS84)')
    pltlib.add_cbar(axa[1], anomaly_im, label='Elevation Anomaly (m)')
    res = 8
    pltlib.add_scalebar(axa[0], res=res)
    plt.tight_layout()
コード例 #2
0
ファイル: mb_plot.py プロジェクト: ywbomhf2019/gmbtools
def scplot(x, y, c, s, sf=16, ax=None, clim=None, legloc='lower right'):
    """
    Create scatter plot with scaled circles
    """
    if clim is None:
        vmin, vmax = get_equal_vmin_vmax(c)
    else:
        vmin, vmax = clim
    if ax is None:
        ax = plt.gca()
    ax.set_facecolor('0.8')
    sc = ax.scatter(x,
                    y,
                    c=c,
                    s=s * sf,
                    edgecolor='k',
                    lw='0.2',
                    cmap='RdBu',
                    vmin=vmin,
                    vmax=vmax)
    cbar = pltlib.add_cbar(ax, sc, label='Mass balance (m we/yr)')
    leg = add_legend(ax, sf=sf, loc=legloc)
    ax.minorticks_on()
    #ax.tick_params(left=True, right=True, bottom=True, top=True)
    return ax
コード例 #3
0
ファイル: dem_anomaly.py プロジェクト: ywbomhf2019/gmbtools
def makefig(dem, hs, anomaly, ds, title=None):
    f,axa = plt.subplots(1,2,figsize=(10,5))
    #dem_clim = (2300, 4200)
    dem_clim = (1600, 2100)
    hs_clim = (1, 255)
    anomaly_clim = (-15, 15)
    hs_im = axa[0].imshow(hs, vmin=hs_clim[0], vmax=hs_clim[1], cmap='gray')
    dem_im = axa[0].imshow(dem, vmin=dem_clim[0], vmax=dem_clim[1], cmap='cpt_rainbow', alpha=0.5)
    res = 8
    pltlib.add_scalebar(axa[0], res=res)
    pltlib.add_cbar(axa[0], dem_im, label='Elevation (m WGS84)')
    anomaly_im = axa[1].imshow(anomaly, vmin=anomaly_clim[0], vmax=anomaly_clim[1], cmap='RdBu')
    pltlib.add_cbar(axa[1], anomaly_im, label='Elevation Anomaly (m)')
    if shp_fn is not None:
        pltlib.shp_overlay(axa[1], ds, shp_fn, color='darkgreen')
    plt.tight_layout()
    for ax in axa:
        pltlib.hide_ticks(ax)
        ax.set_facecolor('k')
        if title is not None:
            ax.set_title(title)
    return f
コード例 #4
0
ファイル: mb_plot.py プロジェクト: whigg/gmbtools
def scplot3D(x, y, z, c, s, sf=4, ax=None, clim=None):
    """
    Create scatter plot with scaled circles
    """
    if clim is None:
        vmin, vmax = get_equal_vmin_vmax(c)
    else:
        vmin, vmax = clim
    ax.set_facecolor('0.8')
    kwargs = {'edgecolor':'k', 'lw':'0.2', 'cmap':'RdBu', 'vmin':vmin, 'vmax':vmax}
    #kwargs = {'cmap':'RdBu', 'vmin':vmin, 'vmax':vmax}
    sc = ax.scatter(x, y, z, c=c, s=s*sf, depthshade=True, **kwargs)
    cbar = pltlib.add_cbar(ax, sc, label='Mass balance (m we/yr)')
    leg = add_legend(ax, sf=sf, loc='lower left')
    ax.minorticks_on()
    #ax.tick_params(left=True, right=True, bottom=True, top=True)
    return ax
コード例 #5
0
ファイル: dem_align.py プロジェクト: eokeeffe/demcoreg
def main(argv=None):
    parser = getparser()
    args = parser.parse_args()

    #Should check that files exist
    ref_dem_fn = args.ref_fn
    src_dem_fn = args.src_fn

    mode = args.mode
    mask_list = args.mask_list
    max_offset = args.max_offset
    max_dz = args.max_dz
    slope_lim = tuple(args.slope_lim)
    tiltcorr = args.tiltcorr
    polyorder = args.polyorder
    res = args.res

    #Maximum number of iterations
    max_iter = args.max_iter

    #These are tolerances (in meters) to stop iteration
    tol = args.tol
    min_dx = tol
    min_dy = tol
    min_dz = tol

    outdir = args.outdir
    if outdir is None:
        outdir = os.path.splitext(src_dem_fn)[0] + '_dem_align'

    if tiltcorr:
        outdir += '_tiltcorr'
        tiltcorr_done = False
        #Relax tolerance for initial round of co-registration
        #tiltcorr_tol = 0.1
        #if tol < tiltcorr_tol:
        #    tol = tiltcorr_tol

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    outprefix = '%s_%s' % (os.path.splitext(os.path.split(src_dem_fn)[-1])[0], \
            os.path.splitext(os.path.split(ref_dem_fn)[-1])[0])
    outprefix = os.path.join(outdir, outprefix)

    print("\nReference: %s" % ref_dem_fn)
    print("Source: %s" % src_dem_fn)
    print("Mode: %s" % mode)
    print("Output: %s\n" % outprefix)

    src_dem_ds = gdal.Open(src_dem_fn)
    ref_dem_ds = gdal.Open(ref_dem_fn)

    #Get local cartesian coordinate system
    #local_srs = geolib.localtmerc_ds(src_dem_ds)
    #Use original source dataset coordinate system
    #Potentially issues with distortion and xyz/tiltcorr offsets for DEM with large extent
    local_srs = geolib.get_ds_srs(src_dem_ds)
    #local_srs = geolib.get_ds_srs(ref_dem_ds)

    #Resample to common grid
    ref_dem_res = float(geolib.get_res(ref_dem_ds, t_srs=local_srs, square=True)[0])
    #Create a copy to be updated in place
    src_dem_ds_align = iolib.mem_drv.CreateCopy('', src_dem_ds, 0)
    src_dem_res = float(geolib.get_res(src_dem_ds, t_srs=local_srs, square=True)[0])
    src_dem_ds = None
    #Resample to user-specified resolution
    ref_dem_ds, src_dem_ds_align = warplib.memwarp_multi([ref_dem_ds, src_dem_ds_align], \
            extent='intersection', res=args.res, t_srs=local_srs, r='cubic')

    res = float(geolib.get_res(src_dem_ds_align, square=True)[0])
    print("\nReference DEM res: %0.2f" % ref_dem_res)
    print("Source DEM res: %0.2f" % src_dem_res)
    print("Resolution for coreg: %s (%0.2f m)\n" % (args.res, res))

    #Iteration number
    n = 1
    #Cumulative offsets
    dx_total = 0
    dy_total = 0
    dz_total = 0

    #Now iteratively update geotransform and vertical shift
    while True:
        print("*** Iteration %i ***" % n)
        dx, dy, dz, static_mask, fig = compute_offset(ref_dem_ds, src_dem_ds_align, src_dem_fn, mode, max_offset, \
                mask_list=mask_list, max_dz=max_dz, slope_lim=slope_lim, plot=True)
        xyz_shift_str_iter = "dx=%+0.2fm, dy=%+0.2fm, dz=%+0.2fm" % (dx, dy, dz)
        print("Incremental offset: %s" % xyz_shift_str_iter)

        dx_total += dx
        dy_total += dy
        dz_total += dz

        xyz_shift_str_cum = "dx=%+0.2fm, dy=%+0.2fm, dz=%+0.2fm" % (dx_total, dy_total, dz_total)
        print("Cumulative offset: %s" % xyz_shift_str_cum)
        #String to append to output filenames
        xyz_shift_str_cum_fn = '_%s_x%+0.2f_y%+0.2f_z%+0.2f' % (mode, dx_total, dy_total, dz_total)

        #Should make an animation of this converging
        if n == 1: 
            #static_mask_orig = static_mask
            if fig is not None:
                dst_fn = outprefix + '_%s_iter%02i_plot.png' % (mode, n)
                print("Writing offset plot: %s" % dst_fn)
                fig.gca().set_title("Incremental: %s\nCumulative: %s" % (xyz_shift_str_iter, xyz_shift_str_cum))
                fig.savefig(dst_fn, dpi=300)

        #Apply the horizontal shift to the original dataset
        src_dem_ds_align = coreglib.apply_xy_shift(src_dem_ds_align, dx, dy, createcopy=False)
        #Should 
        src_dem_ds_align = coreglib.apply_z_shift(src_dem_ds_align, dz, createcopy=False)

        n += 1
        print("\n")
        #If magnitude of shift in all directions is less than tol
        #if n > max_iter or (abs(dx) <= min_dx and abs(dy) <= min_dy and abs(dz) <= min_dz):
        #If magnitude of shift is less than tol
        dm = np.sqrt(dx**2 + dy**2 + dz**2)
        dm_total = np.sqrt(dx_total**2 + dy_total**2 + dz_total**2)

        if dm_total > max_offset:
            sys.exit("Total offset exceeded specified max_offset (%0.2f m). Consider increasing -max_offset argument" % max_offset)

        #Stop iteration
        if n > max_iter or dm < tol:

            if fig is not None:
                dst_fn = outprefix + '_%s_iter%02i_plot.png' % (mode, n)
                print("Writing offset plot: %s" % dst_fn)
                fig.gca().set_title("Incremental:%s\nCumulative:%s" % (xyz_shift_str_iter, xyz_shift_str_cum))
                fig.savefig(dst_fn, dpi=300)

            #Compute final elevation difference
            if True:
                ref_dem_clip_ds_align, src_dem_clip_ds_align = warplib.memwarp_multi([ref_dem_ds, src_dem_ds_align], \
                        res=res, extent='intersection', t_srs=local_srs, r='cubic')
                ref_dem_align = iolib.ds_getma(ref_dem_clip_ds_align, 1)
                src_dem_align = iolib.ds_getma(src_dem_clip_ds_align, 1)
                ref_dem_clip_ds_align = None

                diff_align = src_dem_align - ref_dem_align
                src_dem_align = None
                ref_dem_align = None

                #Get updated, final mask
                static_mask_final = get_mask(src_dem_clip_ds_align, mask_list, src_dem_fn)
                static_mask_final = np.logical_or(np.ma.getmaskarray(diff_align), static_mask_final)
                
                #Final stats, before outlier removal
                diff_align_compressed = diff_align[~static_mask_final]
                diff_align_stats = malib.get_stats_dict(diff_align_compressed, full=True)

                #Prepare filtered version for tiltcorr fit
                diff_align_filt = np.ma.array(diff_align, mask=static_mask_final)
                diff_align_filt = outlier_filter(diff_align_filt, f=3, max_dz=max_dz)
                #diff_align_filt = outlier_filter(diff_align_filt, perc=(12.5, 87.5), max_dz=max_dz)
                slope = get_filtered_slope(src_dem_clip_ds_align)
                diff_align_filt = np.ma.array(diff_align_filt, mask=np.ma.getmaskarray(slope))
                diff_align_filt_stats = malib.get_stats_dict(diff_align_filt, full=True)

            #Fit 2D polynomial to residuals and remove
            #To do: add support for along-track and cross-track artifacts
            if tiltcorr and not tiltcorr_done:
                print("\n************")
                print("Calculating 'tiltcorr' 2D polynomial fit to residuals with order %i" % polyorder)
                print("************\n")
                gt = src_dem_clip_ds_align.GetGeoTransform()

                #Need to apply the mask here, so we're only fitting over static surfaces
                #Note that the origmask=False will compute vals for all x and y indices, which is what we want
                vals, resid, coeff = geolib.ma_fitpoly(diff_align_filt, order=polyorder, gt=gt, perc=(0,100), origmask=False)
                #vals, resid, coeff = geolib.ma_fitplane(diff_align_filt, gt, perc=(12.5, 87.5), origmask=False)

                #Should write out coeff or grid with correction 

                vals_stats = malib.get_stats_dict(vals)

                #Want to have max_tilt check here
                #max_tilt = 4.0 #m
                #Should do percentage
                #vals.ptp() > max_tilt

                #Note: dimensions of ds and vals will be different as vals are computed for clipped intersection
                #Need to recompute planar offset for full src_dem_ds_align extent and apply
                xgrid, ygrid = geolib.get_xy_grids(src_dem_ds_align)
                valgrid = geolib.polyval2d(xgrid, ygrid, coeff) 
                #For results of ma_fitplane
                #valgrid = coeff[0]*xgrid + coeff[1]*ygrid + coeff[2]
                src_dem_ds_align = coreglib.apply_z_shift(src_dem_ds_align, -valgrid, createcopy=False)

                if True:
                    print("Creating plot of polynomial fit to residuals")
                    fig, axa = plt.subplots(1,2, figsize=(8, 4))
                    dz_clim = malib.calcperc_sym(vals, (2, 98))
                    ax = pltlib.iv(diff_align_filt, ax=axa[0], cmap='RdBu', clim=dz_clim, \
                            label='Residual dz (m)', scalebar=False)
                    ax = pltlib.iv(valgrid, ax=axa[1], cmap='RdBu', clim=dz_clim, \
                            label='Polyfit dz (m)', ds=src_dem_ds_align)
                    #if tiltcorr:
                        #xyz_shift_str_cum_fn += "_tiltcorr"
                    tiltcorr_fig_fn = outprefix + '%s_polyfit.png' % xyz_shift_str_cum_fn
                    print("Writing out figure: %s\n" % tiltcorr_fig_fn)
                    fig.savefig(tiltcorr_fig_fn, dpi=300)

                print("Applying tilt correction to difference map")
                diff_align -= vals

                #Should iterate until tilts are below some threshold
                #For now, only do one tiltcorr
                tiltcorr_done=True
                #Now use original tolerance, and number of iterations 
                tol = args.tol
                max_iter = n + args.max_iter
            else:
                break

    if True:
        #Write out aligned difference map for clipped extent with vertial offset removed
        align_diff_fn = outprefix + '%s_align_diff.tif' % xyz_shift_str_cum_fn
        print("Writing out aligned difference map with median vertical offset removed")
        iolib.writeGTiff(diff_align, align_diff_fn, src_dem_clip_ds_align)

    if True:
        #Write out fitered aligned difference map
        align_diff_filt_fn = outprefix + '%s_align_diff_filt.tif' % xyz_shift_str_cum_fn
        print("Writing out filtered aligned difference map with median vertical offset removed")
        iolib.writeGTiff(diff_align_filt, align_diff_filt_fn, src_dem_clip_ds_align)

    #Extract final center coordinates for intersection
    center_coord_ll = geolib.get_center(src_dem_clip_ds_align, t_srs=geolib.wgs_srs)
    center_coord_xy = geolib.get_center(src_dem_clip_ds_align)
    src_dem_clip_ds_align = None

    #Write out final aligned src_dem 
    align_fn = outprefix + '%s_align.tif' % xyz_shift_str_cum_fn
    print("Writing out shifted src_dem with median vertical offset removed: %s" % align_fn)
    #Open original uncorrected dataset at native resolution
    src_dem_ds = gdal.Open(src_dem_fn)
    src_dem_ds_align = iolib.mem_drv.CreateCopy('', src_dem_ds, 0)
    #Apply final horizontal and vertial shift to the original dataset
    #Note: potentially issues if we used a different projection during coregistration!
    src_dem_ds_align = coreglib.apply_xy_shift(src_dem_ds_align, dx_total, dy_total, createcopy=False)
    src_dem_ds_align = coreglib.apply_z_shift(src_dem_ds_align, dz_total, createcopy=False)
    if tiltcorr:
        xgrid, ygrid = geolib.get_xy_grids(src_dem_ds_align)
        valgrid = geolib.polyval2d(xgrid, ygrid, coeff) 
        #For results of ma_fitplane
        #valgrid = coeff[0]*xgrid + coeff[1]*ygrid + coeff[2]
        src_dem_ds_align = coreglib.apply_z_shift(src_dem_ds_align, -valgrid, createcopy=False)
    #Might be cleaner way to write out MEM ds directly to disk
    src_dem_full_align = iolib.ds_getma(src_dem_ds_align)
    iolib.writeGTiff(src_dem_full_align, align_fn, src_dem_ds_align)

    if True:
        #Output final aligned src_dem, masked so only best pixels are preserved
        #Useful if creating a new reference product
        #Can also use apply_mask.py 
        print("Applying filter to shiftec src_dem")
        align_diff_filt_full_ds = warplib.memwarp_multi_fn([align_diff_filt_fn,], res=src_dem_ds_align, extent=src_dem_ds_align, \
                t_srs=src_dem_ds_align)[0]
        align_diff_filt_full = iolib.ds_getma(align_diff_filt_full_ds)
        align_diff_filt_full_ds = None
        align_fn_masked = outprefix + '%s_align_filt.tif' % xyz_shift_str_cum_fn
        iolib.writeGTiff(np.ma.array(src_dem_full_align, mask=np.ma.getmaskarray(align_diff_filt_full)), \
                align_fn_masked, src_dem_ds_align)

    src_dem_full_align = None
    src_dem_ds_align = None

    #Compute original elevation difference
    if True:
        ref_dem_clip_ds, src_dem_clip_ds = warplib.memwarp_multi([ref_dem_ds, src_dem_ds], \
                res=res, extent='intersection', t_srs=local_srs, r='cubic')
        src_dem_ds = None
        ref_dem_ds = None
        ref_dem_orig = iolib.ds_getma(ref_dem_clip_ds)
        src_dem_orig = iolib.ds_getma(src_dem_clip_ds)
        #Needed for plotting
        ref_dem_hs = geolib.gdaldem_mem_ds(ref_dem_clip_ds, processing='hillshade', returnma=True, computeEdges=True)
        src_dem_hs = geolib.gdaldem_mem_ds(src_dem_clip_ds, processing='hillshade', returnma=True, computeEdges=True)
        diff_orig = src_dem_orig - ref_dem_orig
        #Only compute stats over valid surfaces
        static_mask_orig = get_mask(src_dem_clip_ds, mask_list, src_dem_fn)
        #Note: this doesn't include outlier removal or slope mask!
        static_mask_orig = np.logical_or(np.ma.getmaskarray(diff_orig), static_mask_orig)
        #For some reason, ASTER DEM diff have a spike near the 0 bin, could be an issue with masking?
        diff_orig_compressed = diff_orig[~static_mask_orig]
        diff_orig_stats = malib.get_stats_dict(diff_orig_compressed, full=True)

        #Prepare filtered version for comparison 
        diff_orig_filt = np.ma.array(diff_orig, mask=static_mask_orig)
        diff_orig_filt = outlier_filter(diff_orig_filt, f=3, max_dz=max_dz)
        #diff_orig_filt = outlier_filter(diff_orig_filt, perc=(12.5, 87.5), max_dz=max_dz)
        slope = get_filtered_slope(src_dem_clip_ds)
        diff_orig_filt = np.ma.array(diff_orig_filt, mask=np.ma.getmaskarray(slope))
        diff_orig_filt_stats = malib.get_stats_dict(diff_orig_filt, full=True)

        #Write out original difference map
        print("Writing out original difference map for common intersection before alignment")
        orig_diff_fn = outprefix + '_orig_diff.tif'
        iolib.writeGTiff(diff_orig, orig_diff_fn, ref_dem_clip_ds)
        src_dem_clip_ds = None
        ref_dem_clip_ds = None

    if True:
        align_stats_fn = outprefix + '%s_align_stats.json' % xyz_shift_str_cum_fn
        align_stats = {}
        align_stats['src_fn'] = src_dem_fn 
        align_stats['ref_fn'] = ref_dem_fn 
        align_stats['align_fn'] = align_fn 
        align_stats['res'] = {} 
        align_stats['res']['src'] = src_dem_res
        align_stats['res']['ref'] = ref_dem_res
        align_stats['res']['coreg'] = res
        align_stats['center_coord'] = {'lon':center_coord_ll[0], 'lat':center_coord_ll[1], \
                'x':center_coord_xy[0], 'y':center_coord_xy[1]}
        align_stats['shift'] = {'dx':dx_total, 'dy':dy_total, 'dz':dz_total, 'dm':dm_total}
        #This tiltcorr flag gets set to false, need better flag
        if tiltcorr:
            align_stats['tiltcorr'] = {}
            align_stats['tiltcorr']['coeff'] = coeff.tolist()
            align_stats['tiltcorr']['val_stats'] = vals_stats
        align_stats['before'] = diff_orig_stats
        align_stats['before_filt'] = diff_orig_filt_stats
        align_stats['after'] = diff_align_stats
        align_stats['after_filt'] = diff_align_filt_stats
        
        import json
        with open(align_stats_fn, 'w') as f:
            json.dump(align_stats, f)

    #Create output plot
    if True:
        print("Creating final plot")
        kwargs = {'interpolation':'none'}
        #f, axa = plt.subplots(2, 4, figsize=(11, 8.5))
        f, axa = plt.subplots(2, 4, figsize=(16, 8))
        for ax in axa.ravel()[:-1]:
            ax.set_facecolor('k')
            pltlib.hide_ticks(ax)
        dem_clim = malib.calcperc(ref_dem_orig, (2,98))
        axa[0,0].imshow(ref_dem_hs, cmap='gray', **kwargs)
        im = axa[0,0].imshow(ref_dem_orig, cmap='cpt_rainbow', clim=dem_clim, alpha=0.6, **kwargs)
        pltlib.add_cbar(axa[0,0], im, arr=ref_dem_orig, clim=dem_clim, label=None)
        pltlib.add_scalebar(axa[0,0], res=res)
        axa[0,0].set_title('Reference DEM')
        axa[0,1].imshow(src_dem_hs, cmap='gray', **kwargs)
        im = axa[0,1].imshow(src_dem_orig, cmap='cpt_rainbow', clim=dem_clim, alpha=0.6, **kwargs)
        pltlib.add_cbar(axa[0,1], im, arr=src_dem_orig, clim=dem_clim, label=None)
        axa[0,1].set_title('Source DEM')
        #axa[0,2].imshow(~static_mask_orig, clim=(0,1), cmap='gray')
        axa[0,2].imshow(~static_mask, clim=(0,1), cmap='gray', **kwargs)
        axa[0,2].set_title('Surfaces for co-registration')
        dz_clim = malib.calcperc_sym(diff_orig_compressed, (5, 95))
        im = axa[1,0].imshow(diff_orig, cmap='RdBu', clim=dz_clim)
        pltlib.add_cbar(axa[1,0], im, arr=diff_orig, clim=dz_clim, label=None)
        axa[1,0].set_title('Elev. Diff. Before (m)')
        im = axa[1,1].imshow(diff_align, cmap='RdBu', clim=dz_clim)
        pltlib.add_cbar(axa[1,1], im, arr=diff_align, clim=dz_clim, label=None)
        axa[1,1].set_title('Elev. Diff. After (m)')

        #tight_dz_clim = (-1.0, 1.0)
        tight_dz_clim = (-2.0, 2.0)
        #tight_dz_clim = (-10.0, 10.0)
        #tight_dz_clim = malib.calcperc_sym(diff_align_filt, (5, 95))
        im = axa[1,2].imshow(diff_align_filt, cmap='RdBu', clim=tight_dz_clim)
        pltlib.add_cbar(axa[1,2], im, arr=diff_align_filt, clim=tight_dz_clim, label=None)
        axa[1,2].set_title('Elev. Diff. After (m)')

        #Tried to insert Nuth fig here
        #ax_nuth.change_geometry(1,2,1)
        #f.axes.append(ax_nuth)

        bins = np.linspace(dz_clim[0], dz_clim[1], 128)
        axa[1,3].hist(diff_orig_compressed, bins, color='g', label='Before', alpha=0.5)
        axa[1,3].hist(diff_align_compressed, bins, color='b', label='After', alpha=0.5)
        axa[1,3].set_xlim(*dz_clim)
        axa[1,3].axvline(0, color='k', linewidth=0.5, linestyle=':')
        axa[1,3].set_xlabel('Elev. Diff. (m)')
        axa[1,3].set_ylabel('Count (px)')
        axa[1,3].set_title("Source - Reference")
        before_str = 'Before\nmed: %0.2f\nnmad: %0.2f' % (diff_orig_stats['med'], diff_orig_stats['nmad'])
        axa[1,3].text(0.05, 0.95, before_str, va='top', color='g', transform=axa[1,3].transAxes, fontsize=8)
        after_str = 'After\nmed: %0.2f\nnmad: %0.2f' % (diff_align_stats['med'], diff_align_stats['nmad'])
        axa[1,3].text(0.65, 0.95, after_str, va='top', color='b', transform=axa[1,3].transAxes, fontsize=8)

        #This is empty
        axa[0,3].axis('off')

        suptitle = '%s\nx: %+0.2fm, y: %+0.2fm, z: %+0.2fm' % (os.path.split(outprefix)[-1], dx_total, dy_total, dz_total)
        f.suptitle(suptitle)
        f.tight_layout()
        plt.subplots_adjust(top=0.90)

        fig_fn = outprefix + '%s_align.png' % xyz_shift_str_cum_fn
        print("Writing out figure: %s" % fig_fn)
        f.savefig(fig_fn, dpi=300)
コード例 #6
0
ファイル: imviewer.py プロジェクト: dshean/imview
def bma_fig(fig,
            bma,
            cmap='cpt_rainbow',
            clim=None,
            clim_perc=(2, 98),
            bg=None,
            bg_perc=(2, 98),
            n_subplt=1,
            subplt=1,
            label=None,
            title=None,
            contour_int=None,
            contour_fn=None,
            alpha=0.5,
            ticks=False,
            scalebar=None,
            ds=None,
            shp=None,
            imshow_kwargs={'interpolation': 'nearest'},
            cbar_kwargs={'orientation': 'vertical'},
            **kwargs):
    #We don't use the kwargs, just there to save parsing in main

    if clim is None:
        clim = pltlib.get_clim(bma, clim_perc=clim_perc)

    print("Colorbar limits: %0.3f %0.3f" % (clim[0], clim[1]))

    #Link all subplots for zoom/pan
    sharex = sharey = None
    if len(fig.get_axes()) > 0:
        sharex = sharey = fig.get_axes()[0]

    #Hack to catch situations with only 1 subplot, but a subplot number > 1
    if n_subplt == 1:
        subplt = 1

    #One row, multiple columns
    ax = fig.add_subplot(1, n_subplt, subplt, sharex=sharex, sharey=sharey)
    #This occupies the full figure
    #ax = fig.add_axes([0., 0., 1., 1., ])

    #ax.patch.set_facecolor('black')
    ax.patch.set_facecolor('white')

    #Set appropriate nodata value color
    cmap_name = cmap
    cmap = pltlib.cmap_setndv(cmap_name)

    #ax.set_title("Band %i" % subplt, fontsize=10)
    if title is not None:
        ax.set_title(title)

    #If a background image is provided, plot it first
    if bg is not None:
        #Note, alpha=1 is opaque, 0 completely transparent
        #alpha = 0.6
        bg_perc = (4, 96)
        bg_alpha = 1.0
        #bg_clim = malib.calcperc(bg, bg_perc)
        bg_clim = (1, 255)
        bg_cmap_name = 'gray'
        bg_cmap = pltlib.cmap_setndv(bg_cmap_name, cmap_name)
        #bg_cmap = plt.get_cmap(bg_cmap_name)
        #if 'inferno' in cmap_name:
        #    bg_cmap.set_bad('0.5', alpha=1)
        #else:
        #    bg_cmap.set_bad('k', alpha=1)
        #Set the overlay bad values to completely transparent, otherwise darkens the bg
        cmap.set_bad(alpha=0)
        bgplot = ax.imshow(bg, cmap=bg_cmap, clim=bg_clim, alpha=bg_alpha)
        imgplot = ax.imshow(bma,
                            alpha=alpha,
                            cmap=cmap,
                            clim=clim,
                            **imshow_kwargs)
    else:
        imgplot = ax.imshow(bma, cmap=cmap, clim=clim, **imshow_kwargs)

    gt = None
    if ds is not None:
        gt = np.array(ds.GetGeoTransform())
        gt_scale_factor = min(
            np.array([ds.RasterYSize, ds.RasterXSize]) /
            np.array(bma.shape, dtype=float))
        gt[1] *= gt_scale_factor
        gt[5] *= gt_scale_factor
        ds_srs = geolib.get_ds_srs(ds)
        if ticks:
            scale_ticks(ax, ds)
        else:
            pltlib.hide_ticks(ax)
        xres = geolib.get_res(ds)[0]
    else:
        pltlib.hide_ticks(ax)
    #This forces the black line outlining the image subplot to snap to the actual image dimensions
    #depreciated in 2.2
    #ax.set_adjustable('box-forced')

    if cbar_kwargs:
        #Should set the format based on dtype of input data
        #cbar_kwargs['format'] = '%i'
        #cbar_kwargs['format'] = '%0.1f'
        #cbar_kwargs['orientation'] = 'horizontal'

        #Determine whether we need to add extend triangles to colorbar
        cbar_kwargs['extend'] = pltlib.get_cbar_extend(bma, clim)

        #Add the colorbar to the axes
        cbar = pltlib.add_cbar(ax,
                               imgplot,
                               label=label,
                               cbar_kwargs=cbar_kwargs)

    #Plot contours every contour_int interval and update colorbar appropriately
    if contour_int is not None:
        if contour_fn is not None:
            contour_bma = iolib.fn_getma(contour_fn)
            contour_bma_clim = malib.calcperc(contour_bma)
        else:
            contour_bma = bma
            contour_bma_clim = clim

        #PIG bed ridge contours
        #bma_clim = (-1300, -300)
        #Jak front shear margin contours
        #bma_clim = (2000, 4000)
        contour_bma_clim = (100, 250)
        cstart = int(np.floor(contour_bma_clim[0] / contour_int)) * contour_int
        cend = int(np.ceil(contour_bma_clim[1] / contour_int)) * contour_int

        #Turn off dashed negative (beds are below sea level)
        #matplotlib.rcParams['contour.negative_linestyle'] = 'solid'

        clvl = np.arange(cstart, cend + 1, contour_int)
        contour_prop = {
            'levels': clvl,
            'linestyle': '-',
            'linewidths': 0.5,
            'alpha': 1.0
        }
        #contours = ax.contour(contour_bma, colors='k', **contour_prop)
        #contour_cmap = 'gray'
        contour_cmap = 'gray_r'
        #This prevents white contours
        contour_cmap_clim = (0, contour_bma_clim[-1])
        contours = ax.contour(contour_bma, cmap=contour_cmap, vmin=contour_cmap_clim[0], \
                vmax=contour_cmap_clim[-1], **contour_prop)

        #Add labels
        ax.clabel(contours,
                  inline=True,
                  inline_spacing=0,
                  fontsize=4,
                  fmt='%i')

        #Update the cbar with contour locations
        #cbar.add_lines(contours)
        #cbar.set_ticks(contours.levels)

    #Plot shape overlay, moved code to pltlib
    if shp is not None:
        pltlib.shp_overlay(ax, ds, shp, gt=gt, color='k')

    if scalebar:
        scale_ticks(ax, ds)
        sb_loc = pltlib.best_scalebar_location(bma)
        #Force scalebar position
        #sb_loc = 'lower right'
        pltlib.add_scalebar(ax, xres, location=sb_loc)
        if not ticks:
            pltlib.hide_ticks(ax)

    #Set up interactive display
    global gbma
    gbma = bma
    global ggt
    ggt = gt

    #Clicking on a subplot will make it active for z-coordinate display
    fig.canvas.mpl_connect('button_press_event', onclick)
    fig.canvas.mpl_connect('axes_enter_event', enter_axis)

    #Add support for interactive z-value display
    ax.format_coord = format_coord
コード例 #7
0
ファイル: mb_plot_gpd.py プロジェクト: whigg/gmbtools
def make_map(mb_dissolve_df=None, glac_df_mb=None, region_df=None, col='mb_mwea', border_df=None, \
        basin_df=None, crs=crs, extent=None, hs=None, hs_extent=None, clim=None):

    fig, ax = plt.subplots(figsize=(10, 8))
    ax.set_aspect('equal')

    cmap = 'RdBu'
    label = 'Mass Balance (m we/yr)'
    if 't1' in col:
        cmap = 'inferno'
        label = 'Source Date (year)'

    if clim is None:
        clim = (glac_df_mb[col].min(), glac_df_mb[col].max())

    #This is cartopy-enabled axes
    #ax = plt.axes(projection=crs)

    #Currently unsupported for AEA
    #gl = ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')

    if hs is not None:
        print("Plotting image")
        hs_style = {
            'cmap': 'gray',
            'origin': 'upper',
            'extent': cartopy_extent(hs_extent),
            'transform': crs
        }
        ax.imshow(hs, **hs_style)

    if border_df is not None:
        print("Plotting borders")
        border_style = {
            'facecolor': '0.95',
            'edgecolor': 'k',
            'linewidth': 0.7
        }
        border_df.plot(ax=ax, **border_style)

    if region_df is not None:
        #This is original region_col
        #region_style = {'column':col_name, 'cmap':'cpt_rainbow', 'edgecolor':'k', 'linewidth':0.5, 'alpha':0.2}
        #region_style = {'column':'Name', 'cmap':'gray', 'edgecolor':'k', 'linewidth':0.5, 'alpha':0.3}
        #region_style = {'cmap':'cpt_rainbow', 'edgecolor':'k', 'linewidth':0.5, 'alpha':0.05}
        region_style = {
            'facecolor': 'none',
            'edgecolor': 'k',
            'linewidth': 0.3,
            'alpha': 0.4
        }
        region_df.plot(ax=ax, **region_style)

    if basin_df is not None:
        basin_style = {
            'facecolor': 'none',
            'edgecolor': 'k',
            'linewidth': 0.3,
            'alpha': 0.4
        }
        basin_df.plot(ax=ax, **basin_style)

    #https://stackoverflow.com/questions/36008648/colorbar-on-geopandas
    # fake up the array of the scalar mappable. Urgh...
    sc = plt.cm.ScalarMappable(cmap=cmap,
                               norm=plt.Normalize(vmin=clim[0], vmax=clim[1]))
    sc._A = []

    if mb_dissolve_df is not None:
        #Plot single values for region or basin
        #This was HMA
        #scaling_f = 0.2
        #CONUS
        scaling_f = 3
        x = mb_dissolve_df['centroid_x']
        y = mb_dissolve_df['centroid_y']
        #s = scaling_f*mb_dissolve_df[('area_m2_sum')]/1E6
        #s = scaling_f*mb_dissolve_df[('Area_sum')]
        s = scaling_f * mb_dissolve_df[('Area_all', '')]
        #c = mb_dissolve_df[('mb_mwea_mean')]
        c = mb_dissolve_df['mb_mwea', 'mean']
        sc_style = {'cmap': cmap, 'edgecolor': 'k', 'linewidth': 0.5}
        sc = ax.scatter(x, y, s, c, vmin=clim[0], vmax=clim[1], **sc_style)
        #Add labels
        for k, v in mb_dissolve_df.iterrows():
            #lbl = '%0.2f +/- %0.2f' % (v[('mb_mwea_mean')], v[('mb_mwea_sigma_mean')])
            lbl = '%0.2f +/- %0.2f' % (v[col, 'mean'], v[col + '_sigma',
                                                         'mean'])
            ax.annotate(lbl,
                        xy=(v['centroid_x'], v['centroid_y']),
                        xytext=(1, 0),
                        textcoords='offset points',
                        family='sans-serif',
                        fontsize=8,
                        color='k')

    if glac_df_mb is not None:
        print("Plotting glacier polygons")
        glac_style = {'edgecolor': 'k', 'linewidth': 0.5}
        glac_ax = glac_df_mb.plot(ax=ax,
                                  column=col,
                                  cmap=cmap,
                                  vmin=clim[0],
                                  vmax=clim[1],
                                  **glac_style)

    #This is minx, miny, maxx, maxy
    if extent is None:
        if glac_df_mb is not None:
            extent = glac_df_mb.total_bounds
        else:
            extent = mb_dissolve_df.total_bounds
    #For cartopy axes
    #ax.set_extent(cartopy_extent(extent), crs=crs)
    ax.set_xlim(extent[0], extent[2])
    ax.set_ylim(extent[1], extent[3])

    #Adding colorbar doesn't work with the cartopy axes
    pltlib.add_cbar(ax, sc, label=label)
    pltlib.add_scalebar(ax, res=1)
    pltlib.hide_ticks(ax)

    plt.tight_layout()

    return fig
コード例 #8
0
for n, dem_fn in enumerate(dem_fn_list):
    print('%i of %i: %s' % (n+1, len(dem_fn_list), dem_fn))
    #print(dem_fn)
    #dem_ds = iolib.fn_getds(dem_fn)
    #dem = iolib.ds_getma(dem_ds)
    dem_fn = stack.fn_list[n]
    #title = dem_fn
    title = None
    dem = stack.ma_stack[n]
    anomaly = anomaly_stack[n]
    #dem_clim = malib.calcperc(stack.ma_stack, (2,98))
    #dem_hs_fn = os.path.splitext(dem_fn)[0]+'_hs_az315.tif'
    #dem_hs = iolib.fn_getma(dem_hs_fn)
    dem_hs = geolib.gdaldem_mem_ma(dem, dem_ds, returnma=True)
    #dt = timelib.fn_getdatetime(dem_fn)
    dt = stack.date_list[n]
    if dt is not None:
        title = dt.strftime('%Y-%m-%d')
    #f = makefig(dem, dem_hs, anomaly, ds=dem_ds, title=title)
    f,ax = plt.subplots()
    im = ax.imshow(anomaly, clim=anomaly_clim, cmap='RdBu')
    pltlib.add_cbar(ax, im, label='Elevation anomaly (m)')
    pltlib.add_scalebar(ax, res=stack.res, location='lower left')
    pltlib.hide_ticks(ax)
    ax.set_facecolor('k')
    if title is not None:
        ax.set_title(title)
    out_fn = os.path.join(outdir, os.path.splitext(os.path.split(dem_fn)[-1])[0]+'_anomaly.png')
    f.savefig(out_fn, bbox_inches='tight', dpi=150)

コード例 #9
0
def make_map(mb_dissolve_df=None,
             glac_df_mb=None,
             agg_df=None,
             col=('mb_mwea', 'mean'),
             border_df=None,
             crs=crs,
             extent=None,
             hs=None,
             hs_extent=None,
             clim=None,
             labels='val',
             title=None):

    fig, ax = plt.subplots(figsize=(10, 8))
    ax.set_aspect('equal')
    legend = add_legend(ax, sf=scaling_f)
    if title is not None:
        ax.set_title(title)

    if clim is None:
        #clim = (glac_df_mb[col].min(), glac_df_mb[col].max())
        clim = malib.calcperc_sym(mb_dissolve_df[col], perc=(1, 99))

    cmap = 'RdBu'
    if 'mb_mwea' in col:
        label = 'Mass Balance (m we/yr)'
    elif 'mb_Gta' in col:
        label = 'Mass Balance (Gt/yr)'
    elif 'meltwater' in col:
        label = 'Excess Meltwater Runoff (Gt/yr)'
        #Reverse, as these are negative values
        cmap = 'YlOrRd_r'
        #cmap = 'inferno'
        clim = malib.calcperc(mb_dissolve_df[col], perc=(0, 99))
    elif 't1' in col:
        cmap = 'inferno'
        label = 'Source Date (year)'

    #This is cartopy-enabled axes
    #ax = plt.axes(projection=crs)

    #Currently unsupported for AEA
    #gl = ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')

    if hs is not None:
        print("Plotting image")
        hs_style = {
            'cmap': 'gray',
            'origin': 'upper',
            'extent': cartopy_extent(hs_extent),
            'transform': crs
        }
        ax.imshow(hs, **hs_style)

    if border_df is not None:
        print("Plotting borders")
        border_style = {
            'facecolor': '0.65',
            'edgecolor': 'k',
            'linewidth': 0.7
        }
        border_df.plot(ax=ax, **border_style)

    if agg_df is not None:
        print("Plotting agg boundaries")
        #This was to get colored regions
        #agg_style = {'cmap':'cpt_rainbow', 'edgecolor':'none', 'linewidth':0, 'alpha':0.05}
        agg_style = {
            'cmap': 'summer',
            'edgecolor': 'none',
            'linewidth': 0,
            'alpha': 0.05
        }
        #agg_style = {'facecolor':'0.95','edgecolor':'k', 'linewidth':0.3, 'alpha':0.2}
        agg_df.plot(ax=ax, **agg_style)

    if glac_df_mb is not None:
        print("Plotting glacier polygons")
        glac_style = {'edgecolor': 'k', 'linewidth': 0.1, 'alpha': 0.2}
        #This plots mb color ramp for each glacier polygon
        #glac_ax = glac_df_mb.plot(ax=ax, column=col[0], cmap=cmap, vmin=clim[0], vmax=clim[1], **glac_style)
        #This plots outlines
        glac_ax = glac_df_mb.plot(ax=ax, facecolor='none', **glac_style)

    if agg_df is not None:
        agg_style = {'facecolor': 'none', 'edgecolor': 'w', 'linewidth': 0.5}
        agg_df.plot(ax=ax, **agg_style)

    #https://stackoverflow.com/questions/36008648/colorbar-on-geopandas
    # fake up the array of the scalar mappable so we can plot colorbar. Urgh...
    sc = plt.cm.ScalarMappable(cmap=cmap,
                               norm=plt.Normalize(vmin=clim[0], vmax=clim[1]))
    sc._A = []

    if mb_dissolve_df is not None:
        print("Plotting scatterplot of %s values" % (col, ))
        #Plot single values for region or basin
        x = mb_dissolve_df['centroid_x']
        y = mb_dissolve_df['centroid_y']
        #Scale by total glacier area in each polygon
        s = scaling_f * mb_dissolve_df[('Area_all', 'sum')]
        c = mb_dissolve_df[col]
        sc_style = {
            'cmap': cmap,
            'edgecolor': 'k',
            'linewidth': 0.5,
            'alpha': 0.8
        }
        sc = ax.scatter(x, y, s, c, vmin=clim[0], vmax=clim[1], **sc_style)
        #Add labels
        text_kw = {'family': 'sans-serif', 'fontsize': 8, 'color': 'k'}
        if labels is not None:
            print("Adding annotations")
            for k, v in mb_dissolve_df.iterrows():
                #lbl = '%0.2f +/- %0.2f' % (v[col], v[(col[0]+'_sigma',col[1])])
                if labels == 'name+val':
                    lbl = '%s\n%+0.2f' % (k, v[col])
                else:
                    lbl = '%+0.2f' % v[col]
                #ax.annotate(lbl, xy=(v['centroid_x'],v['centroid_y']), xytext=(1,0), textcoords='offset points', family='sans-serif', fontsize=6, color='darkgreen')
                txt = ax.annotate(lbl,
                                  xy=(v['centroid_x'], v['centroid_y']),
                                  ha='center',
                                  va='center',
                                  **text_kw)
                txt.set_path_effects([
                    path_effects.Stroke(linewidth=0.75, foreground='w'),
                    path_effects.Normal()
                ])

    #This is minx, miny, maxx, maxy
    if extent is None:
        #if glac_df_mb is not None:
        #    extent = glac_df_mb.total_bounds
        #else:
        extent = mb_dissolve_df.total_bounds

    #For cartopy axes
    #ax.set_extent(cartopy_extent(extent), crs=crs)
    #Pad extent so labels fit within map
    #extent = geolib.pad_extent(extent, perc=0.01, uniform=True)
    ax.set_xlim(extent[0], extent[2])
    ax.set_ylim(extent[1], extent[3])

    #Adding colorbar doesn't work with the cartopy axes
    pltlib.add_cbar(ax, sc, label=label)
    pltlib.add_scalebar(ax, res=1)
    pltlib.hide_ticks(ax)

    plt.tight_layout()

    return fig
コード例 #10
0
ファイル: dem_align.py プロジェクト: maxkoller/demcoreg
def main2(args):
    #Should check that files exist
    dem1_fn = args.ref_fn
    dem2_fn = args.src_fn
    mode = args.mode
    apply_mask = not args.nomask
    max_offset_m = args.max_offset
    tiltcorr = args.tiltcorr

    #These are tolerances (in meters) to stop iteration
    tol = args.tol
    min_dx = tol
    min_dy = tol
    min_dz = tol

    #Maximum number of iterations
    max_n = 10

    outdir = args.outdir
    if outdir is None:
        outdir = os.path.splitext(dem2_fn)[0] + '_dem_align'

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    outprefix = '%s_%s' % (os.path.splitext(os.path.split(dem2_fn)[-1])[0], \
            os.path.splitext(os.path.split(dem1_fn)[-1])[0])
    outprefix = os.path.join(outdir, outprefix)

    print("\nReference: %s" % dem1_fn)
    print("Source: %s" % dem2_fn)
    print("Mode: %s" % mode)
    print("Output: %s\n" % outprefix)

    dem2_ds = gdal.Open(dem2_fn, gdal.GA_ReadOnly)
    #Often the "ref" DEM is high-res lidar or similar
    #This is a shortcut to resample to match "source" DEM
    dem1_ds = warplib.memwarp_multi_fn([
        dem1_fn,
    ],
                                       res=dem2_ds,
                                       extent=dem2_ds,
                                       t_srs=dem2_ds)[0]
    #dem1_ds = gdal.Open(dem1_fn, gdal.GA_ReadOnly)

    #Create a copy to be updated in place
    dem2_ds_align = iolib.mem_drv.CreateCopy('', dem2_ds, 0)
    #dem2_ds_align = dem2_ds

    #Iteration number
    n = 1
    #Cumulative offsets
    dx_total = 0
    dy_total = 0
    dz_total = 0

    #Now iteratively update geotransform and vertical shift
    while True:
        print("*** Iteration %i ***" % n)
        dx, dy, dz, static_mask, fig = compute_offset(dem1_ds,
                                                      dem2_ds_align,
                                                      dem2_fn,
                                                      mode,
                                                      max_offset_m,
                                                      apply_mask=apply_mask)
        if n == 1:
            static_mask_orig = static_mask
        xyz_shift_str_iter = "dx=%+0.2fm, dy=%+0.2fm, dz=%+0.2fm" % (dx, dy,
                                                                     dz)
        print("Incremental offset: %s" % xyz_shift_str_iter)

        #Should make an animation of this converging
        if fig is not None:
            dst_fn = outprefix + '_%s_iter%i_plot.png' % (mode, n)
            print("Writing offset plot: %s" % dst_fn)
            fig.gca().set_title(xyz_shift_str_iter)
            fig.savefig(dst_fn, dpi=300, bbox_inches='tight', pad_inches=0.1)

        #Apply the horizontal shift to the original dataset
        dem2_ds_align = coreglib.apply_xy_shift(dem2_ds_align,
                                                dx,
                                                dy,
                                                createcopy=False)
        dem2_ds_align = coreglib.apply_z_shift(dem2_ds_align,
                                               dz,
                                               createcopy=False)

        dx_total += dx
        dy_total += dy
        dz_total += dz
        print("Cumulative offset: dx=%+0.2fm, dy=%+0.2fm, dz=%+0.2fm" %
              (dx_total, dy_total, dz_total))

        #Fit plane to residuals and remove
        #Might be better to do this after converging
        """
        if tiltcorr:
            print("Applying planar tilt correction")
            gt = dem2_ds_align.GetGeoTransform()
            #Need to compute diff_euler here
            #Copy portions of compute_offset, create new function 
            vals, resid, coeff = geolib.ma_fitplane(diff_euler_align, gt, perc=(4, 96))
            dem2_ds_align = coreglib.apply_z_shift(dem2_ds_align, -vals, createcopy=False)
        """

        n += 1
        print("\n")
        #If magnitude of shift in all directions is less than tol
        #if n > max_n or (abs(dx) <= min_dx and abs(dy) <= min_dy and abs(dz) <= min_dz):
        #If magnitude of shift is less than tol
        dm = np.sqrt(dx**2 + dy**2 + dz**2)
        if n > max_n or dm < tol:
            break

    #String to append to output filenames
    xyz_shift_str_cum = '_%s_x%+0.2f_y%+0.2f_z%+0.2f' % (mode, dx_total,
                                                         dy_total, dz_total)
    if tiltcorr:
        xyz_shift_str_cum += "_tiltcorr"

    #Compute original elevation difference
    if True:
        dem1_clip_ds, dem2_clip_ds = warplib.memwarp_multi([dem1_ds, dem2_ds], \
                res='max', extent='intersection', t_srs=dem2_ds)
        dem1_orig = iolib.ds_getma(dem1_clip_ds, 1)
        dem2_orig = iolib.ds_getma(dem2_clip_ds, 1)
        diff_euler_orig = dem2_orig - dem1_orig
        if not apply_mask:
            static_mask_orig = np.ma.getmaskarray(diff_euler_orig)
        diff_euler_orig_compressed = diff_euler_orig[~static_mask_orig]
        diff_euler_orig_stats = np.array(
            malib.print_stats(diff_euler_orig_compressed))

        #Write out original eulerian difference map
        print(
            "Writing out original euler difference map for common intersection before alignment"
        )
        dst_fn = outprefix + '_orig_dz_eul.tif'
        iolib.writeGTiff(diff_euler_orig, dst_fn, dem1_clip_ds)

    #Compute final elevation difference
    if True:
        dem1_clip_ds_align, dem2_clip_ds_align = warplib.memwarp_multi([dem1_ds, dem2_ds_align], \
                res='max', extent='intersection', t_srs=dem2_ds_align)
        dem1_align = iolib.ds_getma(dem1_clip_ds_align, 1)
        dem2_align = iolib.ds_getma(dem2_clip_ds_align, 1)
        diff_euler_align = dem2_align - dem1_align
        if not apply_mask:
            static_mask = np.ma.getmaskarray(diff_euler_align)
        diff_euler_align_compressed = diff_euler_align[~static_mask]
        diff_euler_align_stats = np.array(
            malib.print_stats(diff_euler_align_compressed))

        #Fit plane to residuals and remove
        if tiltcorr:
            print("Applying planar tilt correction")
            gt = dem1_clip_ds_align.GetGeoTransform()
            #Need to apply the mask here, so we're only fitting over static surfaces
            #Note that the origmask=False will compute vals for all x and y indices, which is what we want
            vals, resid, coeff = geolib.ma_fitplane(np.ma.array(diff_euler_align, mask=static_mask), \
                    gt, perc=(4, 96), origmask=False)
            #Remove planar offset from difference map
            diff_euler_align -= vals
            #Remove planar offset from aligned dem2
            #Note: dimensions of ds and vals will be different as vals are computed for clipped intersection
            #Recompute planar offset for dem2_ds_align extent
            xgrid, ygrid = geolib.get_xy_grids(dem2_ds_align)
            vals = coeff[0] * xgrid + coeff[1] * ygrid + coeff[2]
            dem2_ds_align = coreglib.apply_z_shift(dem2_ds_align,
                                                   -vals,
                                                   createcopy=False)
            if not apply_mask:
                static_mask = np.ma.getmaskarray(diff_euler_align)
            diff_euler_align_compressed = diff_euler_align[~static_mask]
            diff_euler_align_stats = np.array(
                malib.print_stats(diff_euler_align_compressed))
            print("Creating fitplane plot")
            fig, ax = plt.subplots(figsize=(6, 6))
            fitplane_clim = malib.calcperc(vals, (2, 98))
            im = ax.imshow(vals, cmap='cpt_rainbow', clim=fitplane_clim)
            res = float(geolib.get_res(dem2_clip_ds, square=True)[0])
            pltlib.add_scalebar(ax, res=res)
            pltlib.hide_ticks(ax)
            pltlib.add_cbar(ax, im, label='Fit plane residuals (m)')
            fig.tight_layout()
            dst_fn1 = outprefix + '%s_align_dz_eul_fitplane.png' % xyz_shift_str_cum
            print("Writing out figure: %s" % dst_fn1)
            fig.savefig(dst_fn1, dpi=300, bbox_inches='tight', pad_inches=0.1)

        #Compute higher-order fits?
        #Could also attempt to model along-track and cross-track artifacts

        #Write out aligned eulerian difference map for clipped extent with vertial offset removed
        dst_fn = outprefix + '%s_align_dz_eul.tif' % xyz_shift_str_cum
        print(
            "Writing out aligned difference map with median vertical offset removed"
        )
        iolib.writeGTiff(diff_euler_align, dst_fn, dem1_clip_ds)

    #Write out aligned dem_2 with vertial offset removed
    if True:
        dst_fn2 = outprefix + '%s_align.tif' % xyz_shift_str_cum
        print(
            "Writing out shifted dem2 with median vertical offset removed: %s"
            % dst_fn2)
        #Might be cleaner way to write out MEM ds directly to disk
        dem2_align = iolib.ds_getma(dem2_ds_align)
        iolib.writeGTiff(dem2_align, dst_fn2, dem2_ds_align)
        dem2_ds_align = None

    #Create output plot
    if True:
        print("Creating final plot")
        dem1_hs = geolib.gdaldem_mem_ma(dem1_orig, dem1_clip_ds, returnma=True)
        dem2_hs = geolib.gdaldem_mem_ma(dem2_orig, dem2_clip_ds, returnma=True)
        f, axa = plt.subplots(2, 3, figsize=(11, 8.5))
        for ax in axa.ravel()[:-1]:
            ax.set_facecolor('k')
            pltlib.hide_ticks(ax)
        dem_clim = malib.calcperc(dem1_orig, (2, 98))
        axa[0, 0].imshow(dem1_hs, cmap='gray')
        axa[0, 0].imshow(dem1_orig,
                         cmap='cpt_rainbow',
                         clim=dem_clim,
                         alpha=0.6)
        res = float(geolib.get_res(dem1_clip_ds, square=True)[0])
        pltlib.add_scalebar(axa[0, 0], res=res)
        axa[0, 0].set_title('Reference DEM')
        axa[0, 1].imshow(dem2_hs, cmap='gray')
        axa[0, 1].imshow(dem2_orig,
                         cmap='cpt_rainbow',
                         clim=dem_clim,
                         alpha=0.6)
        axa[0, 1].set_title('Source DEM')
        axa[0, 2].imshow(~static_mask_orig, clim=(0, 1), cmap='gray')
        axa[0, 2].set_title('Surfaces for co-registration')
        dz_clim = malib.calcperc_sym(diff_euler_orig_compressed, (5, 95))
        im = axa[1, 0].imshow(diff_euler_orig, cmap='RdBu', clim=dz_clim)
        pltlib.add_cbar(axa[1, 0], im, label=None)
        axa[1, 0].set_title('Elev. Diff. Before (m)')
        im = axa[1, 1].imshow(diff_euler_align, cmap='RdBu', clim=dz_clim)
        pltlib.add_cbar(axa[1, 1], im, label=None)
        axa[1, 1].set_title('Elev. Diff. After (m)')

        #Tried to insert Nuth fig here
        #ax_nuth.change_geometry(1,2,1)
        #f.axes.append(ax_nuth)

        bins = np.linspace(dz_clim[0], dz_clim[1], 128)
        axa[1, 2].hist(diff_euler_orig_compressed,
                       bins,
                       color='g',
                       label='Before',
                       alpha=0.5)
        axa[1, 2].hist(diff_euler_align_compressed,
                       bins,
                       color='b',
                       label='After',
                       alpha=0.5)
        axa[1, 2].axvline(0, color='k', linewidth=0.5, linestyle=':')
        axa[1, 2].set_xlabel('Elev. Diff. (m)')
        axa[1, 2].set_ylabel('Count (px)')
        axa[1, 2].set_title("Source - Reference")
        #axa[1,2].legend(loc='upper right')
        #before_str = 'Before\nmean: %0.2f\nstd: %0.2f\nmed: %0.2f\nnmad: %0.2f' % tuple(diff_euler_orig_stats[np.array((3,4,5,6))])
        #after_str = 'After\nmean: %0.2f\nstd: %0.2f\nmed: %0.2f\nnmad: %0.2f' % tuple(diff_euler_align_stats[np.array((3,4,5,6))])
        before_str = 'Before\nmed: %0.2f\nnmad: %0.2f' % tuple(
            diff_euler_orig_stats[np.array((5, 6))])
        axa[1, 2].text(0.05,
                       0.95,
                       before_str,
                       va='top',
                       color='g',
                       transform=axa[1, 2].transAxes)
        after_str = 'After\nmed: %0.2f\nnmad: %0.2f' % tuple(
            diff_euler_align_stats[np.array((5, 6))])
        axa[1, 2].text(0.65,
                       0.95,
                       after_str,
                       va='top',
                       color='b',
                       transform=axa[1, 2].transAxes)

        suptitle = '%s\nx: %+0.2fm, y: %+0.2fm, z: %+0.2fm' % (
            os.path.split(outprefix)[-1], dx_total, dy_total, dz_total)
        f.suptitle(suptitle)
        f.tight_layout()
        plt.subplots_adjust(top=0.90)

        dst_fn = outprefix + '%s_align.png' % xyz_shift_str_cum
        print("Writing out figure: %s" % dst_fn)
        f.savefig(dst_fn, dpi=300, bbox_inches='tight', pad_inches=0.1)

        #Removing residual planar tilt can introduce additional slope/aspect dependent offset
        #Want to run another round of main dem_align after removing planar tilt
        if tiltcorr:
            print("\n Rerunning after applying tilt correction \n")
            #Create copy of original arguments
            import copy
            args2 = copy.copy(args)
            #Use aligned, tilt-corrected DEM as input src_fn for second round
            args2.src_fn = dst_fn2
            #Assume we've already corrected most of the tilt during first round (also prevents endless loop)
            args2.tiltcorr = False
            main2(args2)
コード例 #11
0
ファイル: swe.py プロジェクト: jmichellehu/snowtools
        prism = iolib.ds_getma(prism_ds)/1000.
        #Apply SWE mask, so we are only considering valid pixels
        prism = np.ma.array(prism, mask=np.ma.getmaskarray(swe))
        nax = 3
        figsize = (12, 4)

if True:
    #Map plots
    f, axa = plt.subplots(1, nax, figsize=figsize, sharex=True, sharey=True, subplot_kw={'aspect':'equal', 'adjustable':'box-forced'})
    hs_im = axa[0].imshow(hs, vmin=hs_clim[0], vmax=hs_clim[1], cmap='gray')
    dem_im = axa[0].imshow(dem1, vmin=dem_clim[0], vmax=dem_clim[1], cmap='cpt_rainbow', alpha=0.5)
    axa[0].set_facecolor('k')
    swe_im = axa[1].imshow(swe, vmin=swe_clim[0], vmax=swe_clim[1], cmap='inferno')
    axa[1].set_facecolor('0.3')
    axa[0].set_title('Late Summer %i' % dem1_ts.year, fontdict={'fontsize':8})
    pltlib.add_cbar(axa[0], dem_im, label='Elevation (m WGS84)')
    pltlib.add_scalebar(axa[0], res)
    axa[1].set_title('WY%i (Summer %i to Spring %i Elev. Diff.)' % (wy, dem1_ts.year, (dem1_ts.year+1)), fontdict={'fontsize':8})
    pltlib.add_cbar(axa[1], swe_im, label=r'SWE Estimate (m w.e., $\rho_s$=0.5)')
    if prism is not None:
        #Should use f.add_subplot() here
        prism_im = axa[2].imshow(prism, vmin=swe_clim[0], vmax=swe_clim[1], cmap='inferno')
        axa[2].set_facecolor('0.3')
        axa[2].set_title('~30-year PRISM Normal: Oct-May Precip', fontdict={'fontsize':8})
        pltlib.add_cbar(axa[2], swe_im, label='Cumulative Precip (m w.e.)')
    for ax in axa:
        pltlib.hide_ticks(ax)
    plt.tight_layout()
    if save:
        fig_fn = '%s_WY%i_SWE_maps.png' % (outprefix, wy)
        if prism is not None:
コード例 #12
0
ファイル: filter_glas_control.py プロジェクト: pahbs/evhr
    # Get the elev dif b/w GLAS and DEM
    dz = z_fltr_mask_coreg - coreg_samp[coreg_samp_idx,0]

    if True:
        print("Creating plot of %i output points" % x_fltr.shape[0] )
        fig_kw = {'figsize':(10,7.5)}
        fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, sharex=True, sharey=True, **fig_kw)

        #Plot DEM color shaded relief
        #
        hs_ma = geolib.gdaldem_wrapper(dem_fn)
        hs_clim = malib.calcperc(hs_ma, perc=(0.5, 99.5))
        dem_clim = malib.calcperc(dem_ma)
        ax1.imshow(hs_ma, cmap='gray', clim=hs_clim)
        im1 = ax1.imshow(dem_ma, cmap=cpt_rainbow, clim=dem_clim, alpha=0.5)
        cbar = pltlib.add_cbar(ax1, im1, label='DEM Elev. (m WGS84)')

        #Plot all points in extent over shaded relief; overplot coreg points
        #
        ax2.imshow(hs_ma, cmap='gray', clim=hs_clim, alpha=0.5)
        # Show 4m ortho
        toa_fn = dem_control.get_toa_fn(dem_fn)
        print("Loading TOA ortho: %s" % toa_fn)
        toa_ds = gdal.Open(toa_fn)
        toa_ma = iolib.ds_getma(toa_ds)
        toa_clim = malib.calcperc(toa_ma, perc=(0.5, 99.5))
        im2 = ax2.imshow(toa_ma, cmap='gray', clim=toa_clim, alpha=1)
        # Show roughmask (should be called 'controlmask' which shows control surfaces
        #dem_roughmask_fn = os.path.splitext(dem_fn)[0]+'_roughmask.tif'
        #dem_roughmask_ds = gdal.Open(dem_roughmask_fn)
        #dem_roughmask_ma = iolib.ds_getma(dem_roughmask_ds)
コード例 #13
0
ファイル: mb.py プロジェクト: ywbomhf2019/gmbtools
        t2_title = z2_date.strftime('%Y-%m-%d')
        axa[0].set_title(t1_title)
        axa[1].set_title(t2_title)
        axa[2].set_title('%s to %s (%0.2f yr)' % (t1_title, t2_title, dt))
        #dz_clim = (-10, 10)
        dz_clim = (-2.0, 2.0)
        dz_im = axa[2].imshow(dhdt,
                              cmap='RdBu',
                              vmin=dz_clim[0],
                              vmax=dz_clim[1])
        for ax in axa:
            pltlib.hide_ticks(ax)
            ax.set_facecolor('k')
        sb_loc = pltlib.best_scalebar_location(z1)
        pltlib.add_scalebar(axa[0], ds_res[0], location=sb_loc)
        pltlib.add_cbar(axa[0], z1_im, label='Elevation (m WGS84)')
        pltlib.add_cbar(axa[1], z2_im, label='Elevation (m WGS84)')
        pltlib.add_cbar(axa[2], dz_im, label='dh/dt (m/yr)')
        plt.tight_layout()
        #Make room for suptitle
        plt.subplots_adjust(top=0.90)
        print("Saving map plot")
        fig_fn = os.path.join(outdir, feat_fn + '_mb_map.png')
        plt.savefig(fig_fn, dpi=300)

        print("Generating aed plot")
        f, axa = plt.subplots(1, 2, figsize=(6, 6))
        f.suptitle(feat_fn)
        axa[0].plot(z1_bin_areas, z_bin_centers, label='%0.2f' % t1)
        axa[0].plot(z2_bin_areas, z_bin_centers, label='%0.2f' % t2)
        axa[0].axhline(z1_ela, ls=':', c='C0')
コード例 #14
0
ファイル: mb_plot.py プロジェクト: whigg/gmbtools
def mapplot(a, field, srs, sf=16, ax=None, clim=None):
    if ax is None:
        f, ax = plt.subplots()
    #Get this from glacier shp or DEM mosaic
    #CONUS
    #extent = [-674693.945810, -729687.166879, 795021.159447, 688725.556370]
    #extent = geolib.pad_extent(geolib.extent_round(extent, precision=1000), width=60000)
    extent = [a['x'].min(), a['y'].min(), a['x'].max(), a['y'].max()]
    print(extent)
    extent = geolib.pad_extent(geolib.extent_round(extent, precision=1000), width=100000)
    print(extent)
    #w = extent[2] - extent[0]
    #h = extent[3] - extent[1]
    w = 2*np.abs([extent[0], extent[2]]).max()
    h = 2*np.abs([extent[1], extent[3]]).max()
    lon_0, lat_0 = (srs.GetProjParm("longitude_of_center"), srs.GetProjParm("latitude_of_center"))
    lat_1, lat_2 = (srs.GetProjParm("standard_parallel_1"), srs.GetProjParm("standard_parallel_2"))
    proj_kwargs = {'projection':'aea','lat_1':lat_1,'lat_2':lat_2,'lon_0':lon_0,'lat_0':lat_0,'ellps':'WGS84'}
    print(proj_kwargs)
    m = Basemap(width=w, height=h, resolution='h', area_thresh=10000, ax=ax, **proj_kwargs)
    #clon, clat, dummy = geolib.cT_helper(extent[0]+w/2.0,extent[1]+h/2.0,0,srs,geolib.wgs_srs)
    xoff, yoff = m(lon_0, lat_0)
    print(xoff, yoff)
    x = a['x'] + xoff
    y = a['y'] + yoff
    m.fillcontinents(color='0.9',zorder=0)
    m.drawcoastlines(linewidth=0.25,zorder=1)
    m.drawcountries(linewidth=0.25,zorder=1)
    m.drawstates(linewidth=0.25,zorder=1)
    m.drawrivers(color='lightblue',linewidth=0.25,zorder=2)
    #m.shadedrelief()
    #m.bluemarble()
    #m.drawmapscale(-115,37,lon_0,lat_0,400,barstyle='fancy',zorder=10)
    parallels = np.arange(0.,91.,5.)
    m.drawparallels(parallels,linewidth=0.5,labels=[True,False,False,False],zorder=2)
    meridians = np.arange(-180.,181.,10.)
    m.drawmeridians(meridians,linewidth=0.5,labels=[False,False,False,True],zorder=2)
    if field == 'mb_mwea':
        cmap = 'RdBu'
        label = 'Mass balance (m we/yr)'
        if clim is None:
            vmin,vmax = get_equal_vmin_vmax(a[field])
        else:
            vmin, vmax = clim
        lw = 0.1
    elif field == 't1':
        cmap = 'inferno'
        label = None 
        vmin = a[field].min()
        vmax = a[field].max()
        lw = 0.0
    print sf
    sc = m.scatter(x, y, c=a[field], cmap=cmap, s=a['area_m2']*sf, \
            edgecolor='k', lw=lw, vmin=vmin, vmax=vmax)
    #ax.set(adjustable='box-forced', aspect='equal')
    #ax.set_facecolor('0.5')
    cbar = pltlib.add_cbar(ax, sc, label=label)
    #plt.tight_laya()
    #plt.savefig('conus_mb.png', dpi=300)
    leg = add_legend(ax, sf=sf, loc='upper right')
    return ax
コード例 #15
0
    pY_fltr_mask = np.atleast_1d(pY_fltr_mask)

    dz = z_fltr_mask - samp[samp_idx,0]

    if True:
        print("Creating plot of %i output points" % x_fltr.shape[0])
        fig_kw = {'figsize':(10,7.5)}
        fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, sharex=True, sharey=True, **fig_kw)

        #Plot DEM color shaded relief
        hs_ma = geolib.gdaldem_wrapper(dem_fn)
        hs_clim = malib.calcperc(hs_ma, perc=(0.5, 99.5))
        dem_clim = malib.calcperc(dem_ma)
        ax1.imshow(hs_ma, cmap='gray', clim=hs_clim)
        im1 = ax1.imshow(dem_ma, cmap=cpt_rainbow, clim=dem_clim, alpha=0.5)
        cbar = pltlib.add_cbar(ax1, im1, label='DEM Elev. (m WGS84)')
       
        #Plot all color points over shaded relief
        im2 = ax2.imshow(hs_ma, cmap='gray', clim=hs_clim, alpha=0.5)
        #Plot all points in black
        sc2 = ax2.scatter(pX_fltr, pY_fltr, s=0.5, c='k', edgecolors='none')
        #Plot valid in color
        c = z_fltr_mask 
        sc2 = ax2.scatter(pX_fltr_mask, pY_fltr_mask, s=0.5, c=c, cmap=cpt_rainbow, vmin=dem_clim[0], vmax=dem_clim[1], edgecolors='none')
        cbar = pltlib.add_cbar(ax2, sc2, label='Pt Elev. (m WGS84)')

        #Plot time
        c = glas_pts_fltr[:,tcol]
        c_decyear = timelib.np_dt2decyear(timelib.np_o2dt(c))
        c = c_decyear
        #vmin = c.min()
コード例 #16
0
def main():
    if len(sys.argv) != 2:
        sys.exit("Usage: %s stack.npz" % os.path.basename(sys.argv[0]))

    stack_fn = sys.argv[1]

    print "Loading stack"
    s = malib.DEMStack(stack_fn=stack_fn, stats=True, trend=True, save=False)
    global d
    d = s.date_list_o

    d_ptp = d[-1] - d[0]
    d_pad = 0.03*d_ptp
    global min_dt
    min_dt = d[0]-d_pad
    global max_dt
    max_dt = d[-1]+d_pad
    #Use these to set bounds to hardcode min/max of all stacks
    #import pytz
    #min_dt = datetime(1999,1,1)
    #min_dt = datetime(2007,1,1, tzinfo=pytz.utc)
    #max_dt = datetime(2015,12,31, tzinfo=pytz.utc)

    global source
    source = np.ma.array(s.source)
    global source_dict
    source_dict = get_source_dict()
    global error 
    error = s.error
    global gt
    gt = s.gt
    global m
    m = s.ma_stack
    val = s.stack_mean
    count = s.stack_count
    std = s.stack_std
    trend = s.stack_trend
    detrended_std = s.stack_detrended_std
    stack_type = 'dem'
    global filter_outliers
    filter_outliers = False 

    global pad
    global geoid_offset
    global plot_trend
    global plot_resid
    global errorbars

    if 'TSX' in source or 'ALOS' in source or 'RS1' in source or 'RS2' in source:
        stack_type = 'velocity' 

    if 'zs' in stack_fn:
        stack_type = 'racmo'

    if 'meltrate' in stack_fn:
        stack_type = 'meltrate'

    if stack_type == 'velocity':
        #pad = 3
        #Use this for Jak stack with RADARSAT data
        pad = 0
        ylabel = 'Velocity (m/yr)'
        ylabel_rel = 'Relative Velocity (m/yr)'
        ylabel_resid = 'Detrended Velocity (m/yr)'
        plot4_label = 'Detrended std (m/yr)'
        hs = None
        alpha = 1.0
        geoid_offset = False
        plot_trend = False
        plot_resid = False
        errorbars = False
        if 'RS' in source:
            filter_outliers = True
    elif stack_type == 'racmo':
        pad = 0
        ylabel = 'RACMOFDM zs (m)'
        ylabel_rel = 'Relative RACMOFDM zs (m)'
        ylabel_resid = 'Detrended RACMOFDM zs (m)'
        plot4_label = 'Detrended std (m)'
        hs = None
        alpha = 1.0
        geoid_offset = False
        plot_trend = True 
        plot_resid = True 
        errorbars = False
    elif stack_type == 'meltrate':
        pad = 3
        ylabel = 'Melt Rate (m/yr)'
        ylabel_rel = 'Relative Melt Rate (m/yr)'
        ylabel_resid = 'Detrended Melt Rate (m/yr)'
        plot4_label = 'Detrended std (m/yr)'
        hs = None
        alpha = 1.0
        geoid_offset = False
        plot_trend = True 
        plot_resid = False 
        errorbars = False
    else:
        #pad = 5
        #pad = 1
        pad = 3
        ylabel = 'Elevation (m EGM2008)'
        ylabel_rel = 'Relative Elevation (m)'
        ylabel_resid = 'Detrended Elevation (m)'
        #plot4_label = 'Detrended std (m)'
        plot4_label = 'Elevation std (m)'
        s.mean_hillshade()
        hs = s.stack_mean_hs
        hs_clim = malib.calcperc(hs, (2,98))
        alpha = 0.6
        geoid_offset = False 
        plot_trend = True
        plot_resid = True 
        errorbars = True

    #Set color cycle
    reset_colors()

    global ms
    ms = 5

    #fig = plt.figure(0, figsize=(14,12), facecolor='white')
    fig = plt.figure(0, figsize=(14,12))

    #These record all points plotted on the context plots
    global ax_pt_list
    ax_pt_list = [[], [], [], []]

    interp = 'none'
    #interp = 'bicubic'

    #Overlay on mean_hs
    #Add colorbars
    imshow_kwargs = {'interpolation':interp}

    val_clim = malib.calcperc(val, (2,98))
    ax0 = fig.add_subplot(221)
    if hs is not None:
        ax0.imshow(hs, cmap='gray', clim=hs_clim, **imshow_kwargs)
    im0 = ax0.imshow(val, cmap=cpt_rainbow, clim=val_clim, alpha=alpha, **imshow_kwargs)
    #This was used for Stanton et al figure
    #val_clim = (0, 50)
    #im0 = ax0.imshow(val, cmap=cmaps.inferno, clim=val_clim, alpha=alpha, **imshow_kwargs)
    ax0.set_adjustable('box-forced')
    pltlib.hide_ticks(ax0)
    pltlib.add_cbar(ax0, im0, ylabel)

    count_clim = malib.calcperc(count, (2,98))
    #count_clim = malib.calcperc(count, (4,100))
    ax1 = fig.add_subplot(222, sharex=ax0, sharey=ax0)
    if hs is not None:
        ax1.imshow(hs, cmap='gray', clim=hs_clim, **imshow_kwargs)
    im1 = ax1.imshow(count, cmap=cmaps.inferno, clim=count_clim, alpha=alpha, **imshow_kwargs)
    ax1.set_adjustable('box-forced')
    pltlib.hide_ticks(ax1)
    pltlib.add_cbar(ax1, im1, 'Count')

    #clim=(-20, 20)
    #trend_clim = malib.calcperc(trend, (1,99))
    #trend_clim = malib.calcperc(trend, (2,98))
    trend_clim = malib.calcperc(trend, (4,96))
    #trend_clim = malib.calcperc(trend, (10,90))
    max_abs_clim = max(np.abs(trend_clim))
    trend_clim = (-max_abs_clim, max_abs_clim)
    ax2 = fig.add_subplot(223, sharex=ax0, sharey=ax0)
    #ax0.set_title("Trend")
    if hs is not None:
        ax2.imshow(hs, cmap='gray', clim=hs_clim, **imshow_kwargs)
    im2 = ax2.imshow(trend, cmap='RdBu', clim=trend_clim, alpha=alpha, **imshow_kwargs)
    ax2.set_adjustable('box-forced')
    pltlib.hide_ticks(ax2)
    pltlib.add_cbar(ax2, im2, 'Linear Trend (m/yr)')

    dstd_clim = (0, malib.calcperc(std, (0,95))[1])
    #dstd_clim = (0, malib.calcperc(detrended_std, (0,98))[1])
    ax3 = fig.add_subplot(224, sharex=ax0, sharey=ax0)
    if hs is not None:
        ax3.imshow(hs, cmap='gray', clim=hs_clim, **imshow_kwargs)
    im3 = ax3.imshow(detrended_std, cmap=cpt_rainbow, clim=dstd_clim, alpha=alpha, **imshow_kwargs)
    #im3 = ax3.imshow(std, cmap=cpt_rainbow, clim=dstd_clim, alpha=alpha, **imshow_kwargs)
    ax3.set_adjustable('box-forced')
    pltlib.hide_ticks(ax3)
    #pltlib.add_cbar(ax3, im3, 'Detrended Std (m)')
    pltlib.add_cbar(ax3, im3, plot4_label)

    global ax_list
    ax_list = [ax0, ax1, ax2, ax3]

    plt.autoscale(tight=True)
    plt.tight_layout()

    cid = fig.canvas.mpl_connect('button_press_event', onclick)

    fig1 = plt.figure(1)
    global ax_rel
    ax_rel = fig1.add_subplot(111)
    fmt_ax(ax_rel, ylabel=ylabel_rel, legend_source=source)

    fig2 = plt.figure(2)
    global ax_abs
    ax_abs = fig2.add_subplot(111)
    fmt_ax(ax_abs, ylabel=ylabel, legend_source=source)

    fig3 = plt.figure(3)
    global ax_resid
    ax_resid = fig3.add_subplot(111)
    fmt_ax(ax_resid, ylabel=ylabel_resid, legend_source=source)
    plt.axhline(0, color='k', linestyle='-', linewidth=0.6)

    """
    #print "Saving figure"
    #fig_fn = os.path.splitext(s.stack_fn)[0] + '_context_maps.pdf'
    fig_fn = os.path.splitext(s.stack_fn)[0] + '_context_maps.png'
    plt.figure(0)
    plt.tight_layout()
    plt.savefig(fig_fn, dpi=300)

    fig_fn = os.path.splitext(s.stack_fn)[0] + '.png'
    plt.figure(2)
    #plt.ylim(70, 350)
    plt.tight_layout()
    plt.savefig(fig_fn, dpi=300)
    """

    plt.show()
コード例 #17
0
ax.set_xlabel('Snow Depth Diff., Pit - DEM (m)')
ax.set_ylabel('Count')
fig_fn = 'snowex_gm_snowdepth_diff_hist.pdf'
f.savefig(fig_fn, dpi=300, bbox_inches='tight')

#Map plot of snow depth
f, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_facecolor('k')
ax.imshow(hs, cmap='gray')
#clim = malib.calcperc(depth, (2,98))
clim = (0, 2)
im = ax.imshow(snowdepth, cmap='inferno', clim=clim, alpha=0.7)
ax.set_ylim(dem.shape[0], 0)
ax.set_xlim(0, dem.shape[1])
pltlib.add_cbar(ax, im, label='Snow Depth (m)')
pltlib.add_scalebar(ax, geolib.get_res(dem_ds)[0])
pltlib.hide_ticks(ax)
fig_fn = 'snowex_gm_snowdepth.png'
f.savefig(fig_fn, dpi=300, bbox_inches='tight')
#Now overlay pits
sc = ax.scatter(x,
                y,
                s=s,
                c=depth,
                cmap='inferno',
                vmin=clim[0],
                vmax=clim[1],
                edgecolors='k')
fig_fn = 'snowex_gm_snowdepth_pitoverlay.png'
f.savefig(fig_fn, dpi=300, bbox_inches='tight')
コード例 #18
0
def bma_fig(fig, bma, cmap='cpt_rainbow', clim=None, clim_perc=(2,98), bg=None, bg_perc=(2,98), n_subplt=1, subplt=1, label=None, title=None, cint=None, alpha=0.5, ticks=False, scalebar=None, ds=None, shp=None, imshow_kwargs={'interpolation':'nearest'}, cbar_kwargs={'extend':'both', 'orientation':'vertical', 'shrink':0.7, 'fraction':0.12, 'pad':0.02}, **kwargs):
    #We don't use the kwargs, just there to save parsing in main
    
    if clim is None:
        clim = malib.calcperc(bma, clim_perc)
        #Deal with masked cases
        if clim[0] == clim[1]:
            if clim[0] > bma.fill_value:
                clim = (bma.fill_value, clim[0])
            else:
                clim = (clim[0], bma.fill_value)
        print "Colorbar limits (%0.1f-%0.1f%%): %0.3f %0.3f" % (clim_perc[0], clim_perc[1], clim[0], clim[1])
    else:
        print "Colorbar limits: %0.3f %0.3f" % (clim[0], clim[1])

    #Link all subplots for zoom/pan
    sharex = sharey = None
    if len(fig.get_axes()) > 0:
        sharex = sharey = fig.get_axes()[0]

    #Hack to catch situations with only 1 subplot, but a subplot number > 1
    if n_subplt == 1:
        subplt = 1

    #One row, multiple columns
    ax = fig.add_subplot(1, n_subplt, subplt, sharex=sharex, sharey=sharey)
    #This occupies the full figure
    #ax = fig.add_axes([0., 0., 1., 1., ])

    #ax.patch.set_facecolor('black')
    ax.patch.set_facecolor('white')

    cmap_name = cmap
    cmap = plt.get_cmap(cmap_name)
    if 'inferno' in cmap_name:
        #Use a gray background
        cmap.set_bad('0.5', alpha=1)
    else:
        #This sets the nodata background to opaque black
        cmap.set_bad('k', alpha=1)
        #cmap.set_bad('w', alpha=1)

    #ax.set_title("Band %i" % subplt, fontsize=10)
    if title is not None:
        ax.set_title(title)

    #If a background image is provided, plot it first
    if bg is not None:
        #Note, 1 is opaque, 0 completely transparent
        #alpha = 0.6
        #bg_perc = (4,96)
        bg_perc = (0.05, 99.95)
        #bg_perc = (1, 99)
        bg_alpha = 1.0
        #bg_alpha = 0.5 
        bg_clim = malib.calcperc(bg, bg_perc)
        bg_cmap_name = 'gray'
        bg_cmap = plt.get_cmap(bg_cmap_name)
        if 'inferno' in cmap_name:
            bg_cmap.set_bad('0.5', alpha=1)
        else:
            bg_cmap.set_bad('k', alpha=1)
        #Set the overlay bad values to completely transparent, otherwise darkens the bg
        cmap.set_bad(alpha=0)
        bgplot = ax.imshow(bg, cmap=bg_cmap, clim=bg_clim, alpha=bg_alpha)
        imgplot = ax.imshow(bma, alpha=alpha, cmap=cmap, clim=clim, **imshow_kwargs)
    else:
        imgplot = ax.imshow(bma, cmap=cmap, clim=clim, **imshow_kwargs)
 
    gt = None
    if ds is not None:
        gt = np.array(ds.GetGeoTransform())
        gt_scale_factor = min(np.array([ds.RasterYSize, ds.RasterXSize])/np.array(bma.shape,dtype=float))
        gt[1] *= gt_scale_factor
        gt[5] *= gt_scale_factor
        ds_srs = geolib.get_ds_srs(ds)
        if ticks:
            scale_ticks(ax, ds)
        else:
            pltlib.hide_ticks(ax)
        xres = geolib.get_res(ds)[0]
    else:
        pltlib.hide_ticks(ax)
    #This forces the black line outlining the image subplot to snap to the actual image dimensions
    ax.set_adjustable('box-forced')

    cbar = True 
    if cbar:
        #Had to turn off the ax=ax for overlay to work
        #cbar = fig.colorbar(imgplot, ax=ax, extend='both', shrink=0.5) 
        #Should set the format based on dtype of input data 
        #cbar_kwargs['format'] = '%i'
        #cbar_kwargs['format'] = '%0.1f'
        #cbar_kwargs['orientation'] = 'horizontal'
        #cbar_kwargs['shrink'] = 0.8

        cbar = pltlib.add_cbar(ax, imgplot, label=label, cbar_kwargs=cbar_kwargs)
   
    #Plot contours every cint interval and update colorbar appropriately
    if cint is not None:
        if bma_c is not None:
            bma_clim = malib.calcperc(bma_c)
            #PIG bed ridge contours
            #bma_clim = (-1300, -300)
            #Jak front shear margin contours
            #bma_clim = (2000, 4000)
            cstart = int(np.floor(bma_clim[0] / cint)) * cint 
            cend = int(np.ceil(bma_clim[1] / cint)) * cint
        else:
            #cstart = int(np.floor(bma.min() / cint)) * cint 
            #cend = int(np.ceil(bma.max() / cint)) * cint
            cstart = int(np.floor(clim[0] / cint)) * cint 
            cend = int(np.ceil(clim[1] / cint)) * cint

        #Turn off dashed negative (beds are below sea level)
        #matplotlib.rcParams['contour.negative_linestyle'] = 'solid'

        clvl = np.arange(cstart, cend+1, cint)
        #contours = ax.contour(bma_c, colors='k', levels=clvl, alpha=0.5)
        contours = ax.contour(bma_c, cmap='gray', linestyle='--', levels=clvl, alpha=1.0)

        #Update the cbar with contour locations
        cbar.add_lines(contours)
        cbar.set_ticks(contours.levels)

    #Plot shape overlay, moved code to pltlib
    if shp is not None:
        pltlib.shp_overlay(ax, ds, shp, gt=gt)

    if scalebar:
        scale_ticks(ax, ds)
        pltlib.add_scalebar(ax, xres)
        if not ticks:
            pltlib.hide_ticks(ax)

    #imgplot.set_cmap(cmap)
    #imgplot.set_clim(clim)
  
    global gbma
    gbma = bma
    global ggt
    ggt = gt

    #Clicking on a subplot will make it active for z-coordinate display
    fig.canvas.mpl_connect('button_press_event', onclick)
    fig.canvas.mpl_connect('axes_enter_event', enter_axis)
    
    #Add support for interactive z-value display 
    ax.format_coord = format_coord
コード例 #19
0
def map_plot(gf, z_bin_edges, outdir, hs=True):
    #print("Generating map plot")
    f, axa = plt.subplots(1, 3, figsize=(10, 7.5))
    #f.suptitle(gf.feat_fn)
    alpha = 1.0
    if hs:
        #z1_hs = geolib.gdaldem_wrapper(gf.out_z1_fn, product='hs', returnma=True, verbose=False)
        #z2_hs = geolib.gdaldem_wrapper(gf.out_z2_fn, product='hs', returnma=True, verbose=False)
        z1_hs = gf.z1_hs
        z2_hs = gf.z2_hs
        hs_clim = malib.calcperc(z2_hs, (2, 98))
        z1_hs_im = axa[0].imshow(z1_hs,
                                 cmap='gray',
                                 vmin=hs_clim[0],
                                 vmax=hs_clim[1])
        z2_hs_im = axa[1].imshow(z2_hs,
                                 cmap='gray',
                                 vmin=hs_clim[0],
                                 vmax=hs_clim[1])
        alpha = 0.5
    z1_im = axa[0].imshow(gf.z1,
                          cmap='cpt_rainbow',
                          vmin=z_bin_edges[0],
                          vmax=z_bin_edges[-1],
                          alpha=alpha)
    z2_im = axa[1].imshow(gf.z2,
                          cmap='cpt_rainbow',
                          vmin=z_bin_edges[0],
                          vmax=z_bin_edges[-1],
                          alpha=alpha)
    axa[0].contour(gf.z1, [
        gf.z1_ela,
    ],
                   linewidths=0.5,
                   linestyles=':',
                   colors='w')
    axa[1].contour(gf.z2, [
        gf.z2_ela,
    ],
                   linewidths=0.5,
                   linestyles=':',
                   colors='w')
    #t1_title = int(np.round(gf.t1))
    #t2_title = int(np.round(gf.t2))
    t1_title = '%0.2f' % gf.t1
    t2_title = '%0.2f' % gf.t2
    #t1_title = gf.t1.strftime('%Y-%m-%d')
    #t2_title = gf.t2.strftime('%Y-%m-%d')
    axa[0].set_title(t1_title)
    axa[1].set_title(t2_title)
    axa[2].set_title('%s to %s (%0.2f yr)' % (t1_title, t2_title, gf.dt))
    #dz_clim = (-10, 10)
    dz_clim = (-2.0, 2.0)
    dz_im = axa[2].imshow(gf.dhdt,
                          cmap='RdBu',
                          vmin=dz_clim[0],
                          vmax=dz_clim[1])
    for ax in axa:
        pltlib.hide_ticks(ax)
        ax.set_facecolor('k')
    sb_loc = pltlib.best_scalebar_location(gf.z1)
    pltlib.add_scalebar(axa[0], gf.res[0], location=sb_loc)
    pltlib.add_cbar(axa[0], z1_im, label='Elevation (m WGS84)')
    pltlib.add_cbar(axa[1], z2_im, label='Elevation (m WGS84)')
    pltlib.add_cbar(axa[2], dz_im, label='dh/dt (m/yr)')
    plt.tight_layout()
    #Make room for suptitle
    #plt.subplots_adjust(top=0.90)
    #print("Saving map plot")
    fig_fn = os.path.join(outdir, gf.feat_fn + '_mb_map.png')
    plt.savefig(fig_fn, bbox_inches='tight', dpi=300)
    plt.close(f)