Exemplo n.º 1
0
def main(opts):
    """
    crossmul
    """
    # prepare input rasters
    referenceSlc = SLC(hdf5file=opts.reference)
    referenceSlcDataset = referenceSlc.getSlcDataset(opts.frequency,
                                                     opts.polarization)
    referenceSlcRaster = isce3.io.raster(filename='', h5=referenceSlcDataset)
    secondarySlc = SLC(hdf5file=opts.secondary)
    if opts.secondaryRaster:
        secondarySlcRaster = isce3.io.raster(filename=opts.secondaryRaster)
    else:
        secondarySlcDataset = secondarySlc.getSlcDataset(
            opts.frequency, opts.polarization)
        secondarySlcRaster = isce3.io.raster(filename='',
                                             h5=secondarySlcDataset)

    # prepare mulitlooked interferogram dimensions
    referenceGrid = referenceSlc.getRadarGrid(opts.frequency)
    length = int(referenceGrid.length / opts.alks)
    width = int(referenceGrid.width / opts.rlks)

    # init output directory(s)
    getDir = lambda filepath: os.path.split(filepath)[0]
    os.makedirs(getDir(opts.intFilePath), exist_ok=True)
    os.makedirs(getDir(opts.cohFilePath), exist_ok=True)

    # prepare output rasters
    driver = gdal.GetDriverByName('ISCE')
    igramDataset = driver.Create(opts.intFilePath, width, length, 1,
                                 gdal.GDT_CFloat32)
    igramRaster = isce3.io.raster(filename='', dataset=igramDataset)
    # coherence only generated when multilooked enabled
    if (opts.alks > 1 or opts.rlks > 1):
        cohDataset = driver.Create(opts.cohFilePath, width, length, 1,
                                   gdal.GDT_Float32)
        cohRaster = isce3.io.raster(filename='', dataset=cohDataset)
    else:
        cohRaster = None

    # prepare optional rasters
    if opts.rgoff:
        rgOffRaster = isce3.io.raster(filename=opts.rgoff)
    else:
        rgOffRaster = None

    if opts.azband:
        dopReference = referenceSlc.getDopplerCentroid()
        dopSecondary = secondarySlc.getDopplerCentroid()
        prf = referenceSlc.getSwathMetadata(
            opts.frequency).nominalAcquisitionPRF
        azimuthBandwidth = opts.azband
    else:
        dopReference = dopSecondary = None
        prf = azimuthBandwidth = 0.0

    # instantiate crossmul object based on user input
    if 'cuda' not in dir(isce3) and opts.gpu:
        warnings.warn('CUDA crossmul not available. Switching to CPU crossmul')
        opts.gpu = False

    if opts.gpu:
        crossmul = isce3.cuda.signal.crossmul()
    else:
        crossmul = isce3.signal.crossmul()

    crossmul.crossmul(referenceSlcRaster,
                      secondarySlcRaster,
                      igramRaster,
                      cohRaster,
                      rngOffset=rgOffRaster,
                      refDoppler=dopReference,
                      secDoppler=dopSecondary,
                      rangeLooks=opts.rlks,
                      azimuthLooks=opts.alks,
                      prf=prf,
                      azimuthBandwidth=azimuthBandwidth)
Exemplo n.º 2
0
def run(cfg: dict, output_hdf5: str = None, resample_type='coarse'):
    '''
    run crossmul
    '''
    # pull parameters from cfg
    ref_hdf5 = cfg['input_file_group']['input_file_path']
    sec_hdf5 = cfg['input_file_group']['secondary_file_path']
    freq_pols = cfg['processing']['input_subset']['list_of_frequencies']
    flatten = cfg['processing']['crossmul']['flatten']

    if flatten is not None:
        flatten_path = cfg['processing']['crossmul']['flatten']

    if output_hdf5 is None:
        output_hdf5 = cfg['product_path_group']['sas_output_file']

    # init parameters shared by frequency A and B
    ref_slc = SLC(hdf5file=ref_hdf5)
    sec_slc = SLC(hdf5file=sec_hdf5)

    error_channel = journal.error('crossmul.run')
    info_channel = journal.info("crossmul.run")
    info_channel.log("starting crossmultipy")

    # check if gpu ok to use
    use_gpu = isce3.core.gpu_check.use_gpu(cfg['worker']['gpu_enabled'],
                                           cfg['worker']['gpu_id'])
    if use_gpu:
        # Set the current CUDA device.
        device = isce3.cuda.core.Device(cfg['worker']['gpu_id'])
        isce3.cuda.core.set_device(device)
        crossmul = isce3.cuda.signal.Crossmul()
    else:
        crossmul = isce3.signal.Crossmul()

    crossmul.range_looks = cfg['processing']['crossmul']['range_looks']
    crossmul.az_looks = cfg['processing']['crossmul']['azimuth_looks']
    crossmul.oversample = cfg['processing']['crossmul']['oversample']
    crossmul.rows_per_block = cfg['processing']['crossmul']['rows_per_block']

    # check if user provided path to raster(s) is a file or directory
    coregistered_slc_path = pathlib.Path(
        cfg['processing']['crossmul']['coregistered_slc_path'])
    coregistered_is_file = coregistered_slc_path.is_file()
    if not coregistered_is_file and not coregistered_slc_path.is_dir():
        err_str = f"{coregistered_slc_path} is invalid; needs to be a file or directory."
        error_channel.log(err_str)
        raise ValueError(err_str)

    t_all = time.time()
    with h5py.File(output_hdf5, 'a', libver='latest', swmr=True) as dst_h5:
        for freq, pol_list in freq_pols.items():
            # get 2d doppler, discard azimuth dependency, and set crossmul dopplers
            ref_dopp = isce3.core.avg_lut2d_to_lut1d(
                ref_slc.getDopplerCentroid(frequency=freq))
            sec_dopp = isce3.core.avg_lut2d_to_lut1d(
                sec_slc.getDopplerCentroid(frequency=freq))
            crossmul.set_dopplers(ref_dopp, sec_dopp)

            freq_group_path = f'/science/LSAR/RIFG/swaths/frequency{freq}'

            if flatten is not None:
                # set frequency dependent range offset raster
                flatten_raster = isce3.io.Raster(
                    f'{flatten_path}/geo2rdr/freq{freq}/range.off')

                # prepare range filter parameters
                rdr_grid = ref_slc.getRadarGrid(freq)
                rg_pxl_spacing = rdr_grid.range_pixel_spacing
                wavelength = rdr_grid.wavelength
                rg_sample_freq = isce3.core.speed_of_light / 2.0 / rg_pxl_spacing
                rg_bandwidth = ref_slc.getSwathMetadata(
                    freq).processed_range_bandwidth

                # set crossmul range filter
                crossmul.set_rg_filter(rg_sample_freq, rg_bandwidth,
                                       rg_pxl_spacing, wavelength)

            for pol in pol_list:
                pol_group_path = f'{freq_group_path}/interferogram/{pol}'

                # prepare reference input raster
                ref_raster_str = f'HDF5:{ref_hdf5}:/{ref_slc.slcPath(freq, pol)}'
                ref_slc_raster = isce3.io.Raster(ref_raster_str)

                # prepare secondary input raster
                if coregistered_is_file:
                    raster_str = f'HDF5:{sec_hdf5}:/{sec_slc.slcPath(freq, pol)}'
                else:
                    raster_str = str(
                        coregistered_slc_path /
                        f'{resample_type}_resample_slc/'
                        f'freq{freq}/{pol}/coregistered_secondary.slc')

                sec_slc_raster = isce3.io.Raster(raster_str)

                # access the HDF5 dataset for a given frequency and polarization
                dataset_path = f'{pol_group_path}/wrappedInterferogram'
                igram_dataset = dst_h5[dataset_path]

                # Construct the output ratster directly from HDF5 dataset
                igram_raster = isce3.io.Raster(
                    f"IH5:::ID={igram_dataset.id.id}".encode("utf-8"),
                    update=True)

                # call crossmul with coherence if multilooked
                if crossmul.range_looks > 1 or crossmul.az_looks > 1:
                    # access the HDF5 dataset for a given frequency and polarization
                    dataset_path = f'{pol_group_path}/coherenceMagnitude'
                    coherence_dataset = dst_h5[dataset_path]

                    # Construct the output ratster directly from HDF5 dataset
                    coherence_raster = isce3.io.Raster(
                        f"IH5:::ID={coherence_dataset.id.id}".encode("utf-8"),
                        update=True)

                    if flatten is not None:
                        crossmul.crossmul(ref_slc_raster, sec_slc_raster,
                                          flatten_raster, igram_raster,
                                          coherence_raster)
                    else:
                        crossmul.crossmul(ref_slc_raster, sec_slc_raster,
                                          igram_raster, coherence_raster)

                    # Allocate raster statistics for coherence
                    compute_stats_real_data(coherence_raster,
                                            coherence_dataset)

                    del coherence_raster
                else:
                    # no coherence without multilook
                    crossmul.crossmul(ref_slc_raster, sec_slc_raster,
                                      igram_raster)
                del igram_raster

                # Allocate stats for rubbersheet offsets
                stats_offsets(dst_h5, freq, pol)

    t_all_elapsed = time.time() - t_all
    info_channel.log(
        f"successfully ran crossmul in {t_all_elapsed:.3f} seconds")