Ejemplo n.º 1
0
def combine_all_files(datestr, input_dirs, output_dir):
    print("\nCombining files for date %s" % datestr)

    xdata, ydata, zdata0 = netcdf_read_write.read_grd_xyz(input_dirs[0] + "/" +
                                                          datestr + ".grd")
    xdata, ydata, zdata1 = netcdf_read_write.read_grd_xyz(input_dirs[1] + "/" +
                                                          datestr + ".grd")
    zdata_total = np.zeros(np.shape(zdata0))

    for j in range(len(ydata)):
        if np.mod(j, 200) == 0:
            print(j)
        for k in range(len(xdata)):
            vector = [zdata0[j][k], zdata1[j][k]]
            # , zdata2[j][k], zdata3[j][k], zdata4[j][k], zdata5[j][k], zdata6[j][k] ];
            zdata_total[j][k] = np.sum(vector)
    output_file = output_dir + "/" + datestr + ".grd"
    output_plot = output_dir + "/" + datestr + ".png"
    netcdf_read_write.produce_output_netcdf(xdata, ydata, zdata_total, "mm",
                                            output_file)
    netcdf_read_write.produce_output_plot(output_file,
                                          datestr,
                                          output_plot,
                                          "mm",
                                          aspect=1.0,
                                          invert_yaxis=True,
                                          vmin=-50,
                                          vmax=100)
    return
Ejemplo n.º 2
0
def write_gmtsar2roipac_phase(input_directory, phasefile, phasefilt_file,
                              ampfile, outfilename, outfile_filt):
    """
	A function that reads phase and amplitude grids in GMT format and writes a Binary format file 
	with the real and imaginary components of the values. 
	"""

    # INPUTS
    [xdata, ydata, phase] = netcdf_read_write.read_grd_xyz(phasefile)
    [xdata, ydata, phasefilt] = netcdf_read_write.read_grd_xyz(phasefilt_file)
    [xdata, ydata, amp] = netcdf_read_write.read_grd_xyz(ampfile)
    width = len(xdata)
    length = len(ydata)

    # # FOR THE PHASE AND AMPLITUDE DATA, reformatting the data and making initial plot.
    phase_1d = np.reshape(phase, (np.size(phase), ))
    phasefilt_1d = np.reshape(phasefilt, (np.size(phasefilt), ))
    amp_1d = np.reshape(amp, (np.size(amp), ))

    amp_1d = [x * 1e12 for x in amp_1d]
    # Fixing the different GMTSAR definition of amplitude.

    # # WRITE THEM OUT IN BINARY FORMAT.
    print("converting phase_1d to real,imag.")
    [real, imag] = phase_amp2real_imag(phase_1d, amp_1d)
    write_binary_roipac_real_imag(outfilename, real, imag)

    print("converting phase_1d_filt to real,imag.")
    [real, imag] = phase_amp2real_imag(phasefilt_1d, amp_1d)
    write_binary_roipac_real_imag(outfile_filt, real, imag)
    return [width, length]
Ejemplo n.º 3
0
def reader(filepathslist):
    """This function takes in a list of filepaths that each contain a 2d array of data, effectively taking
    in a cuboid of data. It splits and stores this data in a named tuple which is returned. This can then be used
    to extract key pieces of information."""
    filepaths = []
    dates_correct, date_deltas = [], []
    xvalues, yvalues, zvalues = [], [], []
    for i in range(len(filepathslist)):
        print(filepathslist[i])
        filepaths.append(filepathslist[i])
        datesplit = filepathslist[i].split('/')
        date_new = datesplit[-2].replace(datesplit[-2][0:7],
                                         str(int(datesplit[-2][0:7]) + 1))
        date_new = date_new.replace(date_new[8:15],
                                    str(int(date_new[8:15]) + 1))
        dates_correct.append(date_new)
        delta = abs(
            datetime.strptime(dates_correct[i][0:7], '%Y%j') -
            datetime.strptime(dates_correct[i][8:15], '%Y%j'))
        date_deltas.append(delta.days / 365.24)
        xdata, ydata, zdata = rwr.read_grd_xyz(filepathslist[i])
        xvalues = xdata
        yvalues = ydata
        zvalues.append(zdata)
        if i == round(len(filepathslist) / 2):
            print('halfway done reading files...')

    mydata = data(filepaths=np.array(filepaths),
                  dates_correct=np.array(dates_correct),
                  date_deltas=np.array(date_deltas),
                  xvalues=np.array(xvalues),
                  yvalues=np.array(yvalues),
                  zvalues=np.array(zvalues))

    return mydata
