コード例 #1
0
def plot_with_landsat(landsatfile, rasterfile, title):
    raster = ice.Raster(rasterfile=rasterfile)

    # Load Landsat 8 raster with same projection window
    e = raster.hdr.extent
    proj_win = [e[0], e[3], e[1], e[2]]
    landsat = ice.Raster(rasterfile=landsatfile, projWin=proj_win)
    fig, axs = plt.subplots(figsize=(9, 9))

    data = np.flip(raster.data, axis=0)
    axs.imshow(landsat.data, extent=e, cmap='binary_r')
    axs.imshow(data, extent=e, cmap='Spectral_r', alpha=0.6)
    plt.title(title)
    plt.show()
コード例 #2
0
def main(args):

    # Load the stack
    stack = ice.Stack(args.stackfile)

    # Check if requested dataset is 2D. If so, view it directly
    if stack[args.key].ndim == 2:
        mean = stack[args.key][()]
    # Otherwise, compute mean
    else:
        mean = stack.mean(key=args.key)

    # Load reference SAR image
    if args.ref is not None:
        sar = ice.Raster(rasterfile=args.ref)
        if sar.hdr != stack.hdr:
            sar.resample(stack.hdr)
        db = 10.0 * np.log10(sar.data)
        low = np.percentile(db.ravel(), 5)
        high = np.percentile(db.ravel(), 99.9)
    else:
        db = None

    fig, ax = plt.subplots()
    vmin, vmax = args.clim
    cmap = ice.get_cmap(args.cmap)
    if db is not None:
        ref = ax.imshow(db,
                        aspect='auto',
                        cmap='gray',
                        vmin=low,
                        vmax=high,
                        extent=stack.hdr.extent)
    im = ax.imshow(mean,
                   aspect='auto',
                   vmin=vmin,
                   vmax=vmax,
                   cmap=cmap,
                   extent=stack.hdr.extent,
                   alpha=args.alpha)
    cbar = plt.colorbar(im, ax=ax, pad=0.02)
    cbar.set_label(args.key)

    plt.show()

    if args.save is not None:
        out = ice.Raster(data=mean, hdr=stack.hdr)
        out.write_gdal(args.save, epsg=args.save_epsg)
コード例 #3
0
def main():
    stack_path = './data/stacks/'
    stack_fname = 'enderlin_velocity_stack.hdf5'
    pre_transect_fname = 'landsat_pre_transect.npy'
    transect_fname = 'landsat_transect.npy'
    landsat8_raster_fname = './data/landsat8/LC08_L1TP_066017_20140224_20170306_01_T1/LC08_L1TP_066017_20140224_20170306_01_T1_B1.TIF'

    # Load Stack header
    hdr = ice.RasterInfo(stackfile=stack_path + stack_fname)

    # Load landsat 8 Raster
    e = hdr.extent
    proj_win = [e[0], e[3], e[1], e[2]]
    raster = ice.Raster(rasterfile=landsat8_raster_fname, projWin=proj_win)

    # Get reference x, y points to extract centerline from
    x, y = get_transect_points(raster, stack_path + pre_transect_fname)

    # plot_transect(raster, x, y, 'Hand crafted transect')
    xs, ys = resample_transect(raster, x, y)

    # Save transect
    s = 100  # Start point along transect (bottom is very noisy)
    np.save(stack_path + transect_fname, [xs[s:], ys[s:]])
    plot_transect(raster, xs[s:], ys[s:],
                  'Columbia Manual Centerline Transect')
コード例 #4
0
def get_stack_data(path, rasterfiles, proj_win):
    """ Load data from each raster into a single array indexed by slice.

    Parameters
    ----------
    path: str
        Path to directory storing all rasters
    rasterfiles: list
        List with the name of each rasterfile in the path
    proj_win: list
        Projection window to load raster data with
    
    Return
    ------
    stack_data: array_like
        Dataset that will back the Stack, stored as (nrasters, nrows, ncols)
    """
    data = []

    for rasterfile in tqdm(rasterfiles):
        raster = ice.Raster(path + rasterfile, projWin=proj_win)
        data.append(raster.data)
        '''
        d = np.array(raster.data)
        d[d < 0] = np.nan
        nan_mask = np.isnan(d)
        d[nan_mask] = np.nanmedian(d)
        smooth_data = sgolay2d(d, 51, 3)
        smooth_data[nan_mask] = np.nan
        data.append(smooth_data)
        '''

    return np.array(data)
