def getDemFile(infile, outfile: str, use_opentopo=False, in_utm=True, post=None, dem_name=None): lat_max, lat_min, lon_max, lon_min = get_bounding_box_file(infile) if use_opentopo: demtype = None url = f'http://opentopo.sdsc.edu/otr/getdem' \ f'?demtype=SRTMGL1&west={lon_min}&south={lat_min}&east={lon_max}&north={lat_max}&outputFormat=GTiff' execute(f'wget -O {outfile} "{url}"') if in_utm: proj = get_utm_proj(lon_min, lon_max, lat_min, lat_max) tmpdem = 'tmpdem_getDemFile_utm.tif' gdal.Warp(tmpdem, outfile, dstSRS=proj, resampleAlg='cubic') shutil.move(tmpdem, outfile) else: dem_type = 'utm' if in_utm else 'latlon' demtype = get_dem(lon_min, lat_min, lon_max, lat_max, outfile, post=post, dem_name=dem_name, dem_type=dem_type) if not os.path.isfile(outfile): logging.error(f'Unable to find output file {outfile}') return outfile, demtype
def makeKMZ(infile, outfile): kmlfile = infile + ".kml" kmzfile = outfile + ".kmz" pngfile = infile + ".png" outpng = outfile + ".png" lrgfile = outfile + "_large.png" # Create the colorized kml file and png image cmd = "mdx.py {0} -kml {1}".format(infile, kmlfile) execute(cmd) #fix the name in the kml file!!! fixKmlName(kmlfile, lrgfile) # scale the PNG image to browse size gdal.Translate("temp.png", pngfile, format="PNG", width=0, height=1024) gdal.Translate("tmpl.png", pngfile, format="PNG", width=0, height=2048) shutil.move("temp.png", pngfile) shutil.move("tmpl.png", lrgfile) # finally, zip the kmz up with zipfile.ZipFile(kmzfile, 'w') as myzip: myzip.write(kmlfile) myzip.write(lrgfile) shutil.move(pngfile, outpng)
def test_execute_uselogging(caplog): with caplog.at_level(logging.INFO): cmd = 'echo "Hello world"' output = execute.execute(cmd, uselogging=True) assert 'Hello world' in output assert 'Proc: Hello world' in caplog.text
def make_products(outfile, pol, cp=None): # Create greyscale geotiff and ASF browse images tiffile = "{out}_{pol}.tif".format(out=outfile, pol=pol) ampfile = createAmp(tiffile, nodata=0) newfile = ampfile.replace(".tif", "_sigma.tif") byteSigmaScale(ampfile, newfile) makeAsfBrowse(newfile, outfile) os.remove(newfile) # Create color ASF browse images if cp is not None: if pol == "vv": basename = "{}_vh".format(outfile) else: basename = "{}_hv".format(outfile) tiffile2 = "{}.tif".format(basename) ampfile2 = createAmp(tiffile2, nodata=0) outfile2 = ampfile2.replace(".tif", "_rgb.tif") threshold = -24 # Direct call to rtc2color overran the memory (128 GB) # rtc2color(ampfile,ampfile2, threshold, outfile2, amp=True, cleanup=True) # Trying this instead execute( f"rtc2color.py -amp -cleanup {ampfile} {ampfile2} {threshold} {outfile2}", uselogging=True) colorname = "{}_rgb".format(outfile) makeAsfBrowse(outfile2, colorname) os.remove(ampfile2) os.remove(outfile2) os.remove(ampfile) # Move results to the PRODUCT directory if not os.path.isdir("PRODUCT"): os.mkdir("PRODUCT") for tiffile in glob.glob("*.tif"): shutil.move(tiffile, "PRODUCT") for txtfile in glob.glob("*_log.txt"): shutil.move(txtfile, "PRODUCT") for pngfile in glob.glob("*.png*"): shutil.move(pngfile, "PRODUCT") for kmzfile in glob.glob("*.kmz"): shutil.move(kmzfile, "PRODUCT")
def test_excute_cmd_fail(): with pytest.raises(ExecuteError): cmd = 'echo "Hello world"; exit 1' try: _ = execute.execute(cmd) except ExecuteError as err: assert 'echo' in str(err) raise
def prepare_files(csv_file): """Download granules and unzip granules Given a CSV file of granule names, download the granules and unzip them, removing the zip files as we go. Note: This will unzip and REMOVE ALL ZIP FILES in the current directory. """ cmd = "get_asf.py %s" % csv_file execute(cmd) os.rmdir("download") for myfile in os.listdir("."): if ".zip" in myfile: try: zip_ref = zipfile.ZipFile(myfile, 'r') zip_ref.extractall(".") zip_ref.close() except: print("Unable to unzip file {}".format(myfile)) else: print("WARNING: {} not recognized as a zip file".format(myfile))
def test_execute_logfile(tmp_path): cmd = 'echo "Hello world"' log_path = tmp_path / 'echo.log' with log_path.open(mode='w') as log_file: output = execute.execute(cmd, logfile=log_file) assert 'Hello world' in output with log_path.open() as log_file: log_str = log_file.read() assert 'Hello world' in log_str
def interf_pwr_s1_lt_tops_proc(reference, secondary, dem, rlooks=10, alooks=2, iterations=5, step=0): # Setup various file names that we'll need ifgname = "{}_{}".format(reference, secondary) SLC2tab = "SLC2_tab" SLC2Rtab = "SLC2R_tab" lt = "{}.lt".format(reference) mpar = reference + ".slc.par" spar = secondary + ".slc.par" mmli = reference + ".mli.par" smli = secondary + ".mli.par" off = ifgname + ".off_temp" # Make a fresh slc2r tab create_slc2r_tab(SLC2tab, SLC2Rtab) if step == 0: if not os.path.isfile(dem): log.info("Currently in directory {}".format(os.getcwd())) log.error("ERROR: Input DEM file {} can't be found!".format(dem)) sys.exit(1) log.info("Input DEM file {} found".format(dem)) log.info("Preparing initial look up table and sim_unw file") execute(f"create_offset {mpar} {spar} {off} 1 {rlooks} {alooks} 0", uselogging=True) execute(f"rdc_trans {mmli} {dem} {smli} {lt}", uselogging=True) execute( f"phase_sim_orb {mpar} {spar} {off} {dem} {ifgname}.sim_unw {mpar} -", uselogging=True) elif step == 1: log.info("Starting initial coregistration with look up table") coregister_data(0, SLC2tab, SLC2Rtab, spar, mpar, mmli, smli, ifgname, reference, secondary, lt, rlooks, alooks, iterations) elif step == 2: log.info("Starting iterative coregistration with look up table") for n in range(1, iterations + 1): coregister_data(n, SLC2tab, SLC2Rtab, spar, mpar, mmli, smli, ifgname, reference, secondary, lt, rlooks, alooks, iterations) elif step == 3: log.info( "Starting single interation coregistration with look up table") coregister_data(iterations + 1, SLC2tab, SLC2Rtab, spar, mpar, mmli, smli, ifgname, reference, secondary, lt, rlooks, alooks, iterations) else: log.error( "ERROR: Unrecognized step {}; must be from 0 - 2".format(step)) sys.exit(1)
def ingest_S1_granule(safe_dir: str, pol: str, looks: int, out_file: str, orbit_file: str = None): """Pre-process S1 imagery into GAMMA format Args: safe_dir: Sentinel-1 SAFE directory location pol: polarization (e.g., 'vv') looks: the number of looks to take out_file: file name of the output GAMMA formatted imagery orbit_file: Orbit file to use (will download a matching orbit file if None) """ pol = pol.lower() granule_type = safe_dir[7:10] # Ingest the granule into gamma format if granule_type == 'GRD': cmd = f'par_S1_GRD {safe_dir}/*/*{pol}*.tiff {safe_dir}/*/*{pol}*.xml {safe_dir}/*/*/calibration-*{pol}*.xml ' \ f'{safe_dir}/*/*/noise-*{pol}*.xml {pol}.grd.par {pol}.grd' execute(cmd, uselogging=True) # Ingest the precision state vectors try: if orbit_file is None: logging.info('Trying to get orbit file information from file {}'.format(safe_dir)) orbit_file, _ = downloadSentinelOrbitFile(safe_dir) logging.debug('Applying precision orbit information') execute(f'S1_OPOD_vec {pol}.grd.par {orbit_file}', uselogging=True) except OrbitDownloadError: logging.warning('Unable to fetch precision state vectors... continuing') if looks > 1.0: cmd = f'multi_look_MLI {pol}.grd {pol}.grd.par {out_file} {out_file}.par {looks} {looks}' execute(cmd, uselogging=True) else: shutil.copy(f'{pol}.grd', out_file) shutil.copy(f'{pol}.grd.par', f'{out_file}.par') else: # Ingest SLC data files into gamma format par_s1_slc_single(safe_dir, pol, orbit_file=orbit_file) date = safe_dir[17:25] burst_tab = getBursts(safe_dir, make_tab_flag=True) shutil.copy(burst_tab, date) # Mosaic the swaths together and copy SLCs over back = os.getcwd() os.chdir(date) SLC_copy_S1_fullSW('../', date, 'SLC_TAB', burst_tab, mode=2, raml=looks * 5, azml=looks) os.chdir(back) shutil.move(f'{date}.mli', out_file) shutil.move(f'{date}.mli.par', f'{out_file}.par')
def par_s1_slc_single(safe_dir, pol='vv', orbit_file=None): """Pre-process S1 SLC imagery into GAMMA format SLCs Args: safe_dir: Sentinel-1 SAFE directory location pol: polarization (e.g., 'vv') orbit_file: Orbit file to use (will download a matching orbit file if None) """ wrk = os.getcwd() pol = pol.lower() logging.info(f'Procesing directory {safe_dir}') image_type = safe_dir[13:16] logging.info(f'Found image type {image_type}') datelong = safe_dir.split('_')[5] acquisition_date = (safe_dir.split('_')[5].split('T'))[0] path = os.path.join(wrk, acquisition_date) if not os.path.exists(path): os.mkdir(path) logging.info(f'SAFE directory is {safe_dir}') logging.info(f'Long date is {datelong}') logging.info(f'Acquisition date is {acquisition_date}') os.chdir(safe_dir) for swath in range(1, 4): cmd = make_cmd(swath, acquisition_date, path, pol=pol) execute(cmd, uselogging=True) os.chdir(path) # Ingest the precision state vectors try: if orbit_file is None: logging.info( f'Trying to get orbit file information from file {safe_dir}') orbit_file, _ = downloadSentinelOrbitFile(safe_dir) logging.info('Applying precision orbit information') execute(f'S1_OPOD_vec {acquisition_date}_001.slc.par {orbit_file}', uselogging=True) execute(f'S1_OPOD_vec {acquisition_date}_002.slc.par {orbit_file}', uselogging=True) execute(f'S1_OPOD_vec {acquisition_date}_003.slc.par {orbit_file}', uselogging=True) except OrbitDownloadError: logging.warning( 'Unable to fetch precision state vectors... continuing') slc = glob.glob('*_00*.slc') slc.sort() par = glob.glob('*_00*.slc.par') par.sort() top = glob.glob('*_00*.tops_par') top.sort() with open(os.path.join(path, 'SLC_TAB'), 'w') as f: for i in range(len(slc)): f.write(f'{slc[i]} {par[i]} {top[i]}\n') # Make a raster version of swath 3 width = getParameter(f'{acquisition_date}_003.slc.par', 'range_samples') execute(f"rasSLC {acquisition_date}_003.slc {width} 1 0 50 10") os.chdir(wrk)
def SLC_copy_S1_fullSW(path, slcname, tabin, burst_tab, mode=2, dem=None, dempath=None, raml=10, azml=2): logging.info("Using range looks {}".format(raml)) logging.info("Using azimuth looks {}".format(azml)) logging.info("Operating in mode {}".format(mode)) logging.info("In directory {}".format(os.getcwd())) if not os.path.isfile(tabin): logging.error("ERROR: Can't find tab file {} in {}".format( tabin, os.getcwd())) f = open(tabin, "r") g = open("TAB_swFULL", "w") for line in f: s = line.split() for i in range(len(s)): g.write("{} ".format(os.path.join(path, s[i]))) g.write("\n") f.close() g.close() wrk = os.getcwd() cmd = "SLC_copy_S1_TOPS {} {} {}".format(tabin, "TAB_swFULL", burst_tab) execute(cmd, uselogging=True) shutil.copy(tabin, path) os.chdir(path) cmd = "SLC_mosaic_S1_TOPS {TAB} {SLC}.slc {SLC}.slc.par {RL} {AL}".format( TAB=tabin, SLC=slcname, RL=raml, AL=azml) execute(cmd, uselogging=True) width = getParameter("{}.slc.par".format(slcname), "range_samples") cmd = "rasSLC {}.slc {} 1 0 50 10".format(slcname, width) execute(cmd, uselogging=True) cmd = "multi_S1_TOPS {TAB} {SLC}.mli {SLC}.mli.par {RL} {AL}".format( TAB=tabin, SLC=slcname, RL=raml, AL=azml) execute(cmd, uselogging=True) mode = int(mode) if mode == 1: logging.info("currently in {}".format(os.getcwd())) logging.info("creating directory DEM") if not os.path.exists("DEM"): os.mkdir("DEM") os.chdir("DEM") mliwidth = getParameter("../{}.mli.par".format(slcname), "range_samples") mlinline = getParameter("../{}.mli.par".format(slcname), "azimuth_lines") cmd = "GC_map_mod ../{SLC}.mli.par - {DP}/{DEM}.par {DP}/{DEM}.dem 2 2 demseg.par demseg ../{SLC}.mli MAP2RDC inc pix ls_map 1 1".format( SLC=slcname, DEM=dem, DP=dempath) execute(cmd, uselogging=True) demwidth = getParameter("demseg.par", "width") cmd = "geocode MAP2RDC demseg {} HGT_SAR_{}_{} {} {}".format( demwidth, raml, azml, mliwidth, mlinline) execute(cmd, uselogging=True) cmd = "gc_map ../{SLC}.mli.par - {DP}/{DEM}.par 1 demseg.par demseg map_to_rdc 2 2 pwr_sim_map - - inc_flat".format( SLC=slcname, DP=dempath, DEM=dem) execute(cmd, uselogging=True) os.chdir("..") shutil.copy(tabin, "SLC{}_tab".format(mode)) os.chdir(wrk)
def test_execute_cmd(): cmd = 'echo "Hello world"' output = execute.execute(cmd) assert 'Hello world' in output
def process_pol(pol, type_, infile, outfile, pixel_size, height, make_tab_flag=True, gamma0_flag=False, offset=None): logging.info("Processing the {pol} polarization".format(pol=pol)) # FIXME: make_tab_flag isn't used... should it be doing something? logging.debug('Unused option make_tab_flag was {make_tab_flag}'.format( make_tab_flag=make_tab_flag)) mgrd = "{outfile}.{pol}.mgrd".format(outfile=outfile, pol=pol) utm = "{outfile}.{pol}.utm".format(outfile=outfile, pol=pol) area_map = "{outfile}_area_map.par".format(outfile=outfile) small_map = "{outfile}_small_map".format(outfile=outfile) look_fact = np.floor((pixel_size / 10.0) + 0.5) if look_fact < 1: look_fact = 1 # Ingest the granule into gamma format ingest_S1_granule(infile, pol, look_fact, mgrd) if gamma0_flag: # Convert sigma-0 to gamma-0 execute(f"radcal_MLI {mgrd} {mgrd}.par - {mgrd}.sigma - 0 0 -1", uselogging=True) execute(f"radcal_MLI {mgrd}.sigma {mgrd}.par - {mgrd}.gamma - 0 0 2", uselogging=True) shutil.move("{mgrd}.gamma".format(mgrd=mgrd), mgrd) # Blank out the bad data at the left and right edges dsx = int( getParameter("{mgrd}.par".format(mgrd=mgrd), "range_samples", uselogging=True)) dsy = int( getParameter("{mgrd}.par".format(mgrd=mgrd), "azimuth_lines", uselogging=True)) if "GRD" in type_: blank_bad_data(mgrd, dsx, dsy, left=20, right=20) # Create geocoding look up table if offset is None: offset = '-' execute( f"gec_map {mgrd}.par {offset} {area_map} {height} {small_map}.par {small_map}.utm_to_rdc", uselogging=True) # Gecode the granule out_size = getParameter("{small_map}.par".format(small_map=small_map), "width", uselogging=True) execute( f"geocode_back {mgrd} {dsx} {small_map}.utm_to_rdc {utm} {out_size}", uselogging=True) # Create the geotiff file tiffile = "{outfile}_{pol}.tif".format(outfile=outfile, pol=pol) execute(f"data2geotiff {small_map}.par {utm} 2 {tiffile}", uselogging=True)
def data2geotiff(inname, outname, dempar, type_): execute(f"data2geotiff {dempar} {inname} {type_} {outname}", uselogging=True)
def create_arc_xml(infile, outfile, input_type, gamma_flag, pwr_flag, filter_flag, looks, pol, cpol, dem_type, spacing, hyp3_ver, gamma_ver, rtc_name): print("create_arc_xml: CWD is {}".format(os.getcwd())) zone = None try: proj_name = getParameter("area.dem.par", "projection_name") if "UTM" in proj_name: zone = getParameter("area.dem.par", "projection_zone") except Exception: pass logging.info("Zone is {}".format(zone)) dem_tiles = get_dem_tile_list() # Create XML metadata files etc_dir = os.path.abspath(os.path.dirname(hyp3_rtc_gamma.etc.__file__)) back = os.getcwd() os.chdir("PRODUCT") now = datetime.datetime.now() date = now.strftime("%Y%m%d") time = now.strftime("%H%M%S") dt = now.strftime("%Y-%m-%dT%H:%M:%S") year = now.year basename = os.path.basename(infile) granulename = os.path.splitext(basename)[0] spacing = int(spacing) flooks = looks * 30 hemi = get_hemisphere(rtc_name) if gamma_flag: power_type = "gamma" else: power_type = "sigma" if pwr_flag: format_type = "power" else: format_type = "amplitude" if filter_flag: filter_str = "A" else: filter_str = "No" if input_type == "SLC": full_type = "Single-Look Complex" else: full_type = "Ground Range Detected" if "NED" in dem_type: if "13" in dem_type: resa = "1/3" resm = 10 elif "1" in dem_type: resa = 1 resm = 30 else: resa = 2 resm = 60 pcs = "WGS 1984 UTM Zone {}{}".format(zone, hemi) elif "SRTMGL" in dem_type: if "1" in dem_type: resa = 1 resm = 30 else: resa = 3 resm = 90 pcs = "WGS 1984 UTM Zone {}{}".format(zone, hemi) elif "EU_DEM" in dem_type: resa = 1 resm = 30 pcs = "WGS 1984 UTM Zone {}{}".format(zone, hemi) elif "GIMP" in dem_type: resa = 1 resm = 30 pcs = "WGS 1984 NSIDC Sea Ice Polar Stereographic North" elif "REMA" in dem_type: resa = 1 resm = 30 pcs = "WGS 1984 Antarctic Polar Stereographic" else: logging.error("Unrecognized DEM type: {}".format(dem_type)) sys.exit(1) for myfile in glob.glob("*.tif"): with open("{}.xml".format(myfile), "wb") as g: this_pol = None if cpol is None: cpol = "ZZ" if pol in myfile or cpol in myfile: template_suffix = '' encoded_jpg = pngtothumb("{}.png".format(outfile)) if pol in myfile: this_pol = pol else: this_pol = cpol elif "ls_map" in myfile: template_suffix = '_ls' execute("pbmmake 100 75 | pnmtopng > white.png", uselogging=True) encoded_jpg = pngtothumb("white.png") os.remove("white.png") elif "inc_map" in myfile: template_suffix = '_inc' encoded_jpg = pngtothumb("{}.png".format( os.path.splitext(myfile)[0])) elif "dem" in myfile: if "NED" in dem_type: template_suffix = '_dem_NED' elif "SRTM" in dem_type: template_suffix = '_dem_SRTM' elif "EU_DEM" in dem_type: template_suffix = '_dem_EUDEM' elif "GIMP" in dem_type: template_suffix = '_dem_GIMP' elif "REMA" in dem_type: template_suffix = '_dem_REMA' else: logging.error( "ERROR: Unrecognized dem type: {}".format(dem_type)) encoded_jpg = pngtothumb("{}.png".format( os.path.splitext(myfile)[0])) else: template_suffix = None encoded_jpg = None if template_suffix is not None: with open( "{}/RTC_GAMMA_Template{}.xml".format( etc_dir, template_suffix), "rb") as f: for line in f: line = line.replace(b"[DATE]", bytes(date, 'utf-8')) line = line.replace( b"[TIME]", bytes("{}00".format(time), 'utf-8')) line = line.replace(b"[DATETIME]", bytes(dt, 'utf-8')) line = line.replace(b"[YEARPROCESSED]", bytes("{}".format(year), 'utf-8')) line = line.replace(b"[YEARACQUIRED]", bytes(infile[17:21], 'utf-8')) line = line.replace(b"[TYPE]", bytes(input_type, 'utf-8')) line = line.replace(b"[FULL_TYPE]", bytes(full_type, 'utf-8')) line = line.replace(b"[THUMBNAIL_BINARY_STRING]", encoded_jpg) if this_pol is not None: line = line.replace(b"[POL]", bytes(this_pol, 'utf-8')) line = line.replace(b"[POWERTYPE]", bytes(power_type, 'utf-8')) line = line.replace(b"[GRAN_NAME]", bytes(granulename, 'utf-8')) line = line.replace(b"[FORMAT]", bytes(format_type, 'utf-8')) line = line.replace(b"[LOOKS]", bytes("{}".format(looks), 'utf-8')) line = line.replace( b"[FILT]", bytes("{}".format(filter_str), 'utf-8')) line = line.replace( b"[FLOOKS]", bytes("{}".format(flooks), 'utf-8')) line = line.replace( b"[SPACING]", bytes("{}".format(spacing), 'utf-8')) line = line.replace( b"[DEM]", bytes("{}".format(dem_type), 'utf-8')) line = line.replace(b"[RESA]", bytes("{}".format(resa), 'utf-8')) line = line.replace(b"[RESM]", bytes("{}".format(resm), 'utf-8')) line = line.replace( b"[HYP3_VER]", bytes("{}".format(hyp3_ver), 'utf-8')) line = line.replace( b"[GAMMA_VER]", bytes("{}".format(gamma_ver), 'utf-8')) line = line.replace( b"[TILES]", bytes("{}".format(dem_tiles), 'utf-8')) line = line.replace(b"[PCS]", bytes("{}".format(pcs), 'utf-8')) g.write(line + b'\n') for myfile in glob.glob("*.png"): with open("{}.xml".format(myfile), "wb") as g: if "rgb" in myfile: scale = 'color' encoded_jpg = pngtothumb("{}_rgb.png".format(outfile)) else: scale = 'grayscale' encoded_jpg = pngtothumb("{}.png".format(outfile)) with open( "{}/RTC_GAMMA_Template_{}_png.xml".format(etc_dir, scale), "rb") as f: for line in f: line = line.replace(b"[DATE]", bytes(date, 'utf-8')) line = line.replace(b"[TIME]", bytes("{}00".format(time), 'utf-8')) line = line.replace(b"[DATETIME]", bytes(dt, 'utf-8')) line = line.replace(b"[YEARPROCESSED]", bytes("{}".format(year), 'utf-8')) line = line.replace(b"[YEARACQUIRED]", bytes(infile[17:21], 'utf-8')) line = line.replace(b"[TYPE]", bytes(input_type, 'utf-8')) line = line.replace(b"[FULL_TYPE]", bytes(full_type, 'utf-8')) line = line.replace(b"[THUMBNAIL_BINARY_STRING]", encoded_jpg) line = line.replace(b"[GRAN_NAME]", bytes(granulename, 'utf-8')) line = line.replace(b"[SPACING]", bytes("{}".format(spacing), 'utf-8')) line = line.replace(b"[DEM]", bytes("{}".format(dem_type), 'utf-8')) line = line.replace(b"[FORMAT]", bytes(format_type, 'utf-8')) line = line.replace(b"[HYP3_VER]", bytes("{}".format(hyp3_ver), 'utf-8')) line = line.replace(b"[GAMMA_VER]", bytes("{}".format(gamma_ver), 'utf-8')) line = line.replace(b"[DEM_TILES]", bytes("{}".format(dem_tiles), 'utf-8')) line = line.replace(b"[PCS]", bytes("{}".format(pcs), 'utf-8')) g.write(line + b"\n") with open(f'{outfile}.README.txt', 'w') as g: with open("{}/README_RTC_GAMMA.txt".format(etc_dir), "r") as f: for line in f: line = line.replace("[DATE]", date) line = line.replace("[TIME]", "{}00".format(time)) line = line.replace("[DATETIME]", dt) line = line.replace("[GRAN_NAME]", granulename) line = line.replace("[YEARPROCESSED]", "{}".format(year)) line = line.replace("[YEARACQUIRED]", infile[17:21]) line = line.replace("[POWERTYPE]", power_type) line = line.replace("[FORMAT]", format_type) line = line.replace("[LOOKS]", "{}".format(looks)) line = line.replace("[FILT]", "{}".format(filter_str)) line = line.replace("[FLOOKS]", "{}".format(flooks)) line = line.replace("[SPACING]", "{}".format(spacing)) line = line.replace("[DEM]", "{}".format(dem_type)) line = line.replace("[RESA]", "{}".format(resa)) line = line.replace("[RESM]", "{}".format(resm)) line = line.replace("[HYP3_VER]", "{}".format(hyp3_ver)) line = line.replace("[GAMMA_VER]", "{}".format(gamma_ver)) line = line.replace("[DEM_TILES]", "{}".format(dem_tiles)) line = line.replace("[PCS]", "{}".format(pcs)) g.write("{}".format(line)) os.chdir(back)
def proc_s1_isce(ss, reference, secondary, gbb=None, xml=False, unwrap=False, dem=None): """Main process ss = subswath to process reference = reference SAFE file secondary = secondary SAFE file gbb = set a geocoding bounding box (south north west east) xml = if True, only create XML file, do not run unwrap = if True, turn on unwrapping dem = Specify external DEM file to use """ options = {'unwrap': unwrap, 'roi': False, 'proc': xml, 'gbb': False, 'dem': False} if gbb is not None: options['gbb'] = True options['gbb_south'] = gbb[0] options['gbb_north'] = gbb[1] options['gbb_west'] = gbb[2] options['gbb_east'] = gbb[3] if dem is not None: options['dem'] = True options['demname'] = dem # g1 and g2 are the two granules that we are processing g1 = reference g2 = secondary t = re.split('_+', g1) md = t[4][0:16] t = re.split('_+', g2) sd = t[4][0:16] bname = '%s_%s' % (md, sd) ssname = 'iw'+str(ss) isce_dir = os.path.join(bname, ssname) options['bname'] = bname options['ss'] = ssname options['swath'] = ss print(g1, g2, options) mkdir_p(bname) mkdir_p(isce_dir) g1_orbit_file, _ = downloadSentinelOrbitFile(g1, directory=isce_dir) g2_orbit_file, _ = downloadSentinelOrbitFile(g2, directory=isce_dir) create_isce_xml(g1, g2, os.path.basename(g1_orbit_file), os.path.basename(g2_orbit_file), options) # execute(f'cd {isce_dir} ; topsApp.py --end=preprocess') # execute(f'cd {isce_dir} ; topsApp.py --start=computeBaselines --end=filter') # if options['unwrap'] == True: # execute(f'cd {isce_dir} ; topsApp.py --dostep=unwrap') # execute(f'cd {isce_dir} ; topsApp.py --dostep=geocode') if options['proc']: execute(f'cd {isce_dir} ; topsApp.py')
def create_iso_xml(outfile, out_name, pol, cpol, in_file, dem_type, log, gamma_ver): hdf5_name = "hdf5_list.txt" path = in_file etc_dir = os.path.abspath(os.path.dirname(hyp3_rtc_gamma.etc.__file__)) shutil.copy("{}/sentinel_xml.xsl".format(etc_dir), "sentinel_xml.xsl") out = "PRODUCT" execute(f"xsltproc --stringparam path {path} --stringparam timestamp timestring" f" --stringparam file_size 1000 --stringparam server stuff" f" --output out.xml sentinel_xml.xsl {path}/manifest.safe", uselogging=True) m = sentinel2meta("out.xml") write_asf_meta(m, "out.meta") ver_file = "{}/manifest.safe".format(path) ipf_ver = None if os.path.exists(ver_file): f = open(ver_file, "r") for line in f: if "IPF" in line: t = line.split('"') ipf_ver = t[3].strip() else: logging.warning("No manifest.safe file found in {}".format(path)) g = open(hdf5_name, "w") g.write("[GAMMA RTC]\n") g.write("granule = {}\n".format(in_file.replace(".SAFE", ""))) g.write("metadata = out.meta\n") geo_dir = "geo_{}".format(pol) dem_seg = "{}/area.dem".format(geo_dir) dem_seg_par = "{}/area.dem_par".format(geo_dir) g.write("oversampled dem file = {}\n".format(dem_seg)) g.write("oversampled dem metadata = {}\n".format(dem_seg_par)) g.write("original dem file = {}/{}_dem.tif\n".format(out, out_name)) g.write("layover shadow mask = {}/{}_ls_map.tif\n".format(out, out_name)) g.write("layover shadow stats = {}/ls_map.stat\n".format(geo_dir)) g.write("incidence angle file = {}/{}_inc_map.tif\n".format(out, out_name)) g.write("incidence angle metadata = {}/inc_map.meta\n".format(geo_dir)) g.write("input {} file = {}\n".format(pol, outfile)) g.write("terrain corrected {pol} metadata = {dir}/tc_{pol}.meta\n".format(pol=pol, dir=geo_dir)) g.write("terrain corrected {} file = {}/{}\n".format(pol, out, outfile)) if cpol: outfile2 = outfile.replace(pol, cpol) g.write("input {} file = {}\n".format(pol, outfile)) geo_dir2 = geo_dir.replace(pol, cpol) g.write("terrain corrected {pol} metadata = {dir}/tc_{pol}.meta\n".format(pol=cpol, dir=geo_dir2)) g.write("terrain corrected {} file = {}/{}\n".format(cpol, out, outfile2)) g.write("initial processing log = {}\n".format(log)) g.write("terrain correction log = {}\n".format(log)) g.write("main log = {}\n".format(log)) g.write("mk_geo_radcal_0 log = {}/mk_geo_radcal_0.log\n".format(geo_dir)) g.write("mk_geo_radcal_1 log = {}/mk_geo_radcal_1.log\n".format(geo_dir)) g.write("mk_geo_radcal_2 log = {}/mk_geo_radcal_2.log\n".format(geo_dir)) g.write("mk_geo_radcal_3 log = {}/mk_geo_radcal_3.log\n".format(geo_dir)) g.write("coreg_check log = coreg_check.log\n") g.write("mli.par file = {}.{}.mgrd.par\n".format(out_name, pol)) g.write("gamma version = {}\n".format(gamma_ver)) g.write("hyp3_rtc version = {}\n".format(hyp3_rtc_gamma.__version__)) g.write("ipf version = {}\n".format(ipf_ver)) g.write("dem source = {}\n".format(dem_type)) g.write("browse image = {}/{}.png\n".format(out, out_name)) g.write("kml overlay = {}/{}.kmz\n".format(out, out_name)) g.close() execute(f"write_hdf5_xml {hdf5_name} {out_name}.xml", uselogging=True) logging.info("Generating {}.iso.xml with {}/rtc_iso.xsl\n".format(out_name, etc_dir)) execute(f"xsltproc {etc_dir}/rtc_iso.xsl {out_name}.xml > {out_name}.iso.xml", uselogging=True) shutil.copy("{}.iso.xml".format(out_name), "{}".format(out))
def process_2nd_pol(in_file, rtc_name, cpol, res, look_fact, gamma_flag, filter_flag, pwr_flag, browse_res, outfile, dem, terms, par=None, area=False, orbit_file=None): if cpol == "VH": mpol = "VV" else: mpol = "HH" mgrd = "{out}.{pol}.mgrd".format(out=outfile, pol=cpol) tif = "image_cal_map.mli.tif" # Ingest the granule into gamma format ingest_S1_granule(in_file, cpol, look_fact, mgrd, orbit_file=orbit_file) width = getParameter("{}.par".format(mgrd), "range_samples") # Apply filtering if requested if filter_flag: el_looks = look_fact * 30 execute(f"enh_lee {mgrd} temp.mgrd {width} {el_looks} 1 7 7", uselogging=True) shutil.move("temp.mgrd", mgrd) options = "-p -n {} -q -c ".format(terms) if gamma_flag: options += "-g " home_dir = os.getcwd() geo_dir = "geo_{}".format(cpol) mdir = "geo_{}".format(mpol) if not os.path.isdir(geo_dir): os.mkdir(geo_dir) shutil.copy("geo_{}/image.diff_par".format(mpol), "{}".format(geo_dir)) os.symlink("../geo_{}/image_0.map_to_rdc".format(mpol), "{}/image_0.map_to_rdc".format(geo_dir)) os.symlink("../geo_{}/image_0.ls_map".format(mpol), "{}/image_0.ls_map".format(geo_dir)) os.symlink("../geo_{}/image_0.inc_map".format(mpol), "{}/image_0.inc_map".format(geo_dir)) os.symlink("../geo_{}/image_0.sim".format(mpol), "{}/image_0.sim".format(geo_dir)) os.symlink("../geo_{}/area.dem_par".format(mpol), "{}/area.dem_par".format(geo_dir)) if par: shutil.copy(par, "{}/image.diff_par".format(geo_dir)) execute(f"mk_geo_radcal {mgrd} {mgrd}.par {dem} {dem}.par {mdir}/area.dem" f" {mdir}/area.dem_par {geo_dir} image {res} 3 {options}", uselogging=True) os.chdir(geo_dir) # Divide sigma0 by sin(theta) to get beta0 execute(f"float_math image_0.inc_map - image_1.sin_theta {width} 7 - - 1 1 - 0") execute(f"float_math image_cal_map.mli image_1.sin_theta image_1.beta {width} 3 - - 1 1 - 0") execute(f"float_math image_1.beta image_0.sim image_1.flat {width} 3 - - 1 1 - 0") # Make geotiff file if gamma_flag: gdal.Translate("tmp.tif", tif, metadataOptions=['Band1={}_gamma0'.format(cpol)]) else: gdal.Translate("tmp.tif", tif, metadataOptions=['Band1={}_sigma0'.format(cpol)]) shutil.move("tmp.tif", tif) # Make browse resolution file createAmp(tif, nodata=0) if res == browse_res: shutil.copy("image_cal_map.mli_amp.tif", "{}_{}_{}m.tif".format(outfile, cpol, browse_res)) else: gdal.Translate("{}_{}_{}m.tif".format(outfile, cpol, browse_res), "image_cal_map.mli_amp.tif", xRes=browse_res, yRes=browse_res) # Create meta files and stats execute(f"asf_import -format geotiff image_cal_map.mli_amp.tif tc_{cpol}", uselogging=True) execute(f"stats -nostat -overmeta -mask 0 tc_{cpol}", uselogging=True) # Move files to product directory out_dir = "../PRODUCT" if not os.path.exists(out_dir): os.mkdir(out_dir) execute(f"data2geotiff area.dem_par image_1.flat 2 {outfile}.flat.tif", uselogging=True) if pwr_flag: shutil.move(tif, "{}/{}".format(out_dir, rtc_name)) else: copy_metadata(tif, "image_cal_map.mli_amp.tif") shutil.move("image_cal_map.mli_amp.tif", "{}/{}".format(out_dir, rtc_name)) if area: shutil.move("{}.flat.tif".format(outfile), "{}/{}_flat_{}.tif".format(out_dir, rtc_name, cpol)) os.chdir(home_dir)
def process_pol(in_file, rtc_name, out_name, pol, res, look_fact, match_flag, dead_flag, gamma_flag, filter_flag, pwr_flag, browse_res, dem, terms, par=None, area=False, orbit_file=None): logging.info("Processing the {} polarization".format(pol)) mgrd = "{out}.{pol}.mgrd".format(out=out_name, pol=pol) tif = "image_cal_map.mli.tif" # Ingest the granule into gamma format ingest_S1_granule(in_file, pol, look_fact, mgrd, orbit_file=orbit_file) width = getParameter("{}.par".format(mgrd), "range_samples") # Apply filter if requested if filter_flag: el_looks = look_fact * 30 execute(f"enh_lee {mgrd} temp.mgrd {width} {el_looks} 1 7 7", uselogging=True) shutil.move("temp.mgrd", mgrd) options = "-p -n {} -q -c ".format(terms) if gamma_flag: options += "-g " logging.info("Running RTC process... initializing") geo_dir = "geo_{}".format(pol) execute(f"mk_geo_radcal {mgrd} {mgrd}.par {dem} {dem}.par {geo_dir}/area.dem" f" {geo_dir}/area.dem_par {geo_dir} image {res} 0 {options}", uselogging=True) if match_flag and not par: fail = False logging.info("Running RTC process... coarse matching") try: execute(f"mk_geo_radcal {mgrd} {mgrd}.par {dem} {dem}.par {geo_dir}/area.dem" f" {geo_dir}/area.dem_par {geo_dir} image {res} 1 {options}", uselogging=True) except ExecuteError: logging.warning("WARNING: Determination of the initial offset failed, skipping initial offset") logging.info("Running RTC process... fine matching") try: execute(f"mk_geo_radcal {mgrd} {mgrd}.par {dem} {dem}.par {geo_dir}/area.dem" f" {geo_dir}/area.dem_par {geo_dir} image {res} 2 {options}", uselogging=True) except ExecuteError: if not dead_flag: logging.error("ERROR: Failed to match images") sys.exit(1) else: logging.warning("WARNING: Coregistration has failed; defaulting to dead reckoning") os.remove("{}/{}".format(geo_dir, "image.diff_par")) fail = True if not fail: try: check_coreg(out_name, res, max_offset=75, max_error=2.0) except CoregistrationError: if not dead_flag: logging.error("ERROR: Failed the coregistration check") sys.exit(1) else: logging.warning("WARNING: Coregistration check has failed; defaulting to dead reckoning") os.remove("{}/{}".format(geo_dir, "image.diff_par")) logging.info("Running RTC process... finalizing") if par: shutil.copy(par, "{}/image.diff_par".format(geo_dir)) execute(f"mk_geo_radcal {mgrd} {mgrd}.par {dem} {dem}.par {geo_dir}/area.dem" f" {geo_dir}/area.dem_par {geo_dir} image {res} 3 {options}", uselogging=True) os.chdir(geo_dir) # Divide sigma0 by sin(theta) to get beta0 execute(f"float_math image_0.inc_map - image_1.sin_theta {width} 7 - - 1 1 - 0") execute(f"float_math image_cal_map.mli image_1.sin_theta image_1.beta {width} 3 - - 1 1 - 0") execute(f"float_math image_1.beta image_0.sim image_1.flat {width} 3 - - 1 1 - 0") # Make Geotiff Files execute(f"data2geotiff area.dem_par image_0.ls_map 5 {out_name}.ls_map.tif", uselogging=True) execute(f"data2geotiff area.dem_par image_0.inc_map 2 {out_name}.inc_map.tif", uselogging=True) execute(f"data2geotiff area.dem_par image_1.flat 2 {out_name}.flat.tif", uselogging=True) execute("data2geotiff area.dem_par area.dem 2 outdem.tif", uselogging=True) gdal.Translate("{}.dem.tif".format(out_name), "outdem.tif", outputType=gdal.GDT_Int16) if gamma_flag: gdal.Translate("tmp.tif", tif, metadataOptions=['Band1={}_gamma0'.format(pol)]) else: gdal.Translate("tmp.tif", tif, metadataOptions=['Band1={}_sigma0'.format(pol)]) shutil.move("tmp.tif", tif) createAmp(tif, nodata=0) # Make meta files and stats execute(f"asf_import -format geotiff {out_name}.ls_map.tif ls_map", uselogging=True) execute("stats -overstat -overmeta ls_map", uselogging=True) execute(f"asf_import -format geotiff {out_name}.inc_map.tif inc_map", uselogging=True) execute("stats -overstat -overmeta -mask 0 inc_map", uselogging=True) execute(f"asf_import -format geotiff image_cal_map.mli_amp.tif tc_{pol}", uselogging=True) execute(f"stats -nostat -overmeta -mask 0 tc_{pol}", uselogging=True) # Make browse resolution tif file if res == browse_res: shutil.copy("image_cal_map.mli_amp.tif", "{}_{}_{}m.tif".format(out_name, pol, browse_res)) else: gdal.Translate("{}_{}_{}m.tif".format(out_name, pol, browse_res), "image_cal_map.mli_amp.tif", xRes=browse_res, yRes=browse_res) # Move files into the product directory out_dir = "../PRODUCT" if not os.path.exists(out_dir): os.mkdir(out_dir) if pwr_flag: shutil.move(tif, "{}/{}".format(out_dir, rtc_name)) else: copy_metadata(tif, "image_cal_map.mli_amp.tif") shutil.move("image_cal_map.mli_amp.tif", "{}/{}".format(out_dir, rtc_name)) shutil.move("{}.ls_map.tif".format(out_name), "{}/{}_ls_map.tif".format(out_dir, out_name)) shutil.move("{}.inc_map.tif".format(out_name), "{}/{}_inc_map.tif".format(out_dir, out_name)) shutil.move("{}.dem.tif".format(out_name), "{}/{}_dem.tif".format(out_dir, out_name)) if area: shutil.move("{}.flat.tif".format(out_name), "{}/{}_flat_{}.tif".format(out_dir, out_name, pol)) os.chdir("..")
def utm2dem(inDem, outDem, demPar, dataType="float"): demParIn = "dem_par.in" dataType = dataType.lower() basename = os.path.basename(inDem) logname = basename + "_utm_dem.log" log = open(logname, "w") print("UTM DEM in GEOTIFF format: {}".format(inDem)) print("output DEM: {}".format(outDem)) print("output DEM parameter file: {}".format(demPar)) print("log file: {}".format(logname)) (x, y, trans, proj, data) = saa.read_gdal_file(saa.open_gdal_file(inDem)) xsize = x ysize = y east = trans[0] north = trans[3] pix_east = trans[1] pix_north = trans[5] ds = gdal.Open(inDem) prj = ds.GetProjection() s = prj.split("[") for t in s: if "false_northing" in t: u = t.split('"') v = u[2].split(",") w = v[1].split("]") false_north = w[0] print("found false_north {}".format(false_north)) srs = osr.SpatialReference(wkt=prj) string = srs.GetAttrValue('projcs') t = string.split(" ") zone = t[5] print("Found zone string {} of length {}".format(zone, len(zone))) if len(zone) == 3: zone = zone[0:2] else: zone = zone[0] print("found zone {}".format(zone)) src = gdal.Open(inDem, gdalconst.GA_ReadOnly) string = src.GetMetadata() pixasarea = string["AREA_OR_POINT"] if "AREA" in pixasarea: print("Pixel as Area! Updating corner coordinates to pixel as point") print("pixel upper northing (m): {} easting (m): {}".format( north, east)) east = east + pix_east / 2.0 north = north + pix_north / 2.0 print("Update pixel upper northing (m): {} easting (m): {}".format( north, east)) pix_size = pix_east print("approximate DEM latitude pixel spacing (m): {}".format(pix_size)) # Create the input file for create_dem_par f = open(demParIn, "w") f.write("UTM\n") f.write("WGS84\n") f.write("1\n") f.write("{}\n".format(zone)) f.write("{}\n".format(false_north)) f.write("{}\n".format(basename)) if "float" in dataType: f.write("REAL*4\n") elif "int16" in dataType: f.write("INTEGER*2\n") f.write("0.0\n") f.write("1.0\n") f.write("{}\n".format(xsize)) f.write("{}\n".format(ysize)) f.write("{} {}\n".format(pix_north, pix_east)) f.write("{} {}\n".format(north, east)) f.close() # Create a new dem par file if os.path.isfile(demPar): os.remove(demPar) execute("create_dem_par {} < {}".format(demPar, demParIn), logfile=log) # Replace 0 with 1; Replace anything <= -32767 with 0; byteswap data[data == 0] = 1 data[data <= -32767] = 0 data = data.byteswap() # Convert to ENVI (binary) format tmptif = "temporary_dem_file.tif" if "float" in dataType: saa.write_gdal_file_float(tmptif, trans, proj, data.astype(np.float32)) elif "int16" in dataType: saa.write_gdal_file(tmptif, trans, proj, data) gdal.Translate(outDem, tmptif, format="ENVI") os.remove(tmptif) os.remove(outDem + ".aux.xml") filename, file_extension = os.path.splitext(outDem) os.remove(outDem.replace(file_extension, ".hdr"))
def isce_process(bname, ss, step): cmd = 'cd {bname}/{ss} ; topsApp.py {step}'.format(bname=bname, ss=ss, step=step) execute(cmd)
def geocode_sentinel(infile, outfile, pixel_size=30.0, height=0, gamma0_flag=False, post=None, offset=None): if not os.path.exists(infile): logging.error("ERROR: Input file {} does not exist".format(infile)) exit(1) if "zip" in infile: zip_ref = zipfile.ZipFile(infile, 'r') zip_ref.extractall(".") zip_ref.close() infile = infile.replace(".zip", ".SAFE") type_ = 'GRD' if 'GRD' in infile else 'SLC' # Create par file covering the area we want to geocode lat_max, lat_min, lon_max, lon_min = get_bounding_box_file(infile) logging.debug("Input Coordinates: {} {} {} {}".format( lat_max, lat_min, lon_max, lon_min)) area_map = f"{outfile}_area_map" demParIn = create_dem_par(area_map, "float", pixel_size, lat_max, lat_min, lon_max, lon_min, post) execute(f"create_dem_par {area_map}.par < {demParIn}", uselogging=True) # Get list of files to process vvlist = glob.glob("{}/*/*vv*.tiff".format(infile)) vhlist = glob.glob("{}/*/*vh*.tiff".format(infile)) hhlist = glob.glob("{}/*/*hh*.tiff".format(infile)) hvlist = glob.glob("{}/*/*hv*.tiff".format(infile)) pol = None cross_pol = None if vvlist: pol = "vv" process_pol(pol, type_, infile, outfile, pixel_size, height, make_tab_flag=True, gamma0_flag=gamma0_flag, offset=offset) if vhlist: process_pol("vh", type_, infile, outfile, pixel_size, height, make_tab_flag=False, gamma0_flag=gamma0_flag, offset=offset) cross_pol = "vh" if hhlist: pol = "hh" process_pol(pol, type_, infile, outfile, pixel_size, height, make_tab_flag=True, gamma0_flag=gamma0_flag, offset=offset) if hvlist: process_pol("hv", type_, infile, outfile, pixel_size, height, make_tab_flag=False, gamma0_flag=gamma0_flag, offset=offset) cross_pol = "hv" make_products(outfile, pol, cp=cross_pol) create_xml_files(infile, outfile, height, type_, gamma0_flag, pixel_size)
def unwrapping_geocoding(reference, secondary, step="man", rlooks=10, alooks=2, trimode=0, npatr=1, npata=1, alpha=0.6): dem = "./DEM/demseg" dempar = "./DEM/demseg.par" lt = "./DEM/MAP2RDC" ifgname = "{}_{}".format(reference, secondary) offit = "{}.off.it".format(ifgname) mmli = reference + ".mli" smli = secondary + ".mli" if not os.path.isfile(dempar): log.error("ERROR: Unable to find dem par file {}".format(dempar)) if not os.path.isfile(lt): log.error("ERROR: Unable to find look up table file {}".format(lt)) if not os.path.isfile(offit): log.error("ERROR: Unable to find offset file {}".format(offit)) width = getParameter(offit, "interferogram_width") mwidth = getParameter(mmli + ".par", "range_samples") swidth = getParameter(smli + ".par", "range_samples") demw = getParameter(dempar, "width") demn = getParameter(dempar, "nlines") ifgf = "{}.diff0.{}".format(ifgname, step) log.info("{} will be used for unwrapping and geocoding".format(ifgf)) log.info("-------------------------------------------------") log.info(" Start unwrapping") log.info("-------------------------------------------------") execute(f"cc_wave {ifgf} {mmli} - {ifgname}.cc {width}", uselogging=True) execute( f"rascc {ifgname}.cc {mmli} {width} 1 1 0 1 1 .1 .9" f" - - - {ifgname}.cc.ras", uselogging=True) execute(f"adf {ifgf} {ifgf}.adf {ifgname}.adf.cc {width} {alpha} - 5", uselogging=True) execute(f"rasmph_pwr {ifgf}.adf {mmli} {width}", uselogging=True) execute( f"rascc {ifgname}.adf.cc {mmli} {width} 1 1 0 1 1 .1 .9" f" - - - {ifgname}.adf.cc.ras", uselogging=True) execute(f"rascc_mask {ifgname}.adf.cc {mmli} {width} 1 1 0 1 1 0.10 0.20 ", uselogging=True) execute( f"mcf {ifgf}.adf {ifgname}.adf.cc {ifgname}.adf.cc_mask.bmp {ifgname}.adf.unw {width} {trimode} 0 0" f" - - {npatr} {npata}", uselogging=True) execute( f"rasrmg {ifgname}.adf.unw {mmli} {width} 1 1 0 1 1 0.33333 1.0 .35 0.0" f" - {ifgname}.adf.unw.ras", uselogging=True) execute( f"dispmap {ifgname}.adf.unw DEM/HGT_SAR_{rlooks}_{alooks} {mmli}.par" f" - {ifgname}.vert.disp 1", uselogging=True) execute(f"rashgt {ifgname}.vert.disp - {width} 1 1 0 1 1 0.028", uselogging=True) execute( f"dispmap {ifgname}.adf.unw DEM/HGT_SAR_{rlooks}_{alooks} {mmli}.par" f" - {ifgname}.los.disp 0", uselogging=True) execute(f"rashgt {ifgname}.los.disp - {width} 1 1 0 1 1 0.028", uselogging=True) log.info("-------------------------------------------------") log.info(" End unwrapping") log.info("-------------------------------------------------") log.info("-------------------------------------------------") log.info(" Start geocoding") log.info("-------------------------------------------------") geocode_back(mmli, mmli + ".geo", mwidth, lt, demw, demn, 0) geocode_back(smli, smli + ".geo", swidth, lt, demw, demn, 0) geocode_back("{}.sim_unw".format(ifgname), "{}.sim_unw.geo".format(ifgname), width, lt, demw, demn, 0) geocode_back("{}.adf.unw".format(ifgname), "{}.adf.unw.geo".format(ifgname), width, lt, demw, demn, 0) geocode_back("{}.adf".format(ifgf), "{}.adf.geo".format(ifgf), width, lt, demw, demn, 1) geocode_back("{}.adf.unw.ras".format(ifgname), "{}.adf.unw.geo.bmp".format(ifgname), width, lt, demw, demn, 2) geocode_back("{}.adf.bmp".format(ifgf), "{}.adf.bmp.geo".format(ifgf), width, lt, demw, demn, 2) geocode_back("{}.cc".format(ifgname), "{}.cc.geo".format(ifgname), width, lt, demw, demn, 0) geocode_back("{}.adf.cc".format(ifgname), "{}.adf.cc.geo".format(ifgname), width, lt, demw, demn, 0) geocode_back("{}.vert.disp.bmp".format(ifgname), "{}.vert.disp.bmp.geo".format(ifgname), width, lt, demw, demn, 2) geocode_back("{}.vert.disp".format(ifgname), "{}.vert.disp.geo".format(ifgname), width, lt, demw, demn, 0) geocode_back("{}.los.disp.bmp".format(ifgname), "{}.los.disp.bmp.geo".format(ifgname), width, lt, demw, demn, 2) geocode_back("{}.los.disp".format(ifgname), "{}.los.disp.geo".format(ifgname), width, lt, demw, demn, 0) data2geotiff(mmli + ".geo", mmli + ".geo.tif", dempar, 2) data2geotiff(smli + ".geo", smli + ".geo.tif", dempar, 2) data2geotiff("{}.sim_unw.geo".format(ifgname), "{}.sim_unw.geo.tif".format(ifgname), dempar, 2) data2geotiff("{}.adf.unw.geo".format(ifgname), "{}.adf.unw.geo.tif".format(ifgname), dempar, 2) data2geotiff("{}.adf.unw.geo.bmp".format(ifgname), "{}.adf.unw.geo.bmp.tif".format(ifgname), dempar, 0) data2geotiff("{}.adf.bmp.geo".format(ifgf), "{}.adf.bmp.geo.tif".format(ifgf), dempar, 0) data2geotiff("{}.cc.geo".format(ifgname), "{}.cc.geo.tif".format(ifgname), dempar, 2) data2geotiff("{}.adf.cc.geo".format(ifgname), "{}.adf.cc.geo.tif".format(ifgname), dempar, 2) data2geotiff("DEM/demseg", "{}.dem.tif".format(ifgname), dempar, 2) data2geotiff("{}.vert.disp.bmp.geo".format(ifgname), "{}.vert.disp.geo.tif".format(ifgname), dempar, 0) data2geotiff("{}.vert.disp.geo".format(ifgname), "{}.vert.disp.geo.org.tif".format(ifgname), dempar, 2) data2geotiff("{}.los.disp.bmp.geo".format(ifgname), "{}.los.disp.geo.tif".format(ifgname), dempar, 0) data2geotiff("{}.los.disp.geo".format(ifgname), "{}.los.disp.geo.org.tif".format(ifgname), dempar, 2) data2geotiff("DEM/inc_flat", "{}.inc.tif".format(ifgname), dempar, 2) execute(f"look_vector {mmli}.par {offit} {dempar} {dem} lv_theta lv_phi", uselogging=True) data2geotiff("lv_theta", "{}.lv_theta.tif".format(ifgname), dempar, 2) data2geotiff("lv_phi", "{}.lv_phi.tif".format(ifgname), dempar, 2) log.info("-------------------------------------------------") log.info(" End geocoding") log.info("-------------------------------------------------")
def coregister_data(cnt, SLC2tab, SLC2Rtab, spar, mpar, mmli, smli, ifgname, reference, secondary, lt, rlooks, alooks, iterations): if cnt < iterations + 1: offi = ifgname + ".off_{}".format(cnt) else: offi = ifgname + ".off.it.corrected.temp" if cnt == 0: offit = "-" elif cnt < iterations + 1: offit = ifgname + ".off.it" else: offit = ifgname + ".off.it.corrected" SLC1tab = "SLC1_tab" srslc = secondary + ".rslc" srpar = secondary + ".rslc.par" execute( f"SLC_interp_lt_S1_TOPS {SLC2tab} {spar} {SLC1tab} {mpar} {lt}" f" {mmli} {smli} {offit} {SLC2Rtab} {srslc} {srpar}", uselogging=True) execute(f"create_offset {mpar} {spar} {offi} 1 {rlooks} {alooks} 0", uselogging=True) if cnt < iterations + 1: cmd_sfx = "256 64 offsets 1 64 256 0.2" else: cmd_sfx = "512 256 - 1 16 64 0.2" execute( f"offset_pwr {reference}.slc {secondary}.rslc {mpar} {srpar} {offi} offs snr {cmd_sfx}", uselogging=True) with open("offsetfit{}.log".format(cnt), "w") as log: execute(f"offset_fit offs snr {offi} - - 0.2 1", uselogging=True, logfile=log) if cnt < iterations + 1: ifg_diff_sfx = f'it{cnt}' else: ifg_diff_sfx = 'man' execute( f"SLC_diff_intf {reference}.slc {secondary}.rslc {mpar} {srpar} {offi}" f" {ifgname}.sim_unw {ifgname}.diff0.{ifg_diff_sfx} {rlooks} {alooks} 0 0", uselogging=True) width = getParameter(offi, "interferogram_width") f"offset_add {offit} {offi} {offi}.temp" execute( f"rasmph_pwr {ifgname}.diff0.{ifg_diff_sfx} {reference}.mli {width} 1 1 0 3 3", uselogging=True) if cnt == 0: offit = ifgname + ".off.it" shutil.copy(offi, offit) elif cnt < iterations + 1: execute(f"offset_add {offit} {offi} {offi}.temp", uselogging=True) shutil.copy("{}.temp".format(offi), offit) else: execute(f"offset_add {offit} {offi} {offi}.out", uselogging=True)
def check_projection(tmpDir, preFullpol, preCrosspol, postFullpol, postCrosspol): pre = gdal.Open(preFullpol) gt = pre.GetGeoTransform() pixelSize = gt[1] preSpatialRef = osr.SpatialReference() preSpatialRef.ImportFromWkt(pre.GetProjectionRef()) post = gdal.Open(postFullpol) postSpatialRef = osr.SpatialReference() postSpatialRef.ImportFromWkt(post.GetProjectionRef()) geoSpatialRef = osr.SpatialReference() geoSpatialRef.ImportFromEPSG(4326) preCoordTrans = osr.CoordinateTransformation(preSpatialRef, geoSpatialRef) postCoordTrans = osr.CoordinateTransformation(postSpatialRef, geoSpatialRef) prePoly = geotiff2polygon(preFullpol) prePoly.Transform(preCoordTrans) preCentroid = prePoly.Centroid() preUtm = int((preCentroid.GetX() + 180.0) / 6.0 + 1.0) postPoly = geotiff2polygon(postFullpol) postPoly.Transform(postCoordTrans) postCentroid = postPoly.Centroid() postUtm = int((postCentroid.GetX() + 180.0) / 6.0 + 1.0) overlap = prePoly.Intersection(postPoly) overlapCentroid = overlap.Centroid() overlapUtm = int((overlapCentroid.GetX() + 180.0) / 6.0 + 1.0) if preUtm != postUtm: print('Pre- and post-event images in different UTM zones.') if preUtm == overlapUtm: print('Reprojecting post-event images to UTM zone %d' % overlapUtm) if postCentroid.GetY() > 0.0: proj = ('EPSG:326{0}'.format(preUtm)) else: proj = ('EPSG:327{0}'.format(preUtm)) fullpol = os.path.join(tmpDir, 'postFullpol.tif') cmd = ("gdalwarp -r bilinear -tr %f %f -t_srs %s %s %s" % \ (pixelSize, pixelSize, proj, postFullpol, fullpol)) execute(cmd) postFullpol = fullpol crosspol = os.path.join(tmpDir, 'postCrosspol.tif') cmd = ("gdalwarp -r bilinear -tr %f %f -t_srs %s %s %s" % \ (pixelSize, pixelSize, proj, postCrosspol, crosspol)) execute(cmd) postCrosspol = crosspol if postUtm == overlapUtm: print('Reprojecting post-event images to UTM zone %d' % overlapUtm) if postCentroid.GetY() > 0.0: proj = ('EPSG:326{0}'.format(postUtm)) else: proj = ('EPSG:327{0}'.format(postUtm)) fullpol = os.path.join(tmpDir, 'preFullpol.tif') execute("gdalwarp -r bilinear -tr %f %f -t_srs %s %s %s" % \ (pixelSize, pixelSize, proj, preFullpol, fullpol)) preFullpol = fullpol crosspol = os.path.join(tmpDir, 'preCrosspol.tif') execute("gdalwarp -r bilinear -tr %f %f -t_srs %s %s %s" % \ (pixelSize, pixelSize, proj, preCrosspol, crosspol)) preCrosspol = crosspol return (preFullpol, preCrosspol, postFullpol, postCrosspol)
def gamma_process(reference_file, secondary_file, rlooks=20, alooks=4, look_flag=False, los_flag=False): log.info("\n\nSentinel-1 differential interferogram creation program\n") wrk = os.getcwd() reference_date = reference_file[17:32] reference_date_short = reference_file[17:25] secondary_date = secondary_file[17:32] secondary_date_short = secondary_file[17:25] igramName = "{}_{}".format(reference_date, secondary_date) if "IW_SLC__" not in reference_file: raise GranuleError(f'Reference file {reference_file} is not of type IW_SLC!') if "IW_SLC__" not in secondary_file: raise GranuleError(f'Secondary file {secondary_file} is not of type IW_SLC!') pol = get_copol(reference_file) log.info("Processing the {} polarization".format(pol)) # Ingest the data files into gamma format log.info("Starting par_S1_SLC") orbit_files = [] for granule in (reference_file, secondary_file): orbit_file, _ = downloadSentinelOrbitFile(granule) par_s1_slc_single(granule, pol, os.path.abspath(orbit_file)) orbit_files.append(orbit_file) # Fetch the DEM file log.info("Getting a DEM file") dem, dem_source = get_dem_file_gamma(reference_file, alooks) log.info("Got dem of type {}".format(dem_source)) # Figure out which bursts overlap between the two swaths burst_tab1, burst_tab2 = get_burst_overlaps(reference_file, secondary_file) log.info("Finished calculating overlap - in directory {}".format(os.getcwd())) shutil.move(burst_tab1, reference_date_short) shutil.move(burst_tab2, secondary_date_short) # Mosaic the swaths together and copy SLCs over log.info("Starting SLC_copy_S1_fullSW.py") reference = reference_date_short secondary = secondary_date_short os.chdir(reference) SLC_copy_S1_fullSW(wrk, reference, "SLC_TAB", burst_tab1, mode=1, dem="big", dempath=wrk, raml=rlooks, azml=alooks) os.chdir("..") os.chdir(secondary) SLC_copy_S1_fullSW(wrk, secondary, "SLC_TAB", burst_tab2, mode=2, raml=rlooks, azml=alooks) os.chdir("..") # Interferogram creation, matching, refinement log.info("Starting interf_pwr_s1_lt_tops_proc.py 0") hgt = "DEM/HGT_SAR_{}_{}".format(rlooks, alooks) interf_pwr_s1_lt_tops_proc(reference, secondary, hgt, rlooks=rlooks, alooks=alooks, iterations=3, step=0) log.info("Starting interf_pwr_s1_lt_tops_proc.py 1") interf_pwr_s1_lt_tops_proc(reference, secondary, hgt, rlooks=rlooks, alooks=alooks, step=1) log.info("Starting interf_pwr_s1_lt_tops_proc.py 2") interf_pwr_s1_lt_tops_proc(reference, secondary, hgt, rlooks=rlooks, alooks=alooks, iterations=3, step=2) g = open("offsetfit3.log") offset = 1.0 for line in g: if "final azimuth offset poly. coeff.:" in line: offset = line.split(":")[1] if float(offset) > 0.02: log.error("ERROR: Found azimuth offset of {}!".format(offset)) sys.exit(1) else: log.info("Found azimuth offset of {}!".format(offset)) output = reference_date_short + "_" + secondary_date_short log.info("Starting s1_coreg_overlap") execute(f"S1_coreg_overlap SLC1_tab SLC2R_tab {output} {output}.off.it {output}.off.it.corrected", uselogging=True) log.info("Starting interf_pwr_s1_lt_tops_proc.py 2") interf_pwr_s1_lt_tops_proc(reference, secondary, hgt, rlooks=rlooks, alooks=alooks, step=3) # Perform phase unwrapping and geocoding of results log.info("Starting phase unwrapping and geocoding") unwrapping_geocoding(reference, secondary, step="man", rlooks=rlooks, alooks=alooks) # Generate metadata log.info("Collecting metadata and output files") os.chdir(wrk) # Move the outputs to the product directory pixel_spacing = int(alooks) * 20 product_name = get_product_name(reference_file, secondary_file, orbit_files, pixel_spacing) os.mkdir(product_name) move_output_files(output, reference, product_name, product_name, los_flag, look_flag) create_readme_file(reference_file, secondary_file, f'{product_name}/{product_name}.README.md.txt', pixel_spacing, dem_source) execute(f"base_init {reference}.slc.par {secondary}.slc.par - - base > baseline.log", uselogging=True) make_parameter_file(igramName, f'{product_name}/{product_name}.txt', alooks, rlooks, dem_source) log.info("Done!!!") return product_name
def geocode_back(inname, outname, width, lt, demw, demn, type_): execute( f"geocode_back {inname} {width} {lt} {outname} {demw} {demn} 0 {type_}", uselogging=True)