def generate_l2_toa_images(self, working_dir):
        """
        :param working_dir:
        :return:
        """
        l_BandsDefinitions = self._plugin.BandsDefinitions
        l_ListOfL2Resolution = l_BandsDefinitions.ListOfL2Resolution  # ListOfStrings
        l_NbL2Res = len(l_ListOfL2Resolution)

        for l2res in range(l_NbL2Res):
            # Current resolution: "R1" or "R2"
            curRes = l_ListOfL2Resolution[l2res]

            # Get the list of band of the current resolution
            listOfL2Bands = l_BandsDefinitions.get_list_of_l2_band_code(curRes)
            nbBand = len(listOfL2Bands)
            list_of_image = []
            # For each band of the current resolution
            for l_StrBandIdL2 in listOfL2Bands:
                # Get the L1 band index associated to the L2 band code
                l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l_StrBandIdL2)
                # Generate the list of L2 TOA images per resolution
                list_of_image.append(self._toa_scalar_list[l1BandIdx])
            out_concatenate = os.path.join(
                working_dir, "L2TOAImageListVector_" + curRes + ".tif")
            param_concatenate = {"il": list_of_image, "out": out_concatenate}
            l2toa_concat_app = OtbAppHandler(
                "ConcatenateDoubleImages",
                param_concatenate,
                write_output=(False
                              or is_croco_on("sentinel2.l1reader.l2toa")))
            self._pipeline.add_otb_app(l2toa_concat_app)
            self._l2toaimagelist.append(
                l2toa_concat_app.getoutput().get("out"))
Ejemplo n.º 2
0
 def run(self, dict_of_input, dict_of_output):
     LOGGER.info("Scattering Correction start")
     scat_working = dict_of_input.get(
         "AppHandler").get_directory_manager().get_temporary_directory(
             "ScatteringProc_", do_always_remove=True)
     l_caching = dict_of_input.get("Params").get("Caching")
     l_writeL2 = dict_of_input.get("Params").get(
         "WriteL2ProductToL2Resolution")
     tocr_sub_image = os.path.join(scat_working, "tocr_sub.tif")
     bands_definition = dict_of_input.get("Plugin").BandsDefinitions
     #  Coarse scattering correction
     param_scattering = {
         "lutmap": dict_of_output["hr_lutmap"],
         "toac": dict_of_output["AtmoAbsIPTOAC"],
         "aot": dict_of_output["AOT_Sub"],
         "edg": dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
         "dtm": dict_of_input.get("DEM").ALC,
         "l2nodata": dict_of_input.get("Params").get("RealL2NoData"),
         "l2bandincoarse":
         bands_definition.get_list_of_band_id_in_l2_coarse(),
         "tocr": tocr_sub_image
     }
     self._scattering_sub_app = OtbAppHandler("ScatteringCorrection",
                                              param_scattering,
                                              write_output=False)
     dict_of_output["TOC_sub"] = self._scattering_sub_app.getoutput(
     )["tocr"]
     # l2 scattering correction
     tocr_list = []
     if l_writeL2:
         l_nbRes = len(bands_definition.ListOfL2Resolution)
         for r in range(0, l_nbRes):
             l_res = bands_definition.ListOfL2Resolution[r]
             tocr_image = os.path.join(scat_working,
                                       "tocr_" + l_res + ".tif")
             if dict_of_input.get("Plugin").WideFieldSensor:
                 param_scattering["anglezone"] = dict_of_input.get(
                     "L1Reader").get_value("AngleZoneMaskList")[r]
             param_scattering["dtm"] = dict_of_input.get("DEM").ALTList[r]
             param_scattering["edg"] = dict_of_input.get(
                 "L1Reader").get_value("L2EDGOutputList")[r]
             param_scattering["aot"] = dict_of_output["AOT_" + l_res]
             param_scattering["toac"] = dict_of_output["L2TOA_" + l_res]
             param_scattering["tocr"] = tocr_image
             l_l2bandids = bands_definition.get_list_of_l2_coarse_band_id_associated_to_l2_band_code(
                 bands_definition.get_list_of_l2_band_code(l_res))
             param_scattering["l2bandincoarse"] = [
                 str(b) for b in l_l2bandids
             ]
             scat_app = OtbAppHandler(
                 "ScatteringCorrection",
                 param_scattering,
                 write_output=is_croco_on("scatteringcorrection"))
             self._l2_pipeline.add_otb_app(scat_app)
             dict_of_output["TOC_" +
                            l_res] = scat_app.getoutput().get("tocr")
             tocr_list.append(dict_of_output["TOC_" + l_res])
         dict_of_output["L2TOCList"] = tocr_list