コード例 #5
0
def plot_transect_landsat(mean, hdr, tx, ty):
    # Plot centerline transect with data mean
    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(9, 7))
    axs[0].imshow(mean, clim=[0, 10], cmap='Spectral_r', extent=hdr.extent)
    axs[0].plot(tx, ty)
    axs[0].set_title("Discovered Centerline Transect")
    axs[0].set_ylabel("Y (m)")
    axs[0].set_xlabel("X (m)")

    # Plot centerrline transect with landsat 8
    landsat8_raster_path = './data/landsat8/LC08_L1TP_066017_20140224_20170306_01_T1/LC08_L1TP_066017_20140224_20170306_01_T1_B1.TIF'
    e = hdr.extent
    proj_win = [e[0], e[3], e[1], e[2]]
    raster = ice.Raster(rasterfile=landsat8_raster_path, projWin=proj_win)
    axs[1].imshow(raster.data, cmap='binary_r', extent=raster.hdr.extent)
    axs[1].plot(tx, ty)
    axs[1].set_ylabel("Y (m)")
    axs[1].set_xlabel("X (m)")
    axs[1].set_title('Landsat 8 View')

    fig.set_tight_layout(True)

    # Save to figures file
    Path("./figures").mkdir(parents=True, exist_ok=True)
    plt.savefig("./figures/columbia_transect_landsat.jpg", dpi=300)
コード例 #6
0
def plot_mean_with_landsat(landsatfile,
                           stackfile,
                           title,
                           lb=float('-inf'),
                           clim=None,
                           fname=None):
    stack = ice.Stack(stackfile, mode='r')

    # Load Landsat 8 raster with same projection window
    e = stack.hdr.extent
    proj_win = [e[0], e[3], e[1], e[2]]
    landsat = ice.Raster(rasterfile=landsatfile, projWin=proj_win)
    fig, axs = plt.subplots(figsize=(9, 9))

    data = np.array(stack._datasets["data"][0])
    # data = np.nanmedian(data, axis=0)

    data[data < lb] = np.nan
    axs.imshow(landsat.data, extent=e, cmap='binary_r')
    im = axs.imshow(data, extent=e, cmap='Spectral_r', alpha=0.6, clim=clim)
    plt.title(title)
    fig.colorbar(im)
    if fname is not None:
        plt.savefig('./figures/' + fname, dpi=300)
    plt.show()

    stack.fid.close()
コード例 #7
0
def create_stack(data_root, dtype, stackfile):
    """ Create Stacks for the different data """
    hdr = None

    # Set a lower bound for valid data
    lb = 0
    if dtype in ['vx', 'vy']:
        lb = -1e6  # m/yr

    # Get dates and data from files
    data_dict = {}
    init_tdec, init_data = [], []
    for root, _, files in os.walk(data_root):
        if len(files) == 0:
            continue

        # Get filename (not very elegant, but it works)
        rasterfile = ''
        for f in files:
            if dtype + '.tif' in f:
                rasterfile = f

        # Get date
        date = root.split('/')[-1]
        tdec = ice.datestr2tdec(yy=int(date[0:4]),
                                mm=int(date[4:6]),
                                dd=int(date[6:8]))
        init_tdec.append(tdec)

        # Get data
        e = [-3130000, -3090000, 642500, 680000]
        proj_win = [e[0], e[3], e[1], e[2]]
        raster = ice.Raster(rasterfile=root + '/' + rasterfile,
                            projWin=proj_win)

        # Apply data bounds
        data = np.array(raster.data)
        data[data < lb] = np.nan

        # Store in dict so that data can be sorted by date before adding to Stack
        data_dict[tdec] = data

        # Store the first header
        if hdr is None:
            hdr = raster.hdr

    # Sort data before adding to Stack
    init_tdec.sort()
    for t in init_tdec:
        init_data.append(data_dict[t])

    # Create Stack
    stack = ice.Stack(stackfile,
                      mode='w',
                      init_tdec=np.array(init_tdec),
                      init_rasterinfo=hdr)
    stack.fid.create_dataset("data", data=np.array(init_data))
    stack.fid.close()
