def initialize_list_of_ozone_parameter_value(pListOfOzone):
        l_ListOfOzoneParameterValue = []
        # Loops on the ozone list
        for f in pListOfOzone:
            # Normaly, the prodcut is uncompress and XML file checked in the PreProcessing method
            l_HDRozoneFilename = os.path.splitext(f)[0] + ".HDR"
            l_OzoneParameterValueType = L2BackwardProcessor.OzoneParameterValue(
            )
            # Load the EarthExplorer XML file
            l_Handler = EarthExplorerXMLFileHandler(l_HDRozoneFilename)
            # Converts these dates in Julian day
            l_OzoneParameterValueType.JulianDayDateStart = date_utils.get_julianday_as_double(
                date_utils.get_datetime_from_utc(
                    l_Handler.get_string_value_of("ValidityStart")))
            l_OzoneParameterValueType.JulianDayDateStop = date_utils.get_julianday_as_double(
                date_utils.get_datetime_from_utc(
                    l_Handler.get_string_value_of("ValidityStop")))
            l_OzoneParameterValueType.Filename = os.path.splitext(
                f)[0] + ".DBL"
            LOGGER.debug("Add Ozone Value for date [" +
                         str(l_OzoneParameterValueType.JulianDayDateStart) +
                         "" +
                         str(l_OzoneParameterValueType.JulianDayDateStop) +
                         "] -> file " + l_OzoneParameterValueType.Filename)
            l_ListOfOzoneParameterValue.append(l_OzoneParameterValueType)

        return l_ListOfOzoneParameterValue
def get_list_of_gipp_filenames_with_mission(dir_name, file_type, mission):
    list_of_gipp = get_list_of_gipp_filenames(dir_name, file_type)
    list_of_gipp_mission = []
    for gip in list_of_gipp:
        gip_xml = gip
        if not os.path.splitext(gip)[1] == ".EEF":
            gip_xml = os.path.splitext(gip)[0] + ".HDR"
        handler = EarthExplorerXMLFileHandler(gip_xml)
        gip_mis = handler.get_string_value_of("Mission")
        if gip_mis == mission:
            list_of_gipp_mission.append(gip)
    return list_of_gipp_mission
def get_listoffilenames_filetype_sorted(dir_name, file_type):
    list_of_gipp = get_list_of_gipp_filenames(dir_name, file_type)
    list_of_gipp_file_type = []
    for gip in list_of_gipp:
        gip_xml = gip
        if not os.path.splitext(gip)[1] == ".EEF":
            gip_xml = os.path.splitext(gip)[0] + ".HDR"
        handler = EarthExplorerXMLFileHandler(gip_xml)
        gip_type = handler.get_string_value_of("File_Type")
        if file_type in gip_type:
            list_of_gipp_file_type.append(gip)
    return list_of_gipp_file_type
