Esempio n. 1
0
def main(iargs=None):
    inps = cmd_line_parse(iargs)
    plt.switch_backend('Agg')  # Backend setting

    # Read data
    k = readfile.read_attribute(inps.file)['FILE_TYPE']
    if k == 'timeseries' and inps.dset and '_' in inps.dset:
        inps.ref_date, inps.dset = inps.dset.split('_')
    else:
        inps.ref_date = None

    data, atr = readfile.read(inps.file, datasetName=inps.dset)
    if k == 'timeseries' and inps.ref_date:
        data -= readfile.read(inps.file, datasetName=inps.ref_date)[0]

    # mask
    mask = pp.read_mask(inps.file,
                        mask_file=inps.mask_file,
                        datasetName=inps.dset,
                        print_msg=True)[0]
    if mask is not None:
        data = np.ma.masked_where(mask == 0., data)
    if inps.zero_mask:
        print('masking pixels with zero value')
        data = np.ma.masked_where(data == 0., data)

    # Data Operation - Display Unit & Rewrapping
    (data, inps.disp_unit, inps.disp_scale,
     inps.wrap) = pp.scale_data4disp_unit_and_rewrap(
         data,
         metadata=atr,
         disp_unit=inps.disp_unit,
         wrap=inps.wrap,
         wrap_range=inps.wrap_range)
    if inps.wrap:
        inps.vlim = inps.wrap_range

    # Output filename
    inps.fig_title = pp.auto_figure_title(inps.file,
                                          datasetNames=inps.dset,
                                          inps_dict=vars(inps))
    if not inps.outfile:
        inps.outfile = '{}.kmz'.format(inps.fig_title)
    inps.outfile = os.path.abspath(inps.outfile)

    # 2. Generate Google Earth KMZ
    write_kmz_file(data, metadata=atr, out_file=inps.outfile, inps=inps)
    return inps.outfile
Esempio n. 2
0
    def configure(self):
        inps = cmd_line_parse(self.iargs)
        inps, self.atr = check_input_file_info(inps)
        inps = update_inps_with_file_metadata(inps, self.atr)

        # copy inps to self object
        for key, value in inps.__dict__.items():
            setattr(self, key, value)

        # read mask
        self.msk, self.mask_file = pp.read_mask(self.file,
                                                mask_file=self.mask_file,
                                                datasetName=self.dset[0],
                                                box=self.pix_box,
                                                print_msg=self.print_msg)
        return
Esempio n. 3
0
def main(iargs=None):
    inps = cmd_line_parse(iargs)
    plt.switch_backend('Agg')  # Backend setting

    # Read data
    data, atr = readfile.read(inps.file, datasetName=inps.dset)

    # mask
    mask = pp.read_mask(inps.file, mask_file=inps.mask_file, datasetName=inps.dset, print_msg=True)[0]
    if mask is not None:
        data = np.ma.masked_where(mask == 0., data)

    # Data Operation - Display Unit & Rewrapping
    (data,
     inps.disp_unit,
     inps.disp_scale,
     inps.wrap) = pp.scale_data4disp_unit_and_rewrap(data,
                                                     metadata=atr,
                                                     disp_unit=inps.disp_unit,
                                                     wrap=inps.wrap,
                                                     wrap_range=inps.wrap_range)
    if inps.wrap:
        inps.vlim = inps.wrap_range

    # Output filename
    inps.fig_title = pp.auto_figure_title(inps.file,
                                          datasetNames=inps.dset,
                                          inps_dict=vars(inps))
    if not inps.outfile:
        inps.outfile = '{}.kmz'.format(inps.fig_title)
    inps.outfile = os.path.abspath(inps.outfile)

    # 2. Generate Google Earth KMZ
    write_kmz_file(data,
                   metadata=atr,
                   out_file=inps.outfile,
                   inps=inps)
    return inps.outfile