コード例 #8
0
def create_stack(data_path,
                 data_table,
                 stack_path,
                 stack_fname,
                 proj_win=None):
    """ Build a new Stack from a set of raster files.

    Parameters
    ----------
    data_path: str
        path to directory storing all downloaded rasters to build Stack from
    data_table: dict
        table of information for each Raster
    stack_path: str
        path to directory that Stack will be saved to
    stack_fname: str
        Stackfile name
    proj_win: list
        Projection window to load each Raster in the Stack with. If None, the
        optimal projection window will be calculated

    Return
    ------
    The projection window used for the Stack
    """
    Path(stack_path).mkdir(parents=True, exist_ok=True)

    # Calculate largest common extent between rasers
    if proj_win is None:
        proj_win = calc_optimal_proj_win(data_path, data_table['fname'])

    print('Using projection window', proj_win)

    # Convert date strings to tdec
    dates = 'start_date' if 'start_date' in data_table.keys() else 'date'
    init_tdec = dates_to_tdec(data_table[dates])

    # Get stack data
    print('Loading Stack data:')
    init_data = get_stack_data(data_path, data_table['fname'], proj_win)
    # Smooth data
    print('Loaded data has shape,', init_data.shape)

    # Get header to init stack with
    init_rasterinfo = ice.Raster(data_path + data_table['fname'][0],
                                 projWin=proj_win).hdr

    # Initialize stack
    print('Initializing Stack and saving to', stack_path + stack_fname)
    stack = ice.Stack(stack_path + stack_fname,
                      mode='w',
                      init_tdec=init_tdec,
                      init_rasterinfo=init_rasterinfo)
    stack.fid.create_dataset("data", data=init_data)
    stack.fid.close()

    return proj_win
コード例 #9
0
ファイル: utils.py プロジェクト: maxfilter/columbia
def resample_landsat():
    landsatfile = './data/landsat8/LC08_L1TP_066017_20140224_20170306_01_T1/LC08_L1TP_066017_20140224_20170306_01_T1_B1.TIF'
    landsat = ice.Raster(rasterfile=landsatfile)

    # Resample raster to new projection
    landsat_out = ice.warp(landsat, target_epsg=3413, n_proc=5, cval=np.nan)

    # Replace with raster of new projection
    landsat_out.write_gdal('./data/landsat8/landsat8_3413.tif', epsg=3413)
コード例 #10
0
def get_stack_mean(stack_path, stack_fname, mean_fname):
    """ Calculate the mean for a Stack and save to a file to save 
    future computational time.

    Parameters
    ----------
    stack_path: str
        Path to directory containing stackfile
    stack_fname: 
        Name of stackfile
    mean_fname:
        Name to save derived mean as

    Returns
    -------
    mean: array_like
        The loaded Stack mean
    """
    if os.path.exists(stack_path + mean_fname):
        # Load mean file if found
        print('Loading mean from file')
        raster = ice.Raster(rasterfile=stack_path + mean_fname)
        return raster.data, raster.hdr
    else:
        # Calculate mean from Stack and save to file
        print('Calculating mean from Stack')
        stack = ice.Stack(stack_path + stack_fname)
        data = np.array(stack._datasets['data'])
        data[data < 0] = np.nan
        mean = np.nanmean(data, axis=0)
        # Save to file for next time
        print('Saving mean to Raster for next time')
        raster = ice.Raster(data=mean, hdr=stack.hdr)
        raster.write_gdal(stack_path + mean_fname)

        stack.fid.close()

        return mean, raster.hdr