Ejemplo n.º 3
0
 def __init__(self, otb_app, parameters, write_output=True):
     self._c1 = MajaOtbCots()
     self._app_name = otb_app
     LOGGER.debug("Initializing : " + self._app_name)
     if "ram" not in list(parameters.keys()):
         curr_ram = int(memory_used_by_process_current(os.getpid()))
         avail_ram = OtbAppHandler.ram_to_use - curr_ram
         if avail_ram < OtbAppHandler.ram_to_use/OtbAppHandler.ram_limit_factor:
             parameters["ram"] = str(OtbAppHandler.ram_to_use / OtbAppHandler.ram_limit_factor)
         else:
             parameters["ram"] = str(avail_ram)
     LOGGER.debug(parameters)
     self._write_output = not is_croco_off() and (write_output or is_croco_on())
     self._c1.pre(otb_app, parameters)
     if not get_test_mode():
         self._run()
     self._post()
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Water vapor post processing Correction start")
        caching = dict_of_input.get("Params").get("Caching")
        wvpp_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "WaterVaporPPProc_", do_always_remove=True)
        vap_default = float(
            dict_of_input.get("L2COMM").get_value("WaterAmountDefaultValue"))
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        l_nbRes = len(bands_definition.ListOfL2Resolution)
        # Does the user choose to compute the VAP image ? (GIP_L2COMM_UseDefaultConstantWaterAmount)
        l_UseDefaultConstantWaterAmount = False
        if not dict_of_input.get("Plugin").WaterVapourDetermination or as_bool(
                dict_of_input.get("L2COMM").get_value(
                    "GIP_L2COMM_UseDefaultConstantWaterAmount")):
            l_UseDefaultConstantWaterAmount = True

        vap_list = []
        vapmask_list = []
        if l_UseDefaultConstantWaterAmount:
            for r in range(0, l_nbRes):
                l_res = bands_definition.ListOfL2Resolution[r]
                vap_image_filename = os.path.join(wvpp_working,
                                                  "vap_" + l_res + ".tif")
                vap_mask_filename = os.path.join(
                    wvpp_working, "vapmask_" + l_res + ".tif:uint8")
                vap_image = constant_image(dict_of_input.get("DEM").ALTList[r],
                                           vap_default,
                                           vap_image_filename,
                                           write_output=False)
                self._l2_pipeline.add_otb_app(vap_image)
                dict_of_output["VAP_" + l_res] = vap_image.getoutput()["out"]
                vap_mask = constant_image(dict_of_input.get("DEM").ALTList[r],
                                          0,
                                          vap_mask_filename,
                                          write_output=False)
                self._l2_pipeline.add_otb_app(vap_mask)
                dict_of_output["VAPMASK_" +
                               l_res] = vap_mask.getoutput()["out"]
                vap_list.append(dict_of_output["VAP_" + l_res])
                vapmask_list.append(dict_of_output["VAPMASK_" + l_res])
        else:
            l_refbandcode = dict_of_input.get("L2COMM").get_value(
                "WaterVaporReferenceBandCode")
            l_bandcode = dict_of_input.get("L2COMM").get_value(
                "WaterVaporBandCode")
            vap_subimage_filename = os.path.join(wvpp_working, "vap_sub.tif")
            vap_submask_filename = os.path.join(wvpp_working,
                                                "vapmask_sub.tif")
            param_watervpp = {
                "tocr":
                dict_of_output["RayleighIPTOCR"],
                "vapin":
                dict_of_output["VAP_Sub"],
                "cldsum":
                dict_of_output[constants.CLOUD_MASK_ALL],
                "defaultwatervapor":
                vap_default,
                "threshold":
                dict_of_input.get("L2COMM").get_value_f(
                    "WaterVaporReflectanceThreshold"),
                "thresholdref":
                dict_of_input.get("L2COMM").get_value_f(
                    "WaterVaporReflectanceThresholdRef"),
                "referencebandcode":
                bands_definition.get_band_id_in_l2_coarse(l_refbandcode),
                "bandcode":
                bands_definition.get_band_id_in_l2_coarse(l_bandcode),
                "nodata":
                dict_of_input.get("L2COMM").get_value_f("VAPNodataValue"),
                "sevalidradius":
                dict_of_input.get("L2COMM").get_value_i(
                    "WaterVaporSEValidRadius"),
                "initwindowradius":
                dict_of_input.get("L2COMM").get_value_i(
                    "WaterVaporInitWindowRadius"),
                "maxwindowradius":
                dict_of_input.get("L2COMM").get_value_i(
                    "WaterVaporMaxWindowRadius"),
                "mask":
                vap_submask_filename + ":uint8",
                "vap":
                vap_subimage_filename
            }
            watervpp_app = OtbAppHandler("WaterVaporPostPro",
                                         param_watervpp,
                                         write_output=False)
            self._l2_pipeline.add_otb_app(watervpp_app)

            for r in range(0, l_nbRes):
                l_res = bands_definition.ListOfL2Resolution[r]
                vap_image_filename = os.path.join(wvpp_working,
                                                  "vap_" + l_res + ".tif")
                vap_mask_filename = os.path.join(wvpp_working,
                                                 "vapmask_" + l_res + ".tif")
                param_subresampling = {
                    "dtm": dict_of_input.get("DEM").ALTList[r],
                    "im": watervpp_app.getoutput()["vap"],
                    "interp": "linear",
                    "out": vap_image_filename
                }
                subresampling_app = OtbAppHandler("Resampling",
                                                  param_subresampling,
                                                  write_output=False)
                self._l2_pipeline.add_otb_app(subresampling_app)

                param_maskresampling = {
                    "dtm": dict_of_input.get("DEM").ALTList[r],
                    "im": watervpp_app.getoutput()["mask"],
                    "interp": "linear",
                    "out": vap_mask_filename + ":uint8",
                    "threshold": 0.25
                }
                maskresampling_app = OtbAppHandler("Resampling",
                                                   param_maskresampling,
                                                   write_output=False)
                self._l2_pipeline.add_otb_app(maskresampling_app)
                if is_croco_on("watervaporpostprocessing"):
                    write_images([
                        subresampling_app.getoutput()["out"],
                        maskresampling_app.getoutput()["out"]
                    ], [vap_image_filename, vap_mask_filename])
                    dict_of_output["VAP_" + l_res] = vap_image_filename
                    dict_of_output["VAPMASK_" + l_res] = vap_mask_filename
                else:
                    dict_of_output[
                        "VAP_" + l_res] = subresampling_app.getoutput()["out"]
                    dict_of_output[
                        "VAPMASK_" +
                        l_res] = maskresampling_app.getoutput()["out"]
                vap_list.append(dict_of_output["VAP_" + l_res])
                vapmask_list.append(dict_of_output["VAPMASK_" + l_res])
        dict_of_output["L2VAPList"] = vap_list
        dict_of_output["L2VAPMaskList"] = vapmask_list
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Cirrus Correction start")
        cirrus_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "CirrusCorrProc_", do_always_remove=True)
        refres_name = dict_of_input.get(
            "Plugin").CirrusCorrectionReferenceResolution
        refres_idx = 9999
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        for i in range(0, len(bands_definition.ListOfL2Resolution)):
            if bands_definition.ListOfL2Resolution[i] == refres_name:
                refres_idx = i
        # TODO test refres_idx
        result = {}
        ref_result = self._gamma_compute_at_res(dict_of_input, dict_of_output,
                                                refres_idx, cirrus_working)
        result[bands_definition.ListOfL2Resolution[refres_idx]] = ref_result
        if ref_result[2]:
            LOGGER.info("Cirrus flag enabled on reference resolution")
            l_gammamean = ref_result[3] * ref_result[4]
            l_nbgamma = ref_result[3]
            for i in range(0, len(bands_definition.ListOfL2Resolution)):
                if bands_definition.ListOfL2Resolution[i] != refres_name:
                    res_result = self._gamma_compute_at_res(
                        dict_of_input, dict_of_output, i, cirrus_working)
                    result[bands_definition.ListOfL2Resolution[i]] = res_result
                    l_gammamean = l_gammamean + res_result[3] * res_result[4]
                    l_nbgamma = l_nbgamma + res_result[3]
            l_gammamean = l_gammamean / l_nbgamma
            l_gamma_max = float(
                dict_of_input.get("L2COMM").get_value(
                    "CirrusCorrectionMaxGammaValue"))
            l_gamma_min = float(
                dict_of_input.get("L2COMM").get_value(
                    "CirrusCorrectionMinGammaValue"))
            # Boundary set for gamma
            if l_gammamean > l_gamma_max:
                l_gammamean = l_gamma_max
            if l_gammamean < l_gamma_min:
                l_gammamean = l_gamma_min

            LOGGER.info("Gamma : " + str(l_gammamean))

            # Apply correction
            for i in range(0, len(bands_definition.ListOfL2Resolution)):
                l_res = bands_definition.ListOfL2Resolution[i]
                corrected_toa = os.path.join(cirrus_working,
                                             "toac_" + l_res + ".tif")
                l_gammaswir = l_gammamean * float(
                    dict_of_input.get("L2COMM").get_value(
                        "CirrusCorrectionSWIRGammaFactor"))
                l_l2bandcodes = bands_definition.get_list_of_l2_band_code(
                    l_res)
                swir_band_list = xml_tools.as_string_list(
                    dict_of_input.get("L2COMM").get_value(
                        "CirrusCorrectionSWIRBandCodes"))
                # Param of cirrus apply app
                param_cirrus_apply = {
                    "l2toa":
                    dict_of_output["AtmoAbsIPTOA_" + l_res],
                    "l2edg":
                    dict_of_input.get("L1Reader").get_value("L2EDGOutputList")
                    [i],
                    "l2dtm":
                    dict_of_input.get("DEM").ALTList[i],
                    "l2cirrus":
                    result[l_res][0],
                    "cirrusmask":
                    result[l_res][1],
                    "resolution":
                    bands_definition.get_l1_resolution(l_res),
                    "maxdistance":
                    float(
                        dict_of_input.get("L2COMM").get_value(
                            "CirrusCorrectionMaxDistance")),
                    "thresholdgain":
                    float(
                        dict_of_input.get("L2COMM").get_value(
                            "CirrusMaskThresholdGain")),
                    "thresholdoffset":
                    float(
                        dict_of_input.get("L2COMM").get_value(
                            "CirrusMaskThresholdOffset")),
                    "thresholdfactor":
                    float(
                        dict_of_input.get("L2COMM").get_value(
                            "CirrusCorrectionThresholdFactor")),
                    "gammamean":
                    l_gammamean,
                    "swirgammafactor":
                    l_gammaswir,
                    "maxreflectance":
                    float(
                        dict_of_input.get("L2COMM").get_value(
                            "CirrusMaxReflectance")),
                    "bandcodelist":
                    l_l2bandcodes,
                    "swirbandcodelist":
                    swir_band_list,
                    "correctedtoa":
                    corrected_toa
                }
                app = OtbAppHandler(
                    "CirrusCorrectionApply",
                    param_cirrus_apply,
                    write_output=(False or is_croco_on("cirruscorrection")))
                self._l2_pipeline.add_otb_app(app)
                dict_of_output["L2TOA_" +
                               l_res] = app.getoutput().get("correctedtoa")
        else:
            LOGGER.info(
                "Cirrus flag disabled on reference resolution, no cirrus correction"
            )
        # return cirrus corrected flag
        dict_of_output["CirrusCorrected"] = ref_result[2]
        return ref_result[2]
    def generate_mask_rasters_gml(self, boundingBoxMap, l2Areas, projectionRef,
                                  satPixFileNames, defectivPixFileNames,
                                  zoneMaskFileNames, gdalRasterizeMaskCmd,
                                  gdalRasterizeDetCmd, working):
        """

        :param boundingBoxMap: BoundingBoxMapType
        :param projectionRef: string
        :param satPixFileNames: ListOfStrings
        :param defectivPixFileNames: ListOfStrings
        :param zoneMaskFileNames: ListOfStrings
        :param gdalRasterizeMaskCmd: string
        :param gdalRasterizeDetCmd: string
        :param working: string
        :return:
        """

        # *******************************************************************************************************
        # Generate mask rasters by rasterizing the gml mask per L2 resolution per band
        # *******************************************************************************************************
        LOGGER.debug("Start GML mask rasterization ...")
        l_BandsDefinitions = self._plugin.BandsDefinitions
        l2Area = None
        l_ListOfL2Resolution = l_BandsDefinitions.ListOfL2Resolution  # ListOfStrings
        l_NbL2Res = len(l_ListOfL2Resolution)

        for l2res in range(l_NbL2Res):
            self._l2defectmasklist.append([])
            self._l2zonemasklist.append([])
            self._l2satimagelist.append([])

        for l2res in range(l_NbL2Res):
            # Current resolution: "R1" or "R2"
            curRes = l_ListOfL2Resolution[l2res]

            # Get the list of band of the current resolution
            listOfL2Bands = l_BandsDefinitions.get_list_of_l2_band_code(curRes)
            nbBand = len(listOfL2Bands)
            l2Area = l2Areas[l2res]
            counts = (0, 0, 0)
            # Generate a constant image that will be used if the gml masks are empty (no feature)
            tmp_constant_filename = os.path.join(
                working, "const_{}.tif:uint8".format(l2res))
            tmp_constant_image_app = constant_image(self._dem.ALTList[l2res],
                                                    0,
                                                    tmp_constant_filename,
                                                    write_output=True)
            tmp_constant_image = tmp_constant_image_app.getoutput()["out"]
            # For each band of the current resolution
            for l_StrBandIdL2 in listOfL2Bands:
                # Get the L1 band index associated to the L2 band code
                l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l_StrBandIdL2)
                LOGGER.debug(
                    "Sentinel2L1ImageFileReaderBase::GenerateMaskRasters: CurrentResol = %s, reading the "
                    "BandId L1 (associated) <%s> with index <%s>.", curRes,
                    l_StrBandIdL2, l1BandIdx)

                # Read the bounding box for the resolution  "l_L1Resolution"
                l_BoundingBox = boundingBoxMap.get(curRes)

                # =======> RASTERIZE GML MASKS
                new_counts = self.rasterize_gml_masks(
                    curRes, l2res, l2Area, projectionRef, l1BandIdx,
                    satPixFileNames[l1BandIdx],
                    defectivPixFileNames[l1BandIdx],
                    zoneMaskFileNames[l1BandIdx], l_BoundingBox,
                    gdalRasterizeMaskCmd, gdalRasterizeDetCmd,
                    tmp_constant_image, working)
                counts = tuple(map(operator.add, counts, new_counts))

            # band loop

            # *******************************************************************************************************
            # L2 Zone mask pipeline connection
            # *******************************************************************************************************
            if counts[0] != 0:
                zone_mask = os.path.join(working,
                                         "Masks_Zone_{}.tif".format(l2res))
                param_concatenate = {
                    "il": self._l2zonemasklist[l2res],
                    "out": zone_mask + ":uint8"
                }
                l2zoneimage_app = OtbAppHandler(
                    "ConcatenateDoubleImages",
                    param_concatenate,
                    write_output=(False
                                  or is_croco_on("sentinel2.l1reader.l2zone")))
                self._l2zoneimagelist.append(
                    l2zoneimage_app.getoutput().get("out"))
                self._pipeline.add_otb_app(l2zoneimage_app)
            else:
                self._l2zoneimagelist.append(self._l2zonemasklist[l2res][0])
            # *******************************************************************************************************
            # PIX image pipeline connection (DEFECTIV PIX)
            # *******************************************************************************************************
            if counts[2] != 0:
                # Compress (concatenate the vector image to binary image)
                pix_vector_mask = os.path.join(
                    working, "Masks_Defect_Vector_{}.tif".format(l2res))
                param_concatenate = {
                    "il": self._l2defectmasklist[l2res],
                    "out": pix_vector_mask + ":uint8"
                }
                pix_vector = OtbAppHandler("ConcatenateDoubleImages",
                                           param_concatenate)
                pix_mask = os.path.join(working,
                                        "Masks_Defect_{}.tif".format(l2res))
                param_binconcatenate = {
                    "im": pix_vector.getoutput().get("out"),
                    "out": pix_mask + ":uint16"
                }
                pix = OtbAppHandler(
                    "BinaryConcatenate",
                    param_binconcatenate,
                    write_output=(False
                                  or is_croco_on("sentinel2.l1reader.l2pix")))
                self._pipeline.add_otb_app(pix)
                self._l2piximagelist.append(pix.getoutput().get("out"))
            else:
                self._l2piximagelist.append(self._l2defectmasklist[l2res][0])
            # end res loop
        LOGGER.debug("End GML mask rasterization ...")