Esempio n. 4
0
def prep_slice(cmd, auto_fig=False):
    """Prepare data from command line as input, for easy call plot_slice() externally
    Parameters: cmd : string, command to be run in terminal
    Returns:    data : 2D np.ndarray, data to be plotted
                atr  : dict, metadata
                inps : namespace, input argument for plot setup
    Example:
        fig, ax = plt.subplots(figsize=[4, 3])
        geo_box = (-91.670, -0.255, -91.370, -0.515)    # W, N, E, S
        cmd = 'view.py geo_velocity.h5 velocity --mask geo_maskTempCoh.h5 '
        cmd += '--sub-lon {w} {e} --sub-lat {s} {n} '.format(w=geo_box[0], n=geo_box[1], e=geo_box[2], s=geo_box[3])
        cmd += '-c jet -v -3 10 --cbar-loc bottom --cbar-nbins 3 --cbar-ext both --cbar-size 5% '
        cmd += '--dem srtm1.dem --dem-nocontour '
        cmd += '--lalo-step 0.2 --lalo-loc 1 0 1 0 --scalebar 0.3 0.80 0.05 --notitle --fontsize 12 '
        d_v, atr ,inps = view.prep_slice(cmd)
        ax, inps, im, cbar = view.plot_slice(ax, d_v, atr, inps)
        plt.show()
    """
    inps = cmd_line_parse(cmd.split()[1:])
    vprint(cmd)
    inps, atr = check_input_file_info(inps)
    inps = update_inps_with_file_metadata(inps, atr)

    inps.msk, inps.mask_file = pp.read_mask(inps.file,
                                            mask_file=inps.mask_file,
                                            datasetName=inps.dset[0],
                                            box=inps.pix_box,
                                            print_msg=inps.print_msg)

    # read data
    data, atr = read(inps.file,
                     datasetName=inps.dset[0],
                     box=inps.pix_box,
                     print_msg=inps.print_msg)
    # reference in time
    if inps.ref_date:
        data -= read(inps.file,
                     datasetName=inps.ref_date,
                     box=inps.pix_box,
                     print_msg=False)[0]
    # reference in space for unwrapPhase
    if (inps.key in ['ifgramStack']
            and inps.dset[0].split('-')[0] == 'unwrapPhase'
            and 'REF_Y' in atr.keys()):
        ref_y, ref_x = int(atr['REF_Y']), int(atr['REF_X'])
        ref_data = read(inps.file,
                        datasetName=inps.dset[0],
                        box=(ref_x, ref_y, ref_x + 1, ref_y + 1),
                        print_msg=False)[0]
        data[data != 0.] -= ref_data
    # masking
    if inps.zero_mask:
        data = np.ma.masked_where(data == 0., data)
    if inps.msk is not None:
        data = np.ma.masked_where(inps.msk == 0., data)

    data, inps = update_data_with_plot_inps(data, atr, inps)

    # matplotlib.Axes
    if auto_fig == True:
        fig, ax = plt.subplots(figsize=[i / 2.0 for i in inps.fig_size],
                               num='Figure')
        return data, atr, inps, ax
    else:
        return data, atr, inps
Esempio n. 5
0
def read_timeseries_data(inps):
    """Read data of time-series files
    Parameters: inps : Namespace of input arguments
    Returns:    ts_data : list of 3D np.array in size of (num_date, length, width)
                mask : 2D np.array in size of (length, width)
                inps : Namespace of input arguments
    """
    ## read list of 3D time-series
    ts_data = []
    for fname in inps.file:
        msg = f'reading timeseries from file {fname}'
        msg += f' with step of {inps.multilook_num} by {inps.multilook_num}' if inps.multilook_num > 1 else ''
        vprint(msg)
        data, atr = readfile.read(fname,
                                  datasetName=inps.date_list,
                                  box=inps.pix_box,
                                  xstep=inps.multilook_num,
                                  ystep=inps.multilook_num)

        if inps.ref_yx and inps.ref_yx != (int(atr.get('REF_Y', -1)), int(atr.get('REF_X', -1))):
            (ry, rx) = subset_and_multilook_yx(inps.ref_yx, inps.pix_box, inps.multilook_num)
            ref_phase = data[:, ry, rx]
            data -= np.tile(ref_phase.reshape(-1, 1, 1), (1, data.shape[-2], data.shape[-1]))
            vprint('reference to pixel: {}'.format(inps.ref_yx))

        if inps.ref_idx is not None:
            vprint('reference to date: {}'.format(inps.date_list[inps.ref_idx]))
            data -= np.tile(data[inps.ref_idx, :, :], (inps.num_date, 1, 1))

        # Display Unit
        (data,
         inps.disp_unit,
         inps.unit_fac) = pp.scale_data2disp_unit(data,
                                                  metadata=atr,
                                                  disp_unit=inps.disp_unit)
        ts_data.append(data)

    ## mask file: input mask file + non-zero ts pixels - ref_point
    mask = pp.read_mask(inps.file[0],
                        mask_file=inps.mask_file,
                        datasetName='displacement',
                        box=inps.pix_box,
                        xstep=inps.multilook_num,
                        ystep=inps.multilook_num,
                        print_msg=inps.print_msg)[0]
    if mask is None:
        mask = np.ones(ts_data[0].shape[-2:], np.bool_)

    ts_stack = np.nansum(ts_data[0], axis=0)
    mask[np.isnan(ts_stack)] = False
    # keep all-zero value for unwrapError time-series
    if atr['UNIT'] not in ['cycle']:
        mask[ts_stack == 0.] = False
    del ts_stack

    # do not mask the reference point
    if inps.ref_yx and inps.ref_yx != (int(atr.get('REF_Y', -1)), int(atr.get('REF_X', -1))):
        (ry, rx) = subset_and_multilook_yx(inps.ref_yx, inps.pix_box, inps.multilook_num)
        mask[ry, rx] = True

    ## default vlim
    inps.dlim = [np.nanmin(ts_data[0]), np.nanmax(ts_data[0])]
    if not inps.vlim:
        inps.cmap_lut, inps.vlim = pp.auto_adjust_colormap_lut_and_disp_limit(ts_data[0],
                                                                              num_multilook=10,
                                                                              print_msg=inps.print_msg)
    vprint('data    range: {} {}'.format(inps.dlim, inps.disp_unit))
    vprint('display range: {} {}'.format(inps.vlim, inps.disp_unit))

    ## default ylim
    num_file = len(inps.file)
    if not inps.ylim:
        ts_data_mli = multilook_data(np.squeeze(ts_data[-1]), 4, 4)
        if inps.zero_first:
            ts_data_mli -= np.tile(ts_data_mli[inps.zero_idx, :, :], (inps.num_date, 1, 1))
        ymin, ymax = (np.nanmin(ts_data_mli[inps.ex_flag != 0]),
                      np.nanmax(ts_data_mli[inps.ex_flag != 0]))
        ybuffer = (ymax - ymin) * 0.05
        inps.ylim = [ymin - ybuffer, ymax + ybuffer]
        if inps.offset:
            inps.ylim[1] += inps.offset * (num_file - 1)
        del ts_data_mli

    return ts_data, mask, inps