コード例 #11
0
def main(args):

    # Load the raster
    r = ice.Raster(args.rasterfile, band=args.band)

    fig, ax = plt.subplots()
    vmin, vmax = args.clim
    cmap = ice.get_cmap(args.cmap)
    im = ax.imshow(r.data, aspect='auto', vmin=vmin, vmax=vmax, cmap=cmap,
                   extent=args.xscale*r.hdr.extent)
    cbar = plt.colorbar(im, ax=ax, pad=0.02)

    plt.tight_layout()
    plt.show()
コード例 #12
0
def main(args):

    # Load the raster/stack and reference raster/stack
    if args.raster.endswith('.h5'):

        # Input stack
        inobj = ice.Stack(args.raster)

        # Reference stack
        ref = ice.Stack(args.reference)

        # Initialize output stack
        outobj = ice.Stack(args.output, mode='w')
        outobj.initialize(inobj.tdec, ref.hdr, data=False, weights=False)

        # Loop over keys to resample
        for key in args.keys:
            print('Resampling', key)
            inobj.resample(ref.hdr, outobj, key=key)

    else:

        # Input raster
        inobj = ice.Raster(rasterfile=args.raster)

        # Reference raster
        ref = ice.Raster(rasterfile=args.reference)

        # Resample
        inobj.resample(ref.hdr)

        # Write to disk
        if args.epsg is not None:
            out_epsg = args.epsg
        else:
            out_epsg = ref.hdr.epsg
        inobj.write_gdal(args.output, epsg=out_epsg, driver=args.driver)
コード例 #13
0
def plot_stream(vx_stackfile, vy_stackfile, v_stackfile, landsatfile, idx=0):
    vx_stack = ice.Stack(vx_stackfile)
    vy_stack = ice.Stack(vy_stackfile)
    v_stack = ice.Stack(v_stackfile)

    e = vx_stack.hdr.extent
    proj_win = [e[0], e[3], e[1], e[2]]
    landsat = ice.Raster(rasterfile=landsatfile, projWin=proj_win)

    v = np.array(v_stack._datasets['data'][idx])
    vx = np.array(vx_stack._datasets['data'][idx])
    vy = np.array(vy_stack._datasets['data'][idx])
    dx = vx_stack.hdr.dx
    dy = vy_stack.hdr.dy

    x0, x1, y0, y1 = vx_stack.hdr.extent

    x = np.arange(x0, x0 + len(vx[0]) * dx, dx)
    y = np.arange(y1, y1 + len(vx) * dy, dy)

    fig, axs = plt.subplots(figsize=(9, 9))
    axs.imshow(landsat.data, extent=e, cmap='binary_r')
    axs.set_ylim(bottom=y0, top=y1)
    axs.set_xlim(left=x0, right=x1)

    cmap = copy.copy(cm.get_cmap('plasma'))
    cmap.set_under(alpha=0)

    stream = axs.streamplot(x,
                            y,
                            vx,
                            vy,
                            color=v,
                            cmap=cmap,
                            norm=Normalize(vmin=150, vmax=5000),
                            density=5)

    fig.colorbar(stream.lines, fraction=0.046, pad=0.04)

    plt.tight_layout()
    plt.savefig('./figures/streamline.jpg', dpi=300)

    plt.show()
