Exemplo n.º 1
0
    def doOrthoByTile(self, rasterList, tileName):

        allOrtho = []
        for i in range(len(rasterList)):
            raster, tileOrigin = rasterList[i]
            manifest = raster.getManifest()
            calibrationApplication = rasterList[i][
                0].GetCalibrationApplication()
            for calib, dep in calibrationApplication:
                image = calib.GetParameterValue("out")
                currentDate = getDateFromS1Raster(image)
                currentPolar = getPolarFromS1Raster(image)
                currentPlatform = getPlatformFromS1Raster(image)
                currentOrbitDirection = getOrbitDirection(manifest)
                outUTMZone = tileName[0:2]
                outUTMNorthern = str(int(tileName[2] >= 'N'))
                workingDirectory = os.path.join(self.outputPreProcess,
                                                tileName)
                if not os.path.exists(workingDirectory):
                    os.makedirs(workingDirectory)
                inEPSG = 4326
                outEPSG = 32600 + int(outUTMZone)
                if not outUTMNorthern == "0":
                    outEPSG = outEPSG + 100
                if self.ManyProjection:
                    [(x, y, dummy)] = converCoord([tileOrigin[0]], inEPSG,
                                                  outEPSG)
                    [(lrx, lry, dummy)] = converCoord([tileOrigin[2]], inEPSG,
                                                      outEPSG)
                    uniqueProj = None
                    refRaster = None
                else:
                    refRaster = FileSearch_AND(
                        self.referencesFolder + "/T" + tileName, True,
                        self.rasterPattern)[0]
                    print("reference raster found : " + refRaster)
                if self.ManyProjection and outUTMNorthern == "1" and y > 0:
                    y = y - 10000000.
                    lry = lry - 10000000.
                if self.ManyProjection and outUTMNorthern == "0" and y < 0:
                    y = y + 10000000.
                    lry = lry + 10000000.

                orthoImageName = currentPlatform+"_"+\
                                 tileName+"_"+\
                                 currentPolar+"_"+\
                                 currentOrbitDirection+"_"+\
                                 currentDate+".tif"

                inputImage = (calib, dep)
                if self.wMode: inputImage = calib.GetParameterValue("out")

                orthoRaster = os.path.join(workingDirectory, orthoImageName)

                if self.ManyProjection:
                    sizeX = abs(lrx - x) / self.outSpacialRes
                    sizeY = abs(lry - y) / self.outSpacialRes

                    ortho, ortho_dep = OtbAppBank.CreateOrthoRectification({
                        "in":
                        inputImage,
                        "out":
                        orthoRaster,
                        "ram":
                        self.RAMPerProcess,
                        "outputs.spacingx":
                        self.outSpacialRes,
                        "outputs.spacingy":
                        -self.outSpacialRes,
                        "outputs.sizex":
                        sizeX,
                        "outputs.sizey":
                        sizeY,
                        "opt.gridspacing":
                        self.gridSpacing,
                        "map.utm.zone":
                        outUTMZone,
                        "map.utm.northhem":
                        outUTMNorthern,
                        "outputs.ulx":
                        x,
                        "outputs.uly":
                        y,
                        "elev.dem":
                        self.SRTM,
                        "elev.geoid":
                        self.geoid,
                        "map":
                        "utm"
                    })
                else:
                    ortho, ortho_dep = OtbAppBank.CreateSuperimposeApplication(
                        {
                            "inr": refRaster,
                            "inm": inputImage,
                            "pixType": "float",
                            "interpolator": "bco",
                            "ram": self.RAMPerProcess,
                            "io.out": orthoRaster,
                            "elev.dem": self.SRTM,
                            "elev.geoid": self.geoid
                        })
                allOrtho.append((ortho, ortho_dep))
        return allOrtho
