diff --git a/contrib/stack/stripmapStack/MaskAndFilter.py b/contrib/stack/stripmapStack/MaskAndFilter.py index c8cd8d6..d60be21 100755 --- a/contrib/stack/stripmapStack/MaskAndFilter.py +++ b/contrib/stack/stripmapStack/MaskAndFilter.py @@ -4,52 +4,80 @@ # Copyright 2016 # -import numpy as np -import argparse import os -import isce -import isceobj +import argparse +import numpy as np +from scipy import ndimage +import matplotlib.pyplot as plt import gdal from gdalconst import GA_ReadOnly -from scipy import ndimage + +# suppress the DEBUG message +import logging +mpl_logger = logging.getLogger('matplotlib') +mpl_logger.setLevel(logging.WARNING) + +import isce +import isceobj +from isceobj.Util.ImageUtil import ImageLib as IML GDAL2NUMPY_DATATYPE = { - -1 : np.uint8, -2 : np.uint16, -3 : np.int16, -4 : np.uint32, -5 : np.int32, -6 : np.float32, -7 : np.float64, -10: np.complex64, -11: np.complex128, - + 1 : np.uint8, + 2 : np.uint16, + 3 : np.int16, + 4 : np.uint32, + 5 : np.int32, + 6 : np.float32, + 7 : np.float64, + 10: np.complex64, + 11: np.complex128, } + +EXAMPLE = '''example: + MaskAndFilter.py -d offset.bip -s offset_snr.bip + MaskAndFilter.py -d offset.bip -s offset_snr.bip --plot +''' + + def createParser(): ''' Command line parser. ''' - parser = argparse.ArgumentParser( description='filters the densOffset, oversamples it and adds back to the geometry offset') + parser = argparse.ArgumentParser(description='Mask and filter the densOffset', + formatter_class=argparse.RawTextHelpFormatter, + epilog=EXAMPLE) parser.add_argument('-d', '--dense_offset', dest='denseOffset', type=str, required=True, help='The dense offsets file obtained from cross correlation or any other approach') parser.add_argument('-s', '--snr', dest='snr', type=str, required=True, help='The SNR of the dense offsets obtained from cross correlation or any other approach') parser.add_argument('-n', '--filter_size', dest='filterSize', type=int, default=8, - help='The size of the median filter') + help='Size of the median filter (default: %(default)s).') parser.add_argument('-t', '--snr_threshold', dest='snrThreshold', type=float, default=5, - help='The snr threshold used to mask the offset') + help='Min SNR used in the offset (default: %(default)s).') + + # output parser.add_argument('-A', '--output_azimuth_offset', dest='outAzimuth', type=str, default='filtAzimuth.off', - help='The azimuth offsets after rubber sheeting') + help='File name of the azimuth offsets after rubber sheeting (default: %(default)s).') parser.add_argument('-R', '--output_range_offset', dest='outRange', type=str, default='filtRange.off', - help='The range offsets after rubber sheeting') + help='File name of the range offsets after rubber sheeting (default: %(default)s).') parser.add_argument('-o', '--output_directory', dest='outDir', type=str, default='./', - help='Output directory') - parser.add_argument('-p', '--plot', dest='plot', action='store_true', default=False, - help='plot the offsets before and after masking and filtering') + help='Output directory (default: %(default)s).') + + # plot + plot = parser.add_argument_group('plot') + plot.add_argument('-p', '--plot', dest='plot', action='store_true', default=False, + help='plot the offsets before and after masking and filtering') + plot.add_argument('-v', dest='vlim', nargs=2, type=float, default=(-0.05, 0.05), + help='display range for offset (default: %(default)s).') + plot.add_argument('--v-snr', dest='vlim_snr', nargs=2, type=float, default=(0, 100), + help='display range for offset SNR (default: %(default)s).') + plot.add_argument('--figsize', dest='figsize', nargs=2, type=float, default=(18, 5), + help='figure size in inch (default: %(default)s).') + plot.add_argument('--save', dest='fig_name', type=str, default=None, + help='save figure as file') return parser @@ -58,7 +86,7 @@ def cmdLineParse(iargs = None): return parser.parse_args(args=iargs) -def read(file, processor='ISCE' , bands=None , dataType=None): +def read(file, processor='ISCE', bands=None, dataType=None): ''' raeder based on GDAL. Args: @@ -73,10 +101,13 @@ def read(file, processor='ISCE' , bands=None , dataType=None): Returns: * data : A numpy array with dimensions : number_of_bands * length * width ''' - + # generate ENVI hdr file and fix the file path in xml + file = os.path.abspath(file) if processor == 'ISCE': - cmd = 'isce2gis.py envi -i ' + file - os.system(cmd) + img, dataname, metaname = IML.loadImage(file) + img.filename = file + img.setAccessMode('READ') + img.renderHdr() dataset = gdal.Open(file,GA_ReadOnly) @@ -97,23 +128,22 @@ def read(file, processor='ISCE' , bands=None , dataType=None): # Fill the array with the Raster bands idx=0 for i in bands: - band=dataset.GetRasterBand(i) - data[idx,:,:] = band.ReadAsArray() - idx+=1 + band=dataset.GetRasterBand(i) + data[idx,:,:] = band.ReadAsArray() + idx+=1 dataset = None return data def write(raster, fileName, nbands, bandType): - - ############ # Create the file driver = gdal.GetDriverByName( 'ENVI' ) dst_ds = driver.Create(fileName, raster.shape[1], raster.shape[0], nbands, bandType ) dst_ds.GetRasterBand(1).WriteArray( raster, 0 ,0 ) - dst_ds = None + return + def fill(data, invalid=None): """ @@ -132,40 +162,46 @@ def fill(data, invalid=None): if invalid is None: invalid = np.isnan(data) ind = ndimage.distance_transform_edt(invalid, - return_distances=False, - return_indices=True) + return_distances=False, + return_indices=True) return data[tuple(ind)] -def mask_filter(inps, band, outName, plot=False): - #masking and Filtering - Offset = read(inps.denseOffset, bands=band) - Offset = Offset[0,:,:] +def mask_filter(inps, band, outName): + """masking and Filtering""" + + # read offset + offset = read(inps.denseOffset, bands=band) + offset = offset[0,:,:] + + # read SNR snr = read(inps.snr, bands=[1]) snr = snr[0,:,:] + snr[np.isnan(snr)] = 0 - # Masking the dense offsets based on SNR - print ('masking the dense offsets with SNR threshold: ', inps.snrThreshold) - Offset[snr