コード例 #14
0
def main():
    enderlin_data_path = './data/enderlin2018/'
    stack_path = './data/stacks/'
    stack_fname = 'enderlin_velocity_stack.hdf5'
    landsat8_raster_fname = './data/landsat8/LC08_L1TP_066017_20140224_20170306_01_T1/LC08_L1TP_066017_20140224_20170306_01_T1_B1.TIF'

    # Get all raster files from manifest
    rasterfiles = []
    with open(enderlin_data_path + 'MANIFEST.TXT', 'r') as f:
        for line in f:
            rasterfiles.append(line.split()[0])

    # plot_with_landsat(landsat8_raster_fname, enderlin_data_path + rasterfiles[0], 'Enderlin velocity on Landsat 8 Image')

    # Get dates and data from files
    init_tdec, init_data = [], []
    for rasterfile in rasterfiles:
        # Parse date from filename
        date = rasterfile.split('_')[1]
        tdec = ice.datestr2tdec(yy=int(date[0:4]),
                                mm=int(date[4:6]),
                                dd=int(date[6:8]))
        init_tdec.append(tdec)
        # Load file and get data
        raster = ice.Raster(rasterfile=enderlin_data_path + rasterfile)
        print(raster.hdr.extent)
        # Data is flipped - not sure why
        init_data.append(np.flip(raster.data, axis=0))

    # Create Stack
    hdr = ice.RasterInfo(rasterfile=enderlin_data_path + rasterfiles[0])
    stack = ice.Stack(stack_path + stack_fname,
                      mode='w',
                      init_tdec=np.array(init_tdec),
                      init_rasterinfo=hdr)
    stack.fid.create_dataset("data", data=np.array(init_data))
    stack.fid.close()

    plot_mean_with_landsat(landsat8_raster_fname, stack_path + stack_fname,
                           "Mean Velocity")
コード例 #15
0
def main(args):
    # Load Stack
    stack = ice.Stack(args.stackfile)

    # Load reference SAR image
    if args.ref is not None:
        sar = ice.Raster(rasterfile=args.ref)
        if sar.hdr != stack.hdr:
            sar.resample(stack.hdr)
        db = 10.0 * np.log10(sar.data)
        low = np.percentile(db.ravel(), 5)
        high = np.percentile(db.ravel(), 99.9)
    else:
        db = None

    # Set up animation
    fig, ax = plt.subplots(figsize=args.figsize)
    data = stack._datasets[args.key]
    cmap = ice.get_cmap(args.cmap)

    # Add ref image if using
    if db is not None:
        ax.imshow(db,
                  aspect='auto',
                  cmap='gray',
                  vmin=low,
                  vmax=high,
                  extent=stack.hdr.extent)

    # Extract reference frame if index provided
    if args.rel_index is not None:
        data_ref = data[args.rel_index]
    else:
        data_ref = 0.0

    im = ax.imshow(data[0] - data_ref,
                   extent=stack.hdr.extent,
                   cmap=cmap,
                   clim=args.clim,
                   alpha=args.alpha)

    # Create title
    datestr = ice.tdec2datestr(stack.tdec[0])
    tx = ax.set_title(args.title + ' ' + datestr, fontweight='bold')

    ax.set_xlabel(args.xlabel)
    ax.set_ylabel(args.ylabel)

    # Add colorbar
    div = make_axes_locatable(ax)
    cax = div.append_axes('right', '5%', '5%')
    cb = fig.colorbar(im, cax=cax)
    cb.set_label(args.clabel)

    # Update the frame
    def animate(i):
        im.set_data(data[i] - data_ref)
        datestr = ice.tdec2datestr(stack.tdec[i])
        tx.set_text(args.title + ' ' + datestr)

    fig.set_tight_layout(True)

    print('Generating animation and saving to', args.save)
    interval = 1000 / args.fps  # Convert fps to interval in milliseconds
    anim = animation.FuncAnimation(fig,
                                   animate,
                                   interval=interval,
                                   frames=len(data),
                                   repeat=True)
    anim.save(args.save, dpi=args.dpi)

    if args.show:
        plt.show()