Exemple #4
0
 def is_an_earth_explorer_l2_product_by_checking_the_satellite(
         p_filename, plugin_base):
     """
     IsAnEarthExplorerL2ProductByCheckingTheSatellite
     :return:
     """
     LOGGER.debug("File detected with the file naming <" + p_filename +
                  ">. Check with the field 'Platform' in the xml file...")
     l_Filename = os.path.splitext(p_filename)[0] + ".HDR"
     xml_handler = EarthExplorerXMLFileHandler(l_Filename)
     if plugin_base.is_valid_with_satellite(
             xml_handler.get_string_value_of("Mission")):
         LOGGER.debug(
             "The filename " + l_Filename +
             " is a 'EarthExplorer' L2 product compatible with the plugin '"
         )
         return True
     return False
    def write_private_ltc_xmlhandler(self,
                                     p_InputL2ImageFileReader,
                                     p_ReferenceProductHeaderId,
                                     p_ReferenceProductInstance,
                                     root_sc_xml_templates,
                                     p_OutputL2PrivateImageFilenamesProvider,
                                     schemaLocationDirectory,
                                     pCurrentDate,
                                     pValidity_Start,
                                     pValidity_Stop,
                                     p_MeanSolarAngles,
                                     p_MeanViewingAngles,
                                     p_EnableCheckXMLFilesWithSchema=True):
        output_filename = p_OutputL2PrivateImageFilenamesProvider.get_ltc_header_filename(
        )
        l_FileDefinition = self.plugin.ShortFileType + "_LUTANX"
        # ---------------------------------------
        # Get global information about the output L2 product
        # ---------------------------------------
        LOGGER.debug("Write the PRIVATE LTC header file ...")
        lReferenceProductHeaderId = p_ReferenceProductHeaderId
        lReferenceProductInstance = p_ReferenceProductInstance
        lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION
        # Get Validity
        lValidity_Start = pValidity_Start
        lValidity_Stop = pValidity_Stop

        current_header_filename = root_sc_xml_templates
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")

        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(pValidity_Start, pValidity_Stop, lFile_Version,
                              output_filename, self.creator,
                              self.plugin.Creator_Version, self.notes,
                              self.system, self.outputfileclass, pCurrentDate,
                              lReferenceProductHeaderId)
        # ---------------------------------------
        # Detect the good GIPP TOCR filename to read data
        # ---------------------------------------
        lNbGippFilenames = len(self.listofgippsfilenames)
        l_found = False
        l_GIPTOCRFilename = ""
        # For each GIPP filename of the list
        for i in range(lNbGippFilenames):
            # Look for the header file of the GIP_L2TOCR
            l_FileName = self.listofgippsfilenames[i]
            l_FileName = os.path.splitext(l_FileName)[0] + ".HDR"
            # ---------------------------------------------------------------------------------------------------
            # Load the file
            l_CanLoad = xml_tools.can_load_file(l_FileName)
            if l_CanLoad:
                # Read the header file of the GIP_L2TOCR
                lHolder = EarthExplorerXMLFileHandler(l_FileName)
                l_Satellite = lHolder.get_string_value("//Mission")
                l_File_Type = lHolder.get_string_value("//File_Type")
                # Check the validity of the found GIPP : camera and type
                if l_Satellite in self.l1imageinformationsproviderbase.Satellite and "GIP_L2TOCR" in l_File_Type:
                    l_GIPTOCRFilename = l_FileName
                    l_found = True

        # The GIP_L2TOCR does not exist
        if not l_found:
            raise MajaExceptionPluginBase(
                "Impossible to detect the TOCR GIPP filename for the current satellite <"
                + self.l1imageinformationsproviderbase.Satellite + "> !")

        # ****************************************************************************************
        # Write LUT_Indexes information
        lTOCRHolder = EarthExplorerXMLFileHandler(l_GIPTOCRFilename)
        # ---------------------------------------------------------------------------------------------------
        # Get the information of the input LUT to copy them in the LTC header file
        output_handler.set_string_value(
            "//LUT_Indexes/Altitude_Indexes",
            lTOCRHolder.get_string_value("//LUT_Indexes/Altitude_Indexes"))
        output_handler.set_string_value(
            "//LUT_Indexes/AOT_Indexes",
            lTOCRHolder.get_string_value("//LUT_Indexes/AOT_Indexes"))
        output_handler.set_string_value(
            "//LUT_Indexes/TOA_Reflectance_Indexes",
            lTOCRHolder.get_string_value(
                "//LUT_Indexes/TOA_Reflectance_Indexes"))
        output_handler.set_string_value(
            "//Mission", self.l1imageinformationsproviderbase.Satellite)
        output_handler.set_string_value("//Reference_Product_Instance",
                                        lReferenceProductInstance)
        l_attr = dict()
        l_attr["unit"] = "nanometer"
        xml_tools.set_string_list_value_sorted_by_attribute(
            output_handler.root,
            self.listofbandtheoreticalwavelengthforltccomposite,
            "//List_of_Band_Theoretical_Wavelength",
            "Band_Theoretical_Wavelength", "count", "sn", 1, l_attr)

        #                    output_handler.save_to_fileAndCheck(output_filename, lSchemaLocationDirectory)
        # ---------------------------------------
        # Check the product validity of the output L2 product
        # ---------------------------------------
        if self.productisvalid:
            output_handler.set_string_value("//Reference_Product_Semantic",
                                            "L2VALD")
        else:
            output_handler.set_string_value("//Reference_Product_Semantic",
                                            "L2NOTV")
        # Initialization used to write the binary data of the LTC file
        listOfLTC = []
        listOfpft = []
        LOGGER.debug(
            "p_OutputL2PrivateImageFilenamesProvider.GetLTCHeaderFileName(): "
            +
            p_OutputL2PrivateImageFilenamesProvider.get_ltc_header_filename())

        # 4.3         l_DestinationDir = Utilities::SetExtension(output_filename, ".DBL.DIR")
        l_DestinationDir = p_OutputL2PrivateImageFilenamesProvider.get_ltc_image_dir_filename(
        )
        LOGGER.debug("l_DestinationDir: " + l_DestinationDir)
        l_DestinationRelativeDir = os.path.basename(l_DestinationDir)
        LOGGER.debug("l_DestinationRelativeDir: " + l_DestinationRelativeDir)
        # ****************************************************************************************
        # Reading Angles Solar and Viewing from LTC of L2 previous product
        if not self.initmode:
            if p_InputL2ImageFileReader is None:
                raise MajaExceptionPluginBase(
                    "Internal error: the variable m_InputL2ImageFileReader is NULL!"
                )
            # p_L2PrivateImageFilenamesProvider.GetLTCHeaderFileName()
            # #m_L2InputImageFilenamesProvider->GetPrivateFileNames().GetLTCHeaderFileName()
            L2_LTCHeaderFilename = p_InputL2ImageFileReader.L2PrivateImageFilenamesProvider.get_ltc_header_filename(
            )
            LOGGER.debug(
                "Nominal mode: open the L2 input LTC HeaderFileName: " +
                L2_LTCHeaderFilename)
            lHolder = EarthExplorerXMLFileHandler(L2_LTCHeaderFilename)
            lHolder.get_l2_product_ltc_information(l_DestinationRelativeDir,
                                                   listOfLTC, listOfpft)
            # Copy all files from the L2 Input directory to the output L2 product
            file_utils.copy_tree(
                p_InputL2ImageFileReader.L2PrivateImageFilenamesProvider.
                get_ltc_image_dir_filename(), l_DestinationDir)
        # ****************************************************************************************
        # Reading Solar and Viewing angles from L1 product
        # Push in the list
        LOGGER.debug("m_IgnoreCurrentLTC: " + str(self.ignorecurrentltc))
        if not self.ignorecurrentltc:
            ltc = EarthExplorerXMLFileHandler.LTCType()
            solarAngles = p_MeanSolarAngles
            #[p_MeanSolarAngles['sun_zenith_angle'], p_MeanSolarAngles['sun_azimuth_angle']]
            LOGGER.debug(solarAngles)
            LOGGER.debug(
                EarthExplorerXMLFileHandler.EE_SCHEMA_DECIMAL_9_FORMAT)
            ltc.solarangle.azimuth = maja_utils.format_to_string(
                float(solarAngles[1]),
                EarthExplorerXMLFileHandler.EE_SCHEMA_DECIMAL_9_FORMAT)
            ltc.solarangle.zenith = maja_utils.format_to_string(
                float(solarAngles[0]),
                EarthExplorerXMLFileHandler.EE_SCHEMA_DECIMAL_9_FORMAT)
            viewingAngles = p_MeanViewingAngles
            ltc.viewingangle.azimuth = maja_utils.format_to_string(
                viewingAngles[1],
                EarthExplorerXMLFileHandler.EE_SCHEMA_DECIMAL_9_FORMAT)
            ltc.viewingangle.zenith = maja_utils.format_to_string(
                viewingAngles[0],
                EarthExplorerXMLFileHandler.EE_SCHEMA_DECIMAL_9_FORMAT)
            ltc.Date = lValidity_Start
            # Add the current LTC header
            pft = EarthExplorerXMLFileHandler.PackagedDBLFileType()
            pft.relativefilepath = os.path.join(
                l_DestinationRelativeDir,
                os.path.basename(p_OutputL2PrivateImageFilenamesProvider.
                                 get_ltc_image_filename()))
            pft.filedefinition = l_FileDefinition
            listOfpft.append(pft)
            listOfLTC.append(ltc)
        # Write to the output handler
        output_handler.write_private_ltc_header(listOfLTC)
        output_handler.write_private_ltc_header_list_of_dbl(listOfpft)
        # ****************************************************************************************
        # Save the file
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, schemaLocationDirectory)
        # ****************************************************************************************
        # Post processing : to be sur that there are only mha listed in the listOfpft present in the directory, remove others files
        # LAIG-FA-MAC-1630-CNES
        # ---------------------------------------------------------------------------------------
        # Get the Listed filename
        l_LTCProductMHAFilename = list()
        for itList in listOfpft:
            # Get file but onlyt the mha (without l_DestinationRelativeDir)
            # Ex :
            # <Relative_File_Path>VE_TEST_VSC_LUTANX_L2VALD_CAMARGUE_20120101_LTC.DBL.DIR/VE_TEST_VSC_LUTANX_L2VALD_CAMARGUE_20120103_LTC.DBL.mha</Relative_File_Path>
            l_LTCProductMHAFilename.append(
                os.path.basename(itList.relativefilepath))
        # ---------------------------------------------------------------------------------------
        # Get the Listed filename in the directory
        l_LTCProductMHAFilenameInDisc = [
            os.path.basename(f) for f in os.listdir(l_DestinationDir)
            if os.path.isfile(os.path.join(l_DestinationDir, f))
        ]

        # Remove the files on disc that are not listed as used
        for f in l_LTCProductMHAFilenameInDisc:
            if f not in l_LTCProductMHAFilename:
                file_utils.remove_file(os.path.join(l_DestinationDir, f))
 def write_private_tec_xmlhandler(self,
                                  tec_header_filename,
                                  p_ReferenceProductHeaderId,
                                  p_Validity_Start,
                                  p_Validity_Stop,
                                  p_Mission,
                                  p_NickName,
                                  p_AcquisitionDate,
                                  p_root_template_directory,
                                  p_SchemaLocationDirectory,
                                  pCurrentDate,
                                  p_EnableCheckXMLFilesWithSchema=True):
     # ---------------------------------------------------------------------------------------------------
     # Date PDV in UTC format
     # Get Validity
     lCreator_Version = self.plugin.Creator_Version
     lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION
     lReferenceProductHeaderId = p_ReferenceProductHeaderId
     l_TECFileType = "TEC_L2VALD"
     # ---------------------------------------------------------------------------------------------------
     if not self.productisvalid:
         l_TECFileType = "TEC_L2NOTV"
     # ---------------------------------------------------------------------------------------------------
     # Write TEC Private EEF
     LOGGER.debug("Write the PRIVATE TEC header file ...")
     output_filename = tec_header_filename  # p_L2ImageFilenamesProvider->GetPrivateFileNames().GetHDRPrivateFileName()
     current_header_filename = os.path.join(
         p_root_template_directory, self.plugin.TEMPLATE_TEC_PRIVATE_EEF)
     if not os.path.exists(current_header_filename):
         raise MajaExceptionPluginBase(
             "Internal error: the template file '" +
             current_header_filename + "' doesn't exist !!")
     # ---------------------------------------------------------------------------------------------
     # Load the template file used to writes the new header file
     # Load the file
     output_handler = EarthExplorerXMLFileHandler(current_header_filename)
     # ---------------------------------------------------------------------------------------------
     # Update bascis parameters
     output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                           output_filename, self.creator, lCreator_Version,
                           self.notes, self.system, self.outputfileclass,
                           pCurrentDate, lReferenceProductHeaderId)
     # Update other parameters
     output_handler.set_string_value("//Mission", p_Mission)
     # File_Type
     output_handler.set_string_value("//File_Type", l_TECFileType)
     # Nick_Name
     output_handler.set_string_value("//Nick_Name", p_NickName)
     # Acquisition_Date
     output_handler.set_string_value("//Acquisition_Date",
                                     p_AcquisitionDate)
     # Save to file
     output_handler.save_to_file(output_filename)
     LOGGER.debug(p_EnableCheckXMLFilesWithSchema)
     if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
         xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
    def write_private_sto_xmlhandler(self,
                                     sto_header_filename,
                                     p_ReferenceProductHeaderId,
                                     p_ReferenceProductInstance,
                                     p_dem,
                                     p_Validity_Start,
                                     p_Validity_Stop,
                                     p_Mission,
                                     p_root_template_directory,
                                     p_SchemaLocationDirectory,
                                     pCurrentDate,
                                     p_EnableCheckXMLFilesWithSchema=True):
        l_CoarseProductImageSizeX = p_dem.CoarseArea.size[0]
        l_CoarseProductImageSizeY = p_dem.CoarseArea.size[1]

        resol = 0
        l_DL2L2CoarseRatio = float(p_dem.L2Areas[resol].size[0]) / float(
            p_dem.CoarseArea.size[0])
        l_L2L2CoarseRatio = int(l_DL2L2CoarseRatio + 0.5)

        # ---------------------------------------------------------------------------------------------------
        # Date PDV in UTC format
        # Get Validity
        lCreator_Version = self.plugin.Creator_Version
        lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION
        lReferenceProductHeaderId = p_ReferenceProductHeaderId
        lReferenceProductInstance = p_ReferenceProductInstance
        # ---------------------------------------------------------------------------------------------------
        # Write STO
        LOGGER.debug("Write the PRIVATE STO header file ...")
        output_filename = sto_header_filename  # p_L2PrivateImageFilenamesProvider.GetSTOHeaderFileName()
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_STO_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        # Update other parameter
        output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)

        output_handler.set_string_value("//Size/Bands",
                                        self.numberofstackimages)
        output_handler.set_list_of_bands(
            self.stolistofstringdates)  # , "//List_of_Bands", "Band")
        # The Band_Theoretical_Wavelength could be not exist (ex: VENUS)
        # Check the existance of the Node in the template xml file, before write the value
        if xml_tools.get_only_value(output_handler.root,
                                    "//Band_Theoretical_Wavelength",
                                    check=True) is not None:
            output_handler.set_string_value(
                "//Band_Theoretical_Wavelength",
                self.correlbandtheoreticalwavelengthforstocomposite)
        # Save to file
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
    def write_private_others_xmlhandler(self,
                                        p_L2PrivateImageFilenamesProvider,
                                        p_ReferenceProductHeaderId,
                                        p_ReferenceProductInstance,
                                        p_dem,
                                        p_Validity_Start,
                                        p_Validity_Stop,
                                        p_Mission,
                                        p_root_template_directory,
                                        p_SchemaLocationDirectory,
                                        pCurrentDate,
                                        p_EnableCheckXMLFilesWithSchema=True):
        LOGGER.debug("L2HeaderWriterBase __dict__ %s", self.__dict__)
        l_CoarseProductImageSizeX = p_dem.CoarseArea.size[0]
        l_CoarseProductImageSizeY = p_dem.CoarseArea.size[1]
        resol = 0
        l_DL2L2CoarseRatio = float(p_dem.L2Areas[resol].size[0]) / float(
            p_dem.CoarseArea.size[0])
        l_L2L2CoarseRatio = int(l_DL2L2CoarseRatio + 0.5)
        LOGGER.debug("Real L2L2CoarseRatio: " + str(l_DL2L2CoarseRatio) +
                     " -> integer: " + str(l_L2L2CoarseRatio))
        # ---------------------------------------------------------------------------------------------------
        # Date PDV in UTC format
        # Get Validity
        lCreator_Version = self.plugin.Creator_Version
        lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION
        lReferenceProductHeaderId = p_ReferenceProductHeaderId
        lReferenceProductInstance = p_ReferenceProductInstance
        # ---------------------------------------------------------------------------------------------------
        # Write CLD
        output_filename = p_L2PrivateImageFilenamesProvider.get_cld_header_filename(
        )
        LOGGER.debug("Write the PRIVATE CLD header file " + output_filename +
                     " ...")
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_CLD_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")

        LOGGER.debug("LoadFile '" + current_header_filename + "...")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        # Update other parameter
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        l_CLDNbBits = self.plugin.NumbersOfBitsInCLDDataIOBand
        output_handler.set_string_value("//Number_of_Significant_Bits",
                                        l_CLDNbBits)
        if l_CLDNbBits <= 8:
            output_handler.set_string_value("//Data_Type", "UNSIGNED_BYTE")
        elif l_CLDNbBits <= 16:
            output_handler.set_string_value("//Data_Type", "UNSIGNED_SHORT")
        else:
            raise MajaExceptionPluginBase(
                "Internal error:  CLD band must be written with [1..16] bits !"
            )
        # save file
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write NDT
        LOGGER.debug("Write the PRIVATE NDT header file ...")
        output_filename = p_L2PrivateImageFilenamesProvider.get_ndt_header_filename(
        )
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_NDT_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        # Update other parameter
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write WAM
        LOGGER.debug("Write the PRIVATE WAM header file ...")
        output_filename = p_L2PrivateImageFilenamesProvider.get_wam_header_filename(
        )
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_WAM_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # -----------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        # Update other parameter
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write CLA
        # LAIG-DM-MAC-1691-CNES : Write CLA only is available (only for VENUS - stereo)
        if os.path.exists(
                p_L2PrivateImageFilenamesProvider.get_cla_image_filename()):
            LOGGER.debug("Write the PRIVATE CLA header file ...")
            output_filename = p_L2PrivateImageFilenamesProvider.get_cla_header_filename(
            )

            current_header_filename = os.path.join(
                p_root_template_directory,
                self.plugin.TEMPLATE_PDTANX_PRIVATE_CLA_HDR)
            if not os.path.exists(current_header_filename):
                raise MajaExceptionPluginBase(
                    "Internal error: the template file '" +
                    current_header_filename + "' doesn't exist !!")
            # ---------------------------------------------------------------------------------------------------
            # Load the file
            output_handler = EarthExplorerXMLFileHandler(
                current_header_filename)
            # ---------------------------------------------------------------------------------------------
            # Update bascis parameters
            output_handler.update(p_Validity_Start, p_Validity_Stop,
                                  lFile_Version, output_filename, self.creator,
                                  lCreator_Version, self.notes, self.system,
                                  self.outputfileclass, pCurrentDate,
                                  lReferenceProductHeaderId)
            # ---------------------------------------------------------------------------------------------
            # Update bascis ANX parameters
            output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                         l_CoarseProductImageSizeY,
                                         lReferenceProductInstance,
                                         self.productisvalid)
            # Update other parameter
            output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
            output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                            l_L2L2CoarseRatio)
            output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                            l_L2L2CoarseRatio)
            output_handler.save_to_file(output_filename)
            if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
                xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write PXD
        LOGGER.debug("Write the PRIVATE PXD header file ...")
        output_filename = p_L2PrivateImageFilenamesProvider.get_pxd_header_filename(
        )
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_PXD_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        # Update other parameter
        output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        # Save to file
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
    def write_private_reflectances_xmlhandler(
            self,
            p_L2PrivateImageFilenamesProvider,
            p_ReferenceProductHeaderId,
            p_ReferenceProductInstance,
            p_dem,
            p_Validity_Start,
            p_Validity_Stop,
            p_Mission,
            p_root_template_directory,
            p_SchemaLocationDirectory,
            pCurrentDate,
            p_EnableCheckXMLFilesWithSchema=True):
        # ---------------------------------------------------------------------------------------------------
        l_CoarseProductImageSizeX = p_dem.CoarseArea.size[0]
        l_CoarseProductImageSizeY = p_dem.CoarseArea.size[1]
        resol = 0
        l_DL2L2CoarseRatio = float(p_dem.L2Areas[resol].size[0]) / float(
            p_dem.CoarseArea.size[0])
        l_L2L2CoarseRatio = int(l_DL2L2CoarseRatio + 0.5)
        LOGGER.debug("Real L2L2CoarseRatio: " + str(l_DL2L2CoarseRatio) +
                     " -> integer: " + str(l_L2L2CoarseRatio))
        # ---------------------------------------------------------------------------------------------------
        # Date PDV in UTC format
        # Get Validity
        lCreator_Version = self.plugin.Creator_Version
        lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION

        lReferenceProductHeaderId = str(p_ReferenceProductHeaderId)
        lReferenceProductInstance = str(p_ReferenceProductInstance)

        # ---------------------------------------------------------------------------------------------------
        # Write RCR
        LOGGER.debug("Write the PRIVATE RCR header file ...")
        output_filename = p_L2PrivateImageFilenamesProvider.get_rcr_header_filename(
        )
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_RCR_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # Attribut for theoretical wavelength
        l_attr = {}
        l_attr["unit"] = "nanometer"
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        output_handler.set_string_value("//Annex_Code", "RCR")
        output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        #        output_handler->SetUIntegersListValueSortedByAttribut(m_ListOfBandTheoreticalWavelengthForReflectancesComposite,
        #                "//List_of_Band_Theoretical_Wavelength", "Band_Theoretical_Wavelength", "count", "sn", l_attr)
        xml_tools.set_string_list_value_sorted_by_attribute(
            output_handler.root,
            self.listofbandtheoreticalwavelengthforreflectancescomposite,
            "//List_of_Band_Theoretical_Wavelength",
            "Band_Theoretical_Wavelength", "count", "sn", 1, l_attr)
        output_handler.set_string_value(
            "//Bands",
            str(
                len(self.
                    listofbandtheoreticalwavelengthforreflectancescomposite)))
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write RTA
        LOGGER.debug("Write the PRIVATE RTA header file ...")
        output_filename = p_L2PrivateImageFilenamesProvider.get_rta_header_filename(
        )
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_RCR_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)
        # Set other values
        output_handler.set_string_value("//Annex_Code", "RTA")
        output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        xml_tools.set_string_list_value_sorted_by_attribute(
            output_handler.root,
            self.listofbandtheoreticalwavelengthforreflectancescomposite,
            "//List_of_Band_Theoretical_Wavelength",
            "Band_Theoretical_Wavelength", "count", "sn", 1, l_attr)
        output_handler.set_string_value(
            "//Bands",
            len(self.listofbandtheoreticalwavelengthforreflectancescomposite))
        # Save file
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write RTC
        LOGGER.debug("Write the PRIVATE RTA header file ...")
        output_filename = p_L2PrivateImageFilenamesProvider.get_rtc_header_filename(
        )
        current_header_filename = os.path.join(
            p_root_template_directory,
            self.plugin.TEMPLATE_PDTANX_PRIVATE_RCR_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaExceptionPluginBase(
                "Internal error: the template file '" +
                current_header_filename + "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------------
        # Load the file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version,
                              output_filename, self.creator, lCreator_Version,
                              self.notes, self.system, self.outputfileclass,
                              pCurrentDate, lReferenceProductHeaderId)
        # ---------------------------------------------------------------------------------------------
        # Update bascis ANX parameters
        output_handler.update_pdtanx(p_Mission, l_CoarseProductImageSizeX,
                                     l_CoarseProductImageSizeY,
                                     lReferenceProductInstance,
                                     self.productisvalid)

        output_handler.set_string_value("//Annex_Code", "RTC")
        output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
        output_handler.set_string_value("//Subsampling_Factor/By_Line",
                                        l_L2L2CoarseRatio)
        output_handler.set_string_value("//Subsampling_Factor/By_Column",
                                        l_L2L2CoarseRatio)
        xml_tools.set_string_list_value_sorted_by_attribute(
            output_handler.root,
            self.listofbandtheoreticalwavelengthforreflectancescomposite,
            "//List_of_Band_Theoretical_Wavelength",
            "Band_Theoretical_Wavelength", "count", "sn", 1, l_attr)
        output_handler.set_string_value(
            "//Bands",
            len(self.listofbandtheoreticalwavelengthforreflectancescomposite))
        # Save file
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid and p_EnableCheckXMLFilesWithSchema:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
Exemple #10
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)
Exemple #11
0
    def initialize(self, filename, working_dir, has_snow):
        file_hdr = os.path.splitext(filename)[0] + ".HDR"
        file_dbl = os.path.splitext(filename)[0] + ".DBL"
        file_dbldir = os.path.splitext(filename)[0] + ".DBL.DIR"
        LOGGER.info("AUX_REFDE2 filename: " + filename)
        # uncompress dbl
        uncompress_dbl_product(file_dbl)

        # DEM filenames provider
        list_of_file = os.listdir(file_dbldir)
        nbresol = 0
        for f in list_of_file:
            if "_ALT" in f and "TIF" in os.path.splitext(f)[1]:
                nbresol = nbresol + 1
        LOGGER.info("Nb resolution found " + str(nbresol))
        self.initialize_res_list(nbresol)

        LOGGER.info(
            "DEMFilenamesProvider::Initialize. Nb resolution computed:" +
            str(len(self._resList)))
        for resol in self._resList:
            LOGGER.debug("DEMFilenamesProvider::Initialize. Prefix resol : " +
                         resol)

        handler = EarthExplorerXMLFileHandler(file_hdr)
        list_of_dbl_files = handler.get_list_of_packaged_dbl_files(True, False)
        LOGGER.info("DEMFileNames found " + str(len(list_of_dbl_files)) +
                    " files")

        for i in range(0, len(list_of_dbl_files)):
            if list_of_dbl_files[i].split('.TIF')[-1]:
                raise MajaDataException(
                    "Wrong file extension detected. Delete the file: " +
                    str(list_of_dbl_files[i]))

        # --------------------------------------
        # Find the correct filename
        for fi in list_of_dbl_files:
            # LAIG - FA - MAC - 1610 - CNES
            # ../ CONTEXTES_ANOMALIES / TMA_VENUS_maccs_errors / 4398 / VE_TEST_AUX_REFDE2_BRASCHAT_0001.DBL.DIR / VE_TEST_AUX_REFDE2_BRASCHAT_0001_SLP.TIF
            # Extract the last value -> SLP
            # ../ CONTEXTES_ANOMALIES / TMA_VENUS_maccs_errors / 4398 / VE_TEST_AUX_REFDE2_BRASCHAT_0001.DBL.DIR / VE_TEST_AUX_REFDE2_BRASCHAT_0001_ALT_R1.TIF
            # Extract the last value -> ALT
            l_splitted = (os.path.splitext(os.path.basename(fi))[0]).split("_")
            l_lenghtlistfilenamename = len(l_splitted)
            # Extract the tow last values -> ex: 0001 _SLP or ALT_R1
            l_keytype = l_splitted[-1]
            if l_lenghtlistfilenamename > 2:
                l_keytype = l_splitted[-2] + "_" + l_keytype
            # --------------------------------------
            # Test if the filename is ALC
            if "ALC" in l_keytype:
                self.ALC = fi
            # --------------------------------------
            # Test if the filename is MSK
            elif "MSK" in l_keytype:
                self.MSK = fi
            # --------------------------------------
            # Test if the filename is ASC
            elif "ASC" in l_keytype:
                self.ASC = fi
            # --------------------------------------
            # Test if the filename is SLC
            elif "SLC" in l_keytype:
                self.__SLCInternal = fi
            else:
                # --------------------------------------
                # Lop under resolutions
                for res in self._resList:
                    # --------------------------------------
                    # Test if the filename is SLP
                    if "SLP" in l_keytype:
                        if res in l_keytype:
                            self.__SLPListInternal.append(fi)
                    # --------------------------------------
                    # Test if the filename is ALT
                    elif "ALT" in l_keytype:
                        if res in l_keytype:
                            self.ALTList.append(fi)
                    # --------------------------------------
                    # Test if the filename is ASP
                    elif "ASP" in l_keytype:
                        if res in l_keytype:
                            self.__ASPListInternal.append(fi)
                    else:
                        LOGGER.debug(
                            "Unknown Filename and associated product type.")
                # endloop resol
        # --------------------------------------
        # Check existent of ALC filename
        if not os.path.exists(self.ALC):
            raise MajaDataException("The ALC file '" + self.ALC +
                                    "' of the DTM doesn't exist !")

        # --------------------------------------
        # Check existent of MSK filename
        if not os.path.exists(self.MSK):
            raise MajaDataException("The MSK file '" + self.MSK +
                                    "' of the DTM doesn't exist !")

        # --------------------------------------
        # Check existent of SLC filename
        if not os.path.exists(self.__SLCInternal):
            raise MajaDataException("The SLC file '" + self.__SLCInternal +
                                    "' of the DTM doesn't exist !")
        else:
            LOGGER.debug("Starting multiply " + self.__SLCInternal + " * " +
                         str(self._coeff))
            self.SLC = os.path.join(
                working_dir, "Mul_" + os.path.basename(self.__SLCInternal))
            self._apps.add_otb_app(
                multiply_by_scalar(self.__SLCInternal,
                                   self._coeff,
                                   output_image=self.SLC))
            mtdat = GdalDatasetInfo(self.__SLCInternal)
            self.CoarseArea = Area()
            self.CoarseArea.size = mtdat.size
            self.CoarseArea.origin = mtdat.origin
            self.CoarseArea.spacing = mtdat.pixel_size
            LOGGER.debug("Done")

        # --------------------------------------
        for resol in range(0, len(self._resList)):
            # --------------------------------------
            # Check existent of SLP filename
            if not os.path.exists(self.__SLPListInternal[resol]):
                raise MajaDataException("One of the SLP file '" +
                                        self.__SLPListInternal[resol] +
                                        "' of the DTM doesn't exist !")
            else:
                LOGGER.debug("Starting multiply " +
                             self.__SLPListInternal[resol] + " * " +
                             str(self._coeff))
                tmp = os.path.join(
                    working_dir,
                    "Mul_" + os.path.basename(self.__SLPListInternal[resol]))
                slp_mul_app = multiply_by_scalar(self.__SLPListInternal[resol],
                                                 self._coeff,
                                                 output_image=tmp,
                                                 write_output=False)
                self._apps.add_otb_app(slp_mul_app)
                mtdat = GdalDatasetInfo(self.__SLPListInternal[resol])
                l2area = Area()
                l2area.size = mtdat.size
                l2area.origin = mtdat.origin
                l2area.spacing = mtdat.pixel_size
                self.ProjRef = mtdat.dataset.GetProjectionRef()
                self.L2Areas.append(l2area)
                LOGGER.debug("Done")
                self.SLPList.append(slp_mul_app.getoutput().get("out"))
            # --------------------------------------
            # Check existent of ALT filename
            if not os.path.exists(self.ALTList[resol]):
                raise MajaDataException("One of the ALT file '" +
                                        self.ALTList[resol] +
                                        "' of the DTM doesn't exist !")
            # --------------------------------------
            # Check existent of ASP filename
            if not os.path.exists(self.__ASPListInternal[resol]):
                raise MajaDataException("One of the ASP file '" +
                                        self.__ASPListInternal[resol] +
                                        "' of the DTM doesn't exist !")
            else:
                LOGGER.debug("Starting multiply " +
                             self.__ASPListInternal[resol] + " * " +
                             str(self._coeff))
                tmp = os.path.join(
                    working_dir,
                    "Mul_" + os.path.basename(self.__ASPListInternal[resol]))
                asp_mul_app = multiply_by_scalar(self.__ASPListInternal[resol],
                                                 self._coeff,
                                                 output_image=tmp,
                                                 write_output=False)
                self._apps.add_otb_app(asp_mul_app)
                LOGGER.debug("Done")
                self.ASPList.append(asp_mul_app.getoutput().get("out"))

        # end loop resol

        LOGGER.debug(nbresol)

        l_cartoCode = xml_tools.get_only_value(
            handler.root,
            "//DEM_Information/Cartographic/Coordinate_Reference_System/Code",
            namespaces=handler.nss,
            check=True)
        l_geoCode = xml_tools.get_only_value(
            handler.root,
            "//DEM_Information/Geographic/Coordinate_Reference_System/Code",
            namespaces=handler.nss,
            check=True)
        if l_cartoCode is not None:
            self.ProjCode = l_cartoCode.text
            self.ProjType = "PROJECTED"
        elif l_geoCode is not None:
            self.ProjCode = l_geoCode.text
            self.ProjType = "GEOGRAPHIC"
        else:
            raise MajaDataException("Unknown DEM type")

        LOGGER.debug("DEM Projection Code: " + self.ProjCode)
        LOGGER.debug("DEM Projection Type: " + self.ProjType)

        self.Site = xml_tools.get_xml_string_value(
            handler.root,
            "//Specific_Product_Header/Instance_Id/Applicable_Site_Nick_Name",
            namespaces=handler.nss)

        if nbresol != 0:
            param_stats = {"im": self.ALTList[0]}
            stat_app = stats(self.ALTList[0])
            self.ALT_Mean = stat_app.getoutput().get("mean")
            self.ALT_Max = stat_app.getoutput().get("max")
            self.ALT_Min = stat_app.getoutput().get("min")
            self.ALT_Stdv = stat_app.getoutput().get("stdv")
            self.ALT_LogicalName = "LOCAL=" + os.path.splitext(
                os.path.basename(file_hdr))[0]
            LOGGER.info("DEM Mean : " + str(self.ALT_Mean))
            LOGGER.info("DEM Max : " + str(self.ALT_Max))
            LOGGER.info("DEM Min : " + str(self.ALT_Min))
            LOGGER.info("DEM Stdv : " + str(self.ALT_Stdv))
    def write_public_xml_handler(self, p_L2ImageFilenamesProvider, p_dem, p_Validity_Start, p_Validity_Stop, p_Mission,
                              p_root_template_directory, p_SchemaLocationDirectory, pCurrentDate):

        l_NumberOfResolutions = len(p_L2ImageFilenamesProvider.get_sre_headers())
        LOGGER.debug("L2ImageFileReader::Initialize Number of resolutions: " + str(l_NumberOfResolutions) + ".")

        # Write Public Headers

        output_filename = ""
        # ---------------------------------------
        # Get global information about the output L2 product
        # ---------------------------------------
        lCreator_Version = self.plugin.Creator_Version
        lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION
        # Get Validity
        lReferenceProductHeaderId = p_L2ImageFilenamesProvider.get_reference_product_header_id()
        lReferenceProductInstance = p_L2ImageFilenamesProvider.get_reference_product_instance()
        LOGGER.debug("p_L2ImageFilenamesProvider->GetReferenceProductHeaderId() <" +
                     p_L2ImageFilenamesProvider.get_reference_product_header_id() + ">")
        LOGGER.debug("p_L2ImageFilenamesProvider->GetReferenceProductInstance() <" +
                     p_L2ImageFilenamesProvider.get_reference_product_instance() + ">")
        LOGGER.debug("Writing xml file <" + output_filename + ">...")
        # ---------------------------------------------------------------------------------------------------
        l_CoarseProductImageSizeX = p_dem.CoarseArea.size[0]
        l_CoarseProductImageSizeY = p_dem.CoarseArea.size[1]

        l_CLDNbBits = self.plugin.NumbersOfBitsInCLDDataIOBand
        l_BandsDefinitions = self.plugin.BandsDefinitions

        # *************************************************************************************************************
        # Write headers for all resolution
        # *************************************************************************************************************
        for resol in range(l_NumberOfResolutions):
            # Resolution information
            # ---------------------------------------
            l_ProductImageSizeX = p_dem.L2Areas[resol].size[0]
            l_ProductImageSizeY = p_dem.L2Areas[resol].size[1]
            l_ResollistOfBandCode = l_BandsDefinitions.get_list_of_l2_band_code(l_BandsDefinitions.ListOfL2Resolution[resol])

            # Write Public Headers
            if self.adjacencyeffectsandslopecorrection:
                # ---------------------------------------------------------------------------------------------------
                # Write FRE
                LOGGER.debug("Write the FRE header file ...")
                output_filename = p_L2ImageFilenamesProvider.get_fre_headers()[resol]

                current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTIMG_HDR)
                output_handler = EarthExplorerXMLFileHandler(current_header_filename)
                if not os.path.exists(current_header_filename):
                    raise MajaDataException(
                        "Internal error: the template file '" +
                        current_header_filename +
                        "' doesn't exist !!")
                LOGGER.debug("Update file ...")
                # Update bascis parameters
                output_handler.update(
                    p_Validity_Start,
                    p_Validity_Stop,
                    lFile_Version,
                    output_filename,
                    self.creator,
                    lCreator_Version,
                    self.notes,
                    self.system,
                    self.outputfileclass,
                    pCurrentDate,
                    lReferenceProductHeaderId)

                # ---------------------------------------------------------------------------------------------
                # Update bascis ANX parameters
                output_handler.update_pdtanx(
                    p_Mission,
                    l_ProductImageSizeX,
                    l_ProductImageSizeY,
                    lReferenceProductInstance,
                    self.productisvalid)
                output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
                output_handler.set_list_of_bands(l_ResollistOfBandCode)  # , "//List_of_Bands", "Band")
                output_handler.set_string_value("//Bands", str(len(l_ResollistOfBandCode)))
                # Save File
                LOGGER.debug("SaveFile file ...")
                output_handler.save_to_file(output_filename)
                if self.checkxmlfileswithschema and self.productisvalid:
                    xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)

            # ---------------------------------------------------------------------------------------------------
            # Write SRE
            LOGGER.debug("Write the SRE header file ...")
            output_filename = p_L2ImageFilenamesProvider.get_sre_headers()[resol]
            LOGGER.debug("p_L2ImageFilenamesProvider->GetReferenceProductHeaderId() <" +
                         p_L2ImageFilenamesProvider.get_reference_product_header_id() + ">")
            LOGGER.debug("p_L2ImageFilenamesProvider->GetReferenceProductInstance() <" +
                         p_L2ImageFilenamesProvider.get_reference_product_instance() + ">")
            LOGGER.debug("Writing xml file <" + output_filename + ">...")
            current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTIMG_HDR)
            if not os.path.exists(current_header_filename):
                raise MajaDataException(
                    "Internal error: the template file '" + current_header_filename + "' doesn't exist !!")
            output_handler = EarthExplorerXMLFileHandler(current_header_filename)
            # ---------------------------------------------------------------------------------------------
            # Update bascis parameters
            output_handler.update(
                p_Validity_Start,
                p_Validity_Stop,
                lFile_Version,
                output_filename,
                self.creator,
                lCreator_Version,
                self.notes,
                self.system,
                self.outputfileclass,
                pCurrentDate,
                lReferenceProductHeaderId)
            # ---------------------------------------------------------------------------------------------
            # Update bascis ANX parameters
            output_handler.update_pdtanx(p_Mission, l_ProductImageSizeX, l_ProductImageSizeY, lReferenceProductInstance,
                                         self.productisvalid)
            output_handler.set_string_value("//Nodata_Value", self.nodatavalue)
            output_handler.set_list_of_bands(l_ResollistOfBandCode)  # , "//List_of_Bands", "Band")
            output_handler.set_string_value("//Bands", str(len(l_ResollistOfBandCode)))
            LOGGER.debug("SaveFile file ...")
            # Save File
            output_handler.save_to_file(output_filename)
            if self.checkxmlfileswithschema and self.productisvalid:
                xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
            # ---------------------------------------------------------------------------------------------------
            # Write ATB
            LOGGER.debug("Write the ATB header file ...")
            output_filename = p_L2ImageFilenamesProvider.get_atb_header_filenames()[resol]
            current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTANX_ATB_HDR)
            if not os.path.exists(current_header_filename):
                raise MajaDataException(
                    "Internal error: the template file '" + current_header_filename + "' doesn't exist !!")
            output_handler = EarthExplorerXMLFileHandler(current_header_filename)
            # ---------------------------------------------------------------------------------------------
            # Update bascis parameters
            output_handler.update(
                p_Validity_Start,
                p_Validity_Stop,
                lFile_Version,
                output_filename,
                self.creator,
                lCreator_Version,
                self.notes,
                self.system,
                self.outputfileclass,
                pCurrentDate,
                lReferenceProductHeaderId)

            # ---------------------------------------------------------------------------------------------
            # Update bascis ANX parameters
            output_handler.update_pdtanx(p_Mission, l_ProductImageSizeX, l_ProductImageSizeY, lReferenceProductInstance,
                                         self.productisvalid)
            output_handler.set_string_value("//VAP_Nodata_Value", self.vapnodatavalue)
            output_handler.set_string_value("//VAP_Quantification_Value", self.vapquantificationvalue)
            output_handler.set_string_value("//AOT_Nodata_Value", self.aotnodatavalue)
            output_handler.set_string_value("//AOT_Quantification_Value", self.aotquantificationvalue)
            # Save File
            output_handler.save_to_file(output_filename)
            if self.checkxmlfileswithschema and self.productisvalid:
                xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
            # ---------------------------------------------------------------------------------------------------
            # Write CLD
            LOGGER.debug("Write the CLD header file ...")
            output_filename = p_L2ImageFilenamesProvider.get_cld_header_filenames()[resol]
            current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTANX_CLD_HDR)
            if not os.path.exists(current_header_filename):
                raise MajaDataException(
                    "Internal error: the template file '" + current_header_filename + "' doesn't exist !!")
            output_handler = EarthExplorerXMLFileHandler(current_header_filename)
            # ---------------------------------------------------------------------------------------------
            # Update bascis parameters
            output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version, output_filename, self.creator,
                                  lCreator_Version,
                                  self.notes, self.system, self.outputfileclass, pCurrentDate,
                                  lReferenceProductHeaderId)
            output_handler.set_string_value("//Number_of_Significant_Bits", l_CLDNbBits)
            if l_CLDNbBits <= 8:
                output_handler.set_string_value("//Data_Type", "UNSIGNED_BYTE")
            elif l_CLDNbBits <= 16:
                output_handler.set_string_value("//Data_Type", "UNSIGNED_SHORT")
            else:
                raise MajaDataException("Internal error:  CLD band must be written with [1..16] bits !")
            # ---------------------------------------------------------------------------------------------
            # Update bascis ANX parameters
            output_handler.update_pdtanx(p_Mission, l_ProductImageSizeX, l_ProductImageSizeY, lReferenceProductInstance,
                                         self.productisvalid)
            # Save File
            output_handler.save_to_file(output_filename)
            if self.checkxmlfileswithschema and self.productisvalid:
                xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
            # ---------------------------------------------------------------------------------------------------
            # Write MSK
            LOGGER.debug("Write the MSK header file ...")
            output_filename = p_L2ImageFilenamesProvider.get_msk_header_filenames()[resol]
            current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTANX_MSK_HDR)
            if not os.path.exists(current_header_filename):
                raise MajaDataException(
                    "Internal error: the template file '" + current_header_filename + "' doesn't exist !!")
            output_handler = EarthExplorerXMLFileHandler(current_header_filename)
            # ---------------------------------------------------------------------------------------------
            # Update bascis parameters
            output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version, output_filename, self.creator,
                                  lCreator_Version,
                                  self.notes, self.system, self.outputfileclass, pCurrentDate,
                                  lReferenceProductHeaderId)
            # ---------------------------------------------------------------------------------------------
            # Update bascis ANX parameters
            output_handler.update_pdtanx(p_Mission, l_ProductImageSizeX, l_ProductImageSizeY, lReferenceProductInstance,
                                         self.productisvalid)
            # Save File
            output_handler.save_to_file(output_filename)
            if self.checkxmlfileswithschema and self.productisvalid:
                xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
            # ---------------------------------------------------------------------------------------------------
            # Write QLT
            LOGGER.debug("Write the QLT header file ...")
            output_filename = p_L2ImageFilenamesProvider.get_qlt_header_filenames()[resol]
            current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTANX_QLT_HDR)
            if not os.path.exists(current_header_filename):
                raise MajaDataException(
                    "Internal error: the template file '" + current_header_filename + "' doesn't exist !!")
            output_handler = EarthExplorerXMLFileHandler(current_header_filename)
            # ---------------------------------------------------------------------------------------------
            # Update bascis parameters
            output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version, output_filename, self.creator,
                                  lCreator_Version,
                                  self.notes, self.system, self.outputfileclass, pCurrentDate,
                                  lReferenceProductHeaderId)
            # ---------------------------------------------------------------------------------------------
            # Update bascis ANX parameters
            output_handler.update_pdtanx(p_Mission, l_ProductImageSizeX, l_ProductImageSizeY, lReferenceProductInstance,
                                         self.productisvalid)
            # Save File
            output_handler.save_to_file(output_filename)
            if self.checkxmlfileswithschema and self.productisvalid:
                xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
        # ---------------------------------------------------------------------------------------------------
        # Write QLK
        LOGGER.debug("Write the QLK header file ...")
        output_filename = p_L2ImageFilenamesProvider.get_qlk_header_filename()
        current_header_filename = os.path.join(p_root_template_directory, self.plugin.TEMPLATE_PDTQLK_HDR)
        if not os.path.exists(current_header_filename):
            raise MajaDataException(
                "Internal error: the template file '" + current_header_filename + "' doesn't exist !!")
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------------------------------------------------------------
        # Update bascis parameters
        output_handler.update(p_Validity_Start, p_Validity_Stop, lFile_Version, output_filename, self.creator,
                              lCreator_Version,
                              self.notes, self.system, self.outputfileclass, pCurrentDate,
                              lReferenceProductHeaderId)
        output_handler.set_string_value("//Mission", p_Mission)
        output_handler.set_string_value("//Lines", l_CoarseProductImageSizeY)
        output_handler.set_string_value("//Columns", l_CoarseProductImageSizeX)
        # Instance_Id node
        output_handler.set_string_value("//Reference_Product_Instance", lReferenceProductInstance)
        if self.productisvalid:
            output_handler.set_string_value("//Reference_Product_Semantic", "L2VALD")
        else:
            output_handler.set_string_value("//Reference_Product_Semantic", "L2NOTV")
        # Set the band informations
        listOfBands = list()
        listOfBands.append(self.quicklookredbandcode)
        listOfBands.append(self.quicklookgreenbandcode)
        listOfBands.append(self.quicklookbluebandcode)
        output_handler.set_string_value("//Bands", "3")
        RedBandId = 0
        GreenBandId = 0
        BlueBandId = 0
        ResolutionIndex = 0
        l_ResolutionForQLK = ""
        l_BandsDefinitions.get_l2_information_for_quicklook_band_code(
            self.quicklookredbandcode,
            self.quicklookgreenbandcode,
            self.quicklookbluebandcode
        )
        l_DL2L2CoarseRatio = float(p_dem.L2Areas[ResolutionIndex].size[0]) / float(p_dem.CoarseArea.size[0])
        l_L2L2CoarseRatio = int(l_DL2L2CoarseRatio + 0.5)

        output_handler.set_string_value("//Subsampling_Factor", l_L2L2CoarseRatio)
        output_handler.set_list_of_bands(listOfBands)  # , "//List_of_Bands", "Band")
        # Save File
        output_handler.save_to_file(output_filename)
        if self.checkxmlfileswithschema and self.productisvalid:
            xml_tools.check_xml(output_filename, p_SchemaLocationDirectory)
    def write_global_xmlhandler(self, root_sc_xml_templates,
                                l1_input_xmlhandler, l1_input_tile_xmlhandler,
                                output_filename, p_FileType,
                                schemaLocationDirectory):
        input_dir = self.l2imagefilenamesprovider.m_PublicDirectory
        # Get global information about the output L2 product
        # ---------------------------------------
        lCurrentDate = datetime.datetime.now().strftime(
            EarthExplorerXMLFileHandler.XML_FORMAT_DATE)
        lCreator_Version = "3.2.2"
        lFile_Version = self.plugin.L2PRODUCT_FILE_VERSION
        # ---------------------------------------------------------------------------------------------------
        LOGGER.debug("Write the GLOBAL header file ...")
        current_header_filename = root_sc_xml_templates
        if not os.path.exists(current_header_filename):
            raise MajaDataException("Internal error: the template file '" +
                                    current_header_filename +
                                    "' doesn't exist !!")
        # ---------------------------------------------------------------------------------------------
        # Load the template file used to writes the new header file
        output_handler = EarthExplorerXMLFileHandler(current_header_filename)
        # ---------------------------------------
        # Set the main header information
        # ---------------------------------------
        EE_File_Name = os.path.basename(output_filename)
        LOGGER.debug("output_filename: " + output_filename)
        output_handler.set_string_value("//File_Version", lFile_Version)
        output_handler.set_string_value("//File_Name",
                                        os.path.splitext(EE_File_Name)[0])
        output_handler.set_string_value("//Mission",
                                        l1_input_xmlhandler.get_mission())
        output_handler.set_string_value(
            "//Validity_Start",
            l1_input_tile_xmlhandler.get_validity_start_date())
        output_handler.set_string_value(
            "//Validity_Stop",
            l1_input_tile_xmlhandler.get_validity_stop_date())
        output_handler.set_string_value("//Creator", self.creator)
        output_handler.set_string_value("//Creator_Version", lCreator_Version)
        output_handler.set_string_value("//Notes", self.notes)
        output_handler.set_string_value("//System", self.system)
        output_handler.set_string_value("//File_Class", self.outputfileclass)
        output_handler.set_string_value("//File_Type", p_FileType)
        output_handler.set_string_value("//Creation_Date", lCurrentDate)
        # Nick_Name
        # Format site under 8 characters
        output_handler.set_string_value(
            "//Nick_Name",
            maja_utils.get_formated_site(
                self.l1imageinformationsproviderbase.Site))

        # Acquisition_Date
        output_handler.set_string_value(
            "//Acquisition_Date",
            l1_input_tile_xmlhandler.get_date_formated_yyyymmdd())

        if self.initmode:
            output_handler.set_string_value(
                "//Processing_Flags_And_Modes/Processing_Mode", "INIT_MODE")
        elif self.backwardmode:
            output_handler.set_string_value(
                "//Processing_Flags_And_Modes/Processing_Mode",
                "BACKWARD_MODE")
        else:
            output_handler.set_string_value(
                "//Processing_Flags_And_Modes/Processing_Mode",
                "STANDARD_MODE")
        output_handler.set_string_value(
            "//Adjacency_Effects_And_Slope_Correction",
            self.adjacencyeffectsandslopecorrection)
        output_handler.set_string_value(
            "//Processing_Flags_And_Modes/Cirrus_Correction",
            self.cirruscorrection)

        # Acquisition_Date_Time
        output_handler.set_string_value(
            "//Acquisition_Date_Time",
            l1_input_tile_xmlhandler.get_date_formated_utc())
        output_handler.set_string_value(
            "//Acquisition_Orbit_Number",
            l1_input_xmlhandler.get_string_value_of("OrbitNumber"))

        output_handler.set_string_value("//Web_Site_URL",
                                        self.plugin.WebSiteURL)

        output_handler.set_string_value("//Processing_Information/Date_Time",
                                        lCurrentDate)

        LOGGER.debug("Before WriteListofGIPPFiles")

        # ---------------------------------------
        # List_of_GIPP_Files
        # ---------------------------------------
        listOfGipps = EarthExplorerUtilities.convert_gipp_filenames_to_file_types(
            self.listofgippsfilenames)
        output_handler.write_list_of_gipp_files(listOfGipps)
        # ---------------------------------------
        # List_of_Quality_Indexes
        # ---------------------------------------
        listOfQualityInd = []
        qI = EarthExplorerXMLFileHandler.QualityIndex()
        qI.Code = EarthExplorerXMLFileHandler.CONST_RAIN_DETECTED_NV
        qI.Value = str(self.rainflag)
        listOfQualityInd.append(qI)
        qI = EarthExplorerXMLFileHandler.QualityIndex()
        qI.Code = EarthExplorerXMLFileHandler.CONST_HOTSPOT_DETECTED_NV
        qI.Value = str(self.hotspotflag)
        listOfQualityInd.append(qI)
        qI = EarthExplorerXMLFileHandler.QualityIndex()
        qI.Code = EarthExplorerXMLFileHandler.CONST_SUNGLINT_DETECTED_NV
        qI.Value = str(self.sunglintflag)
        listOfQualityInd.append(qI)
        # Since 4-1-0, writes the Cirrus flag if GetCirrusFlag() OR
        # GetCirrusMasking() activated. See #126349 (  FD
        # https:#thor.si.c-s.fr/gf/project/maccs/tracker/?action=TrackerItemEdit&tracker_item_id=126349
        # )
        if self.plugin.CirrusFlag or self.plugin.CirrusMasking:
            qI = EarthExplorerXMLFileHandler.QualityIndex()
            qI.Code = EarthExplorerXMLFileHandler.CONST_CIRRUS_DETECTED_NV
            qI.Value = str(self.cirrusflag)
            listOfQualityInd.append(qI)

        output_handler.write_list_of_quality_indexes(listOfQualityInd)

        # ---------------------------------------
        # Set information in the Geometry block for each resolution
        # ---------------------------------------
        l_UL_R1 = l1_input_tile_xmlhandler.get_geoposition_upperleftcorner(10)
        l_UL_R2 = l1_input_tile_xmlhandler.get_geoposition_upperleftcorner(20)

        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='10']/Geoposition/ULX",
            str(l_UL_R1.x))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='10']/Geoposition/ULY",
            str(l_UL_R1.y))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='20']/Geoposition/ULX",
            str(l_UL_R2.x))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='20']/Geoposition/ULY",
            str(l_UL_R2.y))

        l_DIM_R1 = l1_input_tile_xmlhandler.get_geoposition_dims(10)
        l_DIM_R2 = l1_input_tile_xmlhandler.get_geoposition_dims(20)

        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='10']/Geoposition/XDIM",
            str(l_DIM_R1.x))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='10']/Geoposition/YDIM",
            str(l_DIM_R1.y))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='20']/Geoposition/XDIM",
            str(l_DIM_R2.x))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='20']/Geoposition/YDIM",
            str(l_DIM_R2.y))

        l_ProductImageSizeX = self.dem.L2Areas[0].size[0]
        l_ProductImageSizeY = self.dem.L2Areas[0].size[1]
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='10']/Size/Lines",
            str(l_ProductImageSizeY))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='10']/Size/Columns",
            str(l_ProductImageSizeX))

        l_ProductImageSizeX = self.dem.L2Areas[1].size[0]
        l_ProductImageSizeY = self.dem.L2Areas[1].size[1]
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='20']/Size/Lines",
            str(l_ProductImageSizeY))
        output_handler.set_string_value(
            "//Image_Information/List_of_Resolutions/Resolution[@r='20']/Size/Columns",
            str(l_ProductImageSizeX))

        # ---------------------------------------
        # Set solar and viewing angles
        # ---------------------------------------
        # LAIG-FA-MAC-135662-CS : values set in the l1_input_tile_handler and not in the l1_input_handler
        # Solar_Angles
        xml_tools.replace_node(l1_input_tile_xmlhandler.root,
                               "//Tile_Angles/Mean_Sun_Angle",
                               output_handler.root, "//Mean_Sun_Angle")
        xml_tools.replace_node(l1_input_tile_xmlhandler.root,
                               "//Mean_Viewing_Incidence_Angle_List",
                               output_handler.root,
                               "//Mean_Viewing_Incidence_Angle_List")

        xml_tools.copies_to_child(l1_input_tile_xmlhandler.root,
                                  "//Tile_Angles/Sun_Angles_Grid/*",
                                  output_handler.root, "//Solar_Angles")
        xml_tools.copies_to_child(l1_input_tile_xmlhandler.root,
                                  "//Viewing_Incidence_Angles_Grids",
                                  output_handler.root,
                                  "//List_of_Viewing_Angles")

        output_handler.set_string_value(
            "//Reflectance_Quantification_Value",
            f"{self.reflectancequantificationvalue:.9f}")

        # ---------------------------------------
        # Set Cloud_Percentage
        # ---------------------------------------
        output_handler.set_string_value("//Cloud_Percentage", self.cloudrate)

        # ---------------------------------------
        # Set Snow_Percentage
        # ---------------------------------------
        output_handler.set_string_value("//Snow_Percentage", self.snowrate)

        # ---------------------------------------
        # Set DEM statistics
        # ---------------------------------------
        output_handler.set_string_value("//Used_DEM/DEM_Reference",
                                        self.dem.ALT_LogicalName)
        output_handler.set_string_value("//Used_DEM/Statistics/Min",
                                        f"{self.dem.ALT_Min:.9f}")
        output_handler.set_string_value("//Used_DEM/Statistics/Max",
                                        f"{self.dem.ALT_Max:.9f}")
        output_handler.set_string_value("//Used_DEM/Statistics/Average",
                                        f"{self.dem.ALT_Mean:.9f}")
        output_handler.set_string_value(
            "//Used_DEM/Statistics/Standard_Deviation",
            f"{self.dem.ALT_Stdv:.9f}")

        output_handler.set_string_value("//Nodata_Value", self.nodatavalue)

        # , "//List_of_Used_Product_Ids", "Used_Product_Id");
        output_handler.set_list_of_used_products_ids(
            self.listofusedproductsids)

        # ---------------------------------------
        # Resolution information
        # ---------------------------------------
        l_NbResolution = len(
            self.l2imagefilenamesprovider.get_sre_regular_filenames())
        for resol in range(l_NbResolution):
            # ---------------------------------------
            # Product_Organization block per resolution
            # ---------------------------------------
            # Image_File
            regular_sre = self.l2imagefilenamesprovider.get_sre_regular_filenames(
            )[resol]
            output_handler.set_string_value(
                "//Product_Organization/Image_File/Nature", regular_sre.Nature)
            output_handler.set_string_value(
                "//Product_Organization/Image_File/Logical_Name",
                regular_sre.LogicalName)
            output_handler.set_string_value(
                "//Product_Organization/Image_File/File_Location",
                regular_sre.FileLocation)
            if self.adjacencyeffectsandslopecorrection:
                regular_fre = self.l2imagefilenamesprovider.get_fre_regular_filenames(
                )[resol]
                xml_tools.copy_to_sibling_inplace(
                    output_handler.root, "//Product_Organization/Image_File",
                    "//Product_Organization/Image_File")
                output_handler.set_string_value(
                    "//Product_Organization/Image_File/Nature",
                    regular_fre.Nature)
                output_handler.set_string_value(
                    "//Product_Organization/Image_File/Logical_Name",
                    regular_fre.LogicalName)
                output_handler.set_string_value(
                    "//Product_Organization/Image_File/File_Location",
                    regular_fre.FileLocation)

        # Quicklook_File
        regular_qlk = self.l2imagefilenamesprovider.get_qlk_regular_file()[0]
        output_handler.set_string_value(
            "//Product_Organization/Quicklook_File/Nature", regular_qlk.Nature)
        output_handler.set_string_value(
            "//Product_Organization/Quicklook_File/Logical_Name",
            regular_qlk.LogicalName)
        output_handler.set_string_value(
            "//Product_Organization/Quicklook_File/File_Location",
            regular_qlk.FileLocation)
        # Annex
        output_handler.write_list_of_annex_files(
            self.l2imagefilenamesprovider.get_list_of_annex_file())
        # Private_Data_File
        regular_hdr = self.l2imagefilenamesprovider.get_private_filenames(
        ).get_hdr_regular_file()
        output_handler.set_string_value(
            "//Product_Organization/Private_Data_File/Nature",
            regular_hdr.Nature)
        output_handler.set_string_value(
            "//Product_Organization/Private_Data_File/Logical_Name",
            regular_hdr.LogicalName)
        output_handler.set_string_value(
            "//Product_Organization/Private_Data_File/File_Location",
            regular_hdr.FileLocation)

        # Reference_SystemCode
        output_handler.set_string_value(
            "//Coordinate_Reference_System/Code",
            l1_input_tile_xmlhandler.get_string_value_of(
                "ReferenceSystemCode"))
        output_handler.set_string_value(
            "//Coordinate_Reference_System/Short_Description",
            l1_input_tile_xmlhandler.get_string_value_of(
                "ReferenceSystemShortDescription"))
        # Save to file
        output_handler.save_to_file(output_filename)
        # Check the GLOBAL header even if  not valid (NOTV)
        if self.checkxmlfileswithschema:
            xml_tools.check_xml(output_filename, schemaLocationDirectory)