def runMultilook(in_dir, out_dir, alks, rlks, in_ext='.rdr', out_ext='.rdr', method='gdal', fbase_list=[ 'hgt', 'incLocal', 'lat', 'lon', 'los', 'shadowMask', 'waterMask' ]): """ Multilook geometry files. """ from iscesys.Parsers.FileParserFactory import createFileParser from mroipac.looks.Looks import Looks msg = 'generate multilooked geometry files with alks={} and rlks={}'.format( alks, rlks) if method == 'isce': msg += ' using mroipac.looks.Looks() ...' else: msg += ' using gdal.Translate() ...' print('-' * 50 + '\n' + msg) # create 'geom_reference' directory os.makedirs(out_dir, exist_ok=True) # multilook files one by one for fbase in fbase_list: in_file = os.path.join(in_dir, '{}{}'.format(fbase, in_ext)) out_file = os.path.join(out_dir, '{}{}'.format(fbase, out_ext)) if all(os.path.isfile(in_file + ext) for ext in ['', '.vrt', '.xml']): print('multilook {}'.format(in_file)) # option 1 - Looks module (isce) if method == 'isce': xmlProp = createFileParser('xml').parse(in_file + '.xml')[0] if ('image_type' in xmlProp and xmlProp['image_type'] == 'dem'): inImage = isceobj.createDemImage() else: inImage = isceobj.createImage() inImage.load(in_file + '.xml') inImage.filename = in_file lkObj = Looks() lkObj.setDownLooks(alks) lkObj.setAcrossLooks(rlks) lkObj.setInputImage(inImage) lkObj.setOutputFilename(out_file) lkObj.looks() # option 2 - gdal_translate (gdal) elif method == 'gdal': ds = gdal.Open(in_file, gdal.GA_ReadOnly) in_wid = ds.RasterXSize in_len = ds.RasterYSize out_wid = int(in_wid / rlks) out_len = int(in_len / alks) src_wid = out_wid * rlks src_len = out_len * alks options_str = '-of ENVI -a_nodata 0 -outsize {ox} {oy} -srcwin 0 0 {sx} {sy} '.format( ox=out_wid, oy=out_len, sx=src_wid, sy=src_len) gdal.Translate(out_file, ds, options=options_str) dso = gdal.Open(out_file, gdal.GA_ReadOnly) gdal.Translate(out_file + '.vrt', dso, options=gdal.TranslateOptions(format='VRT')) # generate ISCE .xml file if not os.path.isfile(out_file + '.xml'): from isce.applications.gdal2isce_xml import gdal2isce_xml gdal2isce_xml(out_file + '.vrt') else: raise ValueError( 'un-supported multilook method: {}'.format(method)) # copy the full resolution xml/vrt file from ./merged/geom_reference to ./geom_reference # to facilitate the number of looks extraction # the file path inside .xml file is not, but should, updated if in_file != out_file + '.full': shutil.copy(in_file + '.xml', out_file + '.full.xml') shutil.copy(in_file + '.vrt', out_file + '.full.vrt') return out_dir
# (this is approximate, could use instead exact expression) if inps.res_meter != '': gdal_opts = gdal.WarpOptions(format='ENVI', outputType=gdal.GDT_Int16, dstSRS='EPSG:4326', xRes=float(inps.res_meter) / 110 / 1000, yRes=float(inps.res_meter) / 110 / 1000, targetAlignedPixels=True) # res_degree = float(inps.res_meter)/110/1000 elif inps.res_seconds != '': gdal_opts = gdal.WarpOptions( format='ENVI', outputType=gdal.GDT_Int16, dstSRS='EPSG:4326', xRes=float(inps.res_seconds) * 1 / 60 * 1 / 60, yRes=float(inps.res_seconds) * 1 / 60 * 1 / 60, targetAlignedPixels=True) # res_degree = float(1/60*1/60*float(inps.res_seconds)) # The ENVI filename of the coarse DEM to be generated coarse_dem_envi = os.path.join(input_path, "Coarse_" + input_dem_envi) # Using gdal to down-sample the WGS84 DEM # cmd = "gdalwarp -t_srs EPSG:4326 -ot Int16 -of ENVI -tap -tr " + str(res_degree) + " " + str(res_degree) + " " + inps.input_dem_vrt + " " + coarse_dem_envi # os.system(cmd) ds = gdal.Warp(coarse_dem_envi, inps.input_dem_vrt, options=gdal_opts) ds = None # Generating the ISCE xml and vrt of this coarse DEM gdal2isce_xml(coarse_dem_envi + '.vrt')