コード例 #16
0
def main(args):

    # Load the stack
    stack = ice.Stack(args.stackfile)

    # Get frame
    if args.frame == 'initial':
        mean = stack.slice(0, key=args.key)
    elif args.frame == 'final':
        mean = stack.slice(stack.Nt - 1, key=args.key)
    elif args.frame == 'mean':
        mean = stack.mean(key=args.key)
    elif args.frame == 'std':
        mean = stack.std(key=args.key)
    else:
        raise ValueError('Unsupported frame type.')

    # If model directory is given, load model stack (full fit)
    mstack = None
    if args.mfile is not None:
        mstack = ice.Stack(args.mfile)

    # Load reference SAR image
    if args.ref is not None:
        sar = ice.Raster(rasterfile=args.ref)
        if sar.hdr != stack.hdr:
            sar.resample(stack.hdr)
        db = 10.0 * np.log10(sar.data.astype(np.float32))
        low = np.percentile(db.ravel(), 5)
        high = np.percentile(db.ravel(), 99.9)
    else:
        db = None

    # Initialize image plot
    fig, ax = plt.subplots()
    vmin, vmax = args.clim
    cmap = ice.get_cmap(args.cmap)
    if db is not None:
        ref = ax.imshow(db,
                        aspect='auto',
                        cmap='gray',
                        vmin=low,
                        vmax=high,
                        extent=stack.hdr.extent)
    im = ax.imshow(mean,
                   aspect='auto',
                   vmin=vmin,
                   vmax=vmax,
                   cmap=cmap,
                   extent=stack.hdr.extent,
                   alpha=args.alpha)
    cbar = plt.colorbar(im, ax=ax, pad=0.02)
    cbar.set_label(args.key)

    # Initialize plot for time series for a given pixel
    pts, axts = plt.subplots(figsize=(10, 6))

    # Define action for clicking on deformation map
    def printcoords(event):

        if event.inaxes != ax:
            return

        # Get cursor coordinates
        y, x = event.ydata, event.xdata

        # Print out pixel locaton
        i, j = stack.hdr.xy_to_imagecoord(x, y)
        print('Row: %d Col: %d' % (i, j))

        # Get time series for cursor location
        d = stack.timeseries(xy=(x, y), key=args.key)

        # Plot data and fit
        axts.clear()
        if args.sigma:
            w = stack.timeseries(xy=(x, y), key='weights')
            sigma = 1.0 / w
            axts.errorbar(stack.tdec, d, yerr=sigma, fmt='o')
        else:
            axts.plot(stack.tdec, d, 'o')

        if mstack is not None:
            fit = mstack.timeseries(xy=(x, y), key=args.mkey)
            axts.plot(mstack.tdec, fit, 'o')

        axts.set_xlabel('Year')
        axts.set_ylabel('Velocity')

        pts.canvas.draw()

    cid = fig.canvas.mpl_connect('button_press_event', printcoords)
    plt.show()
    fig.canvas.mpl_disconnect(cid)
コード例 #17
0
def main(args):

    # Load the raster
    raster = ice.Raster(rasterfile=args.rasterfile)

    # Load reference SAR image
    if args.ref is not None:
        sar = ice.Raster(rasterfile=args.ref)
        if sar.hdr != raster.hdr:
            sar.resample(raster.hdr)
        db = 10.0 * np.log10(sar.data)
        low = np.percentile(db.ravel(), 5)
        high = np.percentile(db.ravel(), 99.9)
    else:
        db = None

    fig, ax = plt.subplots()
    vmin, vmax = args.clim
    cmap = ice.get_cmap(args.cmap)
    if db is not None:
        ref = ax.imshow(db, aspect='auto', cmap='gray', vmin=low, vmax=high,
                        extent=raster.hdr.extent)
    im = ax.imshow(raster.data, aspect='auto', vmin=vmin, vmax=vmax, cmap=cmap,
                   extent=raster.hdr.extent, alpha=args.alpha)

    # Define action for clicking on deformation map
    xpts = []; ypts = []
    def printcoords(event):

        global line

        if event.inaxes != ax:
            return

        # Get cursor coordinates if left-click
        if event.button == 1:
            y, x = event.ydata, event.xdata
            xpts.append(x)
            ypts.append(y)

            if line is None:
                line, = ax.plot(xpts, ypts, '-o')
            else:
                line.set_xdata(xpts)
                line.set_ydata(ypts)

        # Right-click clear the points
        elif event.button == 3:
            xpts.clear()
            ypts.clear()
            line.set_xdata(xpts)
            line.set_ydata(ypts)
           
        fig.canvas.draw() 

    cid = fig.canvas.mpl_connect('button_press_event', printcoords)
    plt.show()
    fig.canvas.mpl_disconnect(cid)

    # Save the points
    np.savetxt(args.output, np.column_stack((xpts, ypts)))