def fill(dem, dem_out, logpath, replace=False): width = ReadPar(dem + ".par").width path_dem = os.path.dirname(dem_out) rpl_flg = 0 dtype = 4 # replace values value = 0 new_value = 1 run([ "replace_values", dem, value, new_value, dem + "_temp", width, rpl_flg, dtype ], path_dem, logpath) value = -32768 new_value = 0 run([ "replace_values", dem + "_temp", value, new_value, dem + "_temp2", width, rpl_flg, dtype ], path_dem, logpath) # interpolate missing values r_max = 9 np_min = 40 np_max = 81 w_mode = 2 run([ "interp_ad", dem + "_temp2", dem_out, width, r_max, np_min, np_max, w_mode, dtype ], path_dem, logpath) # remove temporary files os.remove(dem + "_temp") os.remove(dem + "_temp2") # duplicate parameter file for newly created dem shutil.copy(dem + ".par", dem_out + ".par") # create ENVI header file hdr(dem_out + ".par") if replace: for item in [ dem + x for x in ["", ".par", ".hdr", ".aux.xml"] if os.path.isfile(dem + x) ]: os.remove(item)
def main(): print "#############################################" print "preparing SRTM mosaic:" # read parameter textfile par = ReadPar(os.path.join(os.getcwd(), "PAR/srtm_preparation.par")) demdir = None if hasattr(par, "SRTM_archive"): if os.path.isdir(par.SRTM_archive): demdir = par.SRTM_archive parfiles = finder(os.getcwd(), ["*slc.par", "*mli.par", "*cal.par"]) # define (and create) directories for processing results and logfile path_dem = os.path.join(os.getcwd(), "DEM/") path_log = os.path.join(os.getcwd(), "LOG/GEO/") for path in [path_log, path_dem]: if not os.path.exists(path): os.makedirs(path) # find SRTM tiles for mosaicing demlist = hgt_collect(parfiles, path_dem, demdir=demdir) # remove files created by this function for item in finder(path_dem, ["mosaic*", "dem*", "*.par"]): os.remove(item) if len(demlist) == 0: raise IOError("no hgt files found") # perform mosaicing if multiple files are found if len(demlist) > 1: print "mosaicing..." dem = os.path.join(path_dem, "mosaic") mosaic(demlist, dem) else: dem = demlist[0] dempar(dem) fill(dem, os.path.join(path_dem, "dem_final"), path_log) dem = os.path.join(path_dem, "dem_final") # transform DEM to UTM if par.utm == "True": print "transforming to UTM..." transform(dem, dem + "_utm", int(par.targetres)) hdr(dem + "_utm.par") print "...done" print "#############################################"
def swap(data, outname): rast = raster.Raster(data) dtype = rast.dtype if rast.format != "ENVI": raise IOError("only ENVI format supported") dtype_lookup = { "Int16": 2, "CInt16": 2, "Int32": 4, "Float32": 4, "CFloat32": 4, "Float64": 8 } if dtype not in dtype_lookup: raise IOError("data type " + dtype + " not supported") sp.check_call(["swap_bytes", data, outname, str(dtype_lookup[dtype])], stdout=sp.PIPE) header = HDRobject(data + ".hdr") header.byte_order = 1 hdr(header, outname + ".hdr")
if len({"HH_slc", "VV_slc", "HV_slc", "t12", "t13", "HH_mli"} & set(scene.__dict__.keys())) == 6: counter += 1 print scene.basename rlks = ReadPar(scene.HH_mli + ".par").range_looks azlks = ReadPar(scene.HH_mli + ".par").azimuth_looks run([ "CLOUDE_DEC", scene.HH_slc, scene.HV_slc, scene.VV_slc, scene.t12, scene.t13, ReadPar(scene.HH_slc + ".par").range_samples, scene.basename, rlks, azlks ], os.path.dirname(scene.t12), path_log) # create envi header files (note: number of lines must be reduced by 1 on import into envi) for i in range(1, 4): hdr( scene.HH_mli + ".par", os.path.join(os.path.dirname(scene.t12), scene.basename) + "_ctd_" + str(i) + "_mag.hdr") if counter == 0: print "no scenes with required scattering and coherency matrix elements found" # rename files to consistent pattern for pattern in ["*.ctd*", "*.mag", "*.pha"]: for filename in finder(os.getcwd(), [pattern]): os.rename( filename, filename.replace(pattern.strip("*"), pattern.strip("*").replace(".", "_"))) print "...done" print "#############################################"
# create list of scene tuple objects tuples = grouping() print "#############################################" print "creating krogager decomposition..." for scene in tuples: if len( set(["HH_slc", "rl", "ll", "rr", "HH_mli"]) & set(scene.__dict__.keys())) == 5: print scene.basename path_out = os.path.dirname(scene.rl) mlipar = scene.HH_mli + ".par" rlks = ReadPar(mlipar).range_looks azlks = ReadPar(mlipar).azimuth_looks run([ "multi_look", scene.rl, scene.HH_slc + ".par", scene.basename + "_sphere", mlipar, rlks, azlks ], path_out, path_log) run([ "diplane_helix", scene.ll, scene.rr, scene.HH_slc + ".par", scene.basename + "_diplane", scene.basename + "_helix", mlipar, rlks, azlks, "-", "-", "-" ], path_out, path_log) for tag in ["_sphere", "_helix", "_diplane"]: hdr(scene.HH_mli + ".par", os.path.join(path_out, scene.basename) + tag + ".hdr") print "...done" print "#############################################"
print "#############################################" print "creating fd3c decomposition..." for scene in tuples: if len({"HH_slc_cal", "VV_slc_cal", "HV_slc_cal", "t13", "HH_slc_cal_mli"} & set(scene.__dict__.keys())) == 5: print scene.basename rlks = ReadPar(scene.HH_mli + ".par").range_looks azlks = ReadPar(scene.HH_mli + ".par").azimuth_looks run([ "FD3C_DEC", scene.HH_slc, scene.HV_slc, scene.VV_slc, scene.t13, ReadPar(scene.HH_slc + ".par").range_samples, scene.basename, rlks, azlks ], os.path.dirname(scene.t13), path_log) for tag in ["_fdd_pd", "_fdd_ps", "_fdd_pv"]: hdr( scene.HH_mli + ".par", os.path.join(os.path.dirname(scene.t13), scene.basename) + tag + ".hdr") # rename files to consistent pattern for pattern in ["*.fdd*"]: for filename in finder(os.getcwd(), [pattern]): os.rename( filename, filename.replace(pattern.strip("*"), pattern.strip("*").replace(".", "_"))) print "...done" print "#############################################"
dissolve([ "Rscript", os.path.join(os.path.dirname(sys.argv[0]), "multicrop.R"), processlist ])) # update processing list with new names processlist = [x + "_sub1" for x in processlist] # swap byte order of R-processed files to big endian, create hdr file for the final product and remove redundant intermediate files for x in processlist: sp.check_call(["swap_bytes", x, x[:-1], "4"], stdout=sp.PIPE) hdrobj = HDRobject(x + ".hdr") hdrobj.description = hdrobj.description[:-4] + "tfilt" hdrobj.byte_order = "1" hdr(hdrobj, x[:-4] + "tfilt.hdr") os.remove(x) os.remove(x + ".hdr") os.remove(x + ".aux.xml") # update processing list with new names processlist = [x[:-1] for x in processlist] # create table textfile for temporal filter name_ftab = os.path.join(os.getcwd(), "PAR/tab_tempfilt_" + tag) with open(name_ftab, "w") as out: for image in processlist: out.write(image + "\t" + image[:-3] + "tfilt\n") # read header file meta = HDRobject(processlist[0][:-3] + "tfilt.hdr")
# convert file to 32Bit float if data type is 8Bit unsigned integer or 16Bit signed integer if header.data_type in ["1", "2"]: print "converting data type" sp.check_call([{ "1": "uchar2float", "2": "short2float" }[header.data_type], dem, dem + "_f"], stdout=sp.PIPE) os.remove(dem) os.rename(dem + "_f", dem) header.data_type = "4" process += 1 if process > 0: hdr(header) # create gamma parameter file if not os.path.isfile(dem + ".par"): print "creating parameter file" false_northing = "0" if "North" in header.map_info else "10000000" posting = "-" + header.map_info[6] + " " + header.map_info[5] topleft = header.map_info[4] + " " + header.map_info[3] dempar = [ "UTM", "WGS84", "1", header.map_info[7], false_northing, os.path.basename(dem), "", "", "", header.samples, header.lines, posting, topleft ] run(["create_dem_par", dem + ".par"], path_dem, path_log, inlist=dempar) process += 1
nodata = ["-dstnodata", nodata] format = ["-of", "ENVI"] resampling = ["-r", resampling] # find all files matching the defined pattern(s) items = finder(path_in, pattern.split(", "), regex=True if reg == "True" else False) if len(items) > 0: path_out = os.path.dirname(file_out) if not os.path.exists(path_out): os.makedirs(path_out) print "the following files will be stacked to file {0}:".format(file_out) for item in items: print item decision = raw_input("proceed (y/n)?: ") if decision == "y": vrt = file_out+".vrt" sp.check_call(dissolve(["gdalbuildvrt", "-q", "-overwrite", "-separate", vrt, items])) sp.check_call(dissolve(["gdalwarp", "-q", "-overwrite", resampling, format, nodata, extent, targetres, vrt, file_out])) os.remove(vrt) # add band names (basenames of the original files without extension) to the envi header file par = HDRobject(file_out+".hdr") par.band_names = [os.path.splitext(os.path.basename(x))[0] for x in items] hdr(par) else: print "command aborted" else: print "no files matching the defined pattern(s) found"
sim_rdc = basename+"_sim_rdc" snr = basename+"_snr" slope = basename+"_u" aspect = basename+"_v" # define parameter file of the master masterpar = master + ".par" # perform forward geocoding if the corresponding refined lookup table does not yet exist if not os.path.isfile(lut_fine): print"creating initial lookup table" run(["gc_map", masterpar, "-", dem + ".par", dem, dem_sub + ".par", dem_sub, lut_rough, "-", "-", sim_map, slope, aspect, inc, "-", "-", ls_map, par.frame, 2], path_out, path_log) hdr(dem_sub+".par", inc+".hdr") hdr(dem_sub+".par", ls_map+".hdr") hdr(dem_sub+".par", slope+".hdr") hdr(dem_sub+".par", aspect+".hdr") if tnorm: print "initial pixel area estimation" topo_base = os.path.join(path_out, os.path.basename(master)) pix_gamma = topo_base+"_pix_gamma" parfile = master+".par" run(["pixel_area", parfile, dem_sub+".par", dem_sub, lut_rough, ls_map, inc, "-", pix_gamma], path_out, path_log) # read additional parameters samples_dem = ReadPar(dem_sub + ".par").width samples_mli = ReadPar(masterpar).range_samples lines_mli = ReadPar(masterpar).azimuth_lines
par = ReadPar(meta) mlk = Spacing(par, sys.argv[1]) # define (and create) directories for logfile path_log = os.path.join(os.getcwd(), "LOG/ISP/") if not os.path.exists(path_log): os.makedirs(path_log) print "slc range pixel spacing (slant, ground):", par.range_pixel_spacing, mlk.groundRangePS print "slc azimuth pixel spacing:", par.azimuth_pixel_spacing print "number of looks looks (range, azimuth):", mlk.rlks, mlk.azlks print "mli range pixel spacing (slant, ground):", int(mlk.rlks) * float( par.range_pixel_spacing), int(mlk.rlks) * mlk.groundRangePS print "mli azimuth pixel spacing:", int(mlk.azlks) * float( par.azimuth_pixel_spacing) print "-----------" # concatenate output names out_data = data + "_mli" out_meta = out_data + ".par" # set scaling factor scale = 0.000001 if "ERS" in par.sensor else 1.0 # perform gamma command run([ "multi_look", data, meta, out_data, out_meta, mlk.rlks, mlk.azlks, "-", "-", scale ], os.getcwd(), path_log) hdr(out_meta)