Beispiel #1
0
shared.write_unw_from_data_or_geotiff(geotif_or_data=incidence_data,
                                      dest_unw=inc_file,
                                      ifg_proc=1)

shared.write_unw_from_data_or_geotiff(geotif_or_data=elevation_data,
                                      dest_unw=elevation_file,
                                      ifg_proc=1)

header.update(dem_header)
header[ifc.PYRATE_TIME_SPAN] = 0
header[ifc.SLAVE_DATE] = 0
header[ifc.DATA_UNITS] = 'degrees'
header[ifc.DATA_TYPE] = ifc.INCIDENCE
header[ifc.SLAVE_TIME] = 0
shared.write_geotiff(header=header,
                     data_path=elevation_file,
                     dest=dest_lv_theta,
                     nodata=np.nan)

shared.write_geotiff(header=header,
                     data_path=inc_file,
                     dest=dest_inc,
                     nodata=np.nan)

ds = gdal.Open(dest_lv_theta, gdal.GA_ReadOnly)
data_elevation = ds.ReadAsArray()
ds = None

ds = gdal.Open(dest_inc, gdal.GA_ReadOnly)
data_inc = ds.ReadAsArray()
ds = None
Beispiel #2
0
def crop_resample_average(input_tif,
                          extents: Union[List, Tuple],
                          new_res,
                          output_file,
                          thresh,
                          hdr,
                          out_driver_type='GTiff',
                          match_pyrate=False,
                          coherence_path=None,
                          coherence_thresh=None):
    """
    Crop, resample, and average a geotiff image.

    :param str input_tif: Path to input geotiff to resample/crop
    :param tuple extents: Cropping extents (xfirst, yfirst, xlast, ylast)
    :param list new_res: [xres, yres] resolution of output image
    :param str output_file: Path to output resampled/cropped geotiff
    :param float thresh: NaN fraction threshold
    :param str out_driver_type: The output driver; `MEM` or `GTiff` (optional)
    :param bool match_pyrate: Match Legacy output (optional)
    :param dict hdr: dictionary of metadata

    :return: resampled_average: output cropped and resampled image
    :rtype: ndarray
    :return: out_ds: destination gdal dataset object
    :rtype: gdal.Dataset
    """
    dst_ds, _, _, _ = _crop_resample_setup(extents,
                                           input_tif,
                                           new_res,
                                           output_file,
                                           out_bands=2,
                                           dst_driver_type='MEM')

    # make a temporary copy of the dst_ds for PyRate style prepifg
    if (match_pyrate and new_res[0]):
        tmp_ds = gdal.GetDriverByName('MEM').CreateCopy('', dst_ds)
    else:
        tmp_ds = None

    src_ds, src_ds_mem = _setup_source(input_tif)

    if coherence_path and coherence_thresh:
        coherence_masking(src_ds_mem, coherence_path, coherence_thresh)

    elif coherence_path and not coherence_thresh:
        raise ValueError("Coherence file provided without a coherence "
                         "threshold. Please ensure you provide 'cohthresh' "
                         "in your config if coherence masking is enabled.")

    resampled_average, src_ds_mem = gdal_average(dst_ds, src_ds, src_ds_mem,
                                                 thresh)
    src_ds = None
    src_dtype = src_ds_mem.GetRasterBand(1).DataType
    src_gt = src_ds_mem.GetGeoTransform()

    # required to match Legacy output
    if tmp_ds:
        _alignment(input_tif, new_res, resampled_average, src_ds_mem, src_gt,
                   tmp_ds)

    # grab metadata from existing geotiff
    gt = dst_ds.GetGeoTransform()
    wkt = dst_ds.GetProjection()

    # insert metadata from the header
    md = shared.collate_metadata(hdr)

    # update metadata for output

    # TODO: Metadata should be updated immediately as a prepifg/process step is applied
    # move this into the respective steps
    for k, v in md.items():
        if k == ifc.DATA_TYPE:
            # update data type metadata
            if (v == ifc.ORIG) and (coherence_path is not None):
                md.update({ifc.DATA_TYPE: ifc.MLOOKED_COH_MASKED_IFG})
            elif (v == ifc.ORIG) and (coherence_path is None):
                md.update({ifc.DATA_TYPE: ifc.MULTILOOKED})
            elif v == ifc.COH:
                md.update({ifc.DATA_TYPE: ifc.MULTILOOKED_COH})
            elif v == ifc.DEM:
                md.update({ifc.DATA_TYPE: ifc.MLOOKED_DEM})
            elif v == ifc.INCIDENCE:
                md.update({ifc.DATA_TYPE: ifc.MLOOKED_INC})
            else:
                raise TypeError(f'Data Type metadata {v} not recognised')

    add_looks_and_crop_from_header(hdr, md)

    # In-memory GDAL driver doesn't support compression so turn it off.
    creation_opts = ['compress=packbits'] if out_driver_type != 'MEM' else []
    out_ds = shared.gdal_dataset(output_file,
                                 dst_ds.RasterXSize,
                                 dst_ds.RasterYSize,
                                 driver=out_driver_type,
                                 bands=1,
                                 dtype=src_dtype,
                                 metadata=md,
                                 crs=wkt,
                                 geotransform=gt,
                                 creation_opts=creation_opts)

    if out_driver_type != 'MEM':
        shared.write_geotiff(resampled_average, out_ds, np.nan)
        log.info(f"Writing geotiff: {output_file}")
    else:
        out_ds.GetRasterBand(1).WriteArray(resampled_average)
    return resampled_average, out_ds