Esempio n. 6
0
def read_timeseries_data(inps):
    """Read data of time-series files
    Parameters: inps : Namespace of input arguments
    Returns:    ts_data : list of 3D np.array in size of (num_date, length, width)
                mask : 2D np.array in size of (length, width)
                inps : Namespace of input arguments
    """
    # read list of 3D time-series
    ts_data = []
    for fname in inps.file:
        vprint('reading timeseries from file {} ...'.format(fname))
        data, atr = readfile.read(fname,
                                  datasetName=inps.date_list,
                                  box=inps.pix_box)
        try:
            ref_phase = data[:, inps.ref_yx[0] - inps.pix_box[1],
                             inps.ref_yx[1] - inps.pix_box[0]]
            data -= np.tile(ref_phase.reshape(-1, 1, 1),
                            (1, data.shape[-2], data.shape[-1]))
            vprint('reference to pixel: {}'.format(inps.ref_yx))
        except:
            pass
        vprint('reference to date: {}'.format(inps.date_list[inps.ref_idx]))
        data -= np.tile(data[inps.ref_idx, :, :], (inps.num_date, 1, 1))

        # Display Unit
        (data, inps.disp_unit,
         inps.unit_fac) = pp.scale_data2disp_unit(data,
                                                  metadata=atr,
                                                  disp_unit=inps.disp_unit)
        ts_data.append(data)

    # Mask file: input mask file + non-zero ts pixels - ref_point
    mask = np.ones(ts_data[0].shape[-2:], np.bool_)
    msk = pp.read_mask(inps.file[0],
                       mask_file=inps.mask_file,
                       datasetName='displacement',
                       box=inps.pix_box,
                       print_msg=inps.print_msg)[0]
    mask[msk == 0.] = False
    del msk

    ts_stack = np.sum(ts_data[0], axis=0)
    mask[ts_stack == 0.] = False
    mask[np.isnan(ts_stack)] = False
    del ts_stack

    #do not mask the reference point
    try:
        mask[inps.ref_yx[0] - inps.pix_box[1],
             inps.ref_yx[1] - inps.pix_box[0]] = True
    except:
        pass

    #vprint('masking data')
    #ts_mask = np.tile(mask, (inps.num_date, 1, 1))
    #for i in range(len(ts_data)):
    #    ts_data[i][ts_mask == 0] = np.nan
    #    try:
    #        ts_data[i][:, inps.ref_yx[0], inps.ref_yx[1]] = 0.   # keep value on reference pixel
    #    except:
    #        pass
    #del ts_mask

    # default vlim
    inps.dlim = [np.nanmin(ts_data[0]), np.nanmax(ts_data[0])]
    ts_data_mli = multilook_data(np.squeeze(ts_data[0]), 10, 10)
    if not inps.vlim:
        inps.vlim = [
            np.nanmin(ts_data_mli[inps.ex_flag != 0]),
            np.nanmax(ts_data_mli[inps.ex_flag != 0])
        ]
    vprint('data    range: {} {}'.format(inps.dlim, inps.disp_unit))
    vprint('display range: {} {}'.format(inps.vlim, inps.disp_unit))

    # default ylim
    num_file = len(inps.file)
    if not inps.ylim:
        ts_data_mli = multilook_data(np.squeeze(ts_data[-1]), 4, 4)
        if inps.zero_first:
            ts_data_mli -= np.tile(ts_data_mli[inps.zero_idx, :, :],
                                   (inps.num_date, 1, 1))
        ymin, ymax = (np.nanmin(ts_data_mli[inps.ex_flag != 0]),
                      np.nanmax(ts_data_mli[inps.ex_flag != 0]))
        ybuffer = (ymax - ymin) * 0.05
        inps.ylim = [ymin - ybuffer, ymax + ybuffer]
        if inps.offset:
            inps.ylim[1] += inps.offset * (num_file - 1)
    del ts_data_mli

    return ts_data, mask, inps
