def generate_sol1_image(self, sol_h1, working_dir):
     LOGGER.debug("MuscateL1ImageFileReader::GenerateSOL1Image()")
     const_pi_18 = 0.01745329251994329577
     # SOL1 image pipeline connection
     # *********************************************************************************************************
     sol_filename = os.path.join(working_dir, "sol1.tif")
     mean_solar = self._header_handler.get_mean_solar_angles()
     l_sol1 = sol_h1 * math.tan(const_pi_18 * mean_solar[0]) * math.sin(
         const_pi_18 * mean_solar[1])
     l_sol2 = sol_h1 * math.tan(const_pi_18 * mean_solar[0]) * math.cos(
         const_pi_18 * mean_solar[1])
     LOGGER.debug("SOL1 parameters : ")
     LOGGER.debug(mean_solar)
     LOGGER.debug(l_sol1)
     LOGGER.debug(l_sol2)
     cla_app_1 = constant_image(self._dem.ALC,
                                l_sol1,
                                sol_filename,
                                write_output=False)
     cla_app_2 = constant_image(self._dem.ALC,
                                l_sol2,
                                sol_filename,
                                write_output=False)
     out_concatenate = os.path.join(working_dir, "sol1_concat.tif")
     param_concatenate = {
         "il": [
             cla_app_1.getoutput().get("out"),
             cla_app_2.getoutput().get("out")
         ],
         "out":
         out_concatenate
     }
     OtbAppHandler("ConcatenateDoubleImages", param_concatenate)
     self._sol1image = out_concatenate
 def generate_vie_image(self, view_href, working_dir):
     # *********************************************************************************************************
     # VIE image pipeline connection
     # *********************************************************************************************************
     sol_filename = os.path.join(working_dir, "cla_constant_tmp.tif")
     mean_view = self._header_handler.get_mean_viewing_angles()
     l_vie1 = view_href * math.tan(mean_view[0]) * math.sin(mean_view[1])
     l_vie2 = view_href * math.tan(mean_view[0]) * math.cos(mean_view[1])
     vie_app_1 = constant_image(self._dem.ALC,
                                l_vie1,
                                sol_filename,
                                write_output=False)
     vie_app_2 = constant_image(self._dem.ALC,
                                l_vie2,
                                sol_filename,
                                write_output=False)
     out_concatenate = os.path.join(working_dir, "cla_constant.tif")
     param_concatenate = {
         "il": [
             vie_app_1.getoutput().get("out"),
             vie_app_2.getoutput().get("out")
         ],
         "out":
         out_concatenate
     }
     OtbAppHandler("ConcatenateDoubleImages", param_concatenate)
     l_BandsDefinitions = self._plugin.BandsDefinitions
     self._vieimagelist.append(out_concatenate)
Пример #3
0
 def generate_vie_image(self, view_href, working_dir):
     const_pi_18 = math.pi / 180
     vie_filename = os.path.join(working_dir, "vie.tif")
     mean_solar = self._header_handler.get_mean_solar_angles()
     l_vie1 = view_href * math.tan(const_pi_18 * mean_solar[0]) * math.sin(
         const_pi_18 * mean_solar[1])
     l_vie2 = view_href * math.tan(const_pi_18 * mean_solar[0]) * math.cos(
         const_pi_18 * mean_solar[1])
     app_1 = constant_image(self._dem.ALC,
                            l_vie1,
                            vie_filename,
                            write_output=False)
     app_2 = constant_image(self._dem.ALC,
                            l_vie2,
                            vie_filename,
                            write_output=False)
     out_concatenate = os.path.join(working_dir, "vie_concat.tif")
     param_concatenate = {
         "il": [app_1.getoutput().get("out"),
                app_2.getoutput().get("out")],
         "out": out_concatenate
     }
     app = OtbAppHandler("ConcatenateDoubleImages", param_concatenate)
     self._vieimage.append(app.getoutput().get("out"))
     self._vie_pipeline.add_otb_app(app)
Пример #4
0
 def generate_sol1_image(self, sol_h1, working_dir):
     const_pi_18 = math.pi / 180
     sol_filename = os.path.join(working_dir, "sol1.tif")
     mean_solar = self._header_handler.get_mean_solar_angles()
     l_sol1 = sol_h1 * math.tan(const_pi_18 * mean_solar[0]) * math.sin(
         const_pi_18 * mean_solar[1])
     l_sol2 = sol_h1 * math.tan(const_pi_18 * mean_solar[0]) * math.cos(
         const_pi_18 * mean_solar[1])
     cla_app_1 = constant_image(self._dem.ALC,
                                l_sol1,
                                sol_filename,
                                write_output=False)
     cla_app_2 = constant_image(self._dem.ALC,
                                l_sol2,
                                sol_filename,
                                write_output=False)
     out_concatenate = os.path.join(working_dir, "sol1_concat.tif")
     param_concatenate = {
         "il": [
             cla_app_1.getoutput().get("out"),
             cla_app_2.getoutput().get("out")
         ],
         "out":
         out_concatenate
     }
     app = OtbAppHandler("ConcatenateDoubleImages", param_concatenate)
     self._sol1image = app.getoutput().get("out")
     self._sol1_pipeline.add_otb_app(app)
 def generate_cla_images(self, realL1Nodata, working):
     cla_filename = os.path.join(working, "cla_constant_sub.tif")
     cla_image = constant_image(self._dem.ALC,
                                realL1Nodata,
                                cla_filename,
                                write_output=True).getoutput()["out"]
     self._claimage = cla_image
Пример #6
0
    def generate_sat_sub_image(self, l2comm, working):
        # Sat are not provided on landsat 8 thus no need to resample or threshold anything, just create constant
        # images at coarse resolution
        dtm_coarse = self._dem.ALC

        l_ListOfCoarseBandCode = self._plugin.BandsDefinitions.get_list_of_band_id_in_l2_coarse(
        )
        tmp_constant_sat_filename = os.path.join(working,
                                                 "SATSub_constant_image.tif")
        app_constant_sat_app = constant_image(dtm_coarse,
                                              0,
                                              tmp_constant_sat_filename +
                                              ":uint8",
                                              write_output=False)
        self._sat_sub_pipeline.add_otb_app(app_constant_sat_app)
        # ******************************************************************************************************
        # Sub SAT image pipeline connection
        # ******************************************************************************************************
        for band in l_ListOfCoarseBandCode:
            self._satsublist.append(
                app_constant_sat_app.getoutput().get("out"))
        # end band loop
        #concatenate const image to have the vector image with coarse nb bands
        sat_sub_image = os.path.join(working, "sat_sub.tif")
        param_concatenate = {
            "il": self._satsublist,
            "out": sat_sub_image + ":uint8"
        }
        app_concatenate = OtbAppHandler("ConcatenateMaskImages",
                                        param_concatenate)
        self._subsatimage = app_concatenate.getoutput().get("out")
        self._sat_sub_pipeline.add_otb_app(app_concatenate)
        self._sat_sub_pipeline.free_otb_app()
Пример #7
0
    def generate_l2_sat_images(self, l_ListOfL2Resolution, working_dir):
        for l1res in range(len(l_ListOfL2Resolution)):
            curL1Res = l_ListOfL2Resolution[l1res]
            l_ListOfL2BandCode = self._plugin.BandsDefinitions.get_list_of_l2_band_code(
                curL1Res)
            dtm = self._dem.ALTList[0]
            tmp_constant_sat_filename = os.path.join(
                working_dir, "SAT_constant_image_{}.tif".format(l1res))
            app_constant_sat_app = constant_image(dtm,
                                                  0,
                                                  tmp_constant_sat_filename,
                                                  write_output=False)
            self._sat_pipeline.add_otb_app(app_constant_sat_app)
            # ******************************************************************************************************
            # L2 SAT image pipeline connection
            # ******************************************************************************************************
            for band in l_ListOfL2BandCode:
                self._satmasklist.append(
                    app_constant_sat_app.getoutput().get("out"))

            out_sat_concatenate = os.path.join(
                working_dir, "SATVectorImageList{}.tif".format(l1res))
            param_concatenate_sat = {
                "il": self._satmasklist,
                "out": out_sat_concatenate + ":uint8"
            }
            concatenate_sat_app = OtbAppHandler("ConcatenateDoubleImages",
                                                param_concatenate_sat,
                                                write_output=False)
            self._sat_pipeline.add_otb_app(concatenate_sat_app)
            # L2SAT
            self._l2satmasklist.append(
                concatenate_sat_app.getoutput().get("out"))
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("AOT constant start")
        LOGGER.debug("Caching %s", dict_of_input.get("Params").get("Caching"))
        l_caching = dict_of_input.get("Params").get("Caching")
        l_writeL2 = dict_of_input.get("Params").get(
            "WriteL2ProductToL2Resolution")
        aot_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "AOTConstantProc_", do_always_remove=True)
        aot_default = float(
            dict_of_input.get("L2COMM").get_value("DefaultAOT"))
        # Coarse AOT constant
        aot_sub_image = os.path.join(aot_working, "aot_sub.tif")
        self._constant_app = constant_image(dict_of_input.get("DEM").ALC,
                                            aot_default,
                                            aot_sub_image,
                                            write_output=l_caching)
        dict_of_output["AOT_Sub"] = self._constant_app.getoutput()["out"]

        # L2 AOT
        if l_writeL2:
            bands_definition = dict_of_input.get("Plugin").BandsDefinitions
            l_nbRes = len(bands_definition.ListOfL2Resolution)
            aot_list = []
            aotmask_list = []
            for r in range(0, l_nbRes):
                l_res = bands_definition.ListOfL2Resolution[r]
                aot_image = os.path.join(aot_working, "aot_" + l_res + ".tif")
                aot_mask = os.path.join(aot_working,
                                        "aotmask_" + l_res + ".tif:uint8")
                dict_of_output["AOT_" + l_res] = constant_image(
                    dict_of_input.get("DEM").ALTList[r],
                    aot_default,
                    aot_image,
                    write_output=True).getoutput()["out"]
                dict_of_output["AOTMASK_" + l_res] = constant_image(
                    dict_of_input.get("DEM").ALTList[r],
                    0,
                    aot_mask,
                    write_output=True).getoutput()["out"]
                aot_list.append(dict_of_output["AOT_" + l_res])
                aotmask_list.append(dict_of_output["AOTMASK_" + l_res])
            dict_of_output["L2AOTList"] = aot_list
            dict_of_output["L2AOTMaskList"] = aotmask_list
Пример #9
0
 def generate_cla_image(self, working_dir):
     LOGGER.debug(
         "Cloud Altitude (CLA) image doesn't exist. Replaced by constant nodata image. !"
     )
     cla_filename = os.path.join(working_dir, "cla_constant_sub.tif")
     cla_image = constant_image(self._dem.ALC,
                                self._reall1nodata,
                                cla_filename,
                                write_output=True).getoutput()["out"]
     self._claimage = cla_image
