Esempio n. 1
0
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 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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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("..")
Esempio n. 6
0
def check_coreg(sar_file, post, max_offset=50, max_error=2):
    with CoregLogger():
        logging.info(f"SAR file: {sar_file}")
        logging.info(f"Checking coregistration using {post} meters")
        logging.info(f"Setting maximum offset to be {max_offset}")
        logging.info(f"Setting maximum error to be {max_error}")

        myfile = "mk_geo_radcal_2.log"
        if os.path.isdir("geo_HH"):
            mlog = "geo_HH/{}".format(myfile)
        elif os.path.isdir("geo_hh"):
            mlog = "geo_hh/{}".format(myfile)
        elif os.path.isdir("geo_VV"):
            mlog = "geo_VV/{}".format(myfile)
        elif os.path.isdir("geo_vv"):
            mlog = "geo_vv/{}".format(myfile)
        elif os.path.isdir("geo"):
            mlog = "geo/{}".format(myfile)
        else:
            raise CoregistrationError(f"Can't find {myfile}")

        a = np.zeros(6)
        r = np.zeros(6)

        with open(mlog, "r") as g:
            for line in g:
                if 'final range offset poly. coeff.:' in line:
                    tmp = re.split(":", line)
                    vals = tmp[1].split()
                    if len(vals) == 1:
                        r[0] = float(vals[0])
                        logging.info(f"Range offset is {r}")
                    elif len(vals) == 3:
                        r[0] = float(vals[0])
                        r[1] = float(vals[1])
                        r[2] = float(vals[2])
                        logging.info(f"Range polynomial is {r}")
                    elif len(vals) == 6:
                        r[0] = float(vals[0])
                        r[1] = float(vals[1])
                        r[2] = float(vals[2])
                        r[3] = float(vals[3])
                        r[4] = float(vals[4])
                        r[5] = float(vals[5])
                        logging.info(f"Range polynomial is {r}")

                if 'final azimuth offset poly. coeff.:' in line:
                    tmp = re.split(":", line)
                    vals = tmp[1].split()
                    if len(vals) == 1:
                        a[0] = float(vals[0])
                        logging.info(f"Azimuth offset is {a}")
                    elif len(vals) == 3:
                        a[0] = float(vals[0])
                        a[1] = float(vals[1])
                        a[2] = float(vals[2])
                        logging.info(f"Azimuth polynomial is {a}")
                    elif len(vals) == 6:
                        a[0] = float(vals[0])
                        a[1] = float(vals[1])
                        a[2] = float(vals[2])
                        a[3] = float(vals[3])
                        a[4] = float(vals[4])
                        a[5] = float(vals[5])
                        logging.info(f"Azimuth polynomial is {a}")

                if 'final model fit std. dev. (samples) range:' in line:
                    tmp = re.split(":", line)
                    vals = tmp[1].split()
                    rng_error = float(vals[0])
                    val = tmp[2].strip()
                    azi_error = float(val)
                    logging.info(f"Range std dev: {rng_error}  Azimuth std dev: {azi_error}")
                    error = np.sqrt(rng_error * rng_error + azi_error * azi_error)
                    logging.info(f"error is {error}")
                    if error > max_error:
                        logging.warning("error > max_error")
                        logging.warning("std dev is too high, using dead reckoning")
                        logging.warning("Granule failed coregistration")
                        raise CoregistrationError('error > max_error')

        mlog = glob.glob('geo_??/*.diff_par')[0]
        if not mlog:
            mlog = glob.glob('geo/*.diff_par')[0]
            if not mlog:
                raise CoregistrationError("Can't find diff_par file")

        if os.path.exists(mlog):
            ns = int(getParameter(mlog, "range_samp_1"))
            logging.info(f"Number of samples is {ns}")
            nl = int(getParameter(mlog, "az_samp_1"))
            logging.info(f"Number of lines is {nl}")
        else:
            raise CoregistrationError(f"Can't find diff par file {mlog}")

        rpt, apt = calc(1, 1, r, a)
        pt1 = np.sqrt(rpt * rpt + apt * apt)
        logging.info(f"Point 1 offset is {pt1} = sqrt({rpt}**2 + {apt}**2)")

        rpt, apt = calc(ns, 1, r, a)
        pt2 = np.sqrt(rpt * rpt + apt * apt)
        logging.info(f"Point 2 offset is {pt2} = sqrt({rpt}**2 + {apt}**2)")

        rpt, apt = calc(1, nl, r, a)
        pt3 = np.sqrt(rpt * rpt + apt * apt)
        logging.info(f"Point 3 offset is {pt3} = sqrt({rpt}**2 + {apt}**2)")

        rpt, apt = calc(ns, nl, r, a)
        pt4 = np.sqrt(rpt * rpt + apt * apt)
        logging.info(f"Point 4 offset is {pt4} = sqrt({rpt}**2 + {apt}**2)")

        top = max(pt1, pt2, pt3, pt4)
        offset = top * post

        logging.info(f"Found absolute offset of {offset} meters")
        if offset >= max_offset:
            raise CoregistrationError("offset too large, using dead reckoning")

        logging.info("Granule passed coregistration")
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("-------------------------------------------------")
Esempio n. 8
0
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)
Esempio n. 9
0
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)