Exemplo n.º 2
0
def LaunchSARreprojection(rasterList,
                          refRaster=None,
                          tileName=None,
                          SRTM=None,
                          geoid=None,
                          output_directory=None,
                          RAMPerProcess=None,
                          workingDirectory=None):
    """must be use with multiprocessing.Pool
    """
    def writeOutputRaster_2(OTB_App,
                            overwrite=True,
                            workingDirectory=None,
                            dep=None,
                            logger=logger):
        """
        """
        import shutil
        from Common import OtbAppBank as otbApp

        out_param = otbApp.getInputParameterOutput(OTB_App)
        out_raster = OTB_App.GetParameterValue(out_param)

        launch_write = True
        if os.path.exists(out_raster.split("?")[0]) and not overwrite:
            launch_write = False

        if workingDirectory is None and launch_write:
            OTB_App.ExecuteAndWriteOutput()

        elif launch_write:
            out_raster_dir, out_raster_name = os.path.split(out_raster)
            out_workingDir = os.path.join(workingDirectory, out_raster_name)
            out_workingDir = out_workingDir.split("?")[0]
            OTB_App.SetParameterString(out_param, out_workingDir)
            OTB_App.ExecuteAndWriteOutput()
            shutil.copy(out_workingDir, out_raster.split("?")[0])
            if os.path.exists(out_workingDir.replace(".tif", ".geom")):
                shutil.copy(out_workingDir.replace(".tif", ".geom"),
                            out_raster.replace(".tif", ".geom").split("?")[0])
        if not launch_write:
            logger.info("{} already exists and will not be overwrited".format(
                out_raster))

        OTB_App = None
        return out_raster

    date_position = 4

    all_superI_vv = []
    all_superI_vh = []
    all_acquisition_date_vv = []
    all_acquisition_date_vh = []
    dates = []
    all_dep = []
    for date_to_Concatenate in rasterList:
        #Calibration + Superimpose
        vv, vh = date_to_Concatenate.GetImageList()
        SAR_directory, SAR_name = os.path.split(vv)
        currentPlatform = getPlatformFromS1Raster(vv)
        manifest = date_to_Concatenate.getManifest()
        currentOrbitDirection = getOrbitDirection(manifest)
        acquisition_date = SAR_name.split("-")[date_position]
        dates.append(acquisition_date)
        calib_vv = OtbAppBank.CreateSarCalibration({
            "in": vv,
            "lut": "gamma",
            "ram": str(RAMPerProcess)
        })
        calib_vh = OtbAppBank.CreateSarCalibration({
            "in": vh,
            "lut": "gamma",
            "ram": str(RAMPerProcess)
        })
        calib_vv.Execute()
        calib_vh.Execute()
        all_dep.append(calib_vv)
        all_dep.append(calib_vh)
        orthoImageName_vv = "{}_{}_{}_{}_{}".format(currentPlatform, tileName,
                                                    "vv",
                                                    currentOrbitDirection,
                                                    acquisition_date)

        super_vv, super_vv_dep = OtbAppBank.CreateSuperimposeApplication({
            "inr":
            refRaster,
            "inm":
            calib_vv,
            "pixType":
            "float",
            "interpolator":
            "bco",
            "ram":
            RAMPerProcess,
            "elev.dem":
            SRTM,
            "elev.geoid":
            geoid
        })
        orthoImageName_vh = "{}_{}_{}_{}_{}".format(currentPlatform, tileName,
                                                    "vh",
                                                    currentOrbitDirection,
                                                    acquisition_date)

        super_vh, super_vh_dep = OtbAppBank.CreateSuperimposeApplication({
            "inr":
            refRaster,
            "inm":
            calib_vh,
            "pixType":
            "float",
            "interpolator":
            "bco",
            "ram":
            RAMPerProcess,
            "elev.dem":
            SRTM,
            "elev.geoid":
            geoid
        })
        super_vv.Execute()
        super_vh.Execute()
        all_dep.append(super_vv)
        all_dep.append(super_vh)
        all_superI_vv.append(super_vv)
        all_superI_vh.append(super_vh)

        all_acquisition_date_vv.append(orthoImageName_vv)
        all_acquisition_date_vh.append(orthoImageName_vh)

    all_acquisition_date_vv = "_".join(sorted(all_acquisition_date_vv))
    all_acquisition_date_vh = "_".join(sorted(all_acquisition_date_vh))
    #Concatenate thanks to a BandMath
    vv_exp = ",".join(
        ["im{}b1".format(i + 1) for i in range(len(all_superI_vv))])
    vv_exp = "max({})".format(vv_exp)
    SAR_vv = os.path.join(output_directory, all_acquisition_date_vv + ".tif")
    concatAppli_vv = OtbAppBank.CreateBandMathApplication({
        "il":
        all_superI_vv,
        "exp":
        vv_exp,
        "out":
        SAR_vv,
        "ram":
        str(RAMPerProcess),
        "pixType":
        "float"
    })
    vh_exp = ",".join(
        ["im{}b1".format(i + 1) for i in range(len(all_superI_vh))])
    vh_exp = "max({})".format(vh_exp)
    SAR_vh = os.path.join(output_directory, all_acquisition_date_vh + ".tif")
    concatAppli_vh = OtbAppBank.CreateBandMathApplication({
        "il":
        all_superI_vh,
        "exp":
        vh_exp,
        "out":
        SAR_vh,
        "ram":
        str(RAMPerProcess),
        "pixType":
        "float"
    })

    ortho_path = writeOutputRaster_2(concatAppli_vv,
                                     overwrite=False,
                                     workingDirectory=workingDirectory,
                                     dep=all_dep)
    ortho_path = writeOutputRaster_2(concatAppli_vh,
                                     overwrite=False,
                                     workingDirectory=workingDirectory,
                                     dep=all_dep)

    #from the results generate a mask
    super_vv = os.path.join(output_directory, all_acquisition_date_vv + ".tif")
    border_mask = super_vv.replace(".tif", "_BorderMask.tif")
    mask_app, _ = generateBorderMask(super_vv,
                                     border_mask,
                                     RAMPerProcess=RAMPerProcess)
    mask_path = writeOutputRaster(mask_app,
                                  overwrite=False,
                                  workingDirectory=workingDirectory)
    mask_path_geom = mask_path.replace(".tif", ".geom")
    if os.path.exists(mask_path_geom):
        os.remove(mask_path_geom)

    return (SAR_vv, SAR_vh)