Esempio n. 7
0
def main(iargs=None):
    inps = cmd_line_parse(iargs)

    # 1. Read metadata and data
    k = readfile.read_attribute(inps.file)['FILE_TYPE']
    if k == 'timeseries' and inps.dset and '_' in inps.dset:
        inps.ref_date, inps.dset = inps.dset.split('_')
    else:
        inps.ref_date = None
    atr = readfile.read_attribute(inps.file, datasetName=inps.dset)

    # pix_box
    inps.pix_box = subset.subset_input_dict2box(vars(inps), atr)[0]
    inps.pix_box = ut.coordinate(atr).check_box_within_data_coverage(
        inps.pix_box)
    data_box = (0, 0, int(atr['WIDTH']), int(atr['LENGTH']))
    print('data   coverage in y/x: {}'.format(data_box))
    print('subset coverage in y/x: {}'.format(inps.pix_box))
    atr = attr.update_attribute4subset(atr, inps.pix_box)

    # read data
    data = readfile.read(inps.file, datasetName=inps.dset, box=inps.pix_box)[0]
    if k == 'timeseries' and inps.ref_date:
        data -= readfile.read(inps.file,
                              datasetName=inps.ref_date,
                              box=inps.pix_box)[0]

    # mask
    mask = pp.read_mask(inps.file,
                        mask_file=inps.mask_file,
                        datasetName=inps.dset,
                        box=inps.pix_box)[0]
    if mask is not None:
        print('masking out pixels with zero value in file: {}'.format(
            inps.mask_file))
        data[mask == 0] = np.nan
    if inps.zero_mask:
        print('masking out pixels with zero value')
        data[data == 0] = np.nan
    del mask

    # Data Operation - Display Unit & Rewrapping
    (data, inps.disp_unit, inps.disp_scale,
     inps.wrap) = pp.scale_data4disp_unit_and_rewrap(
         data,
         metadata=atr,
         disp_unit=inps.disp_unit,
         wrap=inps.wrap,
         wrap_range=inps.wrap_range)
    if inps.wrap:
        inps.vlim = inps.wrap_range

    # 2. Generate Google Earth KMZ
    # 2.1 Common settings
    # disp min/max and colormap
    cmap_lut = 256
    if not inps.vlim:
        cmap_lut, inps.vlim = pp.auto_adjust_colormap_lut_and_disp_limit(data)
    inps.colormap = pp.auto_colormap_name(atr, inps.colormap)
    inps.colormap = pp.ColormapExt(inps.colormap, cmap_lut).colormap
    inps.norm = colors.Normalize(vmin=inps.vlim[0], vmax=inps.vlim[1])

    # Output filename
    inps.fig_title = pp.auto_figure_title(inps.file,
                                          datasetNames=inps.dset,
                                          inps_dict=vars(inps))
    if not inps.outfile:
        inps.outfile = '{}.kmz'.format(inps.fig_title)
    inps.outfile = os.path.abspath(inps.outfile)

    # 2.2 Write KMZ file
    if 'Y_FIRST' in atr.keys():
        # create ground overlay KML for file in geo-coord
        write_kmz_overlay(
            data,
            meta=atr,
            out_file=inps.outfile,
            inps=inps,
        )

    else:
        # create placemark KML for file in radar-coord
        write_kmz_placemark(
            data,
            meta=atr,
            out_file=inps.outfile,
            geom_file=inps.geom_file,
            inps=inps,
        )

    return inps.outfile