Ejemplo n.º 4
0
def make_referenced_unwrapped(rowref, colref, prior_staging_directory,
                              post_staging_directory):
    files = glob.glob(prior_staging_directory + "/*")
    print("Imposing reference pixel on %d files in %s; saving output in %s" %
          (len(files), prior_staging_directory, post_staging_directory))
    out_dir = post_staging_directory + "/"
    subprocess.call(['mkdir', '-p', out_dir], shell=False)

    for filename in files:
        individual_name = filename.split('/')[-1]
        print(individual_name)
        [xdata, ydata, zdata] = netcdf_read_write.read_grd_xyz(filename)
        # xdata is range/columns, ydata is azimuth/rows

        # Here we subtract the value of zdata[rowref][colref] to fix the refernece pixel.
        # referenced_zdata[rowref][colref]=0 by definition.
        referenced_zdata = np.zeros(np.shape(zdata))
        for i in range(len(ydata)):
            for j in range(len(xdata)):
                referenced_zdata[i][j] = zdata[i][j] - zdata[rowref][colref]
        print(referenced_zdata[rowref][colref])

        outname = out_dir + individual_name
        netcdf_read_write.produce_output_netcdf(xdata, ydata, referenced_zdata,
                                                'phase', outname)
        netcdf_read_write.flip_if_necessary(outname)
    return
Ejemplo n.º 5
0
def remove_plane(filename, planefile, outfile, m1, m2, m3):
    xvalues, yvalues, zvalues = rwr.read_grd_xyz(filename)
    i, j = 0, 0
    new_z = np.zeros((len(yvalues), len(xvalues)))
    for z in np.nditer(zvalues):
        new_z[i, j] = zvalues[i, j] - (m1 + m2 * xvalues[j] + m3 * yvalues[i])
        j += 1
        if j == len(xvalues):
            j = 0
            i += 1
            if i == len(yvalues):
                i = 0
    print(new_z[0, 0])
    new_z = np.flipud(new_z)
    print(new_z[0, 0])
    rwr.produce_output_netcdf(xvalues, yvalues, np.flipud(new_z),
                              'unwrapped phase', outfile)
    rwr.flip_if_necessary(outfile)
    outfile1 = outfile.replace("no_ramp.grd", "no_ramp_comparison.png")

    fr = netcdf.netcdf_file(outfile, 'r')
    xread = fr.variables['x']
    yread = fr.variables['y']
    zread = fr.variables['z']
    zread_copy = zread[:][:].copy()

    fr2 = netcdf.netcdf_file(filename, 'r')
    xread2 = fr2.variables['x']
    yread2 = fr2.variables['y']
    zread2 = fr2.variables['z']
    zread2_copy = zread2[:][:].copy()

    fig = plt.figure(figsize=(7, 10))
    ax1 = plt.subplot(121)
    old = ax1.imshow(zread2_copy, aspect=1.2)
    ax1.invert_xaxis()
    ax1.get_xaxis().set_ticks([])
    ax1.get_yaxis().set_ticks([])
    ax1.set_xlabel("Range", fontsize=16)
    ax1.set_ylabel("Azimuth", fontsize=16)

    ax2 = plt.subplot(122)
    new = ax2.imshow(zread_copy,
                     aspect=1.2,
                     vmin=np.nanmin(zread_copy),
                     vmax=np.nanmax(zread2_copy))
    ax2.invert_xaxis()
    ax2.get_xaxis().set_ticks([])
    ax2.get_yaxis().set_ticks([])
    ax2.set_xlabel("Range", fontsize=16)
    cb = plt.colorbar(new)
    cb.set_label('unwrapped phase', size=16)

    plt.savefig(outfile1)
    plt.close()

    rwr.produce_output_plot(outfile, 'Ramp removed',
                            outfile.replace('grd', 'png'), 'unwraped phase')

    return
