def copy_bands_to_file(src_file_path, dst_file_path, bands=None): # Get info from source product src_prod = ProductIO.readProduct(src_file_path) prod_name = src_prod.getName() prod_type = src_prod.getProductType() width = src_prod.getSceneRasterWidth() height = src_prod.getSceneRasterHeight() if bands is None: bands = src_prod.getBandNames() # Copy geocoding and selected bands from source to destination product dst_prod = Product(prod_name, prod_type, width, height) ProductUtils.copyGeoCoding(src_prod.getBandAt(0), dst_prod) for band in bands: r = ProductUtils.copyBand(band, src_prod, dst_prod, True) if r is None: src_prod.closeIO() raise RuntimeError(src_file_path + " does not contain band " + band) # Write destination product to disk ext = os.path.splitext(dst_file_path)[1] if ext == '.dim': file_type = 'BEAM_DIMAP' elif ext == '.nc': file_type = 'NetCDF-CF' elif ext == '.tif': file_type = 'GeoTIFF-BigTIFF' else: file_type = 'GeoTIFF-BigTIFF' ProductIO.writeProduct(dst_prod, dst_file_path, file_type) src_prod.closeIO() dst_prod.closeIO()
def phaseToHeight(inputfile, NLOOKS): """ This is the central processing chain inputfile is a string of an interferogram file name NLOOKS is the number of range looks to be used for multi-looking # Multi-looking, Goldstein phase filtering, unwrapping and terrain correction are performed The output is then written to file in BEAM-DIMAP format """ TEMP1 = 'ML_fl_temp.dim' TEMP2 = 'height_temp.dim' OUT = '_'.join( [inputfile.split('.dim')[0], 'ML', str(NLOOKS), 'height_TC.dim']) image = ProductIO.readProduct(inputfile) # SNAP Processing ------------------------------- image = createP('Multilook', image, nRgLooks=NLOOKS) image = createP('GoldsteinPhaseFiltering', image, alpha=0.2, useCoherenceMask=True, coherenceThreshold=0.2, writeout=TEMP1) # Numpy processing ----------------------------- image = ProductIO.readProduct(TEMP1) phase = image.getBand( [x for x in list(image.getBandNames()) if 'Phase' in x][0]) phase_data = get_data(phase) recentered = zero_phase(phase_data) unwrapped = unwrap(recentered) unw_deramped = remove_plane(unwrapped, 10000) height = unw_deramped / get_kz(image) height = height.transpose().flatten() # Write to target Product-------------------------- W = image.getBandAt(0).getRasterWidth() H = image.getBandAt(0).getRasterHeight() targetP = snappy.Product('height_product', 'height_type', W, H) ProductUtils.copyMetadata(image, targetP) ProductUtils.copyTiePointGrids(image, targetP) targetP.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP')) for band in ['Phase', 'coh']: bandname = [x for x in list(image.getBandNames()) if band in x][0] ProductUtils.copyBand(bandname, image, band, targetP, True) targetP.setProductReader(ProductIO.getProductReader('BEAM-DIMAP')) ProductIO.writeProduct(targetP, TEMP2, 'BEAM-DIMAP') targetB = targetP.addBand('height', snappy.ProductData.TYPE_FLOAT32) targetB.setUnit('m') targetP.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP')) targetP.writeHeader(TEMP2) targetB.writePixels(0, 0, W, H, height) targetP.closeIO() # Terrain correction------------------------- image = ProductIO.readProduct(TEMP2) image = createP('Terrain-Correction', image, alignToStandardGrid='true', demName='SRTM 1Sec HGT', saveDEM='true') ProductIO.writeProduct(image, OUT, 'BEAM-DIMAP')
data_tmp = GPF.createProduct("Terrain-Correction",params,data) data = data_tmp # Convert to dB (LinearTodBOp.java) params = HashMap() if opts.iangle_value or opts.iangle_image: band_list = list(data.getBandNames()) bands = [] for band in band_list: if not re.search('angle',band.lower()): bands.append(band) params.put('sourceBands',','.join(bands)) data_tmp = GPF.createProduct('linearToFromdB',params,data) if opts.iangle_image: for band in band_list: if re.search('angle',band.lower()): ProductUtils.copyBand(band,data,band,data_tmp,True) elif opts.iangle_value: iangle = np.nan for band in band_list: if re.search('angle',band.lower()): band_data = data.getBand(band) w = band_data.getRasterWidth() h = band_data.getRasterHeight() band_value = np.full((h,w),np.nan,dtype=np.float32) band_data.readPixels(0,0,w,h,band_value) iangle = np.nanmean(band_value[band_value > 0.1]) data = data_tmp # Attach bandname (BandSelectOp.java) band_list = list(data.getBandNames()) bands = [] for band in band_list:
# Read original product data_1 = ProductIO.readProduct(os.path.abspath(fnams[0])) # Attach bandname if not opts.skip_rename_master: dstr = os.path.basename(fnams[0])[0:8] try: dtim = datetime.strptime(dstr, '%Y%m%d') except Exception: raise ValueError('Error in filename >>> ' + fnams[0]) band_list = list(data_1.getBandNames()) bands = [] for band in [band_list[j] for j in opts.band]: if dstr in band: continue band_new = band + '_' + dstr ProductUtils.copyBand(band, data_1, band_new, data_1, True) bands.append(band_new) if len(bands) > 0: params = HashMap() params.put('sourceBands', ','.join(bands)) data_tmp = GPF.createProduct('BandSelect', params, data_1) data_1 = data_tmp # Collocation for i in range(1, len(fnams)): # Read original product data_2 = ProductIO.readProduct(os.path.abspath(fnams[i])) # Attach bandname if not opts.skip_rename_slave: dstr = os.path.basename(fnams[i])[0:8] try: dtim = datetime.strptime(dstr, '%Y%m%d')
# check if band index given is correct if not sys.argv[2] in ['2', '3', '4', '8']: print 'Incorrect band index' # get cli arguments product_file = sys.argv[1] band_index = sys.argv[2] band_name = 'B' + band_index product_name = { 'B2': 'blue', 'B3': 'green', 'B4': 'red', 'B8': 'nir', }[band_name] # input product: open and get dimensions & name input_product = ProductIO.readProduct(product_file) product_width = input_product.getSceneRasterWidth() product_height = input_product.getSceneRasterHeight() product_name = input_product.getName() # output product: copy selected band & save product output_product = Product(product_name, product_name, product_width, product_height) ProductUtils.copyGeoCoding(input_product, output_product) ProductUtils.copyBand(band_name, input_product, output_product, True) ProductIO.writeProduct(output_product, product_name + '.band.dim', 'BEAM-DIMAP') output_product.closeIO()
print 'Product file and band index required' sys.exit(1) # check if band index given is correct if not sys.argv[2] in ['2', '3', '4', '8']: print 'Incorrect band index' # get cli arguments product_file = sys.argv[1] band_index = sys.argv[2] band_name = 'B' + band_index product_name = { 'B2': 'blue', 'B3': 'green', 'B4': 'red', 'B8': 'nir', }[band_name] # input product: open and get dimensions & name input_product = ProductIO.readProduct(product_file) product_width = input_product.getSceneRasterWidth() product_height = input_product.getSceneRasterHeight() product_name = input_product.getName() # output product: copy selected band & save product output_product = Product(product_name, product_name, product_width, product_height) ProductUtils.copyGeoCoding(input_product, output_product) ProductUtils.copyBand(band_name, input_product, output_product, True) ProductIO.writeProduct(output_product, product_name + '.band.dim', 'BEAM-DIMAP') output_product.closeIO()