Пример #10
0
    def generate_pix_mask(self, working_dir):
        LOGGER.debug("Landsat8L1ImageFileReader::GeneratePIXMask()")
        l_BandsDefinitions = self._plugin.BandsDefinitions
        l_ListOfL1Resolution = l_BandsDefinitions.ListOfL1Resolution  # ListOfStrings

        for l1res in range(0, len(l_ListOfL1Resolution)):
            curL1Res = l_ListOfL1Resolution[l1res]
            dtm = self._dem.ALTList[l1res]
            tmp_constant_filename = os.path.join(
                working_dir, "PIX_Masks_const_{}.tif".format(curL1Res))
            app = constant_image(dtm,
                                 0,
                                 tmp_constant_filename + ":uint8",
                                 write_output=False)
            self._l2piximagelist.append(app.getoutput().get("out"))
            self._pix_pipeline.add_otb_app(app)
 def generate_cla_image(self, realL1Nodata, working_dir):
     LOGGER.debug("MuscateL1ImageFileReader::GenerateCLAMask()")
     # CLA image pipeline connection
     # test if a CLA image file is available
     cla_node = xml_tools.get_only_value(
         self._header_handler.root,
         "//Data_List/Data[Data_Properties/NATURE='Cloud_Altitude_Grid']/Data_File_List/DATA_FILE",
         check=True)
     if cla_node is not None:
         self._claimage = self._header_handler.get_cla_image_filename()
     else:
         cla_filename = os.path.join(working_dir, "cla_constant_sub.tif")
         cla_image = constant_image(self._dem.ALC,
                                    realL1Nodata,
                                    cla_filename,
                                    write_output=True).getoutput()["out"]
         self._claimage = cla_image
    def get_viewing_grids(self, band, l2CoarseArea, projectionRef, vieHRef,
                          satFilename, zoneFilename, nodataFilename,
                          listOfZone, boundingBox, viewing_angles,
                          gdalRasterizeMaskCmd, gdalRasterizeDetCmd,
                          gdalRasterizeNdtCmd, nbCoarseBands, working):
        """

        :param band: int
        :param l2CoarseArea: AreaType
        :param projectionRef: string
        :param vieHRef: double
        :param satFilename: string
        :param zoneFilename: string
        :param nodataFilename: string
        :param listOfZone: ListOfUIntegers
        :param boundingBox: BoundingBoxType
        :param viewing_angles:
        :param gdalRasterizeMaskCmd: string
        :param gdalRasterizeDetCmd: string
        :param gdalRasterizeNdtCmd: string
        :return:
        """
        dtm_coarse = self._dem.ALC

        # Set a constant image if the gml masks are empty
        tmp_constant_filename = os.path.join(working, "Masks_sat_const.tif")
        constant_image(dtm_coarse,
                       0,
                       tmp_constant_filename + ":uint8",
                       write_output=True)

        # -----------------------------------------------------------------------------------
        # Rasterize all the gml maks at L2 coarse resolution
        # to generate the viewing grids
        # -----------------------------------------------------------------------------------

        # -----------------------------------------------------------------------------------
        # Detectors FootPrint
        # check if the gml mask contains features
        count = self.get_mask_feature_count(zoneFilename)

        if count > 0:
            # Get the shift between the detector index and the feature index
            shift_res = self.get_detfoo_index_shift(zoneFilename)

            if shift_res[0]:
                l_image_raster_filename = os.path.join(
                    working, "SubZoneMask_band_id_{}.tif".format(band))
                # Additional parameters of the gdal_rasterize system command
                sqlRequest = gdalRasterizeDetCmd + \
                    "-a fid2 -sql 'select fid + {} as fid2, * from MaskFeature'".format(shift_res[1])

                GdalRasterize().internal_rasterize_gml_macro(
                    zoneFilename, boundingBox.xmin, boundingBox.ymin,
                    boundingBox.xmax, boundingBox.ymax, l2CoarseArea.size[0],
                    l2CoarseArea.size[1], projectionRef,
                    l_image_raster_filename, sqlRequest)

                submask_resamp_filename = os.path.join(
                    working,
                    "SubZoneMask_resampled_band_id_{}.tif".format(band))
                resample(l_image_raster_filename, dtm_coarse,
                         submask_resamp_filename, OtbResampleType.LINEAR)

                self._zonemasksublist.append(submask_resamp_filename)
            else:
                LOGGER.debug("Bad Order DETFOO detected")
                l_image_raster_filename = os.path.join(
                    working, "SubZoneMask_band_id_{}.tif".format(band))
                # Additional parameters of the gdal_rasterize system command
                sqlRequest = gdalRasterizeDetCmd + \
                             "-a fid2 -sql 'select fid + 1 as fid2, * from MaskFeature'"

                GdalRasterize().internal_rasterize_gml_macro(
                    zoneFilename, boundingBox.xmin, boundingBox.ymin,
                    boundingBox.xmax, boundingBox.ymax, l2CoarseArea.size[0],
                    l2CoarseArea.size[1], projectionRef,
                    l_image_raster_filename, sqlRequest)
                l_fid_dets = self.get_detfoo_index_detector_list(
                    zoneFilename, 1)
                l_image_changed_filename = os.path.join(
                    working, "SubZoneMaskChanged_band_id_{}.tif".format(band))
                change_values_param = {
                    "im": l_image_raster_filename,
                    "out": l_image_changed_filename + ":uint8",
                    "invals": [str(a) for a in l_fid_dets.keys()],
                    "outvals": [str(a) for a in l_fid_dets.values()]
                }
                change_values_app = OtbAppHandler("ChangeValues",
                                                  change_values_param)

                submask_resamp_filename = os.path.join(
                    working,
                    "SubZoneMask_resampled_band_id_{}.tif".format(band))
                resample(change_values_app.getoutput().get("out"), dtm_coarse,
                         submask_resamp_filename, OtbResampleType.LINEAR)

                self._zonemasksublist.append(submask_resamp_filename)

        else:
            submask_resamp_filename = os.path.join(
                working, "SubZoneMask_resampled_band_id_{}.tif".format(band))
            resample(tmp_constant_filename, dtm_coarse,
                     submask_resamp_filename, OtbResampleType.LINEAR)
            self._zonemasksublist.append(submask_resamp_filename)

        # -----------------------------------------------------------------------------------
        # Saturated pixel mask at L2 coarse
        count = self.get_mask_feature_count(satFilename)

        if count > 0:
            l_image_raster_filename = os.path.join(
                working, "SubSatMask_band_id_{}.tif".format(band))
            GdalRasterize().internal_rasterize_gml_macro(
                satFilename, boundingBox.xmin, boundingBox.ymin,
                boundingBox.xmax, boundingBox.ymax, l2CoarseArea.size[0],
                l2CoarseArea.size[1], projectionRef, l_image_raster_filename,
                gdalRasterizeMaskCmd)
            self._satmasksublist.append(l_image_raster_filename)
        else:
            self._satmasksublist.append(tmp_constant_filename)

        # -----------------------------------------------------------------------------------
        # No_data mask at L2 coarse
        count = self.get_mask_feature_count(nodataFilename, "NODATA")

        if count > 0:
            l_image_raster_filename = os.path.join(
                working, "SubNoDataMask_band_id_{}.tif".format(band))
            GdalRasterize().internal_rasterize_gml_macro(
                nodataFilename, boundingBox.xmin, boundingBox.ymin,
                boundingBox.xmax, boundingBox.ymax, l2CoarseArea.size[0],
                l2CoarseArea.size[1], projectionRef, l_image_raster_filename,
                gdalRasterizeNdtCmd)
            self._nodatamasksublist.append(l_image_raster_filename)
        else:
            self._nodatamasksublist.append(tmp_constant_filename)

        l_VieAnglesGridList = []
        l_nbDetectors = len(viewing_angles)

        # Detector loop
        LOGGER.debug("For each detectors (nb=%s) ...", l_nbDetectors)
        for angle in viewing_angles:
            l_vieAngleFile = angle.write(working)
            # ---------------------------------------------------------------------------------
            # Generate an image with the list of viewing angle values set in the header file
            # ---------------------------------------------------------------------------------
            viewing_grid_filename = os.path.join(
                working,
                "viewing_grid_{}_{}.tif".format(angle.detector_id, band))

            # angle_list_to_image()
            viewing_angle_app = angle_list_to_image(dtm_coarse,
                                                    l_vieAngleFile,
                                                    viewing_grid_filename,
                                                    write_output=False,
                                                    extrapolation=True)

            # Expand at L2Coarse.
            viewing_grid_resamp_filename = os.path.join(
                working,
                "viewing_grid_resamp_{}_{}.tif".format(angle.detector_id,
                                                       band))
            resample(viewing_angle_app.getoutput().get("out"), dtm_coarse,
                     viewing_grid_resamp_filename, OtbResampleType.LINEAR)

            # add images in a list
            l_VieAnglesGridList.append(viewing_grid_resamp_filename)

        # end detector loop
        LOGGER.debug(
            "Start ConcatenatePerZoneVectorImageFilter for band id [%s]...",
            band)

        # -----------------------------------------------------------------------------------
        # Generate the angle images using the zone (detector) mask
        # -----------------------------------------------------------------------------------
        # Concatenate all the detectors
        viewing_concat_filename = os.path.join(
            working, "viewing_concat_{}.tif".format(band))
        param_concat_perzone = {
            "mask": self._zonemasksublist[-1],
            "il": l_VieAnglesGridList,
            "zonelist": listOfZone,
            "out": viewing_concat_filename
        }
        concat_perzone = OtbAppHandler("ConcatenatePerZone",
                                       param_concat_perzone)
        # Multiply by reference altitude
        viewing_grid_mult_filename = os.path.join(
            working, "viewing_grid_mult_{}.tif".format(band))
        param_scaled_solar = {
            "im": viewing_concat_filename,
            "coef": float(vieHRef),
            "out": viewing_grid_mult_filename
        }
        view_scale_app = OtbAppHandler("MultiplyByScalar",
                                       param_scaled_solar,
                                       write_output=True)
        self._vieimagelist.append(view_scale_app.getoutput().get("out"))

        l_nbZones = len(listOfZone)
        LOGGER.debug("Start Loop for Zone (nb=" + str(l_nbZones) + ")...")
        for d in range(l_nbZones):
            l_zone = listOfZone[d]
            # -----------------------------------------------------------------------------------
            # Compute average values of zenithal and azimuthal angles grid per zone (detector in level1B)
            # -----------------------------------------------------------------------------------
            # VAP Reader connection (from ATB)
            tmp_azi = os.path.join(working,
                                   "tmp_azi_{}_{}.tif".format(band, l_zone))
            tmp_azi_image = extract_roi(l_VieAnglesGridList[d], [1], tmp_azi)
            param_stats = {
                "im": tmp_azi,
                "exclude": 1,
                "mask": self._zonemasksublist[band],
                "maskforeground": int(l_zone)
            }
            l2_stat = OtbAppHandler("Stats", param_stats)

            azi_mean = l2_stat.getoutput().get("mean")
            tmp_zen = os.path.join(working,
                                   "tmp_zen_{}_{}.tif".format(band, l_zone))
            tmp_zen_image = extract_roi(l_VieAnglesGridList[d], [0], tmp_zen)
            param_stats = {
                "im": tmp_zen,
                "exclude": 1,
                "mask": self._zonemasksublist[band],
                "maskforeground": int(l_zone)
            }
            l2_stat = OtbAppHandler("Stats", param_stats)
            zen_mean = l2_stat.getoutput().get("mean")
            tmp_mean = (zen_mean, azi_mean)
            l_ViewingAngleMean = grid_to_angle(tmp_mean)
            l_ViewingAngleMeanDeg = (l_ViewingAngleMean[0] * 180.0 / math.pi,
                                     l_ViewingAngleMean[1] * 180.0 / math.pi)
            # Add a vector to mean maps
            if l_zone not in self._meanZenithMap:
                self._meanZenithMap[listOfZone[d]] = ["0"] * nbCoarseBands
            if l_zone not in self._meanAzimuthMap:
                self._meanAzimuthMap[listOfZone[d]] = ["0"] * nbCoarseBands
            self._meanZenithMap[listOfZone[d]][band] = str(
                l_ViewingAngleMeanDeg[0])
            self._meanAzimuthMap[listOfZone[d]][band] = str(
                l_ViewingAngleMeanDeg[1])
            LOGGER.debug(" For BandId[" + str(band) + "], zone [" +
                         str(listOfZone[d]) + "]  . Mean 'GRID View angles'=" +
                         str(tmp_mean) + " . Mean 'View angles'=" +
                         str(l_ViewingAngleMean[0]) + ":" +
                         str(l_ViewingAngleMean[1]) + " rad. ou  " +
                         str(l_ViewingAngleMeanDeg[0]) + ":" +
                         str(l_ViewingAngleMeanDeg[1]) + " deg.")
        LOGGER.debug("Start Loop for Zone done.")
    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 ...")
