コード例 #1
0
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Rain detection computation start")
        rain_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "RainFlagProc_", do_always_remove=True)
        image_list = [
            dict_of_output.get(constants.CLOUD_MASK_ALL_CLOUDS),
            dict_of_output.get("WaterMask")
        ]
        image_list.append(
            dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"))
        image_list.append(dict_of_input.get("L2Reader").get_value("WASImage"))
        image_list.append(dict_of_input.get("L2Reader").get_value("NDTImage"))
        exp = "im1b1 || im2b1 || im3b1 || im4b1 || im5b1"
        or_all = os.path.join(rain_working, "OrAll.tif")

        if dict_of_input.get("Params").get("SnowBandAvailable"):
            image_list.append(dict_of_output.get("cld_snow"))
            exp = exp + " || im6b1"

        param_orall = {"il": image_list, "exp": exp, "out": or_all}
        orall_app = OtbAppHandler("BandMath", param_orall, write_output=False)

        tocr_extract = os.path.join(rain_working, "Tocr_Extract.tif")
        tocrextract_image_app = extract_roi(
            dict_of_output.get("RayleighIPTOCR"),
            [dict_of_input.get("Params").get("WaterBandIndex_DateD")],
            tocr_extract,
            write_output=False)
        rcr_extract = os.path.join(rain_working, "Rcr_Extract.tif")

        rcrextract_image_app = extract_roi(
            dict_of_input.get("L2Reader").get_value("RCRImage"),
            [dict_of_input.get("Params").get("WaterBandIndex_DateDm1")],
            rcr_extract,
            write_output=False)
        param_rcrstat = {
            "im": rcrextract_image_app.getoutput()["out"],
            "mask": orall_app.getoutput()["out"],
        }
        rcrstat_app = OtbAppHandler("Stats", param_rcrstat, write_output=False)
        rcr_mean = rcrstat_app.getoutput()["mean"]

        param_tocrstat = {
            "im": tocrextract_image_app.getoutput()["out"],
            "mask": orall_app.getoutput()["out"],
        }
        tocrstat_app = OtbAppHandler("Stats",
                                     param_tocrstat,
                                     write_output=False)
        tocr_mean = tocrstat_app.getoutput()["mean"]

        diffMean = rcr_mean - tocr_mean

        if diffMean > dict_of_input.get("L2COMM").get_value_f(
                "ReflectanceDecreaseThreshold"):
            dict_of_output["RainFlag"] = True
        else:
            dict_of_output["RainFlag"] = False
コード例 #2
0
    def generate_l2_toa_images(self, l_ListOfL2Resolution, working_dir):

        for l1res in range(len(l_ListOfL2Resolution)):
            curL1Res = l_ListOfL2Resolution[l1res]
            dtm = self._dem.ALTList[l1res]
            l_ListOfL2BandCode = self._plugin.BandsDefinitions.get_list_of_l2_band_code(
                curL1Res)
            LOGGER.debug("Read the nb bands" + str(len(l_ListOfL2BandCode)) +
                         " of the TOA images...")
            l_l1bandidx = [
                self._plugin.BandsDefinitions.get_band_id_in_l1(b)
                for b in l_ListOfL2BandCode
            ]
            tmp_toa_resample = os.path.join(working_dir,
                                            "tmp_toa_resample.tif")
            app_toa_resample = resample(self._l1toaimagelist[l1res],
                                        dtm,
                                        tmp_toa_resample,
                                        OtbResampleType.LINEAR_WITH_RADIUS,
                                        write_output=False)
            self._l2toa_pipeline.add_otb_app(app_toa_resample)
            # L2TOA has less band than L1 so extract
            tmp_toa_roi = os.path.join(
                working_dir, "L2TOAImageList_{}.tif".format(curL1Res))
            l2toa_roi_app = extract_roi(
                app_toa_resample.getoutput().get("out"),
                l_l1bandidx,
                tmp_toa_roi,
                write_output=False)
            self._l2toa_pipeline.add_otb_app(l2toa_roi_app)
            self._l2toaimagelist.append(l2toa_roi_app.getoutput().get("out"))
コード例 #3
0
    def write_quicklook_image(
            filename, pInputImage, p_WritePublicProduct, p_QuicklookRedBandId,
            p_QuicklookGreenBandId, p_QuicklookBlueBandId,
            p_QuicklookMinReflRedBand, p_QuicklookMaxReflRedBand,
            p_QuicklookMinReflGreenBand, p_QuicklookMaxReflGreenBand,
            p_QuicklookMinReflBlueBand, p_QuicklookMaxReflBlueBand,
            p_RealL2NoData, dtm, working_dir):

        # ====================================================================
        # IF PUBLIC PART OF L2 PRODUCT IS WRITTEN
        # ====================================================================
        if p_WritePublicProduct:
            LOGGER.debug("Start QLK wiring...")
            # Select the RGB Band for the QuickLook
            tmp_qlk_red = os.path.join(working_dir, "tmp_qck_red.tif")
            tmp_qlk_green = os.path.join(working_dir, "tmp_qck_green.tif")
            tmp_qlk_blue = os.path.join(working_dir, "tmp_qck_blue.tif")
            tmp_qlk_red_app = extract_roi(pInputImage, [p_QuicklookRedBandId],
                                          tmp_qlk_red,
                                          write_output=False)
            tmp_qlk_green_app = extract_roi(pInputImage,
                                            [p_QuicklookGreenBandId],
                                            tmp_qlk_green,
                                            write_output=False)
            tmp_qlk_blue_app = extract_roi(pInputImage,
                                           [p_QuicklookBlueBandId],
                                           tmp_qlk_blue,
                                           write_output=False)

            L2ImageWriterBase.write_quicklook_image_from_files(
                filename, p_WritePublicProduct,
                tmp_qlk_red_app.getoutput().get("out"),
                tmp_qlk_green_app.getoutput().get("out"),
                tmp_qlk_blue_app.getoutput().get("out"),
                p_QuicklookMinReflRedBand, p_QuicklookMaxReflRedBand,
                p_QuicklookMinReflGreenBand, p_QuicklookMaxReflGreenBand,
                p_QuicklookMinReflBlueBand, p_QuicklookMaxReflBlueBand,
                p_RealL2NoData, dtm, working_dir)
コード例 #4
0
    def GetViewingGrids(self, band, nbL1Bands, vieHRef, satFilename,
                        zoneFilenames, nodataFilename, listOfZone,
                        view_angle_col_step, view_angle_row_step,
                        viewing_angles_zenith, viewing_angles_azimuth,
                        working):
        LOGGER.debug("GetViewingGrids - Band: " + band)

        dtm_coarse = self._dem.ALC
        l_BandsDefinitions = self._plugin.BandsDefinitions
        l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(band)

        # ---- Viewing angle Grid --------------------------------------------------------------------
        l_VieAnglesGridList = []
        l_nbDetectors = len(viewing_angles_azimuth)
        # Detector loop
        LOGGER.debug("For each detectors (nb=%s) ...", l_nbDetectors)
        view_resamp_pipeline = OtbPipelineManager()
        for detId in range(len(viewing_angles_azimuth)):
            # Generate an image with the list of viewing angle values set in the header file
            det = listOfZone[detId]
            LOGGER.debug("ZenithViewingAngles for band " + band + " and det " +
                         det + " ,  nb values: " +
                         str(len(viewing_angles_zenith[detId])))
            LOGGER.debug(viewing_angles_zenith[detId])
            LOGGER.debug("AzimuthViewingAngles " + band + " and det " + det +
                         " nb values: " +
                         str(len(viewing_angles_azimuth[detId])))
            LOGGER.debug(viewing_angles_azimuth[detId])
            output_filename = "/tmp/internal_angles_test_{}_{}.xml".format(
                band, det)
            LOGGER.debug("Angles output_filename : %s", output_filename)
            writer = MajaInternalXmlInputAngles(viewing_angles_zenith[detId],
                                                viewing_angles_azimuth[detId],
                                                view_angle_col_step,
                                                view_angle_row_step,
                                                output_filename)
            writer.write()
            if not os.path.exists(output_filename):
                raise MajaExceptionPluginSentinel2Muscate(
                    "File does not exist " + output_filename)

            viewing_grid_filename = os.path.join(
                working, "viewing_grid_{}_{}.tif".format(det, band))

            # angle_list_to_image()
            viewing_angle_app = angle_list_to_image(dtm_coarse,
                                                    output_filename,
                                                    viewing_grid_filename,
                                                    extrapolation=False,
                                                    write_output=False)
            view_resamp_pipeline.add_otb_app(viewing_angle_app)
            # Expand at L2Coarse.
            viewing_grid_resamp_filename = os.path.join(
                working, "viewing_grid_resamp_{}_{}.tif".format(detId, band))
            viewing_grid_resamp_app = resample(
                viewing_angle_app.getoutput().get("out"),
                dtm_coarse,
                viewing_grid_resamp_filename,
                OtbResampleType.LINEAR,
                write_output=False)
            view_resamp_pipeline.add_otb_app(viewing_grid_resamp_app)
            # add images in a list
            l_VieAnglesGridList.append(
                viewing_grid_resamp_app.getoutput().get("out"))
            # end detector loop

        # Generate the angle images using the zone (detector) mask
        LOGGER.debug(
            "Start ConcatenatePerZoneVectorImageFilter for band id [" + band +
            "]...")

        # Concatenate all the detectors
        viewing_concat_filename = os.path.join(
            working, "viewing_concat_{}.tif".format(band))
        param_concat_perzone = {
            "mask": self._zonemasksublist[l1BandIdx],
            "il": l_VieAnglesGridList,
            "zonelist": listOfZone,
            "out": viewing_concat_filename
        }
        concat_perzone_app = OtbAppHandler("ConcatenatePerZone",
                                           param_concat_perzone,
                                           write_output=False)
        # Multiply by reference altitude
        viewing_grid_mult_filename = os.path.join(
            working, "viewing_grid_mult_{}.tif".format(band))
        param_scaled_solar = {
            "im": concat_perzone_app.getoutput().get("out"),
            "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,
                                        write_output=False)
            param_stats = {
                "im": tmp_azi_image.getoutput().get("out"),
                "exclude": 1,
                "mask": self._zonemasksublist[l1BandIdx],
                "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_app = extract_roi(l_VieAnglesGridList[d], [0],
                                      tmp_zen,
                                      write_output=False)
            param_stats = {
                "im": tmp_zen_app.getoutput().get("out"),
                "exclude": 1,
                "mask": self._zonemasksublist[l1BandIdx],
                "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]] = []
            if l_zone not in self._meanAzimuthMap:
                self._meanAzimuthMap[listOfZone[d]] = []
            self._meanZenithMap[listOfZone[d]].append(
                str(l_ViewingAngleMeanDeg[0]))
            self._meanAzimuthMap[listOfZone[d]].append(
                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.")
        view_resamp_pipeline.free_otb_app()
コード例 #5
0
    def generate_mask_rasters(self, satPixFileNames, defectivPixFileNames,
                              zoneMaskFileNames, noDataMaskFileNames,
                              p_ListOfTOABandCode, working):
        """

        :param satPixFileNames: ListOfStrings
        :param defectivPixFileNames: ListOfStrings
        :param zoneMaskFileNames: ListOfStrings
        :param noDataMaskFileNames: ListOfStrings
        :param p_ListOfTOABandCode: ListOfStrings
        :param working: string
        :return:
        """

        # *******************************************************************************************************
        # Generate mask rasters by rasterizing the gml mask per L2 resolution per band
        # *******************************************************************************************************
        LOGGER.info("Starting 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)
        l_NbL1Band = len(defectivPixFileNames)
        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")
        #constant_image(dtm_coarse, 0, tmp_constant_filename, write_output=True)

        # Initialize the L2 Elements
        for l2res in range(l_NbL2Res):
            self._l2defectmasklist.append([])
            self._l2zonemasklist.append([])
            self._l2satimagelist.append([])
            self._l2dfpimagelist.append(None)
            self._l2zoneimagelist.append(None)
        # Init the coarse elements
        for coarseband in p_ListOfTOABandCode:
            self._satmasksublist.append(None)
            self._nodatamasksublist.append(None)
            self._zonemasksublist.append(None)
            self._l2satmasklist.append(None)

        # Loop on L1Res
        for l1res in range(l_NbL1Res):
            # Current resolution: "R1" or "R2" or "R3"
            curL1Res = l_ListOfL1Resolution[l1res]

            # 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[-1])

            # 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
            l_strDefectFileNameRef = defectivPixFileNames[firstBandIdx]
            l_isDefectSameFilesForBands = True
            l_strSatFileNameRef = satPixFileNames[firstBandIdx]
            l_isSatSameFilesForBands = True
            l_strNoDataFileNameRef = noDataMaskFileNames[firstBandIdx]
            l_isNoDataSameFilesForBands = 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 = l_BandsDefinitions.get_band_id_in_l1(l_StrBandIdL1)
                # Filenameverify
                if l_strDefectFileNameRef != defectivPixFileNames[l1BandIdx]:
                    l_isDefectSameFilesForBands = False
                if l_strSatFileNameRef != satPixFileNames[l1BandIdx]:
                    l_isSatSameFilesForBands = False
                if l_strNoDataFileNameRef != noDataMaskFileNames[l1BandIdx]:
                    l_isNoDataSameFilesForBands = False
            LOGGER.debug("l_isDefectSameFilesForBands = " +
                         str(l_isDefectSameFilesForBands))
            LOGGER.debug("l_isSatSameFilesForBands = " +
                         str(l_isSatSameFilesForBands))
            LOGGER.debug("l_isNoDataSameFilesForBands = " +
                         str(l_isSatSameFilesForBands))

            # Defective PIX (only in L2 resolution)
            if l_isDefectSameFilesForBands:
                if curL1Res in l_ListOfL2Resolution:
                    l_StrBandIdL1 = listOfL1Bands[0]
                    l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(
                        l_StrBandIdL1)
                    LOGGER.debug("l_StrBandIdL1 : ")
                    LOGGER.debug(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 + ":uint8",
                        "nbcomp": nbL1Bands
                    }
                    dfp_mask_app = OtbAppHandler("BinaryToVector",
                                                 param_bintovec_dfp,
                                                 write_output=False)
                    self._l2_dfp_pipeline.add_otb_app(dfp_mask_app)
                    self._l2dfpimagelist[l_ListOfL2Resolution.index(
                        curL1Res)] = dfp_mask_app.getoutput().get("out")
            else:
                raise MajaExceptionPluginSentinel2Muscate(
                    "Product format not supported : not the same file for band on PIX"
                )

            # SAT Mask generation
            if l_isSatSameFilesForBands:
                # Sat Masks generation
                l_StrBandIdL1 = listOfL1Bands[0]
                l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(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 + ":uint8",
                    "nbcomp": nbL1Bands
                }
                sat_mask_app = OtbAppHandler("BinaryToVector",
                                             param_bintovec_dfp,
                                             write_output=True)
                # self._l2_sat_pipeline.add_otb_app(sat_mask_app)
                if curL1Res in l_ListOfL2Resolution:
                    self._l2satmasklist[l_ListOfL2Resolution.index(
                        curL1Res)] = sat_mask_app.getoutput().get("out")
                tmp_sat_resample = os.path.join(
                    working,
                    "tmp_extract_roi_sat_resample_{}.tif".format(curL1Res))
                sat_resamp_app = resample(sat_mask_app.getoutput().get("out"),
                                          dtm_coarse,
                                          tmp_sat_resample,
                                          OtbResampleType.LINEAR,
                                          write_output=False)
                self._sub_sat_pipeline.add_otb_app(sat_resamp_app)
                for l1band in listOfL1Bands:
                    l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l1band)
                    tmp_sat_roi = os.path.join(
                        working, "tmp_extract_roi_sat_{}.tif".format(l1band))
                    tmp_sat_roi_app = extract_roi(
                        sat_resamp_app.getoutput().get("out"), [
                            self.m_headerHandler.get_l1_sat_image_index(
                                l1BandIdx) - 1
                        ],
                        tmp_sat_roi,
                        write_output=False)
                    self._sub_sat_pipeline.add_otb_app(tmp_sat_roi_app)
                    self._satmasksublist[
                        l1BandIdx] = tmp_sat_roi_app.getoutput().get("out")
            else:
                raise MajaExceptionPluginSentinel2Muscate(
                    "Product format not supported : not the same file for band on SAT"
                )

            # No_data mask at L2 coarse
            if l_isNoDataSameFilesForBands:
                ndt_mask = os.path.join(
                    working, "L2_NDT_VecMasks_{}.tif".format(curL1Res))
                l_StrBandIdL1 = listOfL1Bands[0]
                l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l_StrBandIdL1)
                l_NoDataFilename = noDataMaskFileNames[l1BandIdx]
                param_bintovec_ndt = {
                    "im": l_NoDataFilename,
                    "out": ndt_mask + ":uint8",
                    "nbcomp": nbL1Bands
                }
                ndt_mask_app = OtbAppHandler("BinaryToVector",
                                             param_bintovec_ndt,
                                             write_output=False)
                self._subedg_pipeline.add_otb_app(ndt_mask_app)
                tmp_ndt_resample = os.path.join(
                    working,
                    "tmp_extract_roi_ndt_resample_{}.tif".format(curL1Res))
                ndt_resamp_app = resample(ndt_mask_app.getoutput().get("out"),
                                          dtm_coarse,
                                          tmp_ndt_resample,
                                          OtbResampleType.LINEAR,
                                          write_output=False)
                self._subedg_pipeline.add_otb_app(ndt_resamp_app)
                for l1band in listOfL1Bands:
                    l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l1band)
                    tmp_ndt_roi = os.path.join(
                        working, "tmp_extract_roi_ndt_{}.tif".format(l1band))
                    tmp_ndt_roi_app = extract_roi(
                        ndt_resamp_app.getoutput().get("out"), [
                            self.m_headerHandler.get_l1_ndt_image_index(
                                l1BandIdx) - 1
                        ],
                        tmp_ndt_roi,
                        write_output=False)
                    self._subedg_pipeline.add_otb_app(tmp_ndt_roi_app)
                    self._nodatamasksublist[
                        l1BandIdx] = tmp_ndt_roi_app.getoutput().get("out")
            else:
                raise MajaExceptionPluginSentinel2Muscate(
                    "Product format not supported : not the same file for band on NoData"
                )

            # Detectors FootPrint Masks Generation
            # Get all the detectors files realted to this resolution
            # Test if they all refers to the same files
            tmp_res_det_filenames = None
            all_same_det_for_bands = 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 = l_BandsDefinitions.get_band_id_in_l1(l_StrBandIdL1)
                LOGGER.debug(
                    "Sentinel2MuscateL1ImageFileReader::GenerateMaskRasters: CurrentResol = %s, reading the "
                    "BandId L1 (associated) <%s> with index <%s>.", curL1Res,
                    l_StrBandIdL1, l1BandIdx)
                # Detectors FootPrint Masks
                l_bandDetFnames = zoneMaskFileNames[l1BandIdx]
                if tmp_res_det_filenames is not None:
                    if len(tmp_res_det_filenames) != len(l_bandDetFnames):
                        all_same_det_for_bands = False
                        break
                else:
                    tmp_res_det_filenames = l_bandDetFnames
            #Construct file and det number list
            tmp_list_of_detf_filenames = []
            tmp_list_of_detf_num = []
            tmp_list_of_band_idx = []
            if all_same_det_for_bands:
                l_ListOfZone = self.m_headerHandler.get_list_of_zones(
                    listOfL1Bands[0])
                nbDetector = len(l_ListOfZone)
                # For each detector of the current band
                for det in range(nbDetector):
                    tmp_list_of_detf_filenames.append(
                        tmp_res_det_filenames[l_ListOfZone[det]])
                    tmp_list_of_detf_num.append(str(int(l_ListOfZone[det])))
            else:
                #some bands are not in all det rare case
                tmp_det_info_map = {}
                for l_BandIdxL1 in range(len(listOfL1Bands)):
                    # Get the L1 band index associated to the L2 band code
                    l_StrBandIdL1 = listOfL1Bands[l_BandIdxL1]
                    l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(
                        l_StrBandIdL1)
                    LOGGER.debug(
                        "Sentinel2MuscateL1ImageFileReader::GenerateMaskRasters: CurrentResol = %s, reading the "
                        "BandId L1 (associated) <%s> with index <%s>.",
                        curL1Res, l_StrBandIdL1, l1BandIdx)
                    # Detectors FootPrint Masks
                    l_bandDetFnames = zoneMaskFileNames[l1BandIdx]
                    l_ListOfZone = self.m_headerHandler.get_list_of_zones(
                        l_StrBandIdL1)
                    nbDetector = len(l_ListOfZone)
                    # For each detector of the current band
                    for det in range(nbDetector):
                        det_num = l_ListOfZone[det]
                        if det_num not in tmp_det_info_map.keys():
                            tmp_det_info_map[det_num] = (
                                zoneMaskFileNames[l1BandIdx][det_num],
                                [0] * len(listOfL1Bands))
                        tmp_det_info_map[det_num][1][l_BandIdxL1] = \
                            self.m_headerHandler.get_l1_dtf_image_index(l1BandIdx, det_num)[0]
                #once the info map is done
                for det in tmp_det_info_map.keys():
                    det_info = tmp_det_info_map[det]
                    tmp_list_of_detf_filenames.append(det_info[0])
                    tmp_list_of_detf_num.append(str(int(det)))
                    for x in det_info[1]:
                        tmp_list_of_band_idx.append(str(x))
            #create params
            detf_mask = os.path.join(working,
                                     "L1_DETF_Masks_{}.tif".format(curL1Res))
            param_dispacth_zone = {
                "il": tmp_list_of_detf_filenames,
                "out": detf_mask + ':uint8',
                "nbcomp": len(listOfL1Bands),
                "outvals": tmp_list_of_detf_num
            }
            #handle not all det/band case
            if not all_same_det_for_bands:
                param_dispacth_zone["outindexes"] = tmp_list_of_band_idx
            #Call the app
            dispatch_app = OtbAppHandler("DispatchZonesToVector",
                                         param_dispacth_zone,
                                         write_output=True)
            #This is the L2 output
            if curL1Res in l_ListOfL2Resolution:
                self._l2zoneimagelist[l_ListOfL2Resolution.index(
                    curL1Res)] = dispatch_app.getoutput().get("out")
            #extract the coarse bands
            for l1band in listOfL1Bands:
                l1BandIdx = l_BandsDefinitions.get_band_id_in_l1(l1band)
                tmp_zone_roi = os.path.join(
                    working, "tmp_extract_roi_ndt_{}.tif".format(l1band))
                tmp_zone_roi_app = extract_roi(
                    dispatch_app.getoutput().get("out"), [
                        self.m_headerHandler.get_l1_ndt_image_index(l1BandIdx)
                        - 1
                    ],
                    tmp_zone_roi,
                    write_output=False)
                self._subedg_pipeline.add_otb_app(tmp_zone_roi_app)
                tmp_zone_with_nodata = os.path.join(
                    working, "tmp_zone_with_nodata_{}".format(curL1Res))
                masterExpr = "( im1b1 > 0 ) ? im1b1 : -10000"
                param_bandmath_zone = {
                    "il": [tmp_zone_roi_app.getoutput().get("out")],
                    "exp": masterExpr,
                    "out": tmp_zone_with_nodata
                }
                zone_thresh_bandmath_app = OtbAppHandler("BandMathDouble",
                                                         param_bandmath_zone,
                                                         write_output=False)
                # LAIG-FA-MAC-1652-CNES : probleme reech detecteur en unsigned char : detecteur 1 devient 0.....
                tmp_zone_resample = os.path.join(
                    working,
                    "tmp_extract_roi_zone_resample_{}.tif".format(l1BandIdx))
                zone_resample_app = resample(
                    zone_thresh_bandmath_app.getoutput().get("out"),
                    dtm_coarse,
                    tmp_zone_resample,
                    OtbResampleType.LINEAR,
                    write_output=False)
                # threshold everything negative to 0
                Thresholdexpr = "im1b1 > 0 ? im1b1 : 0"
                tmp_zone_threshold = os.path.join(
                    working, "tmp_zone_threshold_{}".format(l1BandIdx))
                param_bandmath_zone_threshold = {
                    "il": [zone_resample_app.getoutput().get("out")],
                    "exp": Thresholdexpr,
                    "out": tmp_zone_threshold
                }
                zone_bandmath_threshold_app = OtbAppHandler(
                    "BandMathDouble",
                    param_bandmath_zone_threshold,
                    write_output=False)
                # Rounding
                tmp_zone_round = os.path.join(
                    working, "tmp_zone_round_{}".format(l1BandIdx))
                param_bandmath_zone_round = {
                    "im": zone_bandmath_threshold_app.getoutput().get("out"),
                    "out": tmp_zone_round
                }
                zone_round_app = OtbAppHandler("RoundImage",
                                               param_bandmath_zone_round,
                                               write_output=True)
                # Add to the official output
                self._zonemasksublist[l1BandIdx] = zone_round_app.getoutput(
                ).get("out")
                # Log current loop
                LOGGER.debug("band loop: " + str(l1BandIdx + 1) + " / " +
                             str(nbL1Bands) + " (" + curL1Res + ")")
            # band loop
            LOGGER.debug("band loop: END")
            self._l2_detf_pipeline.free_otb_app()
コード例 #6
0
    def write_quicklook_image(
            filename,
            pInputImage,
            p_WritePublicProduct,
            p_QuicklookRedBandId,
            p_QuicklookGreenBandId,
            p_QuicklookBlueBandId,
            p_QuicklookMinReflRedBand,
            p_QuicklookMaxReflRedBand,
            p_QuicklookMinReflGreenBand,
            p_QuicklookMaxReflGreenBand,
            p_QuicklookMinReflBlueBand,
            p_QuicklookMaxReflBlueBand,
            p_RealL2NoData,
            dtm,
            working_dir):

        # ====================================================================
        # IF PUBLIC PART OF L2 PRODUCT IS WRITTEN
        # ====================================================================
        if p_WritePublicProduct:
            LOGGER.debug("Start QLK wiring...")
            # Extract min and max reflectance value



            # RealVectorPixelType inputMin;
            #    RealVectorPixelType inputMax;
            #    inputMin.SetSize(3);
            #    inputMax.SetSize(3);
            #    inputMin[0] = static_cast<InternalRealVectorPixelType>(p_QuicklookMinReflRedBand);
            #    #DMGetDouble("QuicklookMinReflRedBand"));
            #    inputMin[1] = static_cast<InternalRealVectorPixelType>(p_QuicklookMinReflGreenBand);
            #    #DMGetDouble("QuicklookMinReflGreenBand"));
            #    inputMin[2] = static_cast<InternalRealVectorPixelType>(p_QuicklookMinReflBlueBand);
            #    #DMGetDouble("QuicklookMinReflBlueBand"));
            #    inputMax[0] = static_cast<InternalRealVectorPixelType>(p_QuicklookMaxReflRedBand);
            #    #DMGetDouble("QuicklookMaxReflRedBand"));
            #    inputMax[1] = static_cast<InternalRealVectorPixelType>(p_QuicklookMaxReflGreenBand);
            #    #DMGetDouble("QuicklookMaxReflGreenBand"));
            # inputMax[2] =
            # static_cast<InternalRealVectorPixelType>(p_QuicklookMaxReflBlueBand);
            #DMGetDouble("QuicklookMaxReflBlueBand"));

            # Select the RGB Band for the QuickLook
            tmp_qlk_red = os.path.join(working_dir, "tmp_qck_red.tif")
            tmp_qlk_green = os.path.join(working_dir, "tmp_qck_green.tif")
            tmp_qlk_blue = os.path.join(working_dir, "tmp_qck_blue.tif")
            tmp_qlk_red_app = extract_roi(pInputImage, [p_QuicklookRedBandId], tmp_qlk_red,write_output=False)
            tmp_qlk_green_app = extract_roi(pInputImage, [p_QuicklookGreenBandId], tmp_qlk_green,write_output=False)
            tmp_qlk_blue_app = extract_roi(pInputImage, [p_QuicklookBlueBandId], tmp_qlk_blue,write_output=False)

            L2ImageWriterBase.write_quicklook_image_from_files(filename,
                                                               p_WritePublicProduct,
                                                               tmp_qlk_red_app.getoutput().get("out"),
                                                               tmp_qlk_green_app.getoutput().get("out"),
                                                               tmp_qlk_blue_app.getoutput().get("out"),
                                                               p_QuicklookMinReflRedBand,
                                                               p_QuicklookMaxReflRedBand,
                                                               p_QuicklookMinReflGreenBand,
                                                               p_QuicklookMaxReflGreenBand,
                                                               p_QuicklookMinReflBlueBand,
                                                               p_QuicklookMaxReflBlueBand,
                                                               p_RealL2NoData,
                                                               dtm,
                                                               working_dir)
コード例 #7
0
    def write_public_images(
            self,
            p_L2ImageFilenamesProvider,
            p_ReflectanceQuantificationValue,
            p_AOTQuantificationValue,
            p_AOTNodataValue,
            p_VAPQuantificationValue,
            p_VAPNodataValue,
            p_CLDDataBandsSelected,
            p_CLDCoreAlgorithmsMapBand,
            p_WritePublicProduct,
            p_EnvCorOption,
            working_dir):
        # == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
        # IF PUBLIC PART OF L2 PRODUCT  IS WRITTEN
        # == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
        if p_WritePublicProduct:
            l_NumberOfResolutions = len(p_L2ImageFilenamesProvider.get_sre_headers())
            LOGGER.debug("L2ImageFileWriterBase::Initialize Number of resolutions: " + str(l_NumberOfResolutions) + ".")
            l_BandsDefinitions = self._plugin.BandsDefinitions
            l_RedBandId = 0
            l_GreenBandId = 0
            l_BlueBandId = 0
            resol_QLK = 0
            l_Resolution = ""
            l_BandsDefinitions = self._plugin.BandsDefinitions
            l_RedBandId, l_BlueBandId, l_GreenBandId = l_BandsDefinitions.get_l2_information_for_quicklook_band_code(
                self._quicklookredbandcode,
                self._quicklookgreenbandcode,
                self._quicklookbluebandcode,
            )
            # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
            # ** ** LOOP on RESOLUTION ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
            for resol in range(0, l_NumberOfResolutions):
                l_StrResolution = l_BandsDefinitions.ListOfL2Resolution[resol]
                # --------------------------------------------------------
                # L2 area AreaType l_AreaToL2Resolution
                l_AreaFile = self._sre_list[resol]
                l_ListOfBand = l_BandsDefinitions.get_list_of_l2_band_code(l_StrResolution)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            LOGGER.debug("Writing L2 resolution image done !")
コード例 #8
0
    def generate_edg_images(self, working_dir):
        dtm_coarse = self._dem.ALC
        # One band equal threshold
        out_edg = os.path.join(working_dir, "tmp_EDG_oneBandEqual.tif")
        param_edg = {
            "im": self._l1toaimagelist[0],
            "thresholdvalue": 0,
            "equalvalue": 255,
            "outsidevalue": 0,
            "out": out_edg + ":uint8"
        }
        onebandequal_app = OtbAppHandler("OneBandEqualThreshold",
                                         param_edg,
                                         write_output=False)

        # Resample to coarse
        LOGGER.debug("Start IPEDGSub.")
        tmp_edg_sub_resample = os.path.join(working_dir, "tmp_edg_sub.tif")
        edg_sub_resample_app = resample(
            onebandequal_app.getoutput().get("out"),
            dtm_coarse,
            tmp_edg_sub_resample,
            OtbResampleType.LINEAR_WITH_RADIUS,
            threshold=0.0,
            padradius=4.0,
            write_output=True)
        # Threshold the output
        #out_sub_edg = os.path.join(working_dir, "tmp_edg_sub_oneBandEqual.tif")
        #param_sub_edg = {"im": edg_sub_resample_app.getoutput().get("out"),
        #                 "thresholdvalue": 0,
        #                 "equalvalue": 0,
        #                 "outsidevalue": 1,
        #                 "out": out_sub_edg + ":uint8"
        #                 }
        #onebandequal_sub_app = OtbAppHandler("OneBandEqualThreshold", param_sub_edg)
        # Put in internal data
        self._edgsubmask = edg_sub_resample_app.getoutput().get("out")
        del onebandequal_app
        del edg_sub_resample_app
        LOGGER.debug("Start IPEDGSub done.")

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

        l_BandsDefinitions = self._plugin.BandsDefinitions
        l_ListOfL2Resolution = l_BandsDefinitions.ListOfL2Resolution  # ListOfStrings
        # At L2 resolution
        l_NbL2Res = len(l_ListOfL2Resolution)
        # Set 1000 to edge pixels to identify the pixel contaminated by an edge pixel after resampling
        #out_thresh = os.path.join(working_dir, "EDGThreshL2.tif")
        #m_L2EDGThresholdApp = binary_threshold(self._edgsubmask,
        #                                         lower_threshold=0,
        #                                         inside_value=1000,
        #                                         outside_value=0,
        #                                         output_image=out_thresh + ":uint8",
        #                                         write_output=False)# //l_ThresholdImageFilter
        #self._l2edg_pipeline.add_otb_app(m_L2EDGThresholdApp)
        for r in range(l_NbL2Res):
            res_str = l_ListOfL2Resolution[r]
            # ExpandFilterPointer => PadAndResampleImageFilter => app ressampling
            out_roi = os.path.join(working_dir,
                                   "IPEDGRealL2_{}.tif".format(res_str))
            roi_app = extract_roi(self._edgsubmask, [0],
                                  out_roi,
                                  write_output=False)
            self._l2edg_pipeline.add_otb_app(roi_app)
            out_ressampling = os.path.join(
                working_dir, "IPEDGRealL2_{}.tif".format(res_str))
            resamp_app = resample(roi_app.getoutput().get("out"),
                                  self._dem.ALTList[r],
                                  out_ressampling,
                                  OtbResampleType.LINEAR,
                                  threshold=0.001,
                                  write_output=False)
            self._l2edg_pipeline.add_otb_app(resamp_app)
            # Set Threshold value to one because the expand filter interpolates values set to 0
            # or 1000 in the first threshold and adds systematically CONST_EPSILON to the output value.
            #m_L2EDGThresholdImage2_out = os.path.join(working_dir, "IPEDGMaskL2_{}.tif".format(res_str))
            #m_L2EDGThresholdApp2 = binary_threshold(resamp_app.getoutput().get("out"),
            #                                          lower_threshold=1,
            #                                          inside_value=1,
            #                                          outside_value=0,
            #                                          output_image=m_L2EDGThresholdImage2_out + ":uint8",
            #                                        write_output=False)
            self._l2edgmasklist.append(resamp_app.getoutput().get("out"))
コード例 #9
0
    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"))
コード例 #10
0
    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 !")
    def read(self, product_info, app_handler, l2comm, dem, pReadL1Mode):
        """product_info,plugin, l2comm,mode

        :param product_info: L1ImageInformationsBase
        :param pReadL1Mode: ReadL1ModeType
        :return:
        """
        LOGGER.debug("Start Venus L1 ImageFileReader ...")
        product_filename = product_info.HeaderFilename
        LOGGER.debug("Start Venus L1 ImageFileReader with the filename: " +
                     product_filename)
        self._plugin.initialize(app_handler)
        self.headerHandler = product_info.HeaderHandler
        self._dem = dem
        # working_dir = get_working_directory("L1Read_", app_handler.get_working_directory())
        working_dir = app_handler.get_directory_manager(
        ).get_temporary_directory("L1Read_", do_always_remove=True)
        self.ReadL1Mode = pReadL1Mode
        self._GIPPL2COMMHandler = l2comm
        l_BandsDefinitions = self._plugin.BandsDefinitions
        # --------------------------------------
        # Initialize the Image filename provider
        l_FilenameProvider = VenusL1ImageFilenames()
        #product_info.FilenamesProvider
        IsValidSatellite = (l_FilenameProvider.initialize(product_filename)
                            is not False)
        if not IsValidSatellite:
            raise MajaPluginVenusException(
                "The file <{}> is not a valid Venus L1 product.".format(
                    product_filename))

        # *********************************************************************************************************
        # Register the Header o the Input image file
        # *********************************************************************************************************
        LOGGER.debug("Load the Venus L1 Header file : '" + product_filename +
                     "'")
        l_L1XMLHandler = HeaderImageEarthExplorerXMLFileHandler(
            product_filename)

        # *********************************************************************************************************
        # TOA Reader connection
        # *********************************************************************************************************
        l_L1NoData = l_L1XMLHandler.get_no_data_value_as_double()
        l_ReflectanceQuantificationValue = l_L1XMLHandler.get_reflectance_quantification_value(
        )
        l_reflectanceMultiplicationValues = []

        if xml_tools.as_bool(l2comm.get_value("CalAdjustOption")):
            l_factor = xml_tools.as_float_list(
                l2comm.get_value("CalAdjustFactor"))
            if len(l_factor) != (VenusL1ImageFileReader.TOALastChannel -
                                 VenusL1ImageFileReader.TOAFirstChannel + 1):
                raise MajaPluginVenusException(
                    "Not the same number of Calibration coeffs than L1 bands")
            for i in range(0, len(l_factor)):
                l_reflectanceMultiplicationValues.append(
                    l_ReflectanceQuantificationValue * l_factor[i])
        else:
            for i in range(VenusL1ImageFileReader.TOALastChannel -
                           VenusL1ImageFileReader.TOAFirstChannel + 1):
                l_reflectanceMultiplicationValues.append(
                    l_ReflectanceQuantificationValue)

        l_RealL1NoData = l_L1NoData * l_ReflectanceQuantificationValue

        # *********************************************************************************************************
        # L1 TOA image pipeline connection
        # *********************************************************************************************************
        if not l_FilenameProvider.m_TOAImageFileName:
            raise MajaPluginVenusException(
                "VenusL1ImageFileReader : The TOA image does not exist !")

        tmp_l1toa_roi = os.path.join(working_dir, "l1toa_roi.tif")
        app_l1_toa_roi = extract_roi(l_FilenameProvider.m_TOAImageFileName, [
            channel
            for channel in range(VenusL1ImageFileReader.TOALastChannel -
                                 VenusL1ImageFileReader.TOAFirstChannel + 1)
        ],
                                     tmp_l1toa_roi,
                                     write_output=False)
        self._l1toa_pipeline.add_otb_app(app_l1_toa_roi)

        tmp_sat_roi = os.path.join(working_dir, "sat_roi.tif")
        app_sat_roi = extract_roi(l_FilenameProvider.m_TOAImageFileName,
                                  [VenusL1ImageFileReader.SATChannel - 1],
                                  tmp_sat_roi,
                                  write_output=False)
        self._sat_pipeline.add_otb_app(app_sat_roi)
        #Multiply scalar by quantif
        tmp_l1toa_mbs = os.path.join(working_dir, "l1toa.tif")
        app_l1toa_mbs = multiply_by_scalar(
            app_l1_toa_roi.getoutput().get("out"),
            l_ReflectanceQuantificationValue,
            output_image=tmp_l1toa_mbs,
            write_output=False)
        self._l1toa_pipeline.add_otb_app(app_l1toa_mbs)
        # update all extract ROI in once
        write_images([
            app_l1toa_mbs.getoutput().get("out"),
            app_sat_roi.getoutput().get("out")
        ], [tmp_l1toa_mbs, tmp_sat_roi])

        self._toascalar = tmp_l1toa_mbs

        # *********************************************************************************************************
        # L1 PIX image pipeline connection
        # *********************************************************************************************************
        tmp_l1pix_roi = os.path.join(working_dir, "l1pix.tif")
        app_l1_pix_roi = extract_roi(l_FilenameProvider.m_TOAImageFileName,
                                     [VenusL1ImageFileReader.PIXChannel - 1],
                                     tmp_l1pix_roi + ":uint16",
                                     write_output=False)
        self._l1pix_pipeline.add_otb_app(app_l1_pix_roi)
        self._l1pix = app_l1_pix_roi.getoutput().get("out")

        # *********************************************************************************************************
        #                                     START READ L1 for ALGORITHMS
        # *********************************************************************************************************
        if pReadL1Mode == ReadL1Mode.READ_L1_MODE_FOR_ALGORITHMS:
            # *********************************************************************************************************
            # L2 PIX image pipeline connection
            # *********************************************************************************************************
            # LAIG-FA-MAC-131720-CS : New for 4.2
            # Before resample, binarytovector -> resample -> vectortobinary
            tmp_l2pix_bin2vec = os.path.join(working_dir, "l2pix_bin2vec.tif")
            param_l2pix_bin2vec = {
                "im": app_l1_pix_roi.getoutput().get("out"),
                "out": tmp_l2pix_bin2vec + ":uint8",
                "nbcomp": VenusL1ImageFileReader.PIXNumberOfComponentsPerPixel
            }
            app_l2pix_bin2vec = OtbAppHandler("BinaryToVector",
                                              param_l2pix_bin2vec,
                                              write_output=False)
            self._l2pix_pipeline.add_otb_app(app_l2pix_bin2vec)

            tmp_l2pix_resample = os.path.join(working_dir,
                                              "l2pix_resample.tif")
            app_l2pix_resample = resample(
                app_l2pix_bin2vec.getoutput().get("out"),
                self._dem.ALTList[0],
                tmp_l2pix_resample,
                OtbResampleType.LINEAR_WITH_RADIUS,
                padradius=4.0,
                threshold=0.0,
                write_output=False)
            self._l2pix_pipeline.add_otb_app(app_l2pix_resample)
            #L2 PIX is concatenate
            tmp_l2pix_binconcat = os.path.join(working_dir, "l2pix.tif")
            param_l2pix_binconcat = {
                "im": app_l2pix_resample.getoutput().get("out"),
                "out": tmp_l2pix_binconcat + ":uint16"
            }
            app_l2pix_binconcat = OtbAppHandler("BinaryConcatenate",
                                                param_l2pix_binconcat,
                                                write_output=False)
            self._l2pix = app_l2pix_binconcat.getoutput().get("out")
            self._l2pix_pipeline.add_otb_app(app_l2pix_binconcat)

            # *********************************************************************************************************
            # L2 EDG image pipeline connection
            # *********************************************************************************************************
            tmp_edg_thresholder = os.path.join(working_dir,
                                               "edg_thresholder1.tif")
            param_edg_thresholder1 = {
                "im": self._toascalar,
                "thresholdvalue": l_RealL1NoData,
                "equalvalue": 255,
                "outsidevalue": 0,
                "out": tmp_edg_thresholder + ":uint8"
            }
            app_edg_thresholder1 = OtbAppHandler("OneBandEqualThreshold",
                                                 param_edg_thresholder1,
                                                 write_output=True)
            self._edg_pipeline.add_otb_app(app_edg_thresholder1)

            tmp_edg_resample = os.path.join(working_dir, "edg_resample.tif")
            app_edg_resample = resample(
                app_edg_thresholder1.getoutput().get("out"),
                self._dem.ALTList[0],
                tmp_edg_resample,
                OtbResampleType.BCO,
                padradius=4.0,
                write_output=True)
            self._edg_pipeline.add_otb_app(app_edg_resample)

            # Threshold the output
            out_sub_edg = os.path.join(working_dir, "edg_thresholder2.tif")
            param_edg_thresholder2 = {
                "im": app_edg_resample.getoutput().get("out"),
                "thresholdvalue": 0,
                "equalvalue": 1,
                "outsidevalue": 0,
                "out": out_sub_edg + ":uint8"
            }
            app_edg_thresholder2 = OtbAppHandler("OneBandEqualThreshold",
                                                 param_edg_thresholder2,
                                                 write_output=True)
            self._edg_pipeline.add_otb_app(app_edg_thresholder2)

            # *********************************************************************************************************
            # IPEDGSub image pipeline connection
            # *********************************************************************************************************
            tmp_edgsub_resample = os.path.join(working_dir,
                                               "edgsub_resample.tif")
            app_edgsub_resample = resample(
                app_edg_thresholder1.getoutput().get("out"),
                self._dem.ALC,
                tmp_edgsub_resample,
                OtbResampleType.LINEAR_WITH_RADIUS,
                padradius=12.0,
                write_output=True)
            self._edg_pipeline.add_otb_app(app_edgsub_resample)
            # Threshold the output
            out_sub_edgsub = os.path.join(working_dir, "edgsub.tif")
            param_edgsub_thresholder2 = {
                "im": app_edgsub_resample.getoutput().get("out"),
                "thresholdvalue": 0,
                "equalvalue": 0,
                "outsidevalue": 1,
                "out": out_sub_edgsub + ":uint8"
            }
            app_edgsub_thresholder2 = OtbAppHandler("OneBandEqualThreshold",
                                                    param_edgsub_thresholder2,
                                                    write_output=True)
            self._edgsub = app_edgsub_thresholder2.getoutput().get("out")
            self._edg_pipeline.add_otb_app(app_edgsub_thresholder2)

            # *********************************************************************************************************
            # L2 TOA image pipeline connection
            # *********************************************************************************************************
            tmp_l2toa_resample = os.path.join(working_dir,
                                              "l2toa_resample.tif")
            app_l2toa_resample = resample(self._toascalar,
                                          self._dem.ALTList[0],
                                          tmp_l2toa_resample,
                                          OtbResampleType.BCO,
                                          padradius=4.0,
                                          write_output=False)
            self._l2toa_pipeline.add_otb_app(app_l2toa_resample)
            l2toa_list = []
            l_toathresholdminvalue = 0
            l_toathresholvalue = -10

            #Apply EDG mask on l2toa resampled
            tmp_l2toa = os.path.join(working_dir, "l2toa.tif")
            app_l2toa = apply_mask(app_l2toa_resample.getoutput().get("out"),
                                   app_edg_thresholder2.getoutput().get("out"),
                                   l_toathresholvalue,
                                   tmp_l2toa,
                                   write_output=False)
            self._l2toa = app_l2toa.getoutput().get("out")
            self._l2toa_pipeline.add_otb_app(app_l2toa)

            # *********************************************************************************************************
            # TOA Sub image pipeline connection
            # *********************************************************************************************************
            tmp_toasub_resample = os.path.join(working_dir,
                                               "toasub_resample.tif")
            app_toasub_resample = resample(self._toascalar,
                                           self._dem.ALC,
                                           tmp_toasub_resample,
                                           OtbResampleType.LINEAR_WITH_RADIUS,
                                           padradius=4.0,
                                           write_output=True)
            self._l2toa_pipeline.add_otb_app(app_toasub_resample)

            # Threshold the output
            out_edgsub_threshold = os.path.join(working_dir,
                                                "edgsubthreshold.tif")
            param_edgsub_threshold = {
                "im": app_edgsub_resample.getoutput().get("out"),
                "thresholdvalue": 0,
                "equalvalue": 1,
                "outsidevalue": 0,
                "out": out_edgsub_threshold + ":uint8"
            }
            app_edgsub_threshold = OtbAppHandler("OneBandEqualThreshold",
                                                 param_edgsub_threshold,
                                                 write_output=True)
            self._edg_pipeline.add_otb_app(app_edgsub_threshold)

            tmp_l2subtoa = os.path.join(working_dir, "toasub.tif")
            app_l2subtoa = apply_mask(
                app_toasub_resample.getoutput().get("out"),
                app_edgsub_threshold.getoutput().get("out"),
                l_toathresholvalue,
                tmp_l2subtoa,
                write_output=True)
            self._toasub = app_l2subtoa.getoutput().get("out")
            self._l2toa_pipeline.add_otb_app(app_l2subtoa)

            # *********************************************************************************************************
            # L2EDG - Actualization of the L2 edge mask
            # *********************************************************************************************************
            #tmp_l2edg_threshold = os.path.join(working_dir, "l2edg_threshold.tif")
            #app_l2edg_threshold = binary_threshold(self._edgsub,
            #                                         lower_threshold=0,
            #                                         inside_value=1000,
            #                                         outside_value=0,
            #                                         output_image=tmp_l2edg_threshold + ":uint8",
            #                                         write_output=True)
            #self._l2edg_pipeline.add_otb_app(app_l2edg_threshold)
            tmp_l2edg_resample = os.path.join(working_dir, "l2edg.tif")
            app_l2edg_resample = resample(self._edgsub,
                                          self._dem.ALTList[0],
                                          tmp_l2edg_resample + ":uint8",
                                          OtbResampleType.LINEAR,
                                          padradius=4.0,
                                          threshold=0.001,
                                          write_output=True)
            self._l2edg = app_l2edg_resample.getoutput().get("out")
            self._l2edg_pipeline.add_otb_app(app_l2edg_resample)

            # *********************************************************************************************************
            # SAT image pipeline connection
            # *********************************************************************************************************
            tmp_sat_bin2vec = os.path.join(working_dir, "sat_bin2vec.tif")
            param_sat_bin2vec = {
                "im": tmp_sat_roi,
                "out": tmp_sat_bin2vec + ":uint8",
                "nbcomp": VenusL1ImageFileReader.SATNumberOfComponentsPerPixel
            }
            app_sat_bin2vec = OtbAppHandler("BinaryToVector",
                                            param_sat_bin2vec,
                                            write_output=False)
            self._sat_pipeline.add_otb_app(app_sat_bin2vec)

            l_l2sat_thresholdvalue = l2comm.get_value_f("SaturationThreshold")

            tmp_sat_resample = os.path.join(working_dir, "l2sat.tif")
            app_sat_resample = resample(app_sat_bin2vec.getoutput().get("out"),
                                        self._dem.ALTList[0],
                                        tmp_sat_resample + ":uint8",
                                        OtbResampleType.BCO,
                                        padradius=4.0,
                                        threshold=l_l2sat_thresholdvalue,
                                        write_output=False)
            self._l2sat = app_sat_resample.getoutput().get("out")
            self._sat_pipeline.add_otb_app(app_sat_resample)

            # *********************************************************************************************************
            # IPSAT Sub image pipeline connection
            # *********************************************************************************************************
            l_sat_subthresholdvalue = l2comm.get_value_f(
                "SaturationThresholdSub")
            tmp_satsub_resample = os.path.join(working_dir, "satsub.tif")
            app_satsub_resample = resample(
                app_sat_bin2vec.getoutput().get("out"),
                self._dem.ALC,
                tmp_satsub_resample + ":uint8",
                OtbResampleType.LINEAR_WITH_RADIUS,
                padradius=4.0,
                threshold=l_sat_subthresholdvalue)
            self._satsub = app_satsub_resample.getoutput().get("out")
            self._sat_pipeline.add_otb_app(app_satsub_resample)

            # *********************************************************************************************************
            # CLA image pipeline connection
            # *********************************************************************************************************

            LOGGER.debug(
                "VenusL1ImageFileReader::Initialize - CLA image filename: '" +
                l_FilenameProvider.m_CLAImageFileName + "'")
            if not l_FilenameProvider.m_CLAImageFileName:
                raise MajaPluginVenusException(
                    "The CLA image does not exist !! ")
            self._cla = l_FilenameProvider.m_CLAImageFileName

            # *********************************************************************************************************
            # SOL1 image pipeline connection
            # *********************************************************************************************************
            LOGGER.debug(
                "VenusL1ImageFileReader::Initialize - SOL image filename: '" +
                l_FilenameProvider.m_SOLImageFileName + "'")
            if not l_FilenameProvider.m_SOLImageFileName:
                raise MajaPluginVenusException(
                    "The SOL image does not exist !! ")
            mtdat = GdalDatasetInfo(l_FilenameProvider.m_TOAImageFileName)
            toaarea = Area()
            toaarea.size = mtdat.size
            toaarea.origin = mtdat.origin
            toaarea.spacing = mtdat.pixel_size
            l_SOLHeaderHandler = HeaderImageEarthExplorerXMLFileHandler(
                l_FilenameProvider.m_SOLHeaderFileName)
            l_L1SOLSubsamplingFactor = l_SOLHeaderHandler.get_sampling_factor()
            LOGGER.debug(l_L1SOLSubsamplingFactor)
            # SOL1
            tmp_sol1_b1 = os.path.join(working_dir, "sol1_B1.tif")
            app_sol1_b1 = multiply_by_scalar(
                l_FilenameProvider.m_SOLImageFileName +
                VenusL1ImageFileReader.SOL1ChannelB1,
                toaarea.spacing[0],
                tmp_sol1_b1,
                write_output=False)
            self._sol_pipeline.add_otb_app(app_sol1_b1)

            tmp_sol1_b2 = os.path.join(working_dir, "sol1_B2.tif")
            app_sol1_b2 = multiply_by_scalar(
                l_FilenameProvider.m_SOLImageFileName +
                VenusL1ImageFileReader.SOL1ChannelB2,
                (-1) * toaarea.spacing[1],
                tmp_sol1_b2,
                write_output=False)
            self._sol_pipeline.add_otb_app(app_sol1_b2)
            tmp_sol1_concat = os.path.join(working_dir, "sol1_concat.tif")
            param_sol1_concat = {
                "il": [
                    app_sol1_b1.getoutput().get("out"),
                    app_sol1_b2.getoutput().get("out")
                ],
                "out":
                tmp_sol1_concat
            }
            app_sol1_concat = OtbAppHandler("ConcatenateDoubleImages",
                                            param_sol1_concat)
            update_projection(l_FilenameProvider.m_TOAImageFileName,
                              app_sol1_concat.getoutput().get("out"),
                              l_L1SOLSubsamplingFactor)

            self._sol_pipeline.add_otb_app(app_sol1_concat)

            tmp_sol1_resample = os.path.join(working_dir, "sol1.tif")
            app_sol1_resample = resample(
                app_sol1_concat.getoutput().get("out"),
                self._dem.ALC,
                tmp_sol1_resample,
                OtbResampleType.LINEAR,
                padradius=4.0)
            self._sol1 = app_sol1_resample.getoutput().get("out")
            self._sol_pipeline.add_otb_app(app_sol1_resample)

            # SOL2
            tmp_sol2_b1 = os.path.join(working_dir, "sol2_B1.tif")
            app_sol2_b1 = multiply_by_scalar(
                l_FilenameProvider.m_SOLImageFileName +
                VenusL1ImageFileReader.SOL2ChannelB1,
                toaarea.spacing[0],
                tmp_sol2_b1,
                write_output=False)
            self._sol_pipeline.add_otb_app(app_sol2_b1)
            tmp_sol2_b2 = os.path.join(working_dir, "sol2_B2.tif")
            app_sol2_b2 = multiply_by_scalar(
                l_FilenameProvider.m_SOLImageFileName +
                VenusL1ImageFileReader.SOL2ChannelB2,
                (-1) * toaarea.spacing[1],
                tmp_sol2_b2,
                write_output=False)
            self._sol_pipeline.add_otb_app(app_sol2_b2)

            tmp_sol2_concat = os.path.join(working_dir, "sol2_concat.tif")
            param_sol2_concat = {
                "il": [
                    app_sol2_b1.getoutput().get("out"),
                    app_sol2_b2.getoutput().get("out")
                ],
                "out":
                tmp_sol2_concat
            }
            app_sol2_concat = OtbAppHandler("ConcatenateDoubleImages",
                                            param_sol2_concat)
            update_projection(l_FilenameProvider.m_TOAImageFileName,
                              app_sol2_concat.getoutput().get("out"),
                              l_L1SOLSubsamplingFactor)
            self._sol_pipeline.add_otb_app(app_sol2_concat)

            tmp_sol2_resample = os.path.join(working_dir, "sol2.tif")
            app_sol2_resample = resample(
                app_sol2_concat.getoutput().get("out"),
                self._dem.ALC,
                tmp_sol2_resample,
                OtbResampleType.LINEAR,
                padradius=4.0)
            self._sol2 = app_sol2_resample.getoutput().get("out")
            self._sol_pipeline.add_otb_app(app_sol2_resample)

            # *********************************************************************************************************
            # DTMVIE image pipeline connection
            # *********************************************************************************************************
            LOGGER.debug(
                "VenusL1ImageFileReader::Initialize - VIE image filename: '" +
                l_FilenameProvider.m_VIEImageFileName + "'")

            l_VIEHeaderHandler = HeaderImageEarthExplorerXMLFileHandler(
                l_FilenameProvider.m_VIEHeaderFileName)
            l_L1VIESubsamplingFactor = l_VIEHeaderHandler.get_sampling_factor()
            LOGGER.debug(l_L1VIESubsamplingFactor)

            tmp_vieb5b1_mult = os.path.join(working_dir, "vie5b1_mult.tif")
            app_vieb5b1_mult = multiply_by_scalar(
                l_FilenameProvider.m_VIEImageFileName +
                VenusL1ImageFileReader.VIEB5ChannelB1,
                toaarea.spacing[0],
                tmp_vieb5b1_mult,
                write_output=False)
            self._dtmvie_pipeline.add_otb_app(app_vieb5b1_mult)

            tmp_vieb5b2_mult = os.path.join(working_dir, "vie5b2_mult.tif")
            app_vieb5b2_mult = multiply_by_scalar(
                l_FilenameProvider.m_VIEImageFileName +
                VenusL1ImageFileReader.VIEB5ChannelB2,
                (-1) * toaarea.spacing[1],
                tmp_vieb5b2_mult,
                write_output=False)
            self._dtmvie_pipeline.add_otb_app(app_vieb5b2_mult)

            tmp_vieb6b1_mult = os.path.join(working_dir, "vie6b1_mult.tif")
            app_vieb6b1_mult = multiply_by_scalar(
                l_FilenameProvider.m_VIEImageFileName +
                VenusL1ImageFileReader.VIEB6ChannelB1,
                toaarea.spacing[0],
                tmp_vieb6b1_mult,
                write_output=False)
            self._dtmvie_pipeline.add_otb_app(app_vieb6b1_mult)

            tmp_vieb6b2_mult = os.path.join(working_dir, "vie6b2_mult.tif")
            app_vieb6b2_mult = multiply_by_scalar(
                l_FilenameProvider.m_VIEImageFileName +
                VenusL1ImageFileReader.VIEB6ChannelB2,
                (-1) * toaarea.spacing[1],
                tmp_vieb6b2_mult,
                write_output=False)
            self._dtmvie_pipeline.add_otb_app(app_vieb6b2_mult)

            tmp_dtmvie_concat = os.path.join(working_dir, "dtmvie_concat.tif")
            param_dtmvie_concat = {
                "il": [
                    app_vieb5b1_mult.getoutput().get("out"),
                    app_vieb5b2_mult.getoutput().get("out"),
                    app_vieb6b1_mult.getoutput().get("out"),
                    app_vieb6b2_mult.getoutput().get("out")
                ],
                "out":
                tmp_dtmvie_concat
            }
            app_dtmvie_concat = OtbAppHandler("ConcatenateDoubleImages",
                                              param_dtmvie_concat)
            update_projection(l_FilenameProvider.m_TOAImageFileName,
                              app_dtmvie_concat.getoutput().get("out"),
                              l_L1VIESubsamplingFactor)
            self._dtmvie_pipeline.add_otb_app(app_dtmvie_concat)

            tmp_dtmvie_resample = os.path.join(working_dir, "dtmvie.tif")
            app_dtmvie_resample = resample(
                app_dtmvie_concat.getoutput().get("out"),
                self._dem.ALC,
                tmp_dtmvie_resample,
                OtbResampleType.LINEAR,
                padradius=4.0)
            self._dtmvie = app_dtmvie_resample.getoutput().get("out")
            self._dtmvie_pipeline.add_otb_app(app_dtmvie_resample)

            # *********************************************************************************************************
            # VIE image pipeline connection
            # *********************************************************************************************************
            tmp_shadowvie_concat = os.path.join(working_dir,
                                                "shadowvie_concat.tif")
            param_shadowvie_concat = {
                "il": [
                    app_vieb5b1_mult.getoutput().get("out"),
                    app_vieb5b2_mult.getoutput().get("out")
                ],
                "out":
                tmp_shadowvie_concat
            }
            app_shadowvie_concat = OtbAppHandler("ConcatenateDoubleImages",
                                                 param_shadowvie_concat)
            self._shadowvie_pipeline.add_otb_app(app_shadowvie_concat)
            update_projection(l_FilenameProvider.m_TOAImageFileName,
                              app_shadowvie_concat.getoutput().get("out"),
                              l_L1VIESubsamplingFactor)
            tmp_shadowvie_resample = os.path.join(working_dir, "shadowvie.tif")
            app_shadowvie_resample = resample(
                app_shadowvie_concat.getoutput().get("out"),
                self._dem.ALC,
                tmp_shadowvie_resample,
                OtbResampleType.LINEAR,
                padradius=4.0)
            self._shadowvie = app_shadowvie_resample.getoutput().get("out")
            self._shadowvie_pipeline.add_otb_app(app_shadowvie_resample)

            # Fill the datas
            self.dict_of_vals["IPEDGSubOutput"] = self._edgsub
            self.dict_of_vals["SOL1Image"] = self._sol1
            self.dict_of_vals["SOL2Image"] = self._sol2
            self.dict_of_vals["DTMVIEImage"] = self._dtmvie
            self.dict_of_vals["IPTOASubOutput"] = self._toasub

            self.dict_of_vals["L2TOAImageList"] = [self._l2toa]
            self.dict_of_vals["ViewingZenithMeanMap"] = self._meanZenithMap
            self.dict_of_vals["ViewingAzimuthMeanMap"] = self._meanAzimuthMap
            self.dict_of_vals["CLAImage"] = self._cla
            self.dict_of_vals["IPSATSubOutput"] = self._satsub
            self.dict_of_vals["ShadowVIEImage"] = self._shadowvie
            if self._plugin.CirrusMasking:
                l_CirrusBandCode = l2comm.get_value("CirrusBandCode")
                l_CirrusBandIdx = self._plugin.BandsDefinitions.get_band_id_in_l2_coarse(
                    l_CirrusBandCode)
                tmp = os.path.join(working_dir, "l1toacirrus.tif")
                app = extract_roi(self._toascalar, [l_CirrusBandIdx - 1], tmp)
                self.dict_of_vals["L1TOACirrusImage"] = app.getoutput().get(
                    "out")
            self.dict_of_vals["L2EDGOutputList"] = [self._l2edg]
            self.dict_of_vals["L2SATImageList"] = [self._l2sat]
            self.dict_of_vals["L2PIXImageList"] = [self._l2pix]
            self.dict_of_vals["L1PIXImageList"] = [self._l1pix]
            self.dict_of_vals["L1TOAImageList"] = [self._toascalar]
コード例 #12
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
コード例 #13
0
    def read(self, product_info, app_handler, l2comm, dem, pReadL1Mode):
        product_filename = product_info.HeaderFilename
        LOGGER.debug("Start Landsat8 L1 ImageFileReader with the filename: " +
                     product_filename)

        self._plugin.initialize(app_handler)
        self._header_handler = product_info.HeaderHandler
        self._GIPPL2COMMHandler = l2comm
        self._dem = dem
        working_dir = app_handler.get_directory_manager(
        ).get_temporary_directory("L1Read_", do_always_remove=True)

        self.ReadL1Mode = pReadL1Mode
        l_BandsDefinitions = self._plugin.BandsDefinitions
        l_factor = xml_tools.as_float_list(l2comm.get_value("CalAdjustFactor"))
        l_ListOfL1Resolution = l_BandsDefinitions.ListOfL1Resolution  # ListOfStrings
        l_ListOfL2Resolution = l_BandsDefinitions.ListOfL2Resolution  # ListOfStrings
        l_SolZenith = self._header_handler.get_sol_zenith()

        # READ THE Inputs
        # *********************************************************************************************************
        # QB Reader connection
        # *********************************************************************************************************
        qb_filename = self._header_handler.get_qbi_image_filename()
        self.generate_qb_mask(qb_filename, working_dir)

        # *********************************************************************************************************
        # TOA Reader connection
        # *********************************************************************************************************
        self.generate_l1_toa_image(l2comm, l_factor, l_SolZenith,
                                   l_ListOfL1Resolution, working_dir)

        # *********************************************************************************************************
        # PIX image pipeline connection
        # *********************************************************************************************************
        self.generate_pix_mask(working_dir)

        # *********************************************************************************************************
        #                                     START READ L1 for ALGORITHMS
        # *********************************************************************************************************
        if pReadL1Mode == ReadL1Mode.READ_L1_MODE_FOR_ALGORITHMS:
            # Get information of areas (footprint) of the product (Origin, Spacing and Size for L2 and
            # L2Coarse resolution)
            l_L2Dems = dem.ALTList
            l_CoarseDem = dem.ALC

            # ************************************************************************************************
            # TOA image pipeline connection
            # ************************************************************************************************
            self.generate_l2_toa_images(l_ListOfL2Resolution, working_dir)

            # ************************************************************************************************
            # L2SAT image pipeline connection
            # ************************************************************************************************
            self.generate_l2_sat_images(l_ListOfL2Resolution, working_dir)

            # ************************************************************************************************
            # L2 EDG image pipeline connection
            # ************************************************************************************************
            self.generate_edg_images(working_dir)

            # ************************************************************************************************
            # TOA Sub image pipeline connection
            # ************************************************************************************************
            self.generate_toa_sub_image(working_dir)

            # *********************************.**************************************************************
            # SAT Sub image pipeline connection
            # ************************************************************************************************
            self.generate_sat_sub_image(l2comm, working_dir)

            # ************************************************************************************************
            # CLA image pipeline connectio
            # ************************************************************************************************
            self.generate_cla_image(working_dir)

            # ************************************************************************************************
            # SOL1 image pipeline connectio
            # ************************************************************************************************
            sol_h1 = self._plugin.ConfigUserCamera.get_Algorithms(
            ).get_GRID_Reference_Altitudes().get_SOLH1()
            self.generate_sol1_image(sol_h1, working_dir)

            # *************************************************************************************************
            # VIE image pipeline connection
            # *************************************************************************************************
            vie_href = self._plugin.ConfigUserCamera.get_Algorithms(
            ).get_GRID_Reference_Altitudes().get_VIEHRef()
            self.generate_vie_image(vie_href, working_dir)

            # Fill the datas
            self.dict_of_vals["IPEDGSubOutput"] = self._edgsubmask
            self.dict_of_vals["L2TOAImageList"] = self._l2toaimagelist
            self.dict_of_vals["L2SATImageList"] = self._l2satmasklist
            self.dict_of_vals["L1PIXImageList"] = self._l2piximagelist
            self.dict_of_vals["L2PIXImageList"] = self._l2piximagelist
            self.dict_of_vals["L2EDGOutputList"] = self._l2edgmasklist
            self.dict_of_vals["IPTOASubOutput"] = self._subtoaimage
            self.dict_of_vals["IPSATSubOutput"] = self._subsatimage
            self.dict_of_vals["CLAImage"] = self._claimage
            self.dict_of_vals["SOL1Image"] = self._sol1image
            self.dict_of_vals["ViewingZenithMeanMap"] = self._meanZenithMap
            self.dict_of_vals["ViewingAzimuthMeanMap"] = self._meanAzimuthMap

            if len(self._vieimage) > 2:
                l_DTMBandCode = l2comm.get_value("DTMViewingDirectionBandCode")
                l_DTMBandIdx = l_BandsDefinitions.get_band_id_in_l2_coarse(
                    l_DTMBandCode)
                LOGGER.info("DTMBandCode= " + l_DTMBandCode)
                self.dict_of_vals["DTMVIEImage"] = self._vieimage[l_DTMBandIdx]
            else:
                self.dict_of_vals["DTMVIEImage"] = self._vieimage[0]
            if len(self._vieimage) > 2:
                l_CLDBandCode = l2comm.get_value("CLDViewingDirectionBandCode")
                l_CLDBandIdx = self._plugin.BandsDefinitions.get_band_id_in_l2_coarse(
                    l_CLDBandCode)
                self.dict_of_vals["ShadowVIEImage"] = self._vieimage[
                    l_CLDBandIdx]
            elif len(self._vieimage) > 1:
                self.dict_of_vals["ShadowVIEImage"] = self._vieimage[1]
            else:
                self.dict_of_vals["ShadowVIEImage"] = self._vieimage[0]
            if self._plugin.CirrusMasking:
                l_CirrusBandCode = l2comm.get_value("CirrusBandCode")
                l_CirrusRes = self._plugin.BandsDefinitions.get_l1_resolution_for_band_code(
                    l_CirrusBandCode)
                l_CirrusBandIdx = self._plugin.BandsDefinitions.get_band_id_in_l1(
                    l_CirrusBandCode)
                tmp_toa_roi = os.path.join(working_dir, "L1TOACirrusImage.tif")
                l2toa_roi_app = extract_roi(self._l1toaimagelist[0],
                                            [l_CirrusBandIdx],
                                            tmp_toa_roi,
                                            write_output=False)
                self._l2toa_pipeline.add_otb_app(l2toa_roi_app)
                self.dict_of_vals[
                    "L1TOACirrusImage"] = l2toa_roi_app.getoutput().get("out")

            self.dict_of_vals["L1TOAImageList"] = self._l1toaimagelist
    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)