Exemplo n.º 3
0
def coregister(insrc,
               inref,
               band,
               bandref,
               resample=1,
               step=256,
               minstep=16,
               minsiftpoints=40,
               iterate=1,
               prec=3,
               mode=2,
               datadir=None,
               pattern='*STACK.tif',
               datatype='S2',
               writeFeatures=False,
               workingDirectory=None):
    """ register an image / a time series on a reference image

    Parameters
    ----------
    insrc : string
        source raster
    inref : string
        reference raster
    band : int
        band number for the source raster
    bandref : int
        band number for the raster reference raster
    resample : boolean
        resample to reference raster resolution
    step : int
        initial step between the geobins
    minstep : int
        minimal step between the geobins when iterates
    minsiftpoints : int
        minimal number of sift points to perform the registration
    iterate : boolean
        argument to iterate with smaller geobin step to find more sift points
    prec : int
        precision between the source and reference image (in source pixel unit)
    mode : int
        registration mode,
        1 : simple registration ;
        2 : time series registration ;
        3 : time series cascade registration (to do)
    datadir : string
        path to the data directory
    pattern : string
        pattern of the STACK files to register
    writeFeatures : boolean
        argument to keep temporary files

    Note
    ------
    This function use the OTB's application **OrthoRectification** and **SuperImpose**
    more documentation for
    `OrthoRectification <https://www.orfeo-toolbox.org/Applications/OrthoRectification.html>`_
    and
    `SuperImpose <https://www.orfeo-toolbox.org/Applications/Superimpose.html>`_
    """
    from Common.FileUtils import ensure_dir
    pathWd = os.path.dirname(
        insrc) if not workingDirectory else workingDirectory
    if os.path.exists(pathWd) == False:
        ensure_dir(pathWd)

    srcClip = os.path.join(pathWd, 'tempSrcClip.tif')
    extractROIApp = OtbAppBank.CreateExtractROIApplication({
        "in": insrc,
        "mode": "fit",
        "mode.fit.im": inref,
        "out": srcClip,
        "pixType": "uint16"
    })
    extractROIApp.ExecuteAndWriteOutput()
    # #SensorModel generation
    SensorModel = os.path.join(pathWd, 'SensorModel.geom')
    PMCMApp = OtbAppBank.CreatePointMatchCoregistrationModel({
        "in":
        srcClip,
        "band1":
        band,
        "inref":
        inref,
        "bandref":
        bandref,
        "resample":
        resample,
        "precision":
        str(prec),
        "mfilter":
        "1",
        "backmatching":
        "1",
        "outgeom":
        SensorModel,
        "initgeobinstep":
        str(step),
        "mingeobinstep":
        str(minstep),
        "minsiftpoints":
        str(minsiftpoints),
        "iterate":
        iterate
    })
    PMCMApp.ExecuteAndWriteOutput()

    # mode 1 : application on the source image
    if mode == 1 or mode == 3:
        outSrc = os.path.join(pathWd, 'temp_file.tif')
        io_Src = str(srcClip + '?&skipcarto=true&geom=' + SensorModel)
        ds = gdal.Open(srcClip)
        prj = ds.GetProjection()
        gt = ds.GetGeoTransform()
        srs = osr.SpatialReference()
        srs.ImportFromWkt(prj)
        code = srs.GetAuthorityCode(None)
        gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5])))))
        ds = None
        orthoRecApp = OtbAppBank.CreateOrthoRectification({
            "in": io_Src,
            "io.out": outSrc,
            "map": "epsg",
            "map.epsg.code": code,
            "opt.gridspacing": gsp,
            "pixType": "uint16"
        })

        if writeFeatures:
            orthoRecApp[0].ExecuteAndWriteOutput()
        else:
            orthoRecApp[0].Execute()

        ext = os.path.splitext(insrc)[1]
        finalOutput = os.path.join(
            pathWd,
            os.path.basename(insrc.replace(ext, ext.replace('.', '_COREG.'))))
        superImposeApp = OtbAppBank.CreateSuperimposeApplication({
            "inr":
            srcClip,
            "inm":
            orthoRecApp[0],
            "out":
            finalOutput,
            "pixType":
            "uint16"
        })
        superImposeApp[0].ExecuteAndWriteOutput()

        shutil.move(finalOutput, insrc.replace(ext,
                                               ext.replace('.', '_COREG.')))
        shutil.move(finalOutput.replace(ext, '.geom'),
                    insrc.replace(ext, '_COREG.geom'))

        # Mask registration if exists
        masks = glob.glob(
            os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep +
            '*BINARY_MASK' + ext)
        if len(masks) != 0:
            for mask in masks:
                srcClip = os.path.join(pathWd, 'tempSrcClip.tif')
                extractROIApp = OtbAppBank.CreateExtractROIApplication({
                    "in":
                    mask,
                    "mode":
                    "fit",
                    "mode.fit.im":
                    inref,
                    "out":
                    srcClip,
                    "pixType":
                    "uint16"
                })
                extractROIApp.ExecuteAndWriteOutput()
                outSrc = os.path.join(pathWd, 'temp_file.tif')
                io_Src = str(mask + '?&skipcarto=true&geom=' + SensorModel)
                orthoRecApp = OtbAppBank.CreateOrthoRectification({
                    "in":
                    io_Src,
                    "io.out":
                    outSrc,
                    "map":
                    "epsg",
                    "map.epsg.code":
                    code,
                    "opt.gridspacing":
                    gsp,
                    "pixType":
                    "uint16"
                })
                if writeFeatures:
                    orthoRecApp[0].ExecuteAndWriteOutput()
                else:
                    orthoRecApp[0].Execute()

                ext = os.path.splitext(insrc)[1]
                finalMask = os.path.join(
                    pathWd,
                    os.path.basename(
                        mask.replace(ext, ext.replace('.', '_COREG.'))))
                superImposeApp = OtbAppBank.CreateSuperimposeApplication({
                    "inr":
                    mask,
                    "inm":
                    orthoRecApp[0],
                    "out":
                    finalMask,
                    "pixType":
                    "uint16"
                })
                superImposeApp[0].ExecuteAndWriteOutput()

                if finalMask != mask.replace(ext, ext.replace('.', '_COREG.')):
                    shutil.move(finalMask,
                                mask.replace(ext, ext.replace('.', '_COREG.')))
                    shutil.move(finalMask.replace(ext, '.geom'),
                                mask.replace(ext, '_COREG.geom'))

        if mode == 3:
            folders = glob.glob(os.path.join(datadir, '*'))
            vhr_ref = inref
            if datatype in ['S2', 'S2_S2C']:
                dates = [
                    os.path.basename(fld).split('_')[1].split("-")[0]
                    for fld in folders
                ]
                ref_date = os.path.basename(insrc).split('_')[1].split("-")[0]
            elif datatype in ['L5', 'L8']:
                dates = [
                    os.path.basename(fld).split('_')[3] for fld in folders
                ]
                ref_date = os.path.basename(insrc).split('_')[3]
            dates.sort()
            ref_date_ind = dates.index(ref_date)
            bandref = band
            clean_dates = [ref_date]
            for date in reversed(dates[:ref_date_ind]):
                inref = glob.glob(
                    os.path.join(datadir, '*' + clean_dates[-1] + '*',
                                 pattern))[0]
                insrc = glob.glob(
                    os.path.join(datadir, '*' + date + '*', pattern))[0]
                srcClip = os.path.join(pathWd, 'srcClip.tif')
                extractROIApp = OtbAppBank.CreateExtractROIApplication({
                    "in":
                    insrc,
                    "mode":
                    "fit",
                    "mode.fit.im":
                    inref,
                    "out":
                    srcClip,
                    "pixType":
                    "uint16"
                })
                extractROIApp.ExecuteAndWriteOutput()
                outSensorModel = os.path.join(pathWd,
                                              'SensorModel_%s.geom' % date)
                try:
                    PMCMApp = OtbAppBank.CreatePointMatchCoregistrationModel({
                        "in":
                        srcClip,
                        "band1":
                        band,
                        "inref":
                        inref,
                        "bandref":
                        bandref,
                        "resample":
                        resample,
                        "precision":
                        str(prec),
                        "mfilter":
                        "1",
                        "backmatching":
                        "1",
                        "outgeom":
                        outSensorModel,
                        "initgeobinstep":
                        str(step),
                        "mingeobinstep":
                        str(minstep),
                        "minsiftpoints":
                        str(minsiftpoints),
                        "iterate":
                        iterate
                    })
                    PMCMApp.ExecuteAndWriteOutput()
                except RuntimeError:
                    shutil.copy(SensorModel, outSensorModel)
                    logger.warning(
                        'Coregistration failed, %s will be process with %s' %
                        (insrc, outSensorModel))
                    continue

                outSrc = os.path.join(pathWd, 'temp_file.tif')
                io_Src = str(srcClip + '?&skipcarto=true&geom=' +
                             outSensorModel)
                ds = gdal.Open(srcClip)
                prj = ds.GetProjection()
                gt = ds.GetGeoTransform()
                srs = osr.SpatialReference()
                srs.ImportFromWkt(prj)
                code = srs.GetAuthorityCode(None)
                gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5])))))
                ds = None
                try:
                    orthoRecApp = OtbAppBank.CreateOrthoRectification({
                        "in":
                        io_Src,
                        "io.out":
                        outSrc,
                        "map":
                        "epsg",
                        "map.epsg.code":
                        code,
                        "opt.gridspacing":
                        gsp,
                        "pixType":
                        "uint16"
                    })

                    if writeFeatures:
                        orthoRecApp[0].ExecuteAndWriteOutput()
                    else:
                        orthoRecApp[0].Execute()
                except RuntimeError:
                    os.remove(outSensorModel)
                    shutil.copy(SensorModel, outSensorModel)
                    logger.warning(
                        'Coregistration failed, %s will be process with %s' %
                        (insrc, outSensorModel))
                    orthoRecApp = OtbAppBank.CreateOrthoRectification({
                        "in":
                        io_Src,
                        "io.out":
                        outSrc,
                        "map":
                        "epsg",
                        "map.epsg.code":
                        code,
                        "opt.gridspacing":
                        gsp,
                        "pixType":
                        "uint16"
                    })
                    continue

                    if writeFeatures:
                        orthoRecApp[0].ExecuteAndWriteOutput()
                    else:
                        orthoRecApp[0].Execute()

                ext = os.path.splitext(insrc)[1]
                finalOutput = os.path.join(
                    pathWd,
                    os.path.basename(
                        insrc.replace(ext, ext.replace('.', '_COREG.'))))
                superImposeApp = OtbAppBank.CreateSuperimposeApplication({
                    "inr":
                    srcClip,
                    "inm":
                    orthoRecApp[0],
                    "out":
                    finalOutput,
                    "pixType":
                    "uint16"
                })
                superImposeApp[0].ExecuteAndWriteOutput()

                shutil.move(finalOutput,
                            insrc.replace(ext, ext.replace('.', '_COREG.')))
                shutil.move(finalOutput.replace(ext, '.geom'),
                            insrc.replace(ext, '_COREG.geom'))

                # Mask registration if exists
                masks = glob.glob(
                    os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep +
                    '*BINARY_MASK' + ext)
                if len(masks) != 0:
                    for mask in masks:
                        srcClip = os.path.join(pathWd, 'srcClip.tif')
                        extractROIApp = OtbAppBank.CreateExtractROIApplication(
                            {
                                "in": mask,
                                "mode": "fit",
                                "mode.fit.im": inref,
                                "out": srcClip,
                                "pixType": "uint16"
                            })
                        extractROIApp.ExecuteAndWriteOutput()
                        outSrc = os.path.join(pathWd, 'temp_file.tif')
                        io_Src = str(srcClip + '?&skipcarto=true&geom=' +
                                     outSensorModel)
                        orthoRecApp = OtbAppBank.CreateOrthoRectification({
                            "in":
                            io_Src,
                            "io.out":
                            outSrc,
                            "map":
                            "epsg",
                            "map.epsg.code":
                            code,
                            "opt.gridspacing":
                            gsp,
                            "pixType":
                            "uint16"
                        })
                        if writeFeatures:
                            orthoRecApp[0].ExecuteAndWriteOutput()
                        else:
                            orthoRecApp[0].Execute()

                        ext = os.path.splitext(insrc)[1]
                        finalMask = os.path.join(
                            pathWd,
                            os.path.basename(
                                mask.replace(ext, ext.replace('.',
                                                              '_COREG.'))))
                        superImposeApp = OtbAppBank.CreateSuperimposeApplication(
                            {
                                "inr": srcClip,
                                "inm": orthoRecApp[0],
                                "out": finalMask,
                                "pixType": "uint16"
                            })
                        superImposeApp[0].ExecuteAndWriteOutput()

                        shutil.move(
                            finalMask,
                            mask.replace(ext, ext.replace('.', '_COREG.')))
                        shutil.move(finalMask.replace(ext, '.geom'),
                                    mask.replace(ext, '_COREG.geom'))

                if not writeFeatures and os.path.exists(outSensorModel):
                    os.remove(outSensorModel)

                if datatype in ['S2', 'S2_S2C']:
                    mtd_file = glob.glob(
                        os.path.join(os.path.dirname(insrc), '*_MTD_ALL*'))[0]
                    cloud_clear = get_S2_Tile_Cloud_Cover(mtd_file)
                    cover = get_S2_Tile_Coverage(mtd_file)
                    if cloud_clear > 0.6 and cover > 0.8:
                        clean_dates.append(date)
                elif datatype in ['L5', 'L8']:
                    mlt_file = glob.glob(
                        os.path.join(os.path.dirname(insrc), '*_MTL*'))[0]
                    cloud_clear = get_L8_Tile_Cloud_Cover(mlt_file)
                    if cloud_clear > 0.6:
                        clean_dates.append(date)

            clean_dates = [ref_date]
            for date in dates[ref_date_ind + 1:]:
                inref = glob.glob(
                    os.path.join(datadir, '*' + clean_dates[-1] + '*',
                                 pattern))[0]
                insrc = glob.glob(
                    os.path.join(datadir, '*' + date + '*', pattern))[0]
                srcClip = os.path.join(pathWd, 'srcClip.tif')
                extractROIApp = OtbAppBank.CreateExtractROIApplication({
                    "in":
                    insrc,
                    "mode":
                    "fit",
                    "mode.fit.im":
                    inref,
                    "out":
                    srcClip,
                    "pixType":
                    "uint16"
                })
                extractROIApp.ExecuteAndWriteOutput()
                outSensorModel = os.path.join(pathWd,
                                              'SensorModel_%s.geom' % date)
                try:
                    PMCMApp = OtbAppBank.CreatePointMatchCoregistrationModel({
                        "in":
                        srcClip,
                        "band1":
                        band,
                        "inref":
                        inref,
                        "bandref":
                        bandref,
                        "resample":
                        resample,
                        "precision":
                        str(prec),
                        "mfilter":
                        "1",
                        "backmatching":
                        "1",
                        "outgeom":
                        outSensorModel,
                        "initgeobinstep":
                        str(step),
                        "mingeobinstep":
                        str(minstep),
                        "minsiftpoints":
                        str(minsiftpoints),
                        "iterate":
                        iterate
                    })
                    PMCMApp.ExecuteAndWriteOutput()
                except RuntimeError:
                    shutil.copy(SensorModel, outSensorModel)
                    logger.warning(
                        'Coregistration failed, %s will be process with %s' %
                        (insrc, outSensorModel))
                    continue

                outSrc = os.path.join(pathWd, 'temp_file.tif')
                io_Src = str(srcClip + '?&skipcarto=true&geom=' +
                             outSensorModel)
                ds = gdal.Open(srcClip)
                prj = ds.GetProjection()
                gt = ds.GetGeoTransform()
                srs = osr.SpatialReference()
                srs.ImportFromWkt(prj)
                code = srs.GetAuthorityCode(None)
                gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5])))))
                ds = None
                try:
                    orthoRecApp = OtbAppBank.CreateOrthoRectification({
                        "in":
                        io_Src,
                        "io.out":
                        outSrc,
                        "map":
                        "epsg",
                        "map.epsg.code":
                        code,
                        "opt.gridspacing":
                        gsp,
                        "pixType":
                        "uint16"
                    })

                    if writeFeatures:
                        orthoRecApp[0].ExecuteAndWriteOutput()
                    else:
                        orthoRecApp[0].Execute()
                except RuntimeError:
                    os.remove(outSensorModel)
                    shutil.copy(SensorModel, outSensorModel)
                    orthoRecApp = OtbAppBank.CreateOrthoRectification({
                        "in":
                        io_Src,
                        "io.out":
                        outSrc,
                        "map":
                        "epsg",
                        "map.epsg.code":
                        code,
                        "opt.gridspacing":
                        gsp,
                        "pixType":
                        "uint16"
                    })
                    continue

                    if writeFeatures:
                        orthoRecApp[0].ExecuteAndWriteOutput()
                    else:
                        orthoRecApp[0].Execute()

                ext = os.path.splitext(insrc)[1]
                finalOutput = os.path.join(
                    pathWd,
                    os.path.basename(
                        insrc.replace(ext, ext.replace('.', '_COREG.'))))
                superImposeApp = OtbAppBank.CreateSuperimposeApplication({
                    "inr":
                    srcClip,
                    "inm":
                    orthoRecApp[0],
                    "out":
                    finalOutput,
                    "pixType":
                    "uint16"
                })
                superImposeApp[0].ExecuteAndWriteOutput()

                shutil.move(finalOutput,
                            insrc.replace(ext, ext.replace('.', '_COREG.')))
                shutil.move(finalOutput.replace(ext, '.geom'),
                            insrc.replace(ext, '_COREG.geom'))

                # Mask registration if exists
                masks = glob.glob(
                    os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep +
                    '*BINARY_MASK' + ext)
                if len(masks) != 0:
                    for mask in masks:
                        srcClip = os.path.join(pathWd, 'srcClip.tif')
                        extractROIApp = OtbAppBank.CreateExtractROIApplication(
                            {
                                "in": mask,
                                "mode": "fit",
                                "mode.fit.im": inref,
                                "out": srcClip,
                                "pixType": "uint16"
                            })
                        extractROIApp.ExecuteAndWriteOutput()
                        outSrc = os.path.join(pathWd, 'temp_file.tif')
                        io_Src = str(srcClip + '?&skipcarto=true&geom=' +
                                     outSensorModel)
                        orthoRecApp = OtbAppBank.CreateOrthoRectification({
                            "in":
                            io_Src,
                            "io.out":
                            outSrc,
                            "map":
                            "epsg",
                            "map.epsg.code":
                            code,
                            "opt.gridspacing":
                            gsp,
                            "pixType":
                            "uint16"
                        })
                        if writeFeatures:
                            orthoRecApp[0].ExecuteAndWriteOutput()
                        else:
                            orthoRecApp[0].Execute()

                        ext = os.path.splitext(insrc)[1]
                        finalMask = os.path.join(
                            pathWd,
                            os.basename(
                                mask.replace(ext, ext.replace('.',
                                                              '_COREG.'))))
                        superImposeApp = OtbAppBank.CreateSuperimposeApplication(
                            {
                                "inr": srcClip,
                                "inm": orthoRecApp[0],
                                "out": finalMask,
                                "pixType": "uint16"
                            })
                        superImposeApp[0].ExecuteAndWriteOutput()

                        shutil.move(
                            finalMask,
                            mask.replace(ext, ext.replace('.', '_COREG.')))
                        shutil.move(finalMask.replace(ext, '.geom'),
                                    mask.replace(ext, '_COREG.geom'))

                if writeFeatures == False and os.path.exists(outSensorModel):
                    os.remove(outSensorModel)

                if datatype in ['S2', 'S2_S2C']:
                    mtd_file = glob.glob(
                        os.path.join(os.path.dirname(insrc), '*_MTD_ALL*'))[0]
                    cloud_clear = get_S2_Tile_Cloud_Cover(mtd_file)
                    cover = get_S2_Tile_Coverage(mtd_file)
                    if cloud_clear > 0.6 and cover > 0.8:
                        clean_dates.append(date)
                elif datatype in ['L5', 'L8']:
                    mlt_file = glob.glob(
                        os.path.join(os.path.dirname(insrc), '*_MTL*'))[0]
                    cloud_clear = get_L8_Tile_Cloud_Cover(mlt_file)
                    if cloud_clear > 0.6:
                        clean_dates.append(date)

        if not writeFeatures and os.path.exists(SensorModel):
            os.remove(SensorModel)
    # mode 2 : application on the time series
    elif mode == 2:
        ext = os.path.splitext(insrc)[1]
        file_list = glob.glob(datadir + os.sep + '*' + os.sep + pattern)
        for insrc in file_list:
            srcClip = os.path.join(pathWd, 'tempSrcClip.tif')
            extractROIApp = OtbAppBank.CreateExtractROIApplication({
                "in":
                insrc,
                "mode":
                "fit",
                "mode.fit.im":
                inref,
                "out":
                srcClip,
                "pixType":
                "uint16"
            })
            extractROIApp.ExecuteAndWriteOutput()
            outSrc = os.path.join(pathWd, 'temp_file.tif')
            io_Src = str(srcClip + '?&skipcarto=true&geom=' + SensorModel)
            ds = gdal.Open(srcClip)
            prj = ds.GetProjection()
            gt = ds.GetGeoTransform()
            srs = osr.SpatialReference()
            srs.ImportFromWkt(prj)
            code = srs.GetAuthorityCode(None)
            gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5])))))
            ds = None
            orthoRecApp = OtbAppBank.CreateOrthoRectification({
                "in":
                io_Src,
                "io.out":
                outSrc,
                "map":
                "epsg",
                "map.epsg.code":
                code,
                "opt.gridspacing":
                gsp,
                "pixType":
                "uint16"
            })

            if writeFeatures:
                orthoRecApp[0].ExecuteAndWriteOutput()
            else:
                orthoRecApp[0].Execute()

            ext = os.path.splitext(insrc)[1]
            finalOutput = os.path.join(
                pathWd,
                os.path.basename(
                    insrc.replace(ext, ext.replace('.', '_COREG.'))))
            superImposeApp = OtbAppBank.CreateSuperimposeApplication({
                "inr":
                srcClip,
                "inm":
                orthoRecApp[0],
                "out":
                finalOutput,
                "pixType":
                "uint16"
            })
            superImposeApp[0].ExecuteAndWriteOutput()

            shutil.move(finalOutput,
                        insrc.replace(ext, ext.replace('.', '_COREG.')))
            shutil.move(finalOutput.replace(ext, '.geom'),
                        insrc.replace(ext, '_COREG.geom'))

            # Mask registration if exists
            masks = glob.glob(
                os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep +
                '*BINARY_MASK*' + ext)
            if len(masks) != 0:
                for mask in masks:
                    srcClip = os.path.join(pathWd, 'tempSrcClip.tif')
                    extractROIApp = OtbAppBank.CreateExtractROIApplication({
                        "in":
                        mask,
                        "mode":
                        "fit",
                        "mode.fit.im":
                        inref,
                        "out":
                        srcClip,
                        "pixType":
                        "uint16"
                    })
                    extractROIApp.ExecuteAndWriteOutput()
                    outSrc = os.path.join(pathWd, 'temp_file.tif')
                    io_Src = str(srcClip + '?&skipcarto=true&geom=' +
                                 SensorModel)
                    orthoRecApp = OtbAppBank.CreateOrthoRectification({
                        "in":
                        io_Src,
                        "io.out":
                        outSrc,
                        "map":
                        "epsg",
                        "map.epsg.code":
                        code,
                        "opt.gridspacing":
                        gsp,
                        "pixType":
                        "uint16"
                    })
                    if writeFeatures:
                        orthoRecApp[0].ExecuteAndWriteOutput()
                    else:
                        orthoRecApp[0].Execute()

                    ext = os.path.splitext(insrc)[1]
                    finalMask = os.path.join(
                        pathWd,
                        os.path.basename(
                            mask.replace(ext, ext.replace('.', '_COREG.'))))
                    superImposeApp = OtbAppBank.CreateSuperimposeApplication({
                        "inr":
                        srcClip,
                        "inm":
                        orthoRecApp[0],
                        "out":
                        finalMask,
                        "pixType":
                        "uint16"
                    })
                    superImposeApp[0].ExecuteAndWriteOutput()

                    shutil.move(finalMask,
                                mask.replace(ext, ext.replace('.', '_COREG.')))
                    shutil.move(finalMask.replace(ext, '.geom'),
                                mask.replace(ext, '_COREG.geom'))

        os.remove(srcClip)
        if not writeFeatures and os.path.exists(SensorModel):
            os.remove(SensorModel)

    return None