def setup_l2_image_writer(self, p_OutputL2ImageFileWriter, in_global_dict,
                          out_global_dict, l_WriteL2Products,
                          l_CopyPrivateFromL2InputToL2Output, l_IsDefaultAOT):
    l2_write_working = in_global_dict.get(
        "AppHandler").get_directory_manager().get_temporary_directory(
            "L2Write_", do_always_remove=True)
    l_WriteL2ProductToL2Resolution = in_global_dict["Params"][
        "WriteL2ProductToL2Resolution"]
    l_EnvCorOption = in_global_dict["Params"]["EnvCorOption"]

    p_OutputL2ImageFileWriter.set_dtm(in_global_dict["DEM"])
    p_OutputL2ImageFileWriter.set_current_plugin_base(
        p_OutputL2ImageFileWriter.plugin)
    p_OutputL2ImageFileWriter.plugin.initialize(
        in_global_dict.get("AppHandler"))
    p_OutputL2ImageFileWriter.set_copy_private_from_l2_input_to_l2_output(
        l_CopyPrivateFromL2InputToL2Output)
    p_OutputL2ImageFileWriter.set_write_l2_products(l_WriteL2Products)
    p_OutputL2ImageFileWriter.set_output_directory(
        in_global_dict["Params"]["L2OutputDirectory"])
    p_OutputL2ImageFileWriter.set_l2_image_file_reader(
        in_global_dict["L2Reader"])
    p_OutputL2ImageFileWriter.set_l1_image_informations_provider(
        in_global_dict["L1Info"])
    p_OutputL2ImageFileWriter.set_real_l2_nodata(
        in_global_dict["Params"]["RealL2NoData"])
    l_GIPPL2COMMHandler = in_global_dict["L2COMM"]
    l_bandsdefinition = in_global_dict.get("Plugin").BandsDefinitions
    # Set the L1 Data Filter informations
    # ReflectanceQuantification is 0.001(not 1000)
    # VAP_Quantification_Value is 0.050(not 20)
    # AOT_Quantification_Value is 0.005(not 200)
    p_OutputL2ImageFileWriter.set_reflectance_quantification_value(
        in_global_dict["L1Info"].ReflectanceQuantification)
    p_OutputL2ImageFileWriter.set_aot_quantification_value(
        l_GIPPL2COMMHandler.get_value_f("AOTQuantificationValue"))
    p_OutputL2ImageFileWriter.set_vap_quantification_value(
        l_GIPPL2COMMHandler.get_value_f("VAPQuantificationValue"))
    p_OutputL2ImageFileWriter.set_vap_nodata_value(
        l_GIPPL2COMMHandler.get_value_f("VAPNodataValue"))
    p_OutputL2ImageFileWriter.set_aot_nodata_value(
        l_GIPPL2COMMHandler.get_value_f("AOTNodataValue"))
    p_OutputL2ImageFileWriter.set_quicklook_min_refl_redband(
        l_GIPPL2COMMHandler.get_value("QuicklookMinReflRedBand"))
    p_OutputL2ImageFileWriter.set_quicklook_min_refl_greenband(
        l_GIPPL2COMMHandler.get_value("QuicklookMinReflGreenBand"))
    p_OutputL2ImageFileWriter.set_quicklook_min_refl_blueband(
        l_GIPPL2COMMHandler.get_value("QuicklookMinReflBlueBand"))
    p_OutputL2ImageFileWriter.set_quicklook_max_refl_redband(
        l_GIPPL2COMMHandler.get_value("QuicklookMaxReflRedBand"))
    p_OutputL2ImageFileWriter.set_quicklook_max_refl_greenband(
        l_GIPPL2COMMHandler.get_value("QuicklookMaxReflGreenBand"))
    p_OutputL2ImageFileWriter.set_quicklook_max_refl_blueband(
        l_GIPPL2COMMHandler.get_value("QuicklookMaxReflBlueBand"))
    p_OutputL2ImageFileWriter.set_quicklook_red_band_code(
        l_GIPPL2COMMHandler.get_value("QuicklookRedBandCode"))
    p_OutputL2ImageFileWriter.set_quicklook_green_band_code(
        l_GIPPL2COMMHandler.get_value("QuicklookGreenBandCode"))
    p_OutputL2ImageFileWriter.set_quicklook_blue_band_code(
        l_GIPPL2COMMHandler.get_value("QuicklookBlueBandCode"))
    p_OutputL2ImageFileWriter.set_projection_ref(
        in_global_dict["L1Info"].ProjectionRef)
    p_OutputL2ImageFileWriter.set_write_public_product(
        l_WriteL2ProductToL2Resolution)
    p_OutputL2ImageFileWriter.set_env_cor_option(l_EnvCorOption)
    # == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
    # IF NOT BACKWARD MODE OR IF IT IS THE LAST PRODUCT IN BACKWARD MODE
    #  = > WRITE THE PUBLIC PART OF L2 PRODUCT
    # == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
    LOGGER.debug("WriteL2ProductToL2Resolution enable: " +
                 str(l_WriteL2ProductToL2Resolution))
    if l_WriteL2ProductToL2Resolution:
        LOGGER.debug("EnvCorOption enable: " + str(l_EnvCorOption))
        # ---------------------------------------------------------------------------
        # Public
        # ---------------------------------------------------------------------------
        # Images of surface reflectance
        # If environment option is True
        if l_EnvCorOption:
            # Reflectances are corrected from environment effects
            p_OutputL2ImageFileWriter.set_sre_image_list(
                out_global_dict["SRE_List"])
            p_OutputL2ImageFileWriter.set_fre_image_list(
                out_global_dict["FRE_List"])
            # STL and TGS masks are added in the MSK product
            p_OutputL2ImageFileWriter.set_stl_image_list(
                out_global_dict["STL_List"])
            p_OutputL2ImageFileWriter.set_tgs_image_list(
                out_global_dict["TGS_List"])
        else:
            # Reflectances after atmospheric correction
            p_OutputL2ImageFileWriter.set_sre_image_list(
                out_global_dict["L2TOCList"])
            # Note that no FREImage is set
            l_nbRes = len(l_bandsdefinition.ListOfL2Resolution)
            l2_list = []
            for r in range(0, l_nbRes):
                l_res = l_bandsdefinition.ListOfL2Resolution[r]
                const_mask = os.path.join(
                    l2_write_working, "constant_mask_" + l_res + ".tif:uint8")
                out_const_app = constant_image(
                    in_global_dict.get("DEM").ALTList[r], 0, const_mask)
                l2_list.append(out_const_app.getoutput().get("out"))
            p_OutputL2ImageFileWriter.set_stl_image_list(l2_list)
            p_OutputL2ImageFileWriter.set_tgs_image_list(l2_list)

        # Set AOT
        p_OutputL2ImageFileWriter.set_aot_image_list(
            out_global_dict["L2AOTList"])
        p_OutputL2ImageFileWriter.set_tao_image_list(
            out_global_dict["L2AOTMaskList"])

        # ATB Atmospheric and biophysical parameters
        p_OutputL2ImageFileWriter.set_vap_image_list(
            out_global_dict["L2VAPList"])

        # Write the IWC mask, the VAP Interpolated value
        LOGGER.debug("Set the IWCImageList to the L2ImageFileWriter")
        p_OutputL2ImageFileWriter.set_iwc_image_list(
            out_global_dict["L2VAPMaskList"])

        # MSK Geophysical masks
        p_OutputL2ImageFileWriter.set_hid_image(out_global_dict["dtm_hid"])
        p_OutputL2ImageFileWriter.set_shd_image(out_global_dict["dtm_shd"])
        if in_global_dict["Params"]["SnowBandAvailable"]:
            p_OutputL2ImageFileWriter.set_snw_image(
                out_global_dict["cld_snow"])

        # QLT Quality masks
        p_OutputL2ImageFileWriter.set_sat_image_list(
            in_global_dict["L1Reader"].get_value("L2SATImageList"))
        p_OutputL2ImageFileWriter.set_pix_image_list(
            in_global_dict["L1Reader"].get_value("L2PIXImageList"))
        p_OutputL2ImageFileWriter.set_edg_image_list(
            in_global_dict["L1Reader"].get_value("L2EDGOutputList"))
        if in_global_dict["Params"]["DFPMaskAvailable"]:
            p_OutputL2ImageFileWriter.set_dfp_image_list(
                in_global_dict["L1Reader"].get_value("L2DFPImageList"))

    # Note: if p_finalize_backward == True, then the mode is NOMINAL ( not INIT) ?
    # = > NO, because the first step of Backward is to find the first valid product to start the loop of backward.
    # The first valid could be the last product.
    #  In this case, p_finalize_backward == True and the mode is INIT
    LOGGER.debug(in_global_dict["Params"]["CheckingConditionalClouds"])

    if in_global_dict["Params"]["FinalizeBackWard"] and in_global_dict["Params"]["BackWardMode"]\
            and in_global_dict["Params"]["CheckingConditionalClouds"] == False:
        LOGGER.debug(
            "Use case write only CLA, CLD and set WAS from the previous L2 input product. Processing mode INIT is "
            + str(in_global_dict["Params"]["InitMode"]) + ", BACKWARD is '" +
            str(in_global_dict["Params"]["BackWardMode"]))
        p_OutputL2ImageFileWriter.set_write_only_cla_cld(True)
        p_OutputL2ImageFileWriter.set_cld_image(out_global_dict["CLDList"])
        p_OutputL2ImageFileWriter.set_l2_cld_image_list(
            out_global_dict["L2CLDList"])
        p_OutputL2ImageFileWriter.set_cla_image(out_global_dict["cld_l2cla"])
        # FA LAIG - FA - MAC - 885 - CNES - add the test here, only if NOMINAL, read in the input L2 product
        if not in_global_dict["Params"]["InitMode"]:
            p_OutputL2ImageFileWriter.set_was_image(
                in_global_dict["L2Reader"].get_value("WASImage"))
    else:
        # si p_finalize_backward et(p_mode.IsBackward() vrai p_checking_conditional_clouds faux
        # WRITE THE PRIVATE PART OF L2 PRODUCT
        # Composite product
        # Images of reflectance
        p_OutputL2ImageFileWriter.set_rta_image(
            out_global_dict.get("Composite_rta"))
        p_OutputL2ImageFileWriter.set_rtc_image(
            out_global_dict.get("Composite_rtc"))
        p_OutputL2ImageFileWriter.set_rcr_image(
            out_global_dict.get("Composite_rcr"))
        p_OutputL2ImageFileWriter.set_sto_image(
            out_global_dict.get("Composite_sto"))
        # Masks

        p_OutputL2ImageFileWriter.set_pxd_image(
            out_global_dict.get("Composite_pxd"))
        p_OutputL2ImageFileWriter.set_ndt_image(
            out_global_dict.get("Composite_ndt"))
        p_OutputL2ImageFileWriter.set_cld_image(out_global_dict["CLDList"])
        p_OutputL2ImageFileWriter.set_l2_cld_image_list(
            out_global_dict["L2CLDList"])
        p_OutputL2ImageFileWriter.set_cla_image(out_global_dict["cld_l2cla"])
        #  Update output dictionary
        p_OutputL2ImageFileWriter.set_was_image(
            out_global_dict.get("WaterMask"))
        p_OutputL2ImageFileWriter.set_pwa_image(
            out_global_dict.get("PossibleWaterMask"))
        p_OutputL2ImageFileWriter.set_twa_image(
            out_global_dict.get("TestedWaterMask"))
        p_OutputL2ImageFileWriter.set_write_ltc(
            in_global_dict["Params"]["DealingLTC"])

        # LAIG - FA - MAC - 1180 - CNES
        # Remove the latest dates before adding the current one
        # only if the STO image file was updated
        if not l_CopyPrivateFromL2InputToL2Output:
            maxSTODates = l_GIPPL2COMMHandler.get_value_i(
                "NumberOfStackImages")
            l_STOListOfStringDates = in_global_dict["Params"].get(
                "STOListOfStringDates")
            if len(l_STOListOfStringDates) >= int(maxSTODates):
                l_STOListOfStringDates = l_STOListOfStringDates[maxSTODates -
                                                                1::]
            # the order of the list is more understandable with pop_back
            l_STOListOfStringDates.insert(
                0, in_global_dict["L1Info"].ProductDateStr)
            in_global_dict["Params"][
                "STOListOfStringDates"] = l_STOListOfStringDates

        # ---------------------------------------------------------------------------
        # Write LTC only if available
        if in_global_dict["Params"]["DealingLTC"]:
            tmp_ltc = os.path.join(l2_write_working, "tmp_ltc.mha")
            # ---------------------------------------------------------------------------
            # Reduced LUTs with select the composite band
            param_extractchannels = {
                "lutimage":
                out_global_dict.get("cr_lut"),
                "lut":
                tmp_ltc,
                "cl": [
                    str(i) for i in in_global_dict["Params"]
                    ["ListOfBandIndexForLTCComposite_DateD"]
                ]
            }
            extractChannels_app = OtbAppHandler("LutExtractChannels",
                                                param_extractchannels)
            p_OutputL2ImageFileWriter.set_ltc_image(
                extractChannels_app.getoutput()["lut"])

    LOGGER.debug("Starting L2 product writing ...")
    p_OutputL2ImageFileWriter.write(l2_write_working)
    LOGGER.debug("End of L2 product writing ...")
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("AOT estimation computation start")
        LOGGER.debug("Caching %s", dict_of_input.get("Params").get("Caching"))
        l_caching = dict_of_input.get("Params").get("Caching")
        l_writeL2 = dict_of_input.get("Params").get(
            "WriteL2ProductToL2Resolution")

        aot_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "AOTEstimationProc_", do_always_remove=True)

        init_Mode = dict_of_input.get("Params").get("InitMode")
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        # TODO concatenate cloud masks
        param_darkest = {
            "toac":
            dict_of_output["AtmoAbsIPTOAC"],
            "cld":
            dict_of_output[constants.CLOUD_MASK_ALL],
            "dtm":
            dict_of_input.get("DEM").ALC,
            "edg":
            dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
            "shd":
            dict_of_output["dtm_shd"],
            "lutheader":
            dict_of_input.get("L2TOCR"),
            "lutimage":
            dict_of_output["cr_lut"],
            "darkestchanneltoac":
            dict_of_input.get("Params").get("DarkBandIndex_DateD"),
            "thresholddarkpixel":
            float(dict_of_input.get("L2SITE").get_value("DarkPixelThreshold")),
            "darksurfacereflectance":
            float(
                dict_of_input.get("L2SITE").get_value(
                    "DarkestPixelSurfaceReflectance")),
            "darkestchannelrtc":
            dict_of_input.get("Params").get("DarkBandIndex_DateD")
        }
        if not init_Mode:
            param_darkest["l2ndt"] = dict_of_input.get("L2Reader").get_value(
                "NDTImage")
            param_darkest["l2rtc"] = dict_of_input.get("L2Reader").get_value(
                "RTCImage")
        else:
            param_darkest["init"] = init_Mode

        darkest_app = OtbAppHandler("DarkestPixelAOT",
                                    param_darkest,
                                    write_output=True)
        aot_computed = darkest_app.getoutput()["aotcomputed"]
        darkest_aot = darkest_app.getoutput()["darkestaot"]
        l_aotmethod = dict_of_input.get("Params").get("AOTMethod")
        LOGGER.debug("AOT Estimation Computation Method: " + str(l_aotmethod))
        #AOT Params xml holder
        xml_aot_param = AOT_Estimation()
        #Common parameters
        xml_aot_param.set_Sampling_Interval_Min(
            dict_of_input.get("L2COMM").get_value_i("SamplingIntervalMin"))
        xml_aot_param.set_Sampling_Interval_Max(
            dict_of_input.get("L2COMM").get_value_i("SamplingIntervalMax"))
        xml_aot_param.set_Increment_N_Distance(
            dict_of_input.get("L2COMM").get_value_i("IncrementNDistance"))
        xml_aot_param.set_Half_Window_Size(
            dict_of_input.get("L2COMM").get_value_i("HalfWindowSize"))
        xml_aot_param.set_Max_Iterations(
            dict_of_input.get("L2COMM").get_value_i("MaxIterations"))
        xml_aot_param.set_Step_AOT(
            dict_of_input.get("L2COMM").get_value_i("StepAOT"))
        xml_aot_param.set_N_Pix_Min(
            dict_of_input.get("L2COMM").get_value_i("NPixMin"))
        xml_aot_param.set_F_Tolerance(
            dict_of_input.get("L2COMM").get_value_f("FTolerance"))
        xml_aot_param.set_G_Tolerance(
            dict_of_input.get("L2COMM").get_value_f("GTolerance"))
        xml_aot_param.set_X_Tolerance(
            dict_of_input.get("L2COMM").get_value_f("XTolerance"))
        xml_aot_param.set_Epsilon_Function(
            dict_of_input.get("L2COMM").get_value_f("EpsilonFunction"))
        xml_aot_param.set_AOT_initial_D(
            dict_of_input.get("L2COMM").get_value_f("AOTinitialD"))
        xml_aot_param.set_W_dark(
            dict_of_input.get("L2COMM").get_value_f("Wdark"))
        xml_aot_param.set_W_bounds(
            dict_of_input.get("L2COMM").get_value_f("Wbounds"))
        xml_aot_param.set_AOT_min(
            dict_of_input.get("L2COMM").get_value_f("AOTmin"))
        xml_aot_param.set_AOT_Max_Age(
            dict_of_input.get("L2COMM").get_value_f("AOTMaxAge"))
        xml_aot_param.set_SE_Mask_Dilatation_Radius(
            dict_of_input.get("L2COMM").get_value_f("SEMaskDilatationRadius"))
        xml_aot_param.set_SE_Valid_AOT_Radius(
            dict_of_input.get("L2COMM").get_value_f("SEValidAOTRadius"))

        aot_image = os.path.join(aot_working, "aot_computed.tif")
        aot_mask_estimation = os.path.join(aot_working,
                                           "aot_mask_computed.tif")
        param_aot = {
            "toac": dict_of_output["AtmoAbsIPTOAC"],
            "sat": dict_of_input.get("L1Reader").get_value("IPSATSubOutput"),
            "was": dict_of_output["WaterMask"],
            "dtm": dict_of_input.get("DEM").ALC,
            "edg": dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
            "shd": dict_of_output["dtm_shd"],
            "lutheader": dict_of_input.get("L2TOCR"),
            "lutimage": dict_of_output["cr_lut"],
            "cld": dict_of_output[constants.CLOUD_MASK_ALL],
            "reall2nodata": dict_of_input.get("Params").get("RealL2NoData"),
            "jday": dict_of_input.get("Params").get("JDay"),
            "darkestaot": darkest_aot,
            "aotcomputed": aot_image,
            "aotmask": aot_mask_estimation
        }
        if init_Mode:
            param_aot["init"] = init_Mode

        if l_aotmethod == AOTEstimation.MULTISPECTRAL:
            param_aot["mode"] = "multispectral"
        elif l_aotmethod == AOTEstimation.MULTITEMPORAL:
            param_aot["mode"] = "multitemporal"
        elif l_aotmethod == AOTEstimation.SPECTROTEMPORAL:
            param_aot["mode"] = "spectrotemporal"

        if l_aotmethod == AOTEstimation.SPECTROTEMPORAL or l_aotmethod == AOTEstimation.MULTITEMPORAL:
            stdevbandid = bands_definition.get_band_id_in_l2_coarse(
                dict_of_input.get("L2COMM").get_value("StdevBandCode"))
            if not init_Mode:
                param_aot["temporal.lutmap"] = dict_of_input.get(
                    "L2Reader").get_value("LTCImage")
            param_aot["temporal.altitudemean"] = dict_of_input.get(
                "DEM").ALT_Mean
            xml_aot_param.set_Multi_Temporal_Method(
                Multi_Temporal_MethodType())
            xml_aot_param.get_Multi_Temporal_Method().set_First_AOT(
                dict_of_input.get("L2COMM").get_value_f("FirstAOT"))
            xml_aot_param.get_Multi_Temporal_Method().set_Second_AOT(
                dict_of_input.get("L2COMM").get_value_f("SecondAOT"))
            xml_aot_param.get_Multi_Temporal_Method().set_TOA_Reflectance_Min(
                dict_of_input.get("L2COMM").get_value_f("TOAReflectanceMin"))
            xml_aot_param.get_Multi_Temporal_Method().set_TOA_Reflectance_Max(
                dict_of_input.get("L2COMM").get_value_f("TOAReflectanceMax"))
            xml_aot_param.get_Multi_Temporal_Method().set_TOA_Reflectance_Step(
                dict_of_input.get("L2COMM").get_value_f("TOAReflectanceStep"))
            xml_aot_param.get_Multi_Temporal_Method(
            ).set_Min_Difference_Thresholds_Calculation(
                dict_of_input.get("L2COMM").get_value_f(
                    "MinDifferenceThreshold"))
            xml_aot_param.get_Multi_Temporal_Method().set_Relative_Var_Max(
                dict_of_input.get("L2COMM").get_value_f("RelativeVarMax"))
            xml_aot_param.get_Multi_Temporal_Method().set_Var_Band_Code_D(
                dict_of_input.get("Params").get("VarBandIndex_DateD"))
            xml_aot_param.get_Multi_Temporal_Method().set_Var_Band_Code_Dm1(
                dict_of_input.get("Params").get("VarBandIndex_DateDm1"))
            xml_aot_param.get_Multi_Temporal_Method().set_Stdev_Band_Code(
                stdevbandid)
            xml_aot_param.get_Multi_Temporal_Method().set_Std_Noise(
                dict_of_input.get("L2COMM").get_value_f("StdNoise"))
            xml_aot_param.get_Multi_Temporal_Method().set_Stdev_Min(
                dict_of_input.get("L2COMM").get_value_f("StdevMin"))
            xml_aot_param.get_Multi_Temporal_Method().set_AOT_initial_Dm1(
                dict_of_input.get("L2COMM").get_value_f("AOTinitialDm1"))
            xml_aot_param.get_Multi_Temporal_Method().set_W_MT_equation2(
                dict_of_input.get("L2COMM").get_value_f("WMTequation2"))
            xml_aot_param.get_Multi_Temporal_Method().set_MT_AOT_Band_Code_D([
                str(i)
                for i in dict_of_input.get("Params").get("MTAOTbandsDateD")
            ])
            xml_aot_param.get_Multi_Temporal_Method(
            ).set_MT_AOT_Band_Reflectance_Composite_Dm1([
                str(i) for i in dict_of_input.get("Params").get(
                    "MTAOTbandsOfTheReflectancesComposite_DateDm1")
            ])
            xml_aot_param.get_Multi_Temporal_Method(
            ).set_MT_AOT_Band_LUT_Composite_Dm1([
                str(i) for i in dict_of_input.get("Params").get(
                    "MTAOTbandsOfTheLTCComposite_DateDm1")
            ])

        if l_aotmethod == AOTEstimation.MULTISPECTRAL or l_aotmethod == AOTEstimation.SPECTROTEMPORAL:
            lMSAOTRedBand = bands_definition.get_band_id_in_l2_coarse(
                dict_of_input.get("L2COMM").get_value("MSAOTRedBandCode"))
            lNDVIAOTBand1 = bands_definition.get_band_id_in_l2_coarse(
                dict_of_input.get("L2COMM").get_value("NDVIAOTBand1Code"))
            lNDVIAOTBand2 = bands_definition.get_band_id_in_l2_coarse(
                dict_of_input.get("L2COMM").get_value("NDVIAOTBand2Code"))
            l_AOTUsedBandCodes = xml_tools.as_string_list(
                dict_of_input.get("L2COMM").get_value("MSAOTBandCode"))
            lMSAOTbands = [
                str(bands_definition.get_band_id_in_l2_coarse(f))
                for f in l_AOTUsedBandCodes
            ]

            xml_aot_param.set_Multi_Spectral_Method(
                Multi_Spectral_MethodType())
            xml_aot_param.get_Multi_Spectral_Method().set_MS_AOT_Band_Code(
                lMSAOTbands)
            xml_aot_param.get_Multi_Spectral_Method().set_MS_AOT_Red_Band_Code(
                lMSAOTRedBand)
            xml_aot_param.get_Multi_Spectral_Method().set_NDVI_AOT_Band1_Code(
                lNDVIAOTBand1)
            xml_aot_param.get_Multi_Spectral_Method().set_NDVI_AOT_Band2_Code(
                lNDVIAOTBand2)
            xml_aot_param.get_Multi_Spectral_Method().set_Red_TOA_Threshold(
                dict_of_input.get("L2COMM").get_value_f("RedTOAThreshold"))
            xml_aot_param.get_Multi_Spectral_Method().set_NDVI_Threshold(
                dict_of_input.get("L2COMM").get_value_f("NDVIThreshold"))
            xml_aot_param.get_Multi_Spectral_Method().set_MS_Slope(
                dict_of_input.get("L2COMM").get_value_f("MSSlope"))
            xml_aot_param.get_Multi_Spectral_Method().set_MS_YIntercept(
                dict_of_input.get("L2COMM").get_value_f("MSYIntercept"))
            xml_aot_param.get_Multi_Spectral_Method().set_NDVI_Slope(
                dict_of_input.get("L2COMM").get_value_f("NDVISlope"))
            xml_aot_param.get_Multi_Spectral_Method().set_NDVI_YIntercept(
                dict_of_input.get("L2COMM").get_value_f("NDVIYIntercept"))
            xml_aot_param.get_Multi_Spectral_Method().set_W_MS(
                dict_of_input.get("L2COMM").get_value_f("WMS"))

        if l_aotmethod == AOTEstimation.SPECTROTEMPORAL:
            spectrotemp = Spectro_Temporal_MethodType()
            xml_aot_param.set_Spectro_Temporal_Method(spectrotemp)
            xml_aot_param.get_Spectro_Temporal_Method(
            ).set_MT_Weight_Threshold(
                dict_of_input.get("L2COMM").get_value_f("MTWeightThreshold"))
            xml_aot_param.get_Spectro_Temporal_Method().set_A_Coefficient(
                dict_of_input.get("L2COMM").get_value_f("ACoefficient"))
            xml_aot_param.get_Spectro_Temporal_Method().set_B_Coefficient(
                dict_of_input.get("L2COMM").get_value_f("BCoefficient"))

        const_mask_filename = os.path.join(aot_working, "cloud_constzero.tif")
        constant_app = constant_image(dict_of_input.get("DEM").ALC,
                                      0,
                                      const_mask_filename + ":uint8",
                                      write_output=l_caching)
        constant_mask = constant_app.getoutput()["out"]

        if dict_of_input.get("Params").get("SnowBandAvailable"):
            param_aot["snow"] = dict_of_output.get("cld_snow")
        else:
            param_aot["snow"] = constant_mask

        if not init_Mode:
            param_aot["l2ndt"] = dict_of_input.get("L2Reader").get_value(
                "NDTImage")
            param_aot["l2pxd"] = dict_of_input.get("L2Reader").get_value(
                "PXDImage")
            param_aot["l2rtc"] = dict_of_input.get("L2Reader").get_value(
                "RTCImage")
            param_aot["l2rta"] = dict_of_input.get("L2Reader").get_value(
                "RTAImage")

        # Cams related parameters
        if dict_of_input.get("Params").get("UseCamsData"):
            xml_aot_param.set_AOT_KPondCAMS(
                dict_of_input.get("L2COMM").get_value_f("KPondsCams"))
            xml_aot_param.set_AOT_HeightScale(
                dict_of_input.get("L2COMM").get_value_f("HeighScale"))
            param_aot["cams.nodatatau"] = dict_of_input.get("Params").get(
                "RealL2NoData")
            param_aot["cams.altmean"] = dict_of_input.get("DEM").ALT_Mean
            param_aot["cams.aot"] = dict_of_input.get("CAMS_AOT")
            aot_cams_image = os.path.join(aot_working, "aot_cams.tif")
            param_aot["aotcams"] = aot_cams_image

        #Write down the xml interface
        output = io.StringIO()
        output.write('<?xml version="1.0" ?>\n')
        xml_aot_param.export(output,
                             0,
                             name_='AOTEstimation',
                             namespacedef_='',
                             pretty_print=True)
        l_xml_param_filename = os.path.join(aot_working, "AOTParams.xml")
        with open(l_xml_param_filename, "w") as fh:
            fh.write(output.getvalue().replace("    ", "  "))
        LOGGER.info("Writed aot params to " + l_xml_param_filename)
        param_aot["xmlparamsfile"] = l_xml_param_filename
        # Effectively launch the app
        aot_app = OtbAppHandler("AOTEstimation",
                                param_aot,
                                write_output=l_caching)
        if not l_caching:
            self._aot_pipeline.add_otb_app(aot_app)

        aot_mean = aot_app.getoutput()["aotmean"]
        aot_valid = aot_app.getoutput()["aotvalid"]

        # If cams used then no gapfilling needed
        if dict_of_input.get("Params").get("UseCamsData"):
            smoothing_input = aot_app.getoutput()["aotcomputed"]
            mask_input = aot_app.getoutput()["aotmask"]
        else:
            aot_gapfilled_image = os.path.join(aot_working,
                                               "aot_gapfilled.tif")
            aot_gapfilled_mask = os.path.join(aot_working,
                                              "aot_gapfilled_mask.tif")
            param_gapfilling = {
                "im":
                aot_app.getoutput()["aotcomputed"],
                "initwindowsradius":
                dict_of_input.get("L2COMM").get_value_i("InitWindowRadius"),
                "maxwindowsradius":
                dict_of_input.get("L2COMM").get_value_i("MaxWindowRadius"),
                "reall2nodata":
                dict_of_input.get("Params").get("RealL2NoData"),
                "mean":
                aot_mean,
                "hasvalidpixels":
                aot_valid,
                "defaultvalue":
                dict_of_input.get("L2COMM").get_value_f("DefaultAOT"),
                "out":
                aot_gapfilled_image,
                "outmask":
                aot_gapfilled_mask + ":uint8"
            }

            gap_filling_app = OtbAppHandler("GapFilling",
                                            param_gapfilling,
                                            write_output=(l_caching
                                                          or l_writeL2))
            if not (l_caching or l_writeL2):
                self._aot_pipeline.add_otb_app(gap_filling_app)
            smoothing_input = gap_filling_app.getoutput()["out"]
            mask_input = gap_filling_app.getoutput()["outmask"]

        aot_smoothed_image = os.path.join(aot_working, "aot_smoothed.tif")
        param_smoothing = {
            "in":
            smoothing_input,
            "out":
            aot_smoothed_image,
            "type":
            "gaussian",
            "type.gaussian.radius":
            dict_of_input.get("L2COMM").get_value_f("SigmaSmoothingAOT"),
            "type.gaussian.maxkernelwidth":
            4.0 * dict_of_input.get("L2COMM").get_value_f("SigmaSmoothingAOT"),
            "type.gaussian.maxerror":
            1e-15
        }

        smoothed_app = OtbAppHandler("SmoothingBis",
                                     param_smoothing,
                                     write_output=l_caching)
        if (not l_caching):
            self._aot_pipeline.add_otb_app(smoothed_app)

        aot_sub_image = os.path.join(aot_working, "aot_sub.tif")
        param_subresampling = {
            "dtm": dict_of_input.get("DEM").ALC,
            "im": smoothed_app.getoutput()["out"],
            "interp": "linear",
            "out": aot_sub_image
        }

        subresampling_app = OtbAppHandler("Resampling",
                                          param_subresampling,
                                          write_output=(l_caching
                                                        or l_writeL2))
        if (not (l_caching or l_writeL2)):
            self._aot_pipeline.add_otb_app(subresampling_app)
        dict_of_output["AOT_Sub"] = subresampling_app.getoutput()["out"]

        if l_writeL2:
            l_nbRes = len(bands_definition.ListOfL2Resolution)
            aot_list = []
            aotmask_list = []
            for r in range(0, l_nbRes):
                l_res = bands_definition.ListOfL2Resolution[r]
                aot_image = os.path.join(aot_working, "aot_" + l_res + ".tif")
                aot_mask = os.path.join(aot_working,
                                        "aotmask_" + l_res + ".tif")
                param_subresampling["dtm"] = dict_of_input.get(
                    "DEM").ALTList[r]
                param_subresampling["im"] = subresampling_app.getoutput(
                )["out"]
                param_subresampling["out"] = aot_image
                resamp_aot_app = OtbAppHandler("Resampling",
                                               param_subresampling)

                dict_of_output["AOT_" +
                               l_res] = resamp_aot_app.getoutput().get("out")
                param_maskresampling = {
                    "dtm": dict_of_input.get("DEM").ALTList[r],
                    "im": mask_input,
                    "interp": "linear",
                    "out": aot_mask + ":uint8",
                    "threshold": 0.25
                }
                resamp_aotmask_app = OtbAppHandler("Resampling",
                                                   param_maskresampling)
                dict_of_output["AOTMASK_" +
                               l_res] = resamp_aotmask_app.getoutput().get(
                                   "out")
                aot_list.append(resamp_aot_app.getoutput().get("out"))
                aotmask_list.append(resamp_aotmask_app.getoutput().get("out"))
            dict_of_output["L2AOTList"] = aot_list
            dict_of_output["L2AOTMaskList"] = aotmask_list
    def generate_mask_rasters(self, p_ListOfTOABandCode, working):
        # Generate mask rasters by rasterizing the gml mask per L2 resolution per band
        LOGGER.info("Starting Muscate GenerateMaskRasters")

        l_BandsDefinitions = self._plugin.BandsDefinitions
        l2Area = None
        l_ListOfL2Resolution = l_BandsDefinitions.ListOfL2Resolution  # ListOfStrings
        l_NbL2Res = len(l_ListOfL2Resolution)
        l_ListOfL1Resolution = l_BandsDefinitions.ListOfL1Resolution  # ListOfStrings
        l_NbL1Res = len(l_ListOfL1Resolution)
        dtm_coarse = self._dem.ALC

        # Set a constant image if the gml masks are empty
        tmp_constant_filename = os.path.join(working, "Masks_const.tif")

        # initialize the L2 Elements
        for l2res in range(l_NbL2Res):
            self._l2defectmasklist.append([])
            self._l2dfpimagelist.append(None)

        # Init the coarse elements
        for coarseband in p_ListOfTOABandCode:
            self._l2satmasklist.append(None)
        for l1res in range(l_NbL1Res):
            self._satmasksublist.append(None)
        # Test if the plugin has PIX and SAT ?
        has_pix_masks = False
        defectivPixFileNames = []
        pix_node = xml_tools.get_only_value(
            self._header_handler.root,
            "//Mask_List/Mask/Mask_Properties/NATURE[.='Aberrant_Pixels']",
            check=True)
        if pix_node is not None:
            has_pix_masks = True
            defectivPixFileNames = self._header_handler.get_list_of_pix_mask_filenames(
            )
        has_sat_masks = False
        satPixFileNames = []
        sat_node = xml_tools.get_only_value(
            self._header_handler.root,
            "//Mask_List/Mask/Mask_Properties/NATURE[.='Saturation']",
            check=True)
        if sat_node is not None:
            has_sat_masks = True
            satPixFileNames = self._header_handler.get_list_of_l1_sat_image_filenames(
            )

        # Loop on L1Res
        for l1res in range(l_NbL1Res):
            # Current resolution: "R1" or "R2" or "R3"
            curL1Res = l_ListOfL1Resolution[l1res]
            # Create constant mask if it will be necessary at a time
            tmp_constant_coarse_app = None
            tmp_constant_L2_app = None
            if not has_sat_masks or not has_pix_masks:
                tmp_constant_coarse_filename = os.path.join(
                    working, "Masks_const_.tif")
                tmp_constant_coarse_app = constant_image(
                    dtm_coarse,
                    0,
                    tmp_constant_coarse_filename,
                    write_output=False)
                if curL1Res in l_ListOfL2Resolution:
                    tmp_constant_L2_filename = os.path.join(
                        working, "Masks_const_.tif")
                    tmp_constant_L2_app = constant_image(
                        self._dem.ALTList[l1res],
                        0,
                        tmp_constant_L2_filename + ":uint8",
                        write_output=False)
                    self._l2_sat_pipeline.add_otb_app(tmp_constant_L2_app)

            # Get the list of band of the current resolution
            listOfL1Bands = l_BandsDefinitions.get_list_of_l1_band_code(
                curL1Res)
            nbL1Bands = len(listOfL1Bands)
            # For each band of the current resolution
            l_l2zone_pipeline = OtbPipelineManager()
            firstBandIdx = l_BandsDefinitions.get_band_id_in_l1(
                listOfL1Bands[0])

            if has_sat_masks:
                l_strSatFileNameRef = satPixFileNames[firstBandIdx]
                l_isSatSameFilesForBands = True

                for l_BandIdxL1 in range(len(listOfL1Bands)):
                    # Get the L1 band index associated to the L2 band code
                    l_StrBandIdL1 = listOfL1Bands[l_BandIdxL1]
                    l1BandIdx = self._header_handler.get_index_of_band_code(
                        l_StrBandIdL1)
                    # Filenameverify
                    LOGGER.debug(l_StrBandIdL1 + " : " + str(l1BandIdx) +
                                 " : " + satPixFileNames[l1BandIdx])
                    if l_strSatFileNameRef != satPixFileNames[l1BandIdx]:
                        l_isSatSameFilesForBands = False
                LOGGER.debug("l_isSatSameFilesForBands = " +
                             str(l_isSatSameFilesForBands))

                # SAT Mask generation
                if l_isSatSameFilesForBands:
                    # Sat Masks generation
                    l_StrBandIdL1 = listOfL1Bands[0]
                    l1BandIdx = self._header_handler.get_index_of_band_code(
                        l_StrBandIdL1)
                    l_SATFFilename = satPixFileNames[l1BandIdx]
                    sat_mask = os.path.join(
                        working, "L1_SAT_Masks_{}.tif".format(curL1Res))
                    param_bintovec_dfp = {
                        "im": l_SATFFilename,
                        "out": sat_mask,
                        "nbcomp": nbL1Bands
                    }
                    sat_mask_app = OtbAppHandler("BinaryToVector",
                                                 param_bintovec_dfp,
                                                 write_output=False)
                    self._satmask_pipeline.add_otb_app(sat_mask_app)

                    if curL1Res in l_ListOfL2Resolution:
                        tmp_sat_l2_resample = os.path.join(
                            working, "tmp_sat_resample_{}.tif".format(l1res))

                        if l_BandsDefinitions.get_list_of_l1_band_code(curL1Res) != \
                                l_BandsDefinitions.get_list_of_l2_band_code(curL1Res):
                            tmp_l2_band_idx_list = []
                            #Create the list of index to build the current res sat mask
                            for l2band in l_BandsDefinitions.get_list_of_l2_band_code(
                                    curL1Res):
                                l2BandIdxInHeader = self._header_handler.get_index_of_band_code(
                                    l2band)
                                tmp_l2_band_idx_list.append(
                                    self._header_handler.
                                    get_l1_sat_image_index(l2BandIdxInHeader) -
                                    1)
                            tmp_satmask_roi = os.path.join(
                                working,
                                "tmp_sat_extract_{}.tif".format(l1res))
                            tmp_extract_sat_toi_app = extract_roi(
                                sat_mask_app.getoutput()["out"],
                                tmp_l2_band_idx_list,
                                tmp_satmask_roi,
                                write_output=False)
                            self._satmask_pipeline.add_otb_app(
                                tmp_extract_sat_toi_app)
                            resamp_l2_app = resample(
                                tmp_extract_sat_toi_app.getoutput()["out"],
                                self._dem.ALTList[l1res],
                                tmp_sat_l2_resample,
                                OtbResampleType.LINEAR_WITH_RADIUS,
                                threshold=0.5,
                                padradius=4,
                                write_output=False)
                            self._satmask_pipeline.add_otb_app(resamp_l2_app)
                            self._l2satmasklist[l_ListOfL2Resolution.index(
                                curL1Res)] = resamp_l2_app.getoutput().get(
                                    "out")
                        else:
                            resamp_l2_app = resample(
                                sat_mask_app.getoutput()["out"],
                                self._dem.ALTList[l1res],
                                tmp_sat_l2_resample + ":uint8",
                                OtbResampleType.LINEAR_WITH_RADIUS,
                                threshold=0.25,
                                padradius=4,
                                write_output=False)
                            self._satmask_pipeline.add_otb_app(resamp_l2_app)
                            self._l2satmasklist[l_ListOfL2Resolution.index(
                                curL1Res)] = resamp_l2_app.getoutput()["out"]

                    tmp_l1_band_idx_list = []
                    for l1band in listOfL1Bands:
                        l1BandIdxInHeader = self._header_handler.get_index_of_band_code(
                            l1band)
                        tmp_l1_band_idx_list.append(
                            self._header_handler.get_l1_sat_image_index(
                                l1BandIdxInHeader) - 1)
                    tmp_sat_roi = os.path.join(
                        working, "tmp_l1_extract_roi_sat_{}.tif".format(l1res))
                    tmp_sat_roi_app = extract_roi(
                        sat_mask_app.getoutput()["out"],
                        tmp_l1_band_idx_list,
                        tmp_sat_roi,
                        write_output=False)
                    l_sat_subthresholdvalue = self._GIPPL2COMMHandler.get_value_f(
                        "SaturationThresholdSub")
                    tmp_sat_resample = os.path.join(
                        working,
                        "tmp_extract_roi_sat_resample_{}.tif".format(l1res))
                    app_resample = resample(
                        tmp_sat_roi_app.getoutput().get("out"),
                        dtm_coarse,
                        tmp_sat_resample,
                        OtbResampleType.LINEAR_WITH_RADIUS,
                        threshold=l_sat_subthresholdvalue,
                        padradius=4,
                        write_output=True)
                    self._satmasksublist[l1res] = app_resample.getoutput(
                    )["out"]

                else:
                    raise MajaExceptionPluginMuscate(
                        "Product format not supported : not the same file for band on SAT"
                    )
            else:
                # No sat available then put constants masks in outout
                if curL1Res in l_ListOfL2Resolution:
                    self._l2satmasklist[l_ListOfL2Resolution.index(
                        curL1Res)] = tmp_constant_L2_app.getoutput().get("out")
                for l1band in listOfL1Bands:
                    l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l1band)
                    self._satmasksublist[
                        l1BandIdx] = tmp_constant_coarse_app.getoutput().get(
                            "out")

            # Verify if we can optimize the work if its the same file for all bands of
            # the resolution and if the bands are in correct bit order
            if has_pix_masks:
                l_strDefectFileNameRef = defectivPixFileNames[firstBandIdx]
                l_isDefectSameFilesForBands = True
                for l_BandIdxL1 in range(len(listOfL1Bands)):
                    # Get the L1 band index associated to the L2 band code
                    l_StrBandIdL1 = listOfL1Bands[l_BandIdxL1]
                    l1BandIdx = self._header_handler.get_index_of_band_code(
                        l_StrBandIdL1)
                    # Filenameverify
                    if l_strDefectFileNameRef != defectivPixFileNames[
                            l1BandIdx]:
                        l_isDefectSameFilesForBands = False
                LOGGER.debug("l_isDefectSameFilesForBands = " +
                             str(l_isDefectSameFilesForBands))

                # Defective PIX (only in L2 resolution)
                if l_isDefectSameFilesForBands:
                    if curL1Res in l_ListOfL2Resolution:
                        l_StrBandIdL1 = listOfL1Bands[0]
                        l1BandIdx = self._header_handler.get_index_of_band_code(
                            l_StrBandIdL1)

                        self._l2piximagelist.append(
                            defectivPixFileNames[l1BandIdx])

                        dfp_mask = os.path.join(
                            working, "L1_DFP_Masks_{}.tif".format(curL1Res))
                        param_bintovec_dfp = {
                            "im": defectivPixFileNames[l1BandIdx],
                            "out": dfp_mask,
                            "nbcomp": nbL1Bands
                        }

                        dfp_mask_app = OtbAppHandler("BinaryToVector",
                                                     param_bintovec_dfp,
                                                     write_output=False)
                        self._l2_dfp_pipeline.add_otb_app(dfp_mask_app)
                        tmp_dfp_l2_resample = os.path.join(
                            working, "tmp_dfp_resample_{}.tif".format(l1res))
                        resamp_l2_dfp_app = resample(
                            dfp_mask_app.getoutput().get("out"),
                            self._dem.ALTList[l1res],
                            tmp_dfp_l2_resample + ":uint8",
                            OtbResampleType.LINEAR_WITH_RADIUS,
                            threshold=0.25,
                            padradius=4,
                            write_output=False)
                        self._l2dfpimagelist[l_ListOfL2Resolution.index(
                            curL1Res)] = resamp_l2_dfp_app.getoutput().get(
                                "out")
                        self._l2_dfp_pipeline.add_otb_app(resamp_l2_dfp_app)
                else:
                    raise MajaExceptionPluginMuscate(
                        "Product format not supported : not the same file for band on PIX"
                    )
            else:
                if curL1Res in l_ListOfL2Resolution:
                    self._l2dfpimagelist[l_ListOfL2Resolution.index(
                        curL1Res)] = tmp_constant_L2_app.getoutput().get("out")
                    self._l2piximagelist.append(
                        tmp_constant_L2_app.getoutput().get("out"))
    def muscate_read_public_images(self, p_L2XMLHandler, p_PluginBase,
                                   working_dir):
        l_NumberOfResolutions = len(p_PluginBase.ListOfL2Resolutions)
        l_bands_definition = p_PluginBase.BandsDefinitions
        # ****  PUBLIC  DATA      *************************************************************************************
        l_ReflectanceQuantificationValue = 1. / float(
            p_L2XMLHandler.get_string_value_of("QuantificationValue"))
        # Loop on all resolution products
        l_SREList = []
        l_FREList = []
        l_ATBList = []
        l_VAPList = []
        l_AOTList = []
        l_QLTList = []
        l_SATList = []
        l_EDGList = []
        l_CLDList = []
        l_MSKList = []
        l_WATList = []
        l_SNOWList = []
        for resol in range(0, l_NumberOfResolutions):
            l_sres_mtd = "XS"
            if len(p_PluginBase.BandsDefinitions.ListOfL2Resolutions) > 1:
                l_sres_mtd = p_PluginBase.BandsDefinitions.ListOfL2Resolutions[
                    resol]
            l_sres = p_PluginBase.BandsDefinitions.ListOfL2Resolutions[resol]
            # Get the list of band of the current resolution
            listOfL2Bands = p_PluginBase.BandsDefinitions.get_list_of_l2_band_code(
                l_sres)
            # For each band of the current resolution
            l_firstBandIdx = p_PluginBase.BandsDefinitions.get_band_id_in_l2(
                listOfL2Bands[0])
            l_NumberOfComponentsPerPixel = len(
                p_PluginBase.BandsDefinitions.get_list_of_l2_band_code(l_sres))

            # Surface Reflectance reading
            LOGGER.debug(
                "Start reading the Surface reflectance image filenames ...")
            l_ListOfL2SurfaceReflectanceFilenames = p_L2XMLHandler.get_list_of_l2_surface_reflectance_filenames(
                l_sres_mtd)
            l_NumberOfL2SurfaceReflectanceFilenames = len(
                l_ListOfL2SurfaceReflectanceFilenames)
            LOGGER.debug("l_NumberOfL2SurfaceReflectanceFilenames: " +
                         str(l_NumberOfL2SurfaceReflectanceFilenames))

            # Concatenate bands
            tmp_refl_concat = os.path.join(working_dir,
                                           "tmp_sre_concat_" + l_sres + ".tif")
            param_reflectance_concat = {
                "il": l_ListOfL2SurfaceReflectanceFilenames,
                "out": tmp_refl_concat
            }
            sre_concat_app = OtbAppHandler("ConcatenateDoubleImages",
                                           param_reflectance_concat)
            # Multiply by quantification value
            tmp_sre_scale = os.path.join(working_dir,
                                         "tmp_sre_scale_" + l_sres + ".tif")
            param_scaled_sre = {
                "im": sre_concat_app.getoutput().get("out"),
                "coef": l_ReflectanceQuantificationValue,
                "out": tmp_sre_scale
            }
            sre_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_sre)
            l_SREList.append(sre_scal_app.getoutput().get("out"))

            # Read the FRE Flat reflectance
            if self._m_WriteFRE:
                # Flat Reflectance reading
                LOGGER.debug(
                    "Start reading the Flat reflectance image filenames ...")
                l_ListOfL2FlatReflectanceFilenames = []
                l_NumberOfL2FlatReflectanceFilenames = 0
                if self._m_WriteFRE:
                    l_ListOfL2FlatReflectanceFilenames = p_L2XMLHandler.get_list_of_l2_flat_reflectance_filenames(
                        l_sres_mtd)
                    l_NumberOfL2FlatReflectanceFilenames = len(
                        l_ListOfL2FlatReflectanceFilenames)
                # Concatenate bands
                tmp_flat_concat = os.path.join(
                    working_dir, "tmp_fre_concat_" + l_sres + ".tif")
                param_flat_concat = {
                    "il": l_ListOfL2FlatReflectanceFilenames,
                    "out": tmp_flat_concat
                }
                fre_concat_app = OtbAppHandler("ConcatenateDoubleImages",
                                               param_flat_concat)
                # Multiply by quantification value
                tmp_fre_scale = os.path.join(
                    working_dir, "tmp_fre_scale_" + l_sres + ".tif")
                param_scaled_fre = {
                    "im": fre_concat_app.getoutput().get("out"),
                    "coef": l_ReflectanceQuantificationValue,
                    "out": tmp_fre_scale
                }
                fre_scal_app = OtbAppHandler("MultiplyByScalar",
                                             param_scaled_fre)
                l_FREList.append(fre_scal_app.getoutput().get("out"))

            # Read the ATB
            LOGGER.debug(
                "L2ImageFileReaderBase::Initialize - ATB image filename: %s.",
                p_L2XMLHandler.get_l2_atb_filename())
            l_ATBList.append(p_L2XMLHandler.get_l2_atb_filename())
            # Read the VAP
            l_VAPQuantificationValue = 1. / \
                float(p_L2XMLHandler.get_string_value_of("WaterVaporContentQuantificationValue"))
            tmp_vap_roi = os.path.join(working_dir,
                                       "tmp_vap_roi_" + l_sres + ".tif")
            tmp_vap_roi_app = extract_roi(
                p_L2XMLHandler.get_l2_atb_filename(l_sres_mtd),
                [p_L2XMLHandler.get_l2_vap_index()],
                tmp_vap_roi,
                write_output=True)
            tmp_vap_scale = os.path.join(working_dir,
                                         "tmp_vap_scale_" + l_sres + ".tif")
            param_scaled_vap = {
                "im": tmp_vap_roi_app.getoutput()["out"],
                "coef": l_VAPQuantificationValue,
                "out": tmp_vap_scale
            }
            vap_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_vap)
            l_VAPList.append(vap_scal_app.getoutput().get("out"))

            # Read the AOT
            l_AOTQuantificationValue = 1. / \
                float(p_L2XMLHandler.get_string_value_of("AerosolOpticalThicknessQuantificationValue"))

            tmp_aot_roi = os.path.join(working_dir,
                                       "tmp_aot_roi_" + l_sres + ".tif")
            tmp_aot_roi_app = extract_roi(p_L2XMLHandler.get_l2_atb_filename(),
                                          [p_L2XMLHandler.get_l2_aot_index()],
                                          tmp_aot_roi,
                                          write_output=True)
            tmp_aot_scale = os.path.join(working_dir,
                                         "tmp_aot_scale_" + l_sres + ".tif")
            param_scaled_aot = {
                "im": tmp_aot_roi_app.getoutput()["out"],
                "coef": l_AOTQuantificationValue,
                "out": tmp_aot_scale
            }
            aot_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_aot)
            l_AOTList.append(aot_scal_app.getoutput().get("out"))

            # Read the SAT
            l_ListOfL2SATFilenames = p_L2XMLHandler.get_list_of_l2_sat_image_filenames(
                l_sres_mtd)
            isSameSatForBands = True
            l_strSatFileNameRef = l_ListOfL2SATFilenames[l_firstBandIdx]
            l_isSatSameFilesForBands = True

            for l_BandIdxL2 in range(len(listOfL2Bands)):
                # Get the L1 band index associated to the L2 band code
                l_StrBandIdL2 = listOfL2Bands[l_BandIdxL2]
                l2BandIdx = p_L2XMLHandler.get_index_of_band_code(
                    l_StrBandIdL2)
                # Filenameverify
                if l_strSatFileNameRef != l_ListOfL2SATFilenames[l2BandIdx]:
                    l_isSatSameFilesForBands = False
            LOGGER.debug("l_isL2SatSameFilesForBands = " +
                         str(l_isSatSameFilesForBands))

            if l_isSatSameFilesForBands:
                l_StrBandIdL2 = listOfL2Bands[0]
                l2BandIdx = p_L2XMLHandler.get_index_of_band_code(
                    l_StrBandIdL2)
                l_SATFFilename = l_ListOfL2SATFilenames[l2BandIdx]
                sat_mask = os.path.join(working_dir,
                                        "L2_SAT_Masks_{}.tif".format(l_sres))
                param_bintovec_sat = {
                    "im": l_SATFFilename,
                    "out": sat_mask + ":uint8",
                    "nbcomp": len(listOfL2Bands)
                }
                sat_mask_app = OtbAppHandler("BinaryToVector",
                                             param_bintovec_sat,
                                             write_output=True)
                l_SATList.append(sat_mask_app.getoutput().get("out"))
            else:
                raise MajaPluginMuscateException(
                    "Product format not supported : not the same file for band on SAT in L2 product"
                )

            # Read EDG
            LOGGER.debug("Start reading the EDG image")
            l_EDGList.append(p_L2XMLHandler.get_l2_edg_filename(l_sres_mtd))

            # Read MG2
            LOGGER.debug("Start reading the MG2 <" +
                         p_L2XMLHandler.get_l2_mg2_filename(l_sres_mtd) +
                         "> mask filename...")
            tmp_vec_mg2 = os.path.join(working_dir,
                                       "tmp_mg2_vec_" + l_sres + ".tif")
            param_bin_mg2 = {
                "im": p_L2XMLHandler.get_l2_mg2_filename(l_sres_mtd),
                "out": tmp_vec_mg2,
                "nbcomp": 8
            }
            bin_mg2_app = OtbAppHandler("BinaryToVector",
                                        param_bin_mg2,
                                        write_output=True)
            # Read WAT from MG2
            LOGGER.debug("Start reading the WAT image")
            tmp_wat = os.path.join(working_dir, "tmp_wat_" + l_sres + ".tif")
            tmp_wat_app = extract_roi(bin_mg2_app.getoutput()["out"],
                                      [p_L2XMLHandler.get_l2_wat_index()],
                                      tmp_wat)
            l_WATList.append(tmp_wat_app.getoutput().get("out"))

            # Read SNW
            if p_PluginBase.SnowMasking:
                LOGGER.debug("Start reading the SNW image")
                tmp_snow = os.path.join(working_dir,
                                        "tmp_snow_" + l_sres + ".tif")
                tmp_snow_image = extract_roi(
                    tmp_wat_app.getoutput()["out"],
                    [p_L2XMLHandler.get_l2_snow_index()], tmp_snow)
                l_SNOWList.append(tmp_snow_image.getoutput().get("out"))

            # Read CLD
            LOGGER.debug("Start reading the CLD image")
            tmp_cld_vec = os.path.join(working_dir,
                                       "tmp_cld_vec_" + l_sres + ".tif")
            param_vec_cld = {
                "im": p_L2XMLHandler.get_l2_cld_filename(l_sres_mtd),
                "out": tmp_cld_vec + ":double",
                "nbcomp": len(p_PluginBase.CLDCoreAlgorithmsMapBand)
            }
            cld_vec_app = OtbAppHandler("BinaryToVector",
                                        param_vec_cld,
                                        write_output=False)
            # In this case some cld bits are not available in the data
            tmp_cld_zero = os.path.join(working_dir,
                                        "tmp_cld_zero_" + l_sres + ".tif")
            cld_const_zero_app = None
            if len(p_PluginBase.CLDCoreAlgorithmsMapBand) > len(
                    p_PluginBase.CLDDataBandsSelected):
                cld_const_zero_app = constant_image(
                    cld_vec_app.getoutput()["out"], "0",
                    os.path.join(working_dir,
                                 "tmp_zero_cld_" + l_sres + ".tif"))
            l_dict_of_cld = dict()
            for b in list(p_PluginBase.CLDCoreAlgorithmsMapBand.keys()):
                if b in p_PluginBase.CLDDataBandsSelected:
                    tmp_cld_chan = os.path.join(
                        working_dir, "tmp_" + b + "_" + l_sres + ".tif")
                    chan = p_PluginBase.CLDDataBandsSelected.index(b)
                    l_dict_of_cld[b] = extract_roi(
                        tmp_cld_vec, [chan], tmp_cld_chan + ":uint8",
                        working_dir).getoutput()["out"]
                else:
                    l_dict_of_cld[b] = cld_const_zero_app.getoutput()["out"]
            # For GetVectorizedCLDImageList method
            l_CLDList.append(l_dict_of_cld)