Ejemplo n.º 7
0
    def generate_edg_images_from_toa(self, listOfTOAImageFileNames, working):
        """

        :param listOfTOAImageFileNames: list
        :param working: path
        :return:
        """
        # Get the number of band with the number of TOA image files set in the input product directory
        l_NbBand = len(listOfTOAImageFileNames)  # int

        # *******************************************************************************************************
        # L2SubEDG pipeline connection
        # *******************************************************************************************************
        LOGGER.debug("Start IPEDGSub")
        m_OneBandFilterList = []
        m_ResamplingList = []
        m_OrFilterList = []
        tmp_edg_pipe = OtbPipelineManager()
        for i in range(2):
            toaFilename = listOfTOAImageFileNames[i]
            LOGGER.debug(("toaFilename : ", toaFilename))
            out_oneBandValue = os.path.join(working,
                                            "OneBandValue" + str(i) + ".tif")
            band_math_OneBand = one_band_equal_value(
                self._toa_scalar_list[i],
                output_image=out_oneBandValue + ":uint8",
                threshold=0.0,
                write_output=False)
            tmp_edg_pipe.add_otb_app(band_math_OneBand)
            m_OneBandFilterList.append(band_math_OneBand.getoutput()["out"])
            out_resample = os.path.join(working,
                                        "mask_resample" + str(i) + ".tif")
            resample_mask = resample(band_math_OneBand.getoutput()["out"],
                                     self._dem.ALC,
                                     out_resample + ":uint8",
                                     OtbResampleType.LINEAR_WITH_RADIUS,
                                     threshold=0.5,
                                     write_output=False)
            tmp_edg_pipe.add_otb_app(resample_mask)
            m_ResamplingList.append(resample_mask.getoutput()["out"])

        for i in range(l_NbBand - 2):
            out_oneBandValue = os.path.join(
                working, "OneBandValue" + str(i + 2) + ".tif")
            band_math_OneBand = one_band_equal_value(
                self._toa_scalar_list[i + 2],
                output_image=out_oneBandValue + ":uint8",
                threshold=0.0,
                write_output=False)
            tmp_edg_pipe.add_otb_app(band_math_OneBand)
            m_OneBandFilterList.append(band_math_OneBand.getoutput()["out"])

            out_resample = os.path.join(working,
                                        "mask_resample" + str(i + 2) + ".tif")
            resample_mask = resample(band_math_OneBand.getoutput()["out"],
                                     self._dem.ALC,
                                     out_resample + ":uint8",
                                     OtbResampleType.LINEAR_WITH_RADIUS,
                                     threshold=0.5,
                                     write_output=False)
            tmp_edg_pipe.add_otb_app(resample_mask)
            m_ResamplingList.append(resample_mask.getoutput()["out"])

        out_concat = os.path.join(working, "ConcatSubEdgOneBand.tif")
        param_oneband_concat = {
            "il": m_ResamplingList,
            "out": out_concat + ":uint8"
        }
        qoth_concat_app = OtbAppHandler("ConcatenateMaskImages",
                                        param_oneband_concat,
                                        write_output=False)
        tmp_edg_pipe.add_otb_app(qoth_concat_app)
        out_or0 = os.path.join(working, "MaskOrMask_0.tif")
        band_math_or_b1 = one_band_equal_value(
            qoth_concat_app.getoutput().get("out"),
            output_image=out_or0 + ":uint8",
            threshold=1)
        self._edgsubmask = band_math_or_b1.getoutput().get("out")
        tmp_edg_pipe.add_otb_app(band_math_or_b1)
        tmp_edg_pipe.free_otb_app()
        LOGGER.debug("End IPEDGSub.")

        # *******************************************************************************************************
        # L2EDG pipeline connection
        # *******************************************************************************************************
        LOGGER.debug("Start L2EDG ...")

        l_BandsDefinitions = self._plugin.BandsDefinitions
        LOGGER.debug("BANDS DEFINITION")
        LOGGER.debug(l_BandsDefinitions)

        l_ListOfL2Resolution = l_BandsDefinitions.ListOfL2Resolution  # ListOfStrings
        # At L2 resolution
        l_NbL2Res = len(l_ListOfL2Resolution)

        for r in range(l_NbL2Res):
            res_str = l_ListOfL2Resolution[r]
            # Set the threshold to 0.0.001 so that all pixel above 1/1000 to edge pixels
            # to identify the pixel contaminated by an edge pixel after resampling
            # ExpandFilterPointer => PadAndResampleImageFilter => app ressampling
            out_ressampling = os.path.join(
                working, "IPEDGRealL2_{}.tif".format(res_str))
            l2edg_resamp_app = resample(
                self._edgsubmask,
                self._dem.ALTList[r],
                out_ressampling + ":uint8",
                OtbResampleType.LINEAR,
                threshold=0.0,
                write_output=(False
                              or is_croco_on("sentinel2.l1reader.l2edg")))
            self._l2edg_pipeline.add_otb_app(l2edg_resamp_app)
            self._l2edgmasklist.append(l2edg_resamp_app.getoutput().get("out"))
        LOGGER.debug("End L2EDG ...")
    def write_public_images(
            self,
            p_L2ImageFilenamesProvider,
            p_ReflectanceQuantificationValue,
            p_AOTQuantificationValue,
            p_AOTNodataValue,
            p_VAPQuantificationValue,
            p_VAPNodataValue,
            p_CLDDataBandsSelected,
            p_CLDCoreAlgorithmsMapBand,
            p_WritePublicProduct,
            p_EnvCorOption,
            working_dir):
        # == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
        # IF PUBLIC PART OF L2 PRODUCT  IS WRITTEN
        # == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
        if p_WritePublicProduct:
            l_NumberOfResolutions = len(p_L2ImageFilenamesProvider.get_sre_headers())
            LOGGER.debug("L2ImageFileWriterBase::Initialize Number of resolutions: " + str(l_NumberOfResolutions) + ".")
            l_BandsDefinitions = self._plugin.BandsDefinitions
            l_RedBandId = 0
            l_GreenBandId = 0
            l_BlueBandId = 0
            resol_QLK = 0
            l_Resolution = ""
            l_BandsDefinitions = self._plugin.BandsDefinitions
            l_RedBandId, l_BlueBandId, l_GreenBandId = l_BandsDefinitions.get_l2_information_for_quicklook_band_code(
                self._quicklookredbandcode,
                self._quicklookgreenbandcode,
                self._quicklookbluebandcode,
            )
            # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
            # ** ** LOOP on RESOLUTION ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
            for resol in range(0, l_NumberOfResolutions):
                l_StrResolution = l_BandsDefinitions.ListOfL2Resolution[resol]
                # --------------------------------------------------------
                # L2 area AreaType l_AreaToL2Resolution
                l_AreaFile = self._sre_list[resol]
                l_ListOfBand = l_BandsDefinitions.get_list_of_l2_band_code(l_StrResolution)

                LOGGER.debug("L2ImageFileReader::Gen Public image file for the resolution " + l_StrResolution + ".")

                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # Read the Coef apply for SRE and FRE images
                LOGGER.info(
                    "SRE and FRE values multiply by the reflectance quantification value " +
                    str(p_ReflectanceQuantificationValue) +
                    ".")
                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # ** ** PUBLIC  DATA ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                #Store image pointer and filename
                tmp_l2_filename_list = []
                tmp_l2_image_list = []
                tmp_l2_pipe = OtbPipelineManager()
                # START WRITING SRE Image file DATA
                # Caching the SRE image, before computing the QuickLook.
                # Create the scalar image filter
                sre_filename = p_L2ImageFilenamesProvider.get_sre_filenames()[
                                   resol] + file_utils.get_extended_filename_write_image_file_standard()
                param_scaled_sre = {
                    "im": self._sre_list[resol],
                    "coef": p_ReflectanceQuantificationValue,
                    "out": sre_filename+":int16"}
                sre_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_sre,
                                             write_output=is_croco_on("earthexplorer.l2writer.sre"))
                tmp_l2_image_list.append(sre_scal_app.getoutput().get("out"))
                tmp_l2_filename_list.append(sre_filename)
                tmp_l2_pipe.add_otb_app(sre_scal_app)
                #QuickLook stuff
                if resol == resol_QLK :
                    tmp_sre_roi_red = os.path.join(working_dir, "tmp_sre_roi_red.tif")
                    tmp_sre_roi_red_app = extract_roi(self._sre_list[resol], [l_RedBandId],
                                                  tmp_sre_roi_red, write_output=is_croco_on("earthexplorer.l2writer.roi"))
                    tmp_l2_image_list.append(tmp_sre_roi_red_app.getoutput().get("out"))
                    tmp_l2_filename_list.append(tmp_sre_roi_red)
                    self._qckl_red_image = tmp_sre_roi_red
                    tmp_l2_pipe.add_otb_app(tmp_sre_roi_red_app)
                    tmp_sre_roi_green = os.path.join(working_dir, "tmp_sre_roi_green.tif")
                    tmp_sre_roi_green_app = extract_roi(self._sre_list[resol], [l_GreenBandId],
                                                      tmp_sre_roi_green, write_output=is_croco_on("earthexplorer.l2writer.roi"))
                    tmp_l2_image_list.append(tmp_sre_roi_green_app.getoutput().get("out"))
                    tmp_l2_filename_list.append(tmp_sre_roi_green)
                    self._qckl_green_image = tmp_sre_roi_green
                    tmp_l2_pipe.add_otb_app(tmp_sre_roi_green_app)
                    tmp_sre_roi_blue = os.path.join(working_dir, "tmp_sre_roi_blue.tif")
                    tmp_sre_roi_blue_app = extract_roi(self._sre_list[resol], [l_BlueBandId],
                                                      tmp_sre_roi_blue, write_output=is_croco_on("earthexplorer.l2writer.roi"))
                    tmp_l2_image_list.append(tmp_sre_roi_blue_app.getoutput().get("out"))
                    tmp_l2_filename_list.append(tmp_sre_roi_blue)
                    self._qckl_blue_image = tmp_sre_roi_blue
                    tmp_l2_pipe.add_otb_app(tmp_sre_roi_blue_app)

                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # START WRITING FRE Image file DATA
                fre_scal_app = None
                if p_EnvCorOption:
                    fre_filename = p_L2ImageFilenamesProvider.get_fre_filenames()[
                                       resol] + file_utils.get_extended_filename_write_image_file_standard()
                    param_scaled_fre = {
                        "im": self._fre_list[resol],
                        "coef": p_ReflectanceQuantificationValue,
                        "out": fre_filename+":int16"}
                    fre_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_fre,
                                                 write_output=is_croco_on("earthexplorer.l2writer.fre"))
                    #Write SRE and FRE simultaneously
                    tmp_l2_image_list.append(fre_scal_app.getoutput().get("out"))
                    tmp_l2_filename_list.append(fre_filename)
                    tmp_l2_pipe.add_otb_app(fre_scal_app)

                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # START WRITING ATB Image file DATA
                # Initialize the Scalar filter
                # FA1424: Temporary Fix to address cosmetic aspects of FA1424
                # VAPThreshold.SetInput(self.GetVAPImageList()[resol))
                # VAPThreshold.SetOutsideValue(255. / p_VAPQuantificationValue)
                # VAPThreshold.ThresholdAbove(255. / p_VAPQuantificationValue)

                # VAPScalar.SetInput(VAPThreshold.GetOutput())
                # VAPScalar.SetCoef(p_VAPQuantificationValue)
                tmp_vap = os.path.join(working_dir, "tmp_vap_scaled_" + l_StrResolution + ".tif")
                param_bandmath_vap = {
                    "il": [
                        self._l2vapimagelist[resol],
                        self._l2edgimagelist[resol]],
                    "exp": "(im2b1 == 1)?" +
                    str(p_VAPNodataValue) +
                    ":" +
                    "rint(im1b1*" +
                    str(p_VAPQuantificationValue)+")",
                    "out": tmp_vap + ":uint8"}
                vap_scal_app = OtbAppHandler("BandMathDouble", param_bandmath_vap, write_output=False)

                tmp_aot = os.path.join(working_dir, "tmp_aot_scaled_" + l_StrResolution + ".tif")
                param_bandmath_aot = {"il": [self._l2aotlist[resol], self._l2edgimagelist[resol]],
                                      "exp": "(im2b1 == 1)?" + str(p_AOTNodataValue) + ":" + "rint(im1b1*" + str(
                                          p_AOTQuantificationValue)+")",
                                      "out": tmp_aot + ":uint8"
                                      }
                aot_scal_app = OtbAppHandler("BandMathDouble", param_bandmath_aot, write_output=False)
                tmp_l2_pipe.add_otb_app(aot_scal_app)
                atb_filename = p_L2ImageFilenamesProvider.get_atb_image_filename()[resol]
                param_atb_concat = {"il": [vap_scal_app.getoutput().get("out"), aot_scal_app.getoutput().get("out")],
                                    "out": atb_filename + ":uint8"
                                    }
                atb_concat_app = OtbAppHandler("ConcatenateMaskImages", param_atb_concat,
                                               write_output=is_croco_on("earthexplorer.l2writer.atb"))
                tmp_l2_image_list.append(atb_concat_app.getoutput().get("out"))
                tmp_l2_filename_list.append(atb_filename)
                tmp_l2_pipe.add_otb_app(atb_concat_app)
                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # START WRITING MSK Image file DATA
                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # Connect the WAM image
                was_resampled = os.path.join(working_dir, "was_resampled_" + l_StrResolution + ".tif")
                app_resample_was = resample(self._wasimage, self._dtm.ALTList[resol],
                                            was_resampled, threshold=0.25, write_output=False)
                tmp_l2_pipe.add_otb_app(app_resample_was)
                # Connect the HID image
                hid_resampled = os.path.join(working_dir, "hid_resampled_" + l_StrResolution + ".tif")
                app_resample_hid = resample(self._dtm_hid, self._dtm.ALTList[resol],
                                            hid_resampled, threshold=0.25, write_output=False)
                tmp_l2_pipe.add_otb_app(app_resample_hid)
                # Connect the SHDimage
                shd_resampled = os.path.join(working_dir, "shd_resampled_" + l_StrResolution + ".tif")
                app_resample_shd = resample(self._dtm_shd, self._dtm.ALTList[resol],
                                            shd_resampled, threshold=0.25, write_output=False)
                tmp_l2_pipe.add_otb_app(app_resample_shd)

                # Create the MOTH image that concatenates the WAT, HID, SHD, STL and TGS masks
                MOTHImageList = []
                MOTHImageList.append(app_resample_was.getoutput().get("out"))
                MOTHImageList.append(app_resample_hid.getoutput().get("out"))
                MOTHImageList.append(app_resample_shd.getoutput().get("out"))
                # Append STL
                MOTHImageList.append(self._stl_list[resol])
                # Append TGS
                MOTHImageList.append(self._tgs_list[resol])
                app_resample_snow = None
                if self._cld_snow is not None:
                    snow_resampled = os.path.join(working_dir, "snow_resampled_" + l_StrResolution + ".tif")
                    app_resample_snow = resample(self._cld_snow, self._dtm.ALTList[resol],
                                                 snow_resampled, working_dir, 0.25, write_output=False)
                    tmp_l2_pipe.add_otb_app(app_resample_snow)
                    MOTHImageList.append(app_resample_snow.getoutput().get("out"))
                # Concat to get atb
                moth_tmp_concat = os.path.join(working_dir, "tmp_moth_" + l_StrResolution + ".tif")
                param_moth_concat = {"il": MOTHImageList,
                                     "out": moth_tmp_concat
                                     }
                # Concatenate to produce the MOTH file
                app_moth_concat = OtbAppHandler("ConcatenateMaskImages", param_moth_concat, write_output=False)
                tmp_l2_pipe.add_otb_app(app_moth_concat)
                # Binary concatenation of WAT, HID, SHD, STL and TGS masks
                msk_filename = p_L2ImageFilenamesProvider.get_msk_filename()[resol]
                param_moth_binconcat = {"im": app_moth_concat.getoutput().get("out"),
                                        "out": msk_filename + ":uint8"
                                        }
                moth_binconcat_app = OtbAppHandler("BinaryConcatenate", param_moth_binconcat,
                                                   write_output=is_croco_on("earthexplorer.l2writer.msk"))
                tmp_l2_image_list.append(moth_binconcat_app.getoutput().get("out"))
                tmp_l2_filename_list.append(msk_filename)
                tmp_l2_pipe.add_otb_app(moth_binconcat_app)
                # Concatenation of the MSK mask with the CLD and MOTH masks
                # --------------------------------------------------------

                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                # START WRITING QLT Image file DATA
                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
                QOTHImageList = []

                QOTHImageList.append(self._l2edgimagelist[resol])
                QOTHImageList.append(self._l2taomasklist[resol])
                if self._plugin.WaterVapourDetermination:
                    QOTHImageList.append(self._l2iwcmasklist[resol])

                # Concat to get atb
                qoth_tmp_concat = os.path.join(working_dir, "tmp_qoth_" + l_StrResolution + ".tif")
                param_qoth_concat = {"il": QOTHImageList,
                                     "out": qoth_tmp_concat + ":uint8"
                                     }
                qoth_concat_app = OtbAppHandler("ConcatenateImages", param_qoth_concat, write_output=False)
                qoth_tmp_binconcat = os.path.join(working_dir, "tmp_binqoth_" + l_StrResolution + ".tif")
                param_qoth_binconcat = {"im": qoth_concat_app.getoutput().get("out"),
                                        "out": qoth_tmp_binconcat + ":uint8"
                                        }
                qoth_binconcat_app = OtbAppHandler("BinaryConcatenate", param_qoth_binconcat, write_output=False)

                # -------------------------------------------------------
                # Concatenation of the QLT mask with the SAT, PIX and OTH masks
                # --------------------------------------------------------
                # As for the PIX mask, the SAT mask in concatenate in one band where each bit matches one band
                sat_tmp_concat = os.path.join(working_dir, "tmp_sat_" + l_StrResolution + ".tif")
                param_sat_binconcat = {"im": self._l2satimagelist[resol],
                                       "out": sat_tmp_concat + ":uint8"
                                       }
                sat_binconcat_app = OtbAppHandler("BinaryConcatenate", param_sat_binconcat, write_output=False)

                # Create the QLT vector image
                qlt_tmp_concat = os.path.join(working_dir, "tmp_qlt_" + l_StrResolution + ".tif")
                QLTImageList = []
                QLTImageList.append(sat_binconcat_app.getoutput().get("out"))
                QLTImageList.append(self._l2piximagelist[resol])
                QLTImageList.append(qoth_binconcat_app.getoutput().get("out"))
                param_qlt_concat = {"il": QLTImageList,
                                    "out": qlt_tmp_concat + ":uint8"
                                    }
                qlt_concat_app = OtbAppHandler("ConcatenateImages", param_qlt_concat,
                                               write_output=is_croco_on("earthexplorer.l2writer.qlt"))
                tmp_l2_image_list.append(qlt_concat_app.getoutput().get("out"))
                tmp_l2_filename_list.append(p_L2ImageFilenamesProvider.get_qlt_filenames()[resol])
                tmp_l2_pipe.add_otb_app(qlt_concat_app)

                # --------------------------
                # Write all the images at L2 Reso
                write_images(tmp_l2_image_list, tmp_l2_filename_list)
                tmp_l2_pipe.free_otb_app()
                clean_pipe(self._sre_list[resol])
                if p_EnvCorOption:
                    clean_pipe(self._fre_list[resol])

                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
                # START WRITING CLD Public Image file DATA
                # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *

                # Connect the CLD image
                # -------------------------------------
                self.write_cld_image(self._l2cldlist[resol], p_CLDDataBandsSelected,
                                   p_L2ImageFilenamesProvider.get_cld_image_filename()[resol])

            LOGGER.debug("Writing L2 resolution image done !")
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Cloud mask resampling start")
        p_working = dict_of_input.get("AppHandler").get_directory_manager().get_temporary_directory("CloudMaskResampling_",
                                            do_always_remove=True)
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        do_write = is_croco_on("cloudmaskresampling")
        l2_cld_list = []
        for p_res in range(0, len(bands_definition.ListOfL2Resolution)):
            l_res = bands_definition.ListOfL2Resolution[p_res]
            # ---------- Resample All to resolution ---------------
            cld_all_resampled = os.path.join(p_working, "cloud_all_" + l_res + ".tif")
            cld_all_app = resample(dict_of_output[CLOUD_MASK_ALL], dict_of_input.get("DEM").ALTList[p_res],
                     cld_all_resampled, OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_all_app)
            dict_of_output[CLOUD_MASK_ALL + "_" + l_res] = cld_all_app.getoutput().get("out")
            # ---------- Resample All cloud to resolution ---------------
            cld_allclouds_resampled = os.path.join(p_working, "cloud_allclouds_" + l_res + ".tif")
            cld_allclouds_app = resample(dict_of_output[CLOUD_MASK_ALL_CLOUDS], dict_of_input.get("DEM").ALTList[p_res],
                     cld_allclouds_resampled, OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_allclouds_app)
            dict_of_output[CLOUD_MASK_ALL_CLOUDS + "_" + l_res] = cld_allclouds_app.getoutput().get("out")
            # ---------- Resample shadow to resolution ---------------
            cld_shadows_resampled = os.path.join(p_working, "cloud_shadows_" + l_res + ".tif")
            cld_shadows_app = resample(dict_of_output[CLOUD_MASK_SHADOWS], dict_of_input.get("DEM").ALTList[p_res],
                     cld_shadows_resampled, OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_shadows_app)
            dict_of_output[CLOUD_MASK_SHADOWS + "_" + l_res] = cld_shadows_app.getoutput().get("out")
            # ---------- Resample shadvar to resolution ---------------
            cld_shadvar_resampled = os.path.join(p_working, "cloud_shadvar_" + l_res + ".tif")
            cld_shadvar_app = resample(dict_of_output[CLOUD_MASK_SHADVAR], dict_of_input.get("DEM").ALTList[p_res],
                     cld_shadvar_resampled, OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_shadvar_app)
            dict_of_output[CLOUD_MASK_SHADVAR + "_" + l_res] = cld_shadvar_app.getoutput().get("out")
            # ---------- Resample Refl cloud to resolution ---------------
            cld_refl_resampled = os.path.join(p_working, "cloud_refl_" + l_res + ".tif")
            cld_refl_app = resample(dict_of_output[CLOUD_MASK_REFL], dict_of_input.get("DEM").ALTList[p_res], cld_refl_resampled,
                     OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_refl_app)
            dict_of_output[CLOUD_MASK_REFL + "_" + l_res] = cld_refl_app.getoutput().get("out")
            # ---------- Resample ReflVar cloud to resolution ---------------
            cld_reflvar_resampled = os.path.join(p_working, "cloud_reflvar_" + l_res + ".tif")
            cld_reflvar_app = resample(dict_of_output[CLOUD_MASK_REFL_VAR], dict_of_input.get("DEM").ALTList[p_res],
                     cld_reflvar_resampled,
                     OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_reflvar_app)
            dict_of_output[CLOUD_MASK_REFL_VAR + "_" + l_res] = cld_reflvar_app.getoutput().get("out")
            # ---------- Resample Extension cloud to resolution ---------------
            cld_ext_resampled = os.path.join(p_working, "cloud_ext_" + l_res + ".tif")
            cld_ext_app = resample(dict_of_output[CLOUD_MASK_EXTENSION], dict_of_input.get("DEM").ALTList[p_res], cld_ext_resampled,
                     OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_ext_app)
            dict_of_output[CLOUD_MASK_EXTENSION + "_" + l_res] = cld_ext_app.getoutput().get("out")
            # ---------- Resample Alt to resolution ---------------
            cld_alt_resampled = os.path.join(p_working, "cloud_alt_" + l_res + ".tif")
            cld_alt_app = resample(dict_of_output[CLOUD_MASK_ALT], dict_of_input.get("DEM").ALTList[p_res], cld_alt_resampled,
                     OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_alt_app)
            dict_of_output[CLOUD_MASK_ALT + "_" + l_res] = cld_alt_app.getoutput().get("out")
            # ---------- Resample Cirrus cloud to resolution ---------------
            cld_cirrus_resampled = os.path.join(p_working, "cloud_cirrus_" + l_res + ".tif")
            cld_cirrus_app = resample(dict_of_output[CLOUD_MASK_CIRRUS], dict_of_input.get("DEM").ALTList[p_res], cld_cirrus_resampled,
                     OtbResampleType.LINEAR,threshold=0.25,write_output=do_write)
            self._apps.add_otb_app(cld_cirrus_app)
            dict_of_output[CLOUD_MASK_CIRRUS + "_" + l_res] = cld_cirrus_app.getoutput().get("out")

            # APRES MEMORY IN CHAIN CORE ALGORITHMS : V 4-1-0
            #                    Bit 1 - Cloud_Mask.all : summary Logical or of All cloud, extension and shadow masks
            #                    Bit 2 - Cloud_Mask.all clouds: Logical or of All cloud masks and extension
            #                    Bit 3 - Cloud_Mask.shadows : shadows mask from clouds within image
            #                    Bit 4 - Cloud_Mask.shadvar: shadows mask from clouds outside image bit 1 : Cloud_Mask.all
            #                    Bit 5 - Cloud_Mask.refl : reflectance threshold
            #                    Bit 6 - Cloud_Mask.refl_var : reflectance variation threshold
            #                    Bit 7 - Cloud_Mask.extension : extension of the cloud mask
            #                        Bit 8 - Cloud_Mask.alt : stereoscopic mask  =>  VENUS et vide pour les autres
            #                    Bit 9 - Cirrus : Pour  L8 et S2 et vide pour les autres

            # FORMAT DISK
            #                    Bit 1 - Cloud_Mask.all : summary Logical or of All cloud and shadow masks
            #                    Bit 2 - Cloud_Mask.all clouds: Logical or of All cloud masks
            #                    Bit 3 - Cloud_Mask.shadows : shadows mask from clouds within image
            #                    Bit 4 - Cloud_Mask.shadvar: shadows mask from clouds outside image bit 1 : Cloud_Mask.all
            #                    Bit 5 - Cloud_Mask.refl : reflectance threshold
            #                    Bit 6 - Cloud_Mask.refl_var : reflectance variation threshold
            #                    Bit 7 - Cloud_Mask.extension : extension of the cloud mask
            #                    Bit 8 - VENUS : Cloud_Mask.alt : stereoscopic mask
            #                    Bit 8 - L8 et S2 : Bit 9 - Cirrus
            #                    Bit 8 - Vide pour les autres
            cld_list = []
            cld_list.append(dict_of_output[CLOUD_MASK_ALL + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_ALL_CLOUDS + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_SHADOWS + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_SHADVAR + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_REFL + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_REFL_VAR + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_EXTENSION + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_ALT + "_" + l_res])
            cld_list.append(dict_of_output[CLOUD_MASK_CIRRUS + "_" + l_res])
            l2_cld_list.append(cld_list)


        dict_of_output["L2CLDList"] = l2_cld_list