def overlay_gps(netcdfname, smoothing, misfit):
    z = rwr.read_grd(netcdfname)
    ma = np.nanmax(z, axis=None, out=None)
    mi = np.nanmin(z, axis=None, out=None)

    xread, yread, zread = rwr.read_grd_xyz(netcdfname)

    # Make a plot
    fig = plt.figure(figsize=(7, 10))
    ax1 = fig.add_axes([0.0, 0.1, 0.9, 0.8])
    image = plt.imshow(
        zread,
        aspect=1.2,
        extent=[15.984871407, 21116.0151286, 12196, 4.4408920985 * (10**-16)],
        cmap='jet',
        vmin=-10,
        vmax=10)
    plt.gca().invert_xaxis()
    # plt.gca().invert_yaxis()
    plt.title('Reasonable WNSBAS without ramps - smoothing factor: ' +
              str(smoothing) + ' -  Misfit: ' + str(misfit))
    plt.gca().set_xlabel("Range", fontsize=16)
    plt.gca().set_ylabel("Azimuth", fontsize=16)
    scatter = plt.gca().scatter(xfinal,
                                yfinal,
                                c=velfinal,
                                marker='v',
                                s=100,
                                cmap='jet',
                                vmin=-10,
                                vmax=10)
    cb = plt.colorbar(image)
    cb.set_label('velocity in mm/yr', size=16)
    plt.show()
    return
Ejemplo n.º 7
0
def how_many_nans(filename):
    [xdata, ydata, zdata] = netcdf_read_write.read_grd_xyz(filename)
    nan_pixels = np.count_nonzero(np.isnan(zdata))
    total_pixels = np.shape(zdata)[0] * np.shape(zdata)[1]
    print("For file %s: %d pixels of %d are NaNs (%f percent)." %
          (filename, nan_pixels, total_pixels,
           100 * float(nan_pixels / float(total_pixels))))
    return [nan_pixels, total_pixels]
Ejemplo n.º 8
0
def plot_grid_file(filename, figname):
    [xdata, ydata, zdata] = netcdf_read_write.read_grd_xyz(filename)
    plt.figure()
    plt.imshow(zdata)
    plt.colorbar()
    plt.savefig(figname + '.eps')
    plt.close()
    return
Ejemplo n.º 9
0
def make_outlier_mask_for_stack(filelist, maskfile, outlier_cutoff=1e4):
    # Make a mask that is ones and nans
    # Given a co-registered stack
    # If a pixel is above the outlier cutoff in any image of the stack, make a nanmask that masks that pixel.
    x, y, z = netcdf_read_write.read_grd_xyz(
        filelist[1])  # just to get the shape of the outputs
    crazy_mask = np.ones(np.shape(z))
    for ifile in filelist:
        print(ifile)
        x, y, ztemp = netcdf_read_write.read_grd_xyz(ifile)
        for i in range(len(y)):
            for j in range(len(x)):
                if abs(ztemp[i][j]) > outlier_cutoff:
                    crazy_mask[i][j] = np.nan
    # Put all the crazy pixels into a mask (across all images in the stack).
    netcdf_read_write.produce_output_netcdf(x, y, crazy_mask, "", maskfile)
    return
Ejemplo n.º 10
0
def produce_min_max(filename, xyz=False):
    if xyz == False:
        x, y, z = netcdf_read_write.read_netcdf4_xyz(filename)
    else:
        x, y, z = netcdf_read_write.read_grd_xyz(filename)
    print("File:", filename)
    print("Max: ", np.nanmax(z))
    print("Min: ", np.nanmin(z))
    print("Shape: ", np.shape(z))
    return
Ejemplo n.º 11
0
def number_below_value(filename, value):
    [xdata, ydata, zdata] = netcdf_read_write.read_grd_xyz(filename)
    count = 0
    for i in range(len(ydata)):
        for j in range(len(xdata)):
            if zdata[i][j] < value:
                count = count + 1
    total_pixels = np.shape(zdata)[0] * np.shape(zdata)[1]
    print("For file %s: %d pixels of %d are below %f (%f percent)." %
          (filename, count, total_pixels, value,
           100 * float(count / float(total_pixels))))
    return
Ejemplo n.º 12
0
def write_gmtsar2roipac_topo(infile, out_topo):
    """
	A function that reads a GMT topographic grid and writes the corresponding .hgt format
	for use with roipac functions. 
	"""
    [xdata, ydata, topo] = netcdf_read_write.read_grd_xyz(infile);
    width = len(xdata);
    length = len(ydata);
    topo = np.flipud(topo);  # formatting correct when you flip up/down.

    topo_1d = np.reshape(topo, (np.size(topo),));

    # WRITE THE TOPO OUT IN BINARY FORMAT
    write_binary_topo(out_topo, topo_1d, topo_1d, width);
    # outputs(topo_1d, topo_1d, width, length, 'mendocino_topo_orig.eps');
    return [width, length];
Ejemplo n.º 13
0
# Plot the residuals.

import numpy as np
import matplotlib.pyplot as plt
import subprocess
import netcdf_read_write