Пример #18
0
    def read_private_images(self, p_L2PrivateImageFilenamesProvider,
                            p_ReflectanceQuantificationValue, p_PluginBase,
                            working_dir):
        l_NumberOfComponentsPerPixelForPWAAndTWA = len(
            p_PluginBase.BandsDefinitions.L2CoarseBandMap)

        # *************************************************************************************************************
        # Register the Header for the L2 Input image file
        # *************************************************************************************************************
        l_ReflectanceQuantificationValue = p_ReflectanceQuantificationValue
        # Read the  list of wavelength of the RTA/RTC and RCR list of bands in the HDR xml header file
        l_RTAHeaderFilename = p_L2PrivateImageFilenamesProvider.get_rta_header_filename(
        )
        l_RTAHandler = HeaderImageEarthExplorerXMLFileHandler(
            l_RTAHeaderFilename)
        self.ListOfBandTheoreticalWavelengthOfTheReflectancesComposite = l_RTAHandler.get_list_of_band_theoretical_wavelength(
        )

        # ********************************************************************************************************/
        # #* RTA Reader connection */
        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - RTA image filename: " +
            p_L2PrivateImageFilenamesProvider.get_rta_image_filename() + ".")

        tmp_rta_resample = os.path.join(working_dir, "tmp_rta_resample.tif")
        app_rta_resample = resample(
            p_L2PrivateImageFilenamesProvider.get_rta_image_filename(),
            self._dem.ALC,
            tmp_rta_resample,
            OtbResampleType.BCO,
            padradius=4.0,
            write_output=False)
        tmp_rta_scale = os.path.join(working_dir, "tmp_rta_scale.tif")
        param_scaled_rta = {
            "im": app_rta_resample.getoutput().get("out"),
            "coef": l_ReflectanceQuantificationValue,
            "out": tmp_rta_scale
        }
        rta_scal_app = OtbAppHandler("MultiplyByScalar",
                                     param_scaled_rta,
                                     write_output=True)
        self._coarse_pipeline.add_otb_app(rta_scal_app)
        self._coarse_pipeline.add_otb_app(app_rta_resample)
        self.dict_of_vals["RTAImage"] = rta_scal_app.getoutput()["out"]
        # ********************************************************************************************************/
        # * RTC Reader connection */
        # ********************************************************************************************************/
        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - RTC image filename: " +
            p_L2PrivateImageFilenamesProvider.get_rtc_image_filename() + ".")

        tmp_rtc_resample = os.path.join(working_dir, "tmp_rtc_resample.tif")
        app_rtc_resample = resample(
            p_L2PrivateImageFilenamesProvider.get_rtc_image_filename(),
            self._dem.ALC,
            tmp_rtc_resample,
            OtbResampleType.BCO,
            padradius=4.0,
            write_output=False)

        tmp_rtc_scale = os.path.join(working_dir, "tmp_rtc_scale.tif")
        param_scaled_rtc = {
            "im": app_rtc_resample.getoutput().get("out"),
            "coef": l_ReflectanceQuantificationValue,
            "out": tmp_rtc_scale
        }
        rtc_scal_app = OtbAppHandler("MultiplyByScalar",
                                     param_scaled_rtc,
                                     write_output=True)
        self._coarse_pipeline.add_otb_app(rtc_scal_app)
        self._coarse_pipeline.add_otb_app(app_rtc_resample)
        self.dict_of_vals["RTCImage"] = rtc_scal_app.getoutput()["out"]
        # ********************************************************************************************************/
        # * RCR Reader connection */
        # ********************************************************************************************************/
        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - RCR image filename: " +
            p_L2PrivateImageFilenamesProvider.get_rcr_image_filename() + ".")
        tmp_rcr_resample = os.path.join(working_dir, "tmp_rcr_resample.tif")
        app_rcr_resample = resample(
            p_L2PrivateImageFilenamesProvider.get_rcr_image_filename(),
            self._dem.ALC,
            tmp_rcr_resample,
            OtbResampleType.BCO,
            padradius=4.0,
            write_output=False)
        tmp_rcr_scale = os.path.join(working_dir, "tmp_rcr_scale.tif")
        param_scaled_rcr = {
            "im": app_rcr_resample.getoutput().get("out"),
            "coef": l_ReflectanceQuantificationValue,
            "out": tmp_rcr_scale
        }
        rcr_scal_app = OtbAppHandler("MultiplyByScalar",
                                     param_scaled_rcr,
                                     write_output=True)
        self._coarse_pipeline.add_otb_app(rcr_scal_app)
        self._coarse_pipeline.add_otb_app(app_rcr_resample)

        self.dict_of_vals["RCRImage"] = rcr_scal_app.getoutput()["out"]
        # ********************************************************************************************************/
        # * PXD Reader connection */
        # ********************************************************************************************************/
        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - PXD image filename: " +
            p_L2PrivateImageFilenamesProvider.get_pxd_image_filename() + ".")
        tmp_pxd_resample = os.path.join(working_dir, "tmp_pxd_resample.tif")
        app_pxd_resample = resample(
            p_L2PrivateImageFilenamesProvider.get_pxd_image_filename(),
            self._dem.ALC,
            tmp_pxd_resample,
            OtbResampleType.BCO,
            padradius=4.0,
            write_output=True)
        self._coarse_pipeline.add_otb_app(app_pxd_resample)
        self.dict_of_vals["PXDImage"] = app_pxd_resample.getoutput().get("out")

        # ********************************************************************************************************/
        # * WAS Reader connection (WAM product) */
        # ********************************************************************************************************/
        tmp_was = os.path.join(working_dir, "tmp_was.tif")
        was_extr_app = extract_roi(
            p_L2PrivateImageFilenamesProvider.get_wam_image_filename(), [0],
            tmp_was,
            write_output=False)

        tmp_was_resample = os.path.join(working_dir, "tmp_was_resample.tif")
        app_was_resample = resample(was_extr_app.getoutput().get("out"),
                                    self._dem.ALC,
                                    tmp_was_resample,
                                    OtbResampleType.BCO,
                                    padradius=4.0,
                                    write_output=True)
        self._coarse_pipeline.add_otb_app(was_extr_app)
        self._coarse_pipeline.add_otb_app(app_was_resample)
        self.dict_of_vals["WASImage"] = app_was_resample.getoutput()["out"]
        # ********************************************************************************************************/
        # * PWA Reader connection (WAM product) */
        # ********************************************************************************************************/
        tmp_pwa = os.path.join(working_dir, "tmp_pwa.tif")
        pwa_image_app = extract_roi(
            p_L2PrivateImageFilenamesProvider.get_wam_image_filename(), [1],
            tmp_pwa + ":uint8",
            write_output=False)
        self._coarse_pipeline.add_otb_app(pwa_image_app)

        tmp_pwa_resample = os.path.join(working_dir, "tmp_pwa_resample.tif")
        app_pwa_resample = resample(pwa_image_app.getoutput().get("out"),
                                    self._dem.ALC,
                                    tmp_pwa_resample,
                                    OtbResampleType.BCO,
                                    padradius=4.0,
                                    write_output=False)
        self._coarse_pipeline.add_otb_app(app_pwa_resample)

        tmp_vecpwa = os.path.join(working_dir, "tmp_pwa_vec.tif")
        param_binpwa = {
            "im": app_pwa_resample.getoutput()["out"],
            "out": tmp_vecpwa,
            "nbcomp": l_NumberOfComponentsPerPixelForPWAAndTWA
        }
        binpwa_app = OtbAppHandler("BinaryToVector",
                                   param_binpwa,
                                   write_output=True)
        self._coarse_pipeline.add_otb_app(binpwa_app)
        self.dict_of_vals["PWAImage"] = binpwa_app.getoutput()["out"]

        # ********************************************************************************************************/
        # * TWA Reader connection (WAM product) */
        # ********************************************************************************************************/
        tmp_twa = os.path.join(working_dir, "tmp_twa.tif")
        twa_image = extract_roi(
            p_L2PrivateImageFilenamesProvider.get_wam_image_filename(), [2],
            tmp_twa + ":uint8",
            write_output=False)

        tmp_twa_resample = os.path.join(working_dir, "tmp_twa_resample.tif")
        app_twa_resample = resample(twa_image.getoutput().get("out"),
                                    self._dem.ALC,
                                    tmp_twa_resample,
                                    OtbResampleType.BCO,
                                    padradius=4.0,
                                    write_output=False)
        self._coarse_pipeline.add_otb_app(app_twa_resample)

        tmp_vectwa = os.path.join(working_dir, "tmp_twa_vec.tif")
        param_bintwa = {
            "im": app_twa_resample.getoutput().get("out"),
            "out": tmp_vectwa,
            "nbcomp": l_NumberOfComponentsPerPixelForPWAAndTWA
        }
        bintwa_app = OtbAppHandler("BinaryToVector",
                                   param_bintwa,
                                   write_output=True)
        self._coarse_pipeline.add_otb_app(bintwa_app)
        self.dict_of_vals["TWAImage"] = bintwa_app.getoutput()["out"]

        # ********************************************************************************************************/
        # * STO Reader connection */
        # ********************************************************************************************************/
        l_STOHeaderFilename = p_L2PrivateImageFilenamesProvider.get_sto_header_filename(
        )
        l_STOHandler = HeaderImageEarthExplorerXMLFileHandler(
            l_STOHeaderFilename)

        try:
            self.STOBandTheoreticalWavelengthIsPresent = (
                l_STOHandler.get_theoretical_wavelength() is not None)
            self.STOBandTheoreticalWavelengthValue = l_STOHandler.get_theoretical_wavelength(
            )
        except BaseException:
            LOGGER.debug("Could not retrive STO wavelength !!!")

        # 4.3: Get the lis of date (in Bands nodes)
        self.STOListOfStringDates = l_STOHandler.get_list_of_bands()

        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - STO image filename: " +
            p_L2PrivateImageFilenamesProvider.get_sto_image_filename() + ".")

        tmp_sto_resample = os.path.join(working_dir, "tmp_sto_resample.tif")
        app_sto_resample = resample(
            p_L2PrivateImageFilenamesProvider.get_sto_image_filename(),
            self._dem.ALC,
            tmp_sto_resample,
            OtbResampleType.BCO,
            padradius=4.0,
            write_output=False)
        self._coarse_pipeline.add_otb_app(app_sto_resample)

        tmp_sto_scale = os.path.join(working_dir, "tmp_sto_scale.tif")
        param_scaled_sto = {
            "im": app_sto_resample.getoutput().get("out"),
            "coef": l_ReflectanceQuantificationValue,
            "out": tmp_sto_scale
        }
        sto_scal_app = OtbAppHandler("MultiplyByScalar",
                                     param_scaled_sto,
                                     write_output=True)
        self._coarse_pipeline.add_otb_app(sto_scal_app)
        self.dict_of_vals["STOImage"] = sto_scal_app.getoutput()["out"]
        # ********************************************************************************************************/
        # * NDT Reader connection */
        # ********************************************************************************************************/
        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - NDT image filename: " +
            p_L2PrivateImageFilenamesProvider.get_ndt_image_filename() + ".")

        tmp_ndt_resample = os.path.join(working_dir, "tmp_ndt_resample.tif")
        app_ndt_resample = resample(
            p_L2PrivateImageFilenamesProvider.get_ndt_image_filename(),
            self._dem.ALC,
            tmp_ndt_resample,
            OtbResampleType.BCO,
            padradius=4.0,
            threshold=0.25,
            write_output=True)
        self._coarse_pipeline.add_otb_app(app_ndt_resample)
        self.dict_of_vals["NDTImage"] = app_ndt_resample.getoutput().get("out")

        # 4-1-0 : HandlingConnectionCLD DM 1039
        #            #********************************************************************************************************/
        #            #* CLD sub (summary) Reader connection */
        #            #********************************************************************************************************/
        LOGGER.debug(
            "L2ImageFileReaderBase::ReadPrivateImages - CLD image filename: " +
            p_L2PrivateImageFilenamesProvider.get_cld_image_filename() + ".")

        # TODO ATTENTION m_CLDSubOutput est la sortie du Reader (donc pas toute le
        # s 9 bandes algo, ou laors sortie du m_ReorganizeBinaryCLDSub ??!!!
        self.dict_of_vals[
            "CLDSubImage"] = p_L2PrivateImageFilenamesProvider.get_cld_image_filename(
            )

        # MACCS 5.0.0 : DM 982 . Need CLD bands (for example extract Shadow and ShadVar bands in L2CoarseResolution)

        # For GetVectorizedCLDImageList method
        tmp_cld_vec = os.path.join(working_dir, "tmp_cld_vec.tif")
        param_vec_cld = {
            "im": p_L2PrivateImageFilenamesProvider.get_cld_image_filename(),
            "out": tmp_cld_vec,
            "nbcomp": len(p_PluginBase.CLDCoreAlgorithmsMapBand)
        }
        cld_vec_app = OtbAppHandler("BinaryToVector",
                                    param_vec_cld,
                                    write_output=False)
        self._coarse_pipeline.add_otb_app(cld_vec_app)
        cld_vec_image = cld_vec_app.getoutput()["out"]
        # In this case some cld bits are not available in the data
        tmp_cld_zero = os.path.join(working_dir, "tmp_cld_zero.tif")
        cld_const_zero_app = None
        if len(p_PluginBase.CLDCoreAlgorithmsMapBand) > len(
                p_PluginBase.CLDDataBandsSelected):
            cld_const_zero_app = constant_image(cld_vec_image,
                                                0,
                                                os.path.join(
                                                    working_dir,
                                                    "tmp_zero_cld.tif"),
                                                write_output=False)
            self._coarse_pipeline.add_otb_app(cld_const_zero_app)
        self.dict_of_vals["VectorizedCLDSubOutput"] = {}
        for b in list(p_PluginBase.CLDCoreAlgorithmsMapBand.keys()):
            if b in p_PluginBase.CLDDataBandsSelected:
                tmp_cld_chan = os.path.join(working_dir, "tmp_" + b + ".tif")
                chan = p_PluginBase.CLDDataBandsSelected.index(b)
                app_extract_cld = extract_roi(cld_vec_image, [chan],
                                              tmp_cld_chan,
                                              write_output=False)
                self._coarse_pipeline.add_otb_app(app_extract_cld)

                tmp_cld_resample = os.path.join(working_dir,
                                                "tmp_cld_resample.tif")
                app_cld_resample = resample(
                    app_extract_cld.getoutput().get("out"),
                    self._dem.ALC,
                    tmp_cld_resample,
                    OtbResampleType.BCO,
                    padradius=4.0,
                    threshold=0.25,
                    write_output=True)
                self._coarse_pipeline.add_otb_app(app_cld_resample)

                self.dict_of_vals["VectorizedCLDSubOutput"][
                    b] = app_cld_resample.getoutput()["out"]
            else:
                tmp_cld_resample = os.path.join(working_dir,
                                                "tmp_cld_resample.tif")
                app_cld_resample = resample(
                    cld_const_zero_app.getoutput()["out"],
                    self._dem.ALC,
                    tmp_cld_resample,
                    OtbResampleType.BCO,
                    padradius=4.0,
                    threshold=0.25,
                    write_output=True)
                self._coarse_pipeline.add_otb_app(app_cld_resample)

                self.dict_of_vals["VectorizedCLDSubOutput"][
                    b] = app_cld_resample.getoutput()["out"]

        # *************************************************************************************************************
        #  Generate a map of LUT containing all TOCR miniLUTs of the L2 product at D-1 (L2inTOCR)
        # *************************************************************************************************************
        if p_L2PrivateImageFilenamesProvider.get_dealing_ltc():
            LOGGER.debug(
                "L2ImageFileReaderBase::ReadPrivateImages - Start reading LTC ..."
            )
            l_Filename = p_L2PrivateImageFilenamesProvider.get_ltc_header_filename(
            )
            # --------------------------------------------
            # Load the XML file
            LOGGER.debug(
                "L2ImageFileReaderBase::Start loading the LTC header filename <"
                + l_Filename + "> ....")
            l_LUTXMLHandler = GippLUTEarthExplorerXMLFileHandler(l_Filename)

            self.ListOfBandTheoreticalWavelengthOfTheLTCComposite = \
                l_LUTXMLHandler.get_list_of_band_theoretical_wavelength()

            # Read the files
            l_L2P_LTC_ListOfFilenames = l_LUTXMLHandler.get_list_of_packaged_dbl_files(
                True, True)

            # convert the lut indexes
            new_lut_indexes = IndexesType()
            for ind, vals in list(l_LUTXMLHandler.get_lut_indexes().items()):
                if ind == "Solar_Zenith_Angle_Indexes":
                    new_lut_indexes.set_Solar_Zenith_Angle_Indexes(vals)
                elif ind == "Relative_Azimuth_Angle_Indexes":
                    new_lut_indexes.set_Relative_Azimuth_Angle_Indexes(vals)
                elif ind == "Altitude_Indexes":
                    new_lut_indexes.set_Altitude_Indexes(vals)
                elif ind == "AOT_Indexes":
                    new_lut_indexes.set_AOT_Indexes(vals)
                elif ind == "TOA_Reflectance_Indexes":
                    new_lut_indexes.set_TOA_Reflectance_Indexes(vals)
                elif ind == "Zenithal_Angle_Indexes":
                    new_lut_indexes.set_Zenithal_Angle_Indexes(vals)
                elif ind == "View_Zenith_Angle_Indexes":
                    new_lut_indexes.set_View_Zenith_Angle_Indexes(vals)
                elif ind == "Reflectance_Ratio_Indexes":
                    new_lut_indexes.set_Reflectance_Ratio_Indexes(vals)
                else:
                    raise MajaDataException("Unhandled lut index type : " +
                                            ind)

            # Initialize the LookUpTable reader
            # --------------------------------------------
            # Set the list of minilut filenames to the mapReader
            l_lutmap = LUTMap()
            l_JdayRef = date_utils.get_julianday_as_int(
                date_utils.get_datetime_from_yyyymmdd(
                    p_PluginBase.ReferenceDate))

            for f in l_L2P_LTC_ListOfFilenames:
                # Generate the LUT Map
                # --------------------------------------------
                l_jday = maja_utils.get_integer_jday_from_filename(f)
                if l_jday < l_JdayRef:
                    raise MajaDataException(
                        "Cannot fill the LookUpTable with filename <" + f +
                        "> detected at Julian Day " + l_jday +
                        " as it's prior to reference date " +
                        p_PluginBase.ReferenceDate + " (" + l_JdayRef + ").")
                l_jday = int(l_jday - l_JdayRef)
                LOGGER.debug("l_jday " + str(l_jday) + " from filename " + f)
                LOGGER.debug("l_JDayRef : " + str(l_JdayRef))
                # Add cr lut to map
                l_listoffile = List_Of_FilesType()
                l_listoffile.add_Relative_File_Path("LTC_Lut_" + str(l_jday) +
                                                    ".mha")
                file_utils.copy_file(
                    f,
                    os.path.join(working_dir,
                                 "LTC_Lut_" + str(l_jday) + ".mha"))
                l_lut = LUT(index=str(l_jday),
                            Indexes=new_lut_indexes,
                            List_Of_Files=l_listoffile)
                l_lutmap.add_LUT(l_lut)
            # Write down the lut map
            output = io.StringIO()
            output.write('<?xml version="1.0" ?>\n')

            l_lutmap.export(output,
                            0,
                            name_='LUTMap',
                            namespacedef_='',
                            pretty_print=True)
            l_lutmap_filename = os.path.join(working_dir, "LTC_LutMap.xml")
            with open(l_lutmap_filename, "w") as fh:
                fh.write(output.getvalue().replace("    ", "  "))
                LOGGER.info("Writed new gipp lutmap to " + l_lutmap_filename)

            output.close()
            self.dict_of_vals["LTCImage"] = l_lutmap_filename
    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 write_public_images(self, p_PublicDirectory, p_L2BaseFilename,
                            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_BandsDefinitions = self._plugin.BandsDefinitions

            l_ListOfL2Res = l_BandsDefinitions.ListOfL2Resolution
            l_NumberOfResolutions = len(l_ListOfL2Res)
            LOGGER.debug(
                "L2ImageFileWriterBase::Initialize Number of resolutions: " +
                str(l_NumberOfResolutions) + ".")
            # Not use the list of XS band because for Landsat8, the band B9 is not
            # selected in the L2 resolution (only in L2Coarse resolution)
            l_BaseL2FullFilename = os.path.join(p_PublicDirectory,
                                                p_L2BaseFilename)
            l_MASKSDirectory = os.path.join(p_PublicDirectory, "MASKS")
            l_BaseL2FullMASKSFilename = os.path.join(l_MASKSDirectory,
                                                     p_L2BaseFilename)
            file_utils.create_directory(l_MASKSDirectory)
            resol_QLK = 0
            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)

                l_NumberOfBands = len(l_ListOfBand)
                LOGGER.debug(
                    "L2ImageFileReader::Gen Public image file for the resolution "
                    + l_StrResolution + ".")
                l_grpSuffix = ""
                if l_NumberOfResolutions == 1:
                    l_grpSuffix = "XS"
                else:
                    l_grpSuffix = l_ListOfL2Res[resol]
                l_StrResolution = l_BandsDefinitions.ListOfL2Resolution[resol]

                # 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      ************************************************
                # *************************************************************************************************************

                # START WRITING SRE Image file DATA
                # Initialize the Scalar filter
                sre_pipeline = OtbPipelineManager()

                #Extract each channel for each file
                tmp_l2_filename_list = []
                tmp_l2_image_list = []
                tmp_sre_scaled = os.path.join(
                    working_dir,
                    "tmp_sre_multi_round_" + l_StrResolution + ".tif")
                param_scaled_sre = {
                    "im": self._sre_list[resol],
                    "coef": p_ReflectanceQuantificationValue,
                    "out": tmp_sre_scaled
                }
                scaled_sre_app = OtbAppHandler("RoundImage",
                                               param_scaled_sre,
                                               write_output=False)
                sre_pipeline.add_otb_app(scaled_sre_app)
                for i in range(l_NumberOfBands):
                    if resol == resol_QLK and (l_RedBandId == i
                                               or l_GreenBandId == i
                                               or l_BlueBandId == i):
                        tmp_sre_roi = os.path.join(
                            working_dir,
                            "tmp_sre_roi_" + l_ListOfBand[i] + ".tif")
                        tmp_sre_roi_app = extract_roi(self._sre_list[resol],
                                                      [i],
                                                      tmp_sre_roi,
                                                      write_output=False)
                        tmp_l2_image_list.append(
                            tmp_sre_roi_app.getoutput().get("out"))
                        tmp_l2_filename_list.append(tmp_sre_roi)
                        if l_RedBandId == i:
                            self._qckl_red_image = tmp_sre_roi
                        elif l_GreenBandId == i:
                            self._qckl_green_image = tmp_sre_roi
                        elif l_BlueBandId == i:
                            self._qckl_blue_image = tmp_sre_roi
                        sre_pipeline.add_otb_app(tmp_sre_roi_app)
                    tmp_sre_scaled_roi = os.path.join(
                        working_dir,
                        "tmp_sre_scaled_roi_" + l_ListOfBand[i] + ".tif")
                    tmp_sre_scaled_roi_app = extract_roi(
                        scaled_sre_app.getoutput().get("out"), [i],
                        tmp_sre_scaled_roi + ":int16",
                        write_output=False)
                    tmp_l2_image_list.append(
                        tmp_sre_scaled_roi_app.getoutput().get("out"))
                    sre_pipeline.add_otb_app(tmp_sre_scaled_roi_app)
                    tmp_l2_filename_list.append(
                        l_BaseL2FullFilename + "_SRE_" + l_ListOfBand[i] +
                        ".tif" + file_utils.
                        get_extended_filename_write_image_file_standard())

                # START WRITING FRE Image file DATA
                tmp_tgs_filename = os.path.join(
                    working_dir, "tmp_tgs_" + l_StrResolution + ".tif")
                tmp_stl_filename = os.path.join(
                    working_dir, "tmp_stl_" + l_StrResolution + ".tif")
                fre_pipeline = OtbPipelineManager()
                if p_EnvCorOption:
                    # Initialize the Scalar filter
                    tmp_fre_scaled = os.path.join(
                        working_dir,
                        "tmp_fre_multi_round_" + l_StrResolution + ".tif")
                    param_scaled_fre = {
                        "im": self._fre_list[resol],
                        "coef": p_ReflectanceQuantificationValue,
                        "out": tmp_fre_scaled
                    }
                    scaled_fre_app = OtbAppHandler("RoundImage",
                                                   param_scaled_fre,
                                                   write_output=False)
                    fre_pipeline.add_otb_app(scaled_fre_app)
                    # Extract each channel for each file
                    for i in range(l_NumberOfBands):
                        tmp_fre_roi = os.path.join(
                            working_dir,
                            "tmp_fre_roi_" + l_ListOfBand[i] + ".tif")
                        tmp_fre_roi_app = extract_roi(
                            scaled_fre_app.getoutput().get("out"), [i],
                            tmp_fre_roi + ":int16",
                            write_output=False)
                        tmp_l2_image_list.append(
                            tmp_fre_roi_app.getoutput().get("out"))
                        fre_pipeline.add_otb_app(tmp_fre_roi_app)
                        tmp_l2_filename_list.append(
                            l_BaseL2FullFilename + "_FRE_" + l_ListOfBand[i] +
                            ".tif" + file_utils.
                            get_extended_filename_write_image_file_standard())
                    #Add tgs and stl also provided by the envcorr
                    tmp_l2_image_list.append(self._tgs_list[resol])
                    tmp_l2_filename_list.append(tmp_tgs_filename)
                    tmp_l2_image_list.append(self._stl_list[resol])
                    tmp_l2_filename_list.append(tmp_stl_filename)

                # 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)
                atb_pipeline = OtbPipelineManager()
                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) + ":" + "im1b1*" +
                    str(p_VAPQuantificationValue),
                    "out":
                    tmp_vap
                }
                vap_scal_app = OtbAppHandler("BandMathDouble",
                                             param_bandmath_vap,
                                             write_output=False)
                atb_pipeline.add_otb_app(vap_scal_app)

                tmp_round_vap = os.path.join(
                    working_dir, "tmp_vap_round_" + l_StrResolution + ".tif")
                param_round_vap = {
                    "im": vap_scal_app.getoutput().get("out"),
                    "out": tmp_round_vap
                }
                vap_round_app = OtbAppHandler("RoundImage",
                                              param_round_vap,
                                              write_output=False)
                atb_pipeline.add_otb_app(vap_round_app)
                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) + ":" + "im1b1*" +
                    str(p_AOTQuantificationValue),
                    "out":
                    tmp_aot
                }
                aot_scal_app = OtbAppHandler("BandMathDouble",
                                             param_bandmath_aot,
                                             write_output=False)
                atb_pipeline.add_otb_app(aot_scal_app)
                tmp_round_aot = os.path.join(
                    working_dir, "tmp_aot_round_" + l_StrResolution + ".tif")
                param_round_aot = {
                    "im": aot_scal_app.getoutput().get("out"),
                    "out": tmp_round_aot
                }
                aot_round_app = OtbAppHandler("RoundImage",
                                              param_round_aot,
                                              write_output=False)
                atb_pipeline.add_otb_app(aot_round_app)
                atb_filename = l_BaseL2FullFilename + "_ATB_" + l_grpSuffix + ".tif"
                param_atb_concat = {
                    "il": [
                        vap_round_app.getoutput().get("out"),
                        aot_round_app.getoutput().get("out")
                    ],
                    "out":
                    atb_filename + ":uint8" + file_utils.
                    get_extended_filename_write_image_file_standard()
                }

                atb_binconcat_app = OtbAppHandler("ConcatenateImages",
                                                  param_atb_concat,
                                                  write_output=False)
                #tmp_l2_image_list.append(atb_binconcat_app.getoutput().get("out"))
                #tmp_l2_filename_list.append(atb_filename)
                atb_pipeline.add_otb_app(atb_binconcat_app)

                # START WRITING IAB MASK
                iab_pipeline = OtbPipelineManager()
                # Create the image list
                tmp_iab = os.path.join(
                    working_dir, "tmp_iab_concat_" + l_StrResolution + ".tif")
                param_iab_concat = {
                    "il":
                    [self._l2iwcmasklist[resol], self._l2taomasklist[resol]],
                    "out": tmp_iab + ":uint8"
                }
                tmp_iab_concat_app = OtbAppHandler("ConcatenateImages",
                                                   param_iab_concat,
                                                   write_output=False)
                iab_pipeline.add_otb_app(tmp_iab_concat_app)
                iab_filename = l_BaseL2FullMASKSFilename + "_IAB_" + l_grpSuffix + ".tif"
                param_iab_binconcat = {
                    "im":
                    tmp_iab_concat_app.getoutput().get("out"),
                    "out":
                    iab_filename + ":uint8" + file_utils.
                    get_extended_filename_write_image_file_standard()
                }
                iab_binconcat_app = OtbAppHandler("BinaryConcatenate",
                                                  param_iab_binconcat,
                                                  write_output=False)
                iab_pipeline.add_otb_app(iab_binconcat_app)
                write_images([
                    atb_binconcat_app.getoutput().get("out"),
                    iab_binconcat_app.getoutput().get("out")
                ], [atb_filename, iab_filename])

                #tmp_l2_image_list.append(iab_binconcat_app.getoutput().get("out"))
                #tmp_l2_filename_list.append(iab_filename)

                # START WRITING EDG Image file DATA
                tmp_l2_image_list.append(self._l2edgimagelist[resol])
                tmp_l2_filename_list.append(l_BaseL2FullMASKSFilename +
                                            "_EDG_" + l_grpSuffix + ".tif")
                #--------------------------
                # Write all the images at L2 Reso
                write_images(tmp_l2_image_list, tmp_l2_filename_list)
                atb_pipeline.free_otb_app()
                iab_pipeline.free_otb_app()
                fre_pipeline.free_otb_app()
                sre_pipeline.free_otb_app()
                # --------------------------

                #Caching of cloud images needed for MG2
                l_cm2_index = p_CLDCoreAlgorithmsMapBand[CLOUD_MASK_ALL_CLOUDS]
                l_shadows_index = p_CLDCoreAlgorithmsMapBand[
                    CLOUD_MASK_SHADOWS]
                l_shadvar_index = p_CLDCoreAlgorithmsMapBand[
                    CLOUD_MASK_SHADVAR]

                # START WRITING MG2 Image file DATA
                l_mg2_image_list = []
                mg2_pipeline = OtbPipelineManager()
                # Connect the WAM image
                wat_resampled = os.path.join(
                    working_dir, "wat_resampled_" + l_StrResolution + ".tif")
                app_resample_wat = resample(self._wasimage,
                                            self._dtm.ALTList[resol],
                                            wat_resampled,
                                            method=OtbResampleType.LINEAR,
                                            threshold=0.25,
                                            write_output=False)
                l_mg2_image_list.append(
                    app_resample_wat.getoutput().get("out"))
                mg2_pipeline.add_otb_app(app_resample_wat)
                # Extract the CM2 cloud mask
                l_mg2_image_list.append(self._l2cldlist[resol][l_cm2_index])

                #Connect the SNW image if any
                if self._cld_snow is not None:
                    LOGGER.debug(
                        "Snow mask has been successfully computed, adding it to the MG2"
                    )
                    snw_resampled = os.path.join(
                        working_dir,
                        "snw_resampled_" + l_StrResolution + ".tif")
                    app_resample_snw = resample(self._cld_snow,
                                                self._dtm.ALTList[resol],
                                                snw_resampled,
                                                method=OtbResampleType.LINEAR,
                                                threshold=0.25,
                                                write_output=False)
                    l_mg2_image_list.append(
                        app_resample_snw.getoutput().get("out"))
                    mg2_pipeline.add_otb_app(app_resample_snw)
                else:
                    # Add a constant mask
                    tmp_constant_filename = os.path.join(
                        working_dir, "Const_shd_masks.tif")
                    cst_snw_app = constant_image(self._dtm.ALTList[resol],
                                                 0,
                                                 tmp_constant_filename +
                                                 ":uint8",
                                                 write_output=False)
                    l_mg2_image_list.append(cst_snw_app.getoutput().get("out"))
                    mg2_pipeline.add_otb_app(cst_snw_app)

                # Connect the shadow or mask
                tmp_shador_bandmath = os.path.join(
                    working_dir,
                    "tmp_shador_bandmath_" + l_StrResolution + ".tif")
                tmp_band_math_app = band_math([
                    self._l2cldlist[resol][l_shadows_index],
                    self._l2cldlist[resol][l_shadvar_index]
                ],
                                              "im1b1 || im2b1",
                                              tmp_shador_bandmath + ":uint8",
                                              write_output=False)
                l_mg2_image_list.append(
                    tmp_band_math_app.getoutput().get("out"))
                mg2_pipeline.add_otb_app(tmp_band_math_app)
                # 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,
                                            method=OtbResampleType.LINEAR,
                                            threshold=0.25,
                                            write_output=False)
                l_mg2_image_list.append(
                    app_resample_hid.getoutput().get("out"))
                mg2_pipeline.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,
                                            method=OtbResampleType.LINEAR,
                                            threshold=0.25,
                                            write_output=False)
                mg2_pipeline.add_otb_app(app_resample_shd)
                l_mg2_image_list.append(
                    app_resample_shd.getoutput().get("out"))
                if p_EnvCorOption:
                    # Append STL
                    l_mg2_image_list.append(tmp_stl_filename)
                    # Append TGS
                    l_mg2_image_list.append(tmp_tgs_filename)
                else:
                    # Append STL
                    l_mg2_image_list.append(self._stl_list[resol])
                    # Append TGS
                    l_mg2_image_list.append(self._tgs_list[resol])
                # Concatenate all
                tmp_mg2 = os.path.join(
                    working_dir, "tmp_mg2_concat_" + l_StrResolution + ".tif")
                param_mg2_concat = {"il": l_mg2_image_list, "out": tmp_mg2}
                tmp_mg2_concat_app = OtbAppHandler("ConcatenateImages",
                                                   param_mg2_concat,
                                                   write_output=False)
                param_mg2_binconcat = {
                    "im":
                    tmp_mg2_concat_app.getoutput().get("out"),
                    "out":
                    l_BaseL2FullMASKSFilename + "_MG2_" + l_grpSuffix +
                    ".tif" + ":uint8" + file_utils.
                    get_extended_filename_write_image_file_standard()
                }
                mg2_binconcat_app = OtbAppHandler("BinaryConcatenate",
                                                  param_mg2_binconcat,
                                                  write_output=True)
                mg2_pipeline.add_otb_app(mg2_binconcat_app)
                mg2_pipeline.free_otb_app()

                # START WRITING SAT Image file DATA
                # TODO Create the writer with test on number of bands
                param_sat_binconcat = {
                    "im":
                    self._l2satimagelist[resol],
                    "out":
                    l_BaseL2FullMASKSFilename + "_SAT_" + l_grpSuffix +
                    ".tif" + ":uint8" + file_utils.
                    get_extended_filename_write_image_file_standard()
                }
                sat_binconcat_app = OtbAppHandler("BinaryConcatenate",
                                                  param_sat_binconcat,
                                                  write_output=True)

                # START WRITING PIX Image file DATA
                if "PIXImages" in self._l1_image_info.MuscateData:
                    LOGGER.debug(
                        "The L1 product have 'Aberrant_Pixels' masks. There are writed in the L2 out product..."
                    )
                    otb_file_utils.otb_copy_image_to_file(
                        self._l2piximagelist[resol],
                        l_BaseL2FullMASKSFilename + "_PIX_" + l_grpSuffix +
                        ".tif")
                else:
                    LOGGER.debug("No PIX node detected to write")

                # START WRITING USI Image file DATA
                if "Node_Useful_Image" in self._l1_image_info.MuscateData:
                    LOGGER.debug(
                        "The L1 product have 'Useful_Image' files. There are copied in the L2 out product..."
                    )

                    # Write the USI in the Xml file, in the Useful_Imagenode !
                    l_XPathRootUSI_In = "//Mask[Mask_Properties/NATURE='Useful_Image']/Mask_File_List/MASK_FILE[@group_id='{}']"
                    xnodes_in = xml_tools.get_all_values(
                        self._l1_image_info.MuscateData["Node_Useful_Image"],
                        l_XPathRootUSI_In.format(l_grpSuffix))
                    # Get the path in the xml product filename
                    lPath = os.path.dirname(self._l1_image_info.HeaderFilename)
                    for node_in in xnodes_in:
                        l_FullPathFilename = os.path.join(lPath, node_in.text)
                        # Expand threshold the file
                        usi_resampled = l_BaseL2FullMASKSFilename + "_USI_" + l_grpSuffix + ".tif"\
                                        + file_utils.get_extended_filename_write_mask_file_muscate()
                        resample(l_FullPathFilename,
                                 self._dtm.ALTList[resol],
                                 usi_resampled,
                                 threshold=0.25,
                                 method=OtbResampleType.LINEAR,
                                 write_output=True)

                    else:
                        LOGGER.debug(
                            "No 'Useful_Image' mask detected in the L1 product."
                        )
                # Fin si manage USI

                # START WRITING DFP Image file DATA (=DFP in MUSCATE) #TODO
                if self._l2dfpimagelist is not None:
                    param_dfp_binconcat = {
                        "im":
                        self._l2dfpimagelist[resol],
                        "out":
                        l_BaseL2FullMASKSFilename + "_DFP_" + l_grpSuffix +
                        ".tif"
                    }
                    dfp_binconcat_app = OtbAppHandler("BinaryConcatenate",
                                                      param_dfp_binconcat,
                                                      write_output=True)
                else:
                    LOGGER.debug("DFP Masks not available.")

                # START WRITING CLM (CLD) Image file DATA
                # Connect the CLD image
                # Connect the CLD image
                # -------------------------------------
                l_cld_uses_filenames = False
                for f in self._l2cldlist[resol]:
                    if not otb_is_swig_pointer(f) and os.path.exists(f):
                        l_cld_uses_filenames = True
                self.write_cld_image(self._l2cldlist[resol],
                                     p_CLDDataBandsSelected,
                                     l_BaseL2FullMASKSFilename + "_CLM_" +
                                     l_grpSuffix + ".tif",
                                     use_filenames=l_cld_uses_filenames)
                LOGGER.debug("Writing L2 resolution image done !")