コード例 #15
0
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Environment Correction start")
        env_working = dict_of_input.get("AppHandler").get_directory_manager().get_temporary_directory("EnvoCorrProc_",
                                            do_always_remove=True)
        l_envcorradius = dict_of_input.get("L2COMM").get_value_f("EnvCorrRadius")
        l_envcorsize = (2 * l_envcorradius + 1) * (2 * l_envcorradius + 1)
        caching = dict_of_input.get("Params").get("Caching")
        # Compute rho env sub
        rhoenv_sub_image = os.path.join(env_working, "rhoenv_sub.tif")
        param_computerho = {"toc": dict_of_output["TOC_sub"],
                            "edg": dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
                            "radius": l_envcorradius,
                            "filtercoeff": 1.0 / l_envcorsize,
                            "rhoenv": rhoenv_sub_image}
        computerho_app = OtbAppHandler("ComputeRhoEnv", param_computerho, write_output=True)

        # Interpolate for env
        tdif_filename = os.path.join(env_working, "tdif_sub.tif")
        tdir_filename = os.path.join(env_working, "tdir_sub.tif")
        albd_filename = os.path.join(env_working, "albd_sub.tif")
        param_interpolate = {"luttdir": dict_of_input.get("L2DIRT"),
                             "luttdif": dict_of_input.get("L2DIFT"),
                             "lutalbd": dict_of_input.get("L2ALBD"),
                             "aot": dict_of_output["AOT_Sub"],
                             "dtm": dict_of_input.get("DEM").ALC,
                             "edg": dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
                             "thetav": dict_of_input.get(
                                 "L1Info").ListOfViewingZenithAnglesPerBandAtL2CoarseResolution,
                             "tdir": tdir_filename,
                             "tdif": tdif_filename,
                             "albd": albd_filename
                             }
        interpolate_app = OtbAppHandler("InterpolateForEnvCorr", param_interpolate, write_output=True)
        tdif_image = interpolate_app.getoutput()["tdif"]
        tdir_image = interpolate_app.getoutput()["tdir"]
        albd_image = interpolate_app.getoutput()["albd"]
        # Compute env corr for each l2 resolution
        bands_definition = dict_of_input.get("Plugin").BandsDefinitions
        l_nbRes = len(bands_definition.ListOfL2Resolution)
        sre_list = []
        for r in range(0, l_nbRes):
            l_res = bands_definition.ListOfL2Resolution[r]
            l_l2bandcodes = bands_definition.get_list_of_l2_band_code(l_res)
            l_l2bandidx = [bands_definition.get_band_id_in_l2_coarse(b) for b in l_l2bandcodes]
            tdifl2_filename = os.path.join(env_working, "tdif_" + l_res + ".tif")
            tdirl2_filename = os.path.join(env_working, "tdir_" + l_res + ".tif")
            albdl2_filename = os.path.join(env_working, "albd_" + l_res + ".tif")
            rhoenvl2_filename= os.path.join(env_working, "rhoenv_extract_" + l_res + ".tif")
            # Extract tdif
            tdifl2_image_app = extract_roi(tdif_image, l_l2bandidx, tdifl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(tdifl2_image_app)
            # Extract tdir
            tdirl2_image_app = extract_roi(tdir_image, l_l2bandidx, tdirl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(tdirl2_image_app)
            # Extract albd
            albdl2_image_app = extract_roi(albd_image, l_l2bandidx, albdl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(albdl2_image_app)
            # Extract rhoenv_sub
            rhoenvl2_image_app = extract_roi(computerho_app.getoutput().get("rhoenv"), l_l2bandidx, rhoenvl2_filename, write_output=False)
            self._l2_pipeline.add_otb_app(rhoenvl2_image_app)
            rhoenv_image = os.path.join(env_working, "rhoenv_" + l_res + ".tif")
            sre_image = os.path.join(env_working, "sre_" + l_res + ".tif")
            # Compute env correction
            param_envcorr = {"tdir": tdirl2_image_app.getoutput()["out"],
                             "tdif": tdifl2_image_app.getoutput()["out"],
                             "albd": albdl2_image_app.getoutput()["out"],
                             "rhoenvsub": rhoenvl2_image_app.getoutput()["out"],
                             "nodata": dict_of_input.get("Params").get("RealL2NoData"),
                             "toc": dict_of_output["TOC_" + l_res],
                             "edg": dict_of_input.get("L1Reader").get_value("L2EDGOutputList")[r],
                             "sre": sre_image,
                             "rhoenv": rhoenv_image
                             }
            envcorr_app = OtbAppHandler("EnvCorrection", param_envcorr,write_output=False)
            self._l2_pipeline.add_otb_app(envcorr_app)
            if is_croco_on("envcorrection"):
                write_images([envcorr_app.getoutput().get("sre"), envcorr_app.getoutput().get("rhoenv")],
                             [sre_image, rhoenv_image])
                dict_of_output["SRE_" + l_res] = sre_image
                dict_of_output["RhoEnv_" + l_res] = rhoenv_image
                sre_list.append(sre_image)
            else:
                dict_of_output["SRE_" + l_res] = envcorr_app.getoutput().get("sre")
                dict_of_output["RhoEnv_" + l_res] = envcorr_app.getoutput().get("rhoenv")
                sre_list.append(envcorr_app.getoutput().get("sre"))
        dict_of_output["SRE_List"] = sre_list
コード例 #16
0
    def read_public_images(self, p_L2ImagesFilenamesProvider,
                           p_ReflectanceQuantificationValue, p_PluginBase,
                           working_dir):
        """
        ReadPublicImages
        :param p_L2ImagesFilenamesProvider:
        :param p_ReflectanceQuantificationValue:
        :param p_PluginBase:
        :return:
        """
        l_NumberOfResolutions = len(p_PluginBase.ListOfL2Resolutions)

        # ****  PUBLIC  DATA      ******

        # 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 = p_PluginBase.BandsDefinitions.ListOfL2Resolutions[resol]
            l_NumberOfComponentsPerPixel = len(
                p_PluginBase.BandsDefinitions.GetListOfL2BandCode(l_sres))

            # Register the ATB header for reading VAPQuantificationValue and AOTQuantificationValue
            l_ATB_XMLHandler = HeaderImageEarthExplorerXMLFileHandler(
                p_L2ImagesFilenamesProvider.get_atb_header_filename()[resol])
            l_VAP_QuantificationValue = l_ATB_XMLHandler.get_vap_quantification_value(
            )
            l_AOT_QuantificationValue = l_ATB_XMLHandler.get_aot_quantification_value(
            )

            # ** SRE Reader connection */
            LOGGER.debug(
                "L2ImageFileReaderBase::Initialize - "
                "SRE image filename: %s.",
                p_L2ImagesFilenamesProvider.get_sre_filenames()[resol])
            tmp_sre_scale = os.path.join(working_dir,
                                         "tmp_sre_scale_" + l_sres + ".tif")

            param_scaled_sre = {
                "im": p_L2ImagesFilenamesProvider.get_sre_filenames()[resol],
                "coef": p_ReflectanceQuantificationValue,
                "out": tmp_sre_scale
            }
            sre_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_sre)
            l_SREList.append(tmp_sre_scale)

            # If the product contains FRE image
            if self._m_WriteFRE:
                tmp_fre_scale = os.path.join(
                    working_dir, "tmp_fre_scale_" + l_sres + ".tif")
                param_scaled_fre = {
                    "im":
                    p_L2ImagesFilenamesProvider.get_fre_filenames()[resol],
                    "coef": p_ReflectanceQuantificationValue,
                    "out": tmp_fre_scale
                }
                fre_scal_app = OtbAppHandler("MultiplyByScalar",
                                             param_scaled_fre)
                l_FREList.append(tmp_fre_scale)

            # ATB Reader connection
            LOGGER.debug(
                "L2ImageFileReaderBase::Initialize - ATB image filename: %s.",
                p_L2ImagesFilenamesProvider.get_atb_image_filename()[resol])
            l_ATBList.append(
                p_L2ImagesFilenamesProvider.get_atb_image_filename()[resol])

            # VAP Reader connection (from ATB)
            tmp_vap = os.path.join(working_dir, "tmp_vap_" + l_sres + ".tif")
            tmp_vap_image_app = extract_roi(
                p_L2ImagesFilenamesProvider.get_atb_image_file_name()[resol],
                ["Channel1"],
                tmp_vap,
                write_output=False)
            tmp_vap_scale = os.path.join(working_dir,
                                         "tmp_vap_scale_" + l_sres + ".tif")
            param_scaled_vap = {
                "im": tmp_vap_image_app.getoutput()["out"],
                "coef": l_VAP_QuantificationValue,
                "out": tmp_vap_scale
            }
            vap_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_vap)
            l_VAPList.append(tmp_vap_scale)

            # AOT Reader connection (from ATB)
            tmp_aot = os.path.join(working_dir, "tmp_aot_" + l_sres + ".tif")
            tmp_aot_image_app = extract_roi(
                p_L2ImagesFilenamesProvider.get_atb_image_file_name()[resol],
                ["Channel2"],
                tmp_aot,
                write_output=False)
            tmp_aot_scale = os.path.join(working_dir,
                                         "tmp_aot_scale_" + l_sres + ".tif")
            param_scaled_aot = {
                "im": tmp_aot_image_app.getoutput()["out"],
                "coef": l_AOT_QuantificationValue,
                "out": tmp_aot_scale
            }
            aot_scal_app = OtbAppHandler("MultiplyByScalar", param_scaled_aot)
            l_AOTList.append(tmp_aot_scale)

            # QLT Reader connection */
            LOGGER.debug(
                "L2ImageFileReaderBase::Initialize - QLT image filename: %s.",
                p_L2ImagesFilenamesProvider.get_qlt_filenames()[resol])
            l_QLTList.append(
                p_L2ImagesFilenamesProvider.get_qlt_filenames()[resol])

            # SAT Reader connection (from QLT) */
            tmp_sat = os.path.join(working_dir, "tmp_sat_" + l_sres + ".tif")
            tmp_sat_image_app = extract_roi(
                p_L2ImagesFilenamesProvider.get_qlt_filenames()[resol], [0],
                tmp_sat,
                write_output=False)
            tmp_vec_sat = os.path.join(working_dir,
                                       "tmp_sat_vec_" + l_sres + ".tif")
            param_bin_sat = {
                "im": tmp_sat_image_app.getoutput()["out"],
                "out": tmp_vec_sat,
                "nbcomp": l_NumberOfComponentsPerPixel
            }
            bin_sat_app = OtbAppHandler("BinaryToVector", param_bin_sat)
            l_SATList.append(tmp_vec_sat)

            # EDG Reader connection (from QLT) */
            tmp_qoth = os.path.join(working_dir, "tmp_qoth_" + l_sres + ".tif")
            tmp_qoth_image = extract_roi(
                p_L2ImagesFilenamesProvider.get_qlt_filenames()[resol], [2],
                tmp_qoth,
                write_output=False)
            tmp_vec_qoth = os.path.join(working_dir,
                                        "tmp_qoth_vec_" + l_sres + ".tif")
            param_bin_qoth = {
                "im": tmp_qoth_image.getoutput()["out"],
                "out": tmp_vec_qoth,
                "nbcomp": 2
            }
            bin_qoth_app = OtbAppHandler("BinaryToVector", param_bin_qoth)
            tmp_edg = os.path.join(working_dir, "tmp_edg_" + l_sres + ".tif")
            tmp_edg_image = extract_roi(bin_qoth_app.getoutput()["out"], [0],
                                        tmp_edg, working_dir)
            l_EDGList.append(tmp_edg)

            # CLD Reader connection */
            tmp_cld_vec = os.path.join(working_dir,
                                       "tmp_cld_vec_" + l_sres + ".tif")
            param_vec_cld = {
                "im": p_L2ImagesFilenamesProvider.get_cld_image_filename(),
                "out": tmp_cld_vec,
                "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 = band_math(
                    [cld_vec_app.getoutput()["out"]], "0",
                    os.path.join(working_dir,
                                 "tmp_zero_cld_" + l_sres + ".tif"))
            l_dict_of_cld = {}
            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,
                        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)

            # MSK Reader connection */
            LOGGER.debug(
                "L2ImageFileReaderBase::Initialize - MSK image filename: %s.",
                p_L2ImagesFilenamesProvider.get_msk_filename()[resol])
            l_XMLHandler = EarthExplorerXMLFileHandler(
                p_L2ImagesFilenamesProvider.get_msk_header_file_name()[resol])
            # For Nominal case Spectral camera MSK contains the WAT, HID, SHD, STL and TGS masks
            # If Snow masking is available (For ex for Sentinel2 and Landsat, S4, L8),
            # MSK contains the WAT, HID, SHD, STL, TGS and SNW masks
            l_MSKHDRFilename = p_L2ImagesFilenamesProvider.get_msk_header_file_name(
            )[resol]
            l_MSKNumberOfComponentsPerPixel = int(
                l_XMLHandler.get_string_value_of("SignificantBits"))
            # l_MSKNumberOfComponentsPerPixel = 5 or 6 if SNW
            LOGGER.debug(
                "The MSK header file <%s> have '%s' number of significant bits = NumberOfComponentsPerPixel.",
                l_MSKHDRFilename, l_MSKNumberOfComponentsPerPixel)
            tmp_vec_moth = os.path.join(working_dir,
                                        "tmp_moth_vec_" + l_sres + ".tif")
            param_bin_moth = {
                "im": p_L2ImagesFilenamesProvider.get_msk_filename()[resol],
                "out": tmp_vec_moth,
                "nbcomp": l_MSKNumberOfComponentsPerPixel
            }
            bin_moth_app = OtbAppHandler("BinaryToVector",
                                         param_bin_moth,
                                         write_output=False)

            # Extract WAT from moth
            tmp_wat = os.path.join(working_dir, "tmp_wat_" + l_sres + ".tif")
            tmp_wat_image = extract_roi(bin_moth_app.getoutput()["out"], [0],
                                        tmp_wat)
            l_WATList.append(tmp_wat_image)

            if p_PluginBase.SnowMasking:
                if l_MSKNumberOfComponentsPerPixel < 6:
                    raise MajaExceptionPluginBase(
                        "Error while reading the SNOW band in the MSK product. The SNOW band number is '6', but the MSK have only '{}' bands!"
                        .format(l_MSKNumberOfComponentsPerPixel))
                # TODO: const unsigned int l_IndexSNWChannel(6)
                tmp_snow = os.path.join(working_dir,
                                        "tmp_snow_" + l_sres + ".tif")
                tmp_snow_image = extract_roi(tmp_vec_moth, [5], tmp_snow)
                l_SNOWList.append(tmp_snow_image)
コード例 #17
0
    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.")
コード例 #18
0
    def run(self, dict_of_input, dict_of_output):
        LOGGER.info("Slope Correction start")
        slope_working = dict_of_input.get(
            "AppHandler").get_directory_manager().get_temporary_directory(
                "SlopeCorrProc_", do_always_remove=True)
        caching = dict_of_input.get("Params").get("Caching")
        # Compute transmission
        tdif_image = os.path.join(slope_working, "tdif_sub.tif")
        tdir_image = os.path.join(slope_working, "tdir_sub.tif")
        param_interpolate = {
            "luttdir":
            dict_of_input.get("L2DIRT"),
            "luttdif":
            dict_of_input.get("L2DIFT"),
            "aot":
            dict_of_output["AOT_Sub"],
            "dtm":
            dict_of_input.get("DEM").ALC,
            "edg":
            dict_of_input.get("L1Reader").get_value("IPEDGSubOutput"),
            "thetas":
            float(dict_of_input.get("L1Info").SolarAngle["sun_zenith_angle"]),
            "tdir":
            tdir_image,
            "tdif":
            tdif_image
        }
        interpolate_app = OtbAppHandler("ComputeTransmission",
                                        param_interpolate,
                                        write_output=True)

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

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

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