grdname = "cas_slab1.0_clip.grd"
xyzname = "casc_geometry.xyz"
detrend_xyz = "detrended_geometry.xyz"
detrend_grd = "detrended_N4.grd"

top_range = 47
# degrees north.

[xdata, ydata, zdata] = netcdf_read_write.read_grd_xyz(grdname)
xdata = np.flipud(xdata)

# plt.figure();
# plt.contourf(xdata, ydata, zdata);
# plt.colorbar();
# plt.savefig('test.eps');

outfile = open(xyzname, 'w')
for i in range(len(ydata)):
    for j in range(len(xdata)):
        if ydata[i] < top_range:
            if ~np.isnan(zdata[i][j]):
                outfile.write("%f %f %f\n" % (xdata[j], ydata[i], zdata[i][j]))
outfile.close()
Ejemplo n.º 14
0
def reshape_TS_into_standard(outdir, earlyfile, cofile, latefile, outfile):
    # This is not particular general. It's using hard-coded information about time axis
    # and the timing of the earthquake.
    # It's for track 26509.
    # This function pastes together a pre-seismic, co-seismic, and post-seismic set of time series or jumps.
    # On the same xy grid.
    tolerance = 300
    # Purposely killing all pixels above this value.
    print("Reshaping UAVSAR file into single TS File")
    [tdata1, xdata1, ydata1,
     zdata1] = netcdf_read_write.read_3D_netcdf(earlyfile)
    [xdata2, ydata2, zdata2] = netcdf_read_write.read_grd_xyz(cofile)
    [tdata3, xdata3, ydata3,
     zdata3] = netcdf_read_write.read_3D_netcdf(latefile)
    print(np.shape(zdata1), np.shape(zdata2), np.shape(zdata3))
    ynum = np.shape(zdata1)[1]
    xnum = np.shape(zdata1)[2]
    znum = np.shape(zdata1)[0] + np.shape(zdata3)[0]
    total_data = np.zeros([znum, ynum, xnum])
    print(np.shape(total_data))
    for i in range(np.shape(zdata1)[0]):
        temp = zdata1[i, :, :]
        temp[abs(temp) > tolerance] = np.nan
        # killing outliers
        total_data[i, :, :] = temp
        # doing this six times.
        print("Early data Slice %d" % i)
    temp = zdata2
    temp[abs(temp) > tolerance] = np.nan
    total_data[7, :, :] = np.add(total_data[6, :, :], zdata2)
    # The coseismic chunk
    print("Coseismic data slice 7")
    for i in range(1, np.shape(zdata3)[0]):
        temp = zdata3[i, :, :]
        temp[abs(temp) > tolerance] = np.nan
        # killing outliers
        total_data[i + np.shape(zdata1)[0], :, :] = np.add(
            temp, total_data[7, :, :])
        print("Postseismic data slice %d " % (i + np.shape(zdata1)[0]))
    dtarray = []
    dtarray.append(dt.datetime.strptime("2009-04-24", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2009-09-21", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2010-04-12", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2010-07-01", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2010-12-01", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2011-05-18", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2011-11-10", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2012-09-26", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2013-05-24", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2014-06-11", "%Y-%m-%d"))
    # Hard-coded
    dtarray.append(dt.datetime.strptime("2017-11-01", "%Y-%m-%d"))
    # Hard-coded
    zunits = "mm"
    print(np.shape(total_data))
    netcdf_read_write.produce_output_timeseries(xdata1, ydata1, total_data,
                                                dtarray, zunits, outfile)
    stacking_utilities.plot_full_timeseries(outfile,
                                            dtarray,
                                            outdir + "TS_cumulative.png",
                                            vmin=-50,
                                            vmax=200,
                                            aspect=1 / 8)
    stacking_utilities.plot_incremental_timeseries(outfile,
                                                   dtarray,
                                                   outdir +
                                                   "TS_incremental.png",
                                                   vmin=-50,
                                                   vmax=100,
                                                   aspect=1 / 8)
    return
def inputs(inputfile, demfile):
	[x,y,topo]=netcdf_read_write.read_grd_xyz(demfile);
	[xdata, ydata, zdata]=netcdf_read_write.read_grd_xyz(inputfile);
	topo=np.flipud(topo);
	return [topo, xdata, ydata, zdata];
Ejemplo n.º 16
0
def plane_fitter(filename, outfile):
    """This function fits the inputted data (filename) to the model: z = A + Bx + Cy. It saves the model in
    outfile. It plots a comparison of the data and the model, and also returns the model parameters A, B and C."""
    xvalues, yvalues, zvalues = rwr.read_grd_xyz(filename)
    i, j = 0, 0
    d, x, y = [], [], []
    for z in np.nditer(zvalues):
        if np.isnan(z) == False:
            d.append(zvalues[i, j])
            x.append(xvalues[j])
            y.append(yvalues[i])
        j += 1
        if j == len(xvalues):
            j = 0
            i += 1
            if i == len(yvalues):
                i = 0
    temp = np.ones(len(d))
    G = np.column_stack((temp, x, y))
    GTG = np.dot(np.transpose(G), G)
    GTd = np.dot(np.transpose(G), d)
    soln = np.dot(np.linalg.inv(GTG), GTd)
    i, j = 0, 0
    model_z = np.zeros((len(yvalues), len(xvalues)))
    for z in np.nditer(zvalues):
        model_z[i, j] = soln[0] + soln[1] * xvalues[j] + soln[2] * yvalues[i]
        j += 1
        if j == len(xvalues):
            j = 0
            i += 1
            if i == len(yvalues):
                i = 0
    rwr.produce_output_netcdf(xvalues, yvalues, model_z, 'unwrapped phase',
                              outfile)
    rwr.flip_if_necessary(outfile)
    outfile1 = outfile.replace("model.grd", "model_comparison.png")
    fr = netcdf.netcdf_file(outfile, 'r')
    xread = fr.variables['x']
    yread = fr.variables['y']
    zread = fr.variables['z']
    zread_copy = zread[:][:].copy()

    fr2 = netcdf.netcdf_file(filename, 'r')
    xread2 = fr2.variables['x']
    yread2 = fr2.variables['y']
    zread2 = fr2.variables['z']
    zread2_copy = zread2[:][:].copy()

    fig = plt.figure(figsize=(7, 10))
    ax1 = plt.subplot(121)
    old = ax1.imshow(zread2_copy, aspect=1.2)
    ax1.invert_xaxis()
    ax1.get_xaxis().set_ticks([])
    ax1.get_yaxis().set_ticks([])
    ax1.set_xlabel("Range", fontsize=16)
    ax1.set_ylabel("Azimuth", fontsize=16)

    ax2 = plt.subplot(122)
    new = ax2.imshow(zread_copy,
                     aspect=1.2,
                     vmin=np.nanmin(zread_copy),
                     vmax=np.nanmax(zread2_copy))
    ax2.invert_xaxis()
    ax2.get_xaxis().set_ticks([])
    ax2.get_yaxis().set_ticks([])
    ax2.set_xlabel("Range", fontsize=16)
    cb = plt.colorbar(new)
    cb.set_label('unwrapped phase', size=16)

    plt.savefig(outfile1)
    plt.close()

    return soln[0], soln[1], soln[2]
Ejemplo n.º 17
0
    est = estimated_gpsvel(filenames[i][0])
    misfit = ncf.misfit(velfinal, est)
    print(result(filenames[i][0], misfit, filenames[i][1]))

# z = rwr.read_grd(filenames[-2][0])
# counter = []
# i, j = 0,0
# for x in range(len(xfinal_ratioed)):
# 	pixel_box = z[(yfinal_ratioed[x]-50):(yfinal_ratioed[x]+51),(xfinal_ratioed[x]-50):(xfinal_ratioed[x]+51) ]
# 	print(np.shape(pixel_box))
# 	for v in np.nditer(pixel_box):
# 		if np.isnan(v) == False:
# 			counter.append(v)
# 	print(len(counter))

x1, y1, z1 = rwr.read_grd_xyz('signalspread_please_test.nc')
x2, y2, z2 = rwr.read_grd_xyz(filenames[-2][0])
thing1 = z1[1200:1424, 400:660]
thing2 = z2[1200:1424, 400:660]
rwr.produce_output_netcdf(x1[400:660], y1[1200:1424], thing1, 'signal',
                          'possible_fault_info.grd')
rwr.flip_if_necessary('possible_fault_info.grd')
rwr.produce_output_plot('possible_fault_info.grd', '',
                        'possible_fault_info.png', '%')
rwr.produce_output_netcdf(x2[400:660], y2[1200:1424], thing2, 'velocity',
                          'possible_fault_info_vel.grd')
rwr.flip_if_necessary('possible_fault_info_vel.grd')

[x, y] = np.meshgrid(x1[400:660], y1[1200:1424])
[x_, y_] = np.meshgrid(x2[400:660], y2[1200:1424])