Ejemplo n.º 10
0
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Environment Correction start")
        env_working = dict_of_input.get("AppHandler").get_directory_manager().get_temporary_directory("EnvoCorrProc_",
                                            do_always_remove=True)
        l_envcorradius = dict_of_input.get("L2COMM").get_value_f("EnvCorrRadius")
        l_envcorsize = (2 * l_envcorradius + 1) * (2 * l_envcorradius + 1)
        caching = dict_of_input.get("Params").get("Caching")
        # Compute rho env sub
        rhoenv_sub_image = os.path.join(env_working, "rhoenv_sub.tif")
        param_computerho = {"toc": dict_of_output["TOC_sub"],
                            "edg": dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
                            "radius": l_envcorradius,
                            "filtercoeff": 1.0 / l_envcorsize,
                            "rhoenv": rhoenv_sub_image}
        computerho_app = OtbAppHandler("ComputeRhoEnv", param_computerho, write_output=True)

        # Interpolate for env
        tdif_filename = os.path.join(env_working, "tdif_sub.tif")
        tdir_filename = os.path.join(env_working, "tdir_sub.tif")
        albd_filename = os.path.join(env_working, "albd_sub.tif")
        param_interpolate = {"luttdir": dict_of_input.get("L2DIRT"),
                             "luttdif": dict_of_input.get("L2DIFT"),
                             "lutalbd": dict_of_input.get("L2ALBD"),
                             "aot": dict_of_output["AOT_Sub"],
                             "dtm": dict_of_input.get("DEM").ALC,
                             "edg": dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
                             "thetav": dict_of_input.get(
                                 "L1Info").ListOfViewingZenithAnglesPerBandAtL2CoarseResolution,
                             "tdir": tdir_filename,
                             "tdif": tdif_filename,
                             "albd": albd_filename
                             }
        interpolate_app = OtbAppHandler("InterpolateForEnvCorr", param_interpolate, write_output=True)
        tdif_image = interpolate_app.getoutput()["tdif"]
        tdir_image = interpolate_app.getoutput()["tdir"]
        albd_image = interpolate_app.getoutput()["albd"]
        # Compute env corr for each l2 resolution
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        l_nbRes = len(bands_definition.ListOfL2Resolution)
        sre_list = []
        for r in range(0, l_nbRes):
            l_res = bands_definition.ListOfL2Resolution[r]
            l_l2bandcodes = bands_definition.get_list_of_l2_band_code(l_res)
            l_l2bandidx = [bands_definition.get_band_id_in_l2_coarse(b) for b in l_l2bandcodes]
            tdifl2_filename = os.path.join(env_working, "tdif_" + l_res + ".tif")
            tdirl2_filename = os.path.join(env_working, "tdir_" + l_res + ".tif")
            albdl2_filename = os.path.join(env_working, "albd_" + l_res + ".tif")
            rhoenvl2_filename= os.path.join(env_working, "rhoenv_extract_" + l_res + ".tif")
            # Extract tdif
            tdifl2_image_app = extract_roi(tdif_image, l_l2bandidx, tdifl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(tdifl2_image_app)
            # Extract tdir
            tdirl2_image_app = extract_roi(tdir_image, l_l2bandidx, tdirl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(tdirl2_image_app)
            # Extract albd
            albdl2_image_app = extract_roi(albd_image, l_l2bandidx, albdl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(albdl2_image_app)
            # Extract rhoenv_sub
            rhoenvl2_image_app = extract_roi(computerho_app.getoutput().get("rhoenv"), l_l2bandidx, rhoenvl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(rhoenvl2_image_app)
            rhoenv_image = os.path.join(env_working, "rhoenv_" + l_res + ".tif")
            sre_image = os.path.join(env_working, "sre_" + l_res + ".tif")
            # Compute env correction
            param_envcorr = {"tdir": tdirl2_image_app.getoutput()["out"],
                             "tdif": tdifl2_image_app.getoutput()["out"],
                             "albd": albdl2_image_app.getoutput()["out"],
                             "rhoenvsub": rhoenvl2_image_app.getoutput()["out"],
                             "nodata": dict_of_input.get("Params").get("RealL2NoData"),
                             "toc": dict_of_output["TOC_" + l_res],
                             "edg": dict_of_input.get("L1Reader").get_value("L2EDGOutputList")[r],
                             "sre": sre_image,
                             "rhoenv": rhoenv_image
                             }
            envcorr_app = OtbAppHandler("EnvCorrection", param_envcorr,write_output=False)
            self._l2_pipeline.add_otb_app(envcorr_app)
            if is_croco_on("envcorrection"):
                write_images([envcorr_app.getoutput().get("sre"), envcorr_app.getoutput().get("rhoenv")],
                             [sre_image, rhoenv_image])
                dict_of_output["SRE_" + l_res] = sre_image
                dict_of_output["RhoEnv_" + l_res] = rhoenv_image
                sre_list.append(sre_image)
            else:
                dict_of_output["SRE_" + l_res] = envcorr_app.getoutput().get("sre")
                dict_of_output["RhoEnv_" + l_res] = envcorr_app.getoutput().get("rhoenv")
                sre_list.append(envcorr_app.getoutput().get("sre"))
        dict_of_output["SRE_List"] = sre_list
Ejemplo n.º 11
0
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Slope Correction start")
        slope_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "SlopeCorrProc_", do_always_remove=True)
        caching = dict_of_input.get("Params").get("Caching")
        # Compute transmission
        tdif_image = os.path.join(slope_working, "tdif_sub.tif")
        tdir_image = os.path.join(slope_working, "tdir_sub.tif")
        param_interpolate = {
            "luttdir":
            dict_of_input.get("L2DIRT"),
            "luttdif":
            dict_of_input.get("L2DIFT"),
            "aot":
            dict_of_output["AOT_Sub"],
            "dtm":
            dict_of_input.get("DEM").ALC,
            "edg":
            dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
            "thetas":
            float(dict_of_input.get("L1Info").SolarAngle["sun_zenith_angle"]),
            "tdir":
            tdir_image,
            "tdif":
            tdif_image
        }
        interpolate_app = OtbAppHandler("ComputeTransmission",
                                        param_interpolate,
                                        write_output=True)

        # Compute env corr for each l2 resolution
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        l_nbRes = len(bands_definition.ListOfL2Resolution)
        fre_list = []
        stl_list = []
        tgs_list = []
        for r in range(0, l_nbRes):
            l_res = bands_definition.ListOfL2Resolution[r]
            l_l2bandcodes = bands_definition.get_list_of_l2_band_code(l_res)
            l_l2bandidx = [
                bands_definition.get_band_id_in_l2_coarse(b)
                for b in l_l2bandcodes
            ]
            l_l2bandchannels = ["Channel" + str(b + 1) for b in l_l2bandidx]
            tdifl2_filename = os.path.join(slope_working,
                                           "tdif_" + l_res + ".tif")
            tdirl2_filename = os.path.join(slope_working,
                                           "tdir_" + l_res + ".tif")
            fre_image = os.path.join(slope_working, "fre_" + l_res + ".tif")
            tgs_image = os.path.join(slope_working, "tgs_" + l_res + ".tif")
            stl_image = os.path.join(slope_working, "stl_" + l_res + ".tif")
            incangle_image = os.path.join(slope_working,
                                          "incangle_" + l_res + ".tif")
            # Extract tdif
            tdifl2_app = extract_roi(interpolate_app.getoutput()["tdif"],
                                     l_l2bandidx,
                                     tdifl2_filename,
                                     write_output=False)
            self._l2_app_pipeline.add_otb_app(tdifl2_app)
            tdifl2_image = tdifl2_app.getoutput()["out"]
            # Extract tdir
            tdirl2_app = extract_roi(interpolate_app.getoutput()["tdir"],
                                     l_l2bandidx,
                                     tdirl2_filename,
                                     write_output=False)
            self._l2_app_pipeline.add_otb_app(tdirl2_app)
            tdirl2_image = tdirl2_app.getoutput()["out"]
            # Compute incidence angles
            param_incangle = {
                "demasc":
                dict_of_input.get("DEM").ASPList[r],
                "demslc":
                dict_of_input.get("DEM").SLPList[r],
                "thetas":
                float(
                    dict_of_input.get(
                        "L1Info").SolarAngle["sun_zenith_angle"]),
                "phis":
                float(
                    dict_of_input.get(
                        "L1Info").SolarAngle["sun_azimuth_angle"]),
                "angles":
                incangle_image
            }
            incangle_app = OtbAppHandler("IncidenceAngle",
                                         param_incangle,
                                         write_output=False)
            self._l2_app_pipeline.add_otb_app(incangle_app)
            # Compute slope correction
            l_thetav = []
            l_phiv = []

            for ang in [
                    dict_of_input.get("L1Info").
                    ListOfViewingAnglesPerBandAtL2CoarseResolution[b]
                    for b in l_l2bandidx
            ]:
                l_thetav.append(ang.get("incidence_zenith_angle"))
                l_phiv.append(ang.get("incidence_azimuth_angle"))

            param_slopecorr = {
                "tdirsub":
                tdirl2_image,
                "tdifsub":
                tdifl2_image,
                "dtmasc":
                dict_of_input.get("DEM").ASPList[r],
                "dtmslc":
                dict_of_input.get("DEM").SLPList[r],
                "rhoenv":
                dict_of_output["RhoEnv_" + l_res],
                "incangle":
                incangle_app.getoutput().get("angles"),
                "sre":
                dict_of_output["SRE_" + l_res],
                "thetas":
                float(
                    dict_of_input.get(
                        "L1Info").SolarAngle["sun_zenith_angle"]),
                "phis":
                float(
                    dict_of_input.get(
                        "L1Info").SolarAngle["sun_azimuth_angle"]),
                "thetav":
                l_thetav,
                "phiv":
                l_phiv,
                "mincosi":
                dict_of_input.get("L2COMM").get_value_f("SlopeMinCosI"),
                "mincose":
                dict_of_input.get("L2COMM").get_value_f("MinCosE"),
                "mincosratio":
                dict_of_input.get("L2COMM").get_value_f("MinCosRatio"),
                "nodata":
                dict_of_input.get("Params").get("RealL2NoData"),
                "fre":
                fre_image,
                "tgs":
                tgs_image,
                "stl":
                stl_image
            }
            slope_app = OtbAppHandler("SlopeCorrection",
                                      param_slopecorr,
                                      write_output=False)
            self._l2_app_pipeline.add_otb_app(slope_app)
            if is_croco_on("slopecorrection"):
                dict_of_output["FRE_" + l_res] = fre_image
                dict_of_output["TGS_" + l_res] = tgs_image
                dict_of_output["STL_" + l_res] = stl_image
                write_images([
                    slope_app.getoutput().get("fre"),
                    slope_app.getoutput().get("tgs"),
                    slope_app.getoutput().get("stl")
                ], [fre_image, tgs_image, stl_image])
            else:
                dict_of_output["FRE_" +
                               l_res] = slope_app.getoutput().get("fre")
                dict_of_output["TGS_" +
                               l_res] = slope_app.getoutput().get("tgs")
                dict_of_output["STL_" +
                               l_res] = slope_app.getoutput().get("stl")
            fre_list.append(dict_of_output["FRE_" + l_res])
            tgs_list.append(dict_of_output["TGS_" + l_res])
            stl_list.append(dict_of_output["STL_" + l_res])
        dict_of_output["FRE_List"] = fre_list
        dict_of_output["TGS_List"] = tgs_list
        dict_of_output["STL_List"] = stl_list