Beispiel #3
0
def crop_resample_average(input_tif,
                          extents,
                          new_res,
                          output_file,
                          thresh,
                          out_driver_type='GTiff',
                          match_pyrate=False,
                          hdr=None,
                          coherence_path=None,
                          coherence_thresh=None):
    """
    Crop, resample, and average a geotiff image.

    :param str input_tif: Path to input geotiff to resample/crop
    :param tuple extents: Cropping extents (xfirst, yfirst, xlast, ylast)
    :param list new_res: [xres, yres] resolution of output image
    :param str output_file: Path to output resampled/cropped geotiff
    :param float thresh: NaN fraction threshold
    :param str out_driver_type: The output driver; `MEM` or `GTiff` (optional)
    :param bool match_pyrate: Match Legacy output (optional)
    :param dict hdr: dictionary of metadata

    :return: resampled_average: output cropped and resampled image
    :rtype: ndarray
    :return: out_ds: destination gdal dataset object
    :rtype: gdal.Dataset
    """
    dst_ds, _, _, _ = _crop_resample_setup(extents,
                                           input_tif,
                                           new_res,
                                           output_file,
                                           out_bands=2,
                                           dst_driver_type='MEM')

    # make a temporary copy of the dst_ds for PyRate style prepifg
    tmp_ds = gdal.GetDriverByName('MEM').CreateCopy('', dst_ds) \
        if (match_pyrate and new_res[0]) else None

    src_ds, src_ds_mem = _setup_source(input_tif)

    if coherence_path and coherence_thresh:
        coherence_raster = prepifg_helper.dem_or_ifg(coherence_path)
        coherence_raster.open()
        coherence_ds = coherence_raster.dataset
        coherence_masking(src_ds_mem, coherence_ds, coherence_thresh)
    elif coherence_path and not coherence_thresh:
        raise ValueError(f"Coherence file provided without a coherence "
                         f"threshold. Please ensure you provide 'cohthresh' "
                         f"in your config if coherence masking is enabled.")

    resampled_average, src_ds_mem = \
        gdal_average(dst_ds, src_ds, src_ds_mem, thresh)
    src_dtype = src_ds_mem.GetRasterBand(1).DataType
    src_gt = src_ds_mem.GetGeoTransform()

    # required to match Legacy output
    if tmp_ds:
        _alignment(input_tif, new_res, resampled_average, src_ds_mem, src_gt,
                   tmp_ds)

    # grab metadata from existing geotiff
    gt = dst_ds.GetGeoTransform()
    wkt = dst_ds.GetProjection()

    # TEST HERE IF EXISTING FILE HAS PYRATE METADATA. IF NOT ADD HERE
    if not ifc.DATA_TYPE in dst_ds.GetMetadata() and hdr is not None:
        md = shared.collate_metadata(hdr)
    else:
        md = dst_ds.GetMetadata()

    # update metadata for output
    for k, v in md.items():
        if k == ifc.DATA_TYPE:
            # update data type metadata
            if v == ifc.ORIG and coherence_path:
                md.update({ifc.DATA_TYPE: ifc.COHERENCE})
            elif v == ifc.ORIG and not coherence_path:
                md.update({ifc.DATA_TYPE: ifc.MULTILOOKED})
            elif v == ifc.DEM:
                md.update({ifc.DATA_TYPE: ifc.MLOOKED_DEM})
            elif v == ifc.INCIDENCE:
                md.update({ifc.DATA_TYPE: ifc.MLOOKED_INC})
            elif v == ifc.COHERENCE and coherence_path:
                pass
            elif v == ifc.MULTILOOKED and coherence_path:
                md.update({ifc.DATA_TYPE: ifc.COHERENCE})
            elif v == ifc.MULTILOOKED and not coherence_path:
                pass
            else:
                raise TypeError('Data Type metadata not recognised')

    # In-memory GDAL driver doesn't support compression so turn it off.
    creation_opts = ['compress=packbits'] if out_driver_type != 'MEM' else []
    out_ds = shared.gdal_dataset(output_file,
                                 dst_ds.RasterXSize,
                                 dst_ds.RasterYSize,
                                 driver=out_driver_type,
                                 bands=1,
                                 dtype=src_dtype,
                                 metadata=md,
                                 crs=wkt,
                                 geotransform=gt,
                                 creation_opts=creation_opts)

    shared.write_geotiff(resampled_average, out_ds, np.nan)

    return resampled_average, out_ds