#
# For Esa/lite dissemination project
#
# Serco 02/2015 Lavaux Gilles
#
# 03/03/2015: V: 0.1
#
#
#
#
import os, sys, inspect
import time
import zipfile
import traceback


# add converter package path to sys.path 
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parrent=os.path.dirname(currentdir)
print "##### eoSip converter package dir:%s" % parrent
sys.path.insert(0, parrent)

#
from eoSip_converter.base import ingester, reportMaker
from eoSip_converter.esaProducts import product_EOSIP, product_kompsat2
from eoSip_converter.esaProducts import metadata, browse_metadata
from eoSip_converter.esaProducts import definitions_EoSip,formatUtils
from xml_nodes import rep_rectifiedBrowse
from eoSip_converter.esaProducts.namingConvention import NamingConvention
import eoSip_converter.imageUtil as imageUtil
import eoSip_converter.fileHelper as fileHelper
import eoSip_converter.xmlHelper as xmlHelper




# minimum config version that can be use
MIN_CONFIG_VERSION=1.0
VERSION="Kompsat converter V:1.0.0"
REF_NAME="KO2_OPER_MSC_MUL_1G_20070418T101559_20070418T101601_003858_0065_1045_0001.SIP.ZIP"

class ingester_kompsat2(ingester.Ingester):

        #
        #
        #
        def getVersionImpl(self):
            return VERSION

        #
        #
        #
        def afterStarting(self, **kargs):
                pass

        #
        # config version is like: name_floatVersion
        #
        def checkConfigurationVersion(self):
                global MIN_CONFIG_VERSION
                self._checkConfigurationVersion(self.CONFIG_VERSION, MIN_CONFIG_VERSION)
                

        #
        # prepare metadata from a browse report generation
        #
        def prepareBrowseMetadata(self, processInfo):
                pass


        #
        # called before doing the various reports
        #
        def beforeReportsDone(self, processInfo):
                # alter href mapping
                processInfo.destProduct.metadata.alterMetadataMaping('href', metadata.METADATA_FULL_PACKAGENAME)


        #
        # called after having done the various reports
        #
        def afterReportsDone(self, processInfo):
                self.alterReportXml(processInfo)
        

        #
        # called at the end of the doOneProduct, before the index/shopcart creation
        #
        def afterProductDone(self, processInfo):
            #self.makeKmz(processInfo)
            pass
        
        #
        #
        def alterReportXml(self, processInfo) :
                helper=xmlHelper.XmlHelper()
                helper.setData(processInfo.destProduct.productReport);
                helper.parseData()
                processInfo.addLog(" alterReportXml: product report parsed")
                print " alterReportXml: product report parsed"
                
                # add namespace: <eop:operationalMode codeSpace="urn:esa:eop:SPOT6-7:Naomi:Mode
                aNode = helper.getFirstNodeByPath(None, 'procedure/EarthObservationEquipment/sensor/Sensor/operationalMode', None)
                helper.setNodeAttributeText(aNode, 'codeSpace', 'urn:esa:eop:KOMPSAT-2:MSC:operationalMode')

                helper2=xmlHelper.XmlHelper()
                helper2.setData(helper.prettyPrint());
                helper2.parseData()
                formattedXml = helper2.prettyPrintAll()
                if self.debug!=0:
                        print " new XML: %s " % formattedXml
                fd=open(processInfo.destProduct.reportFullPath, 'w')
                fd.write(formattedXml)
                fd.flush()
                fd.close()
                processInfo.destProduct.productReport=formattedXml
                processInfo.addLog(" alterReportXml: product report changed at path:'%s'" % processInfo.destProduct.reportFullPath)

        #
        #
        #
        def buildEoNames(self, processInfo, namingConvention=None):
            # test default in ingester
            self.buildEoNamesDefault(processInfo, namingConvention)

            # test EoSip package name
            aName = processInfo.destProduct.getSipPackageName()
            if len(aName) != len(REF_NAME):
                print "ref name:%s" % REF_NAME
                print "EoSip name:%s" % aName
                raise Exception("EoSip name has incorrect length:%s VS %s" % (len(aName), len(REF_NAME)))
            if aName.find('@') >= 0 or aName.find('#') > 0:
                raise Exception("SipProductName incomplet:%s" % aName)

    
        #
        # Override
        # this is the first function called by the base ingester
        #
        # as input we have the manifest path. Need to use his parent for the product path
        #
        def createSourceProduct(self, processInfo):
            global debug,logger
            processInfo.srcPath=processInfo.srcPath.replace('\\','/')
            product = product_kompsat2.Product_Kompsat2(processInfo.srcPath)
            product.setDebug(1)
            processInfo.srcProduct = product


        #
        # Override
        #
        def createDestinationProduct(self, processInfo):
            global debug,logger
            eosipP=product_EOSIP.Product_EOSIP()
            #eosipP.setUsePythonZipLib(False)
            eosipP.sourceProductPath = processInfo.srcPath
            processInfo.destProduct = eosipP

            # set naming convention instance
            namingConventionSip = NamingConvention(self.OUTPUT_SIP_PATTERN)
            eosipP.setNamingConventionSipInstance(namingConventionSip)
            eosipP.setNamingConventionEoInstance(namingConventionSip)
            
            processInfo.destProduct.setNamingConventionSipInstance(namingConventionSip)

            
            self.logger.info(" Eo-Sip product created")
            processInfo.addLog(" Eo-Sip product created")

                    
        #
        # Override
        #
        def verifySourceProduct(self, processInfo):
                processInfo.addLog(" verifying product:%s" % (processInfo.srcPath))
                self.logger.info(" verifying product");
                

            
        #
        # Override
        #
        def prepareProducts(self,processInfo):
                processInfo.addLog(" prepare product in:%s" % (processInfo.workFolder))
                self.logger.info(" prepare product");
                processInfo.srcProduct.extractToPath(processInfo.workFolder, processInfo.test_dont_extract)
                
                processInfo.addLog("  extracted inside:%s" % (processInfo.workFolder))
                self.logger.info("  extracted inside:%s" % (processInfo.workFolder))

                self.stretcherAppExe = self.ressourcesProvider.getRessourcePath('stretchAppExe')
                processInfo.srcProduct.stretcherAppExe = self.stretcherAppExe

        #
        # Override
        #
        def extractMetadata(self,met,processInfo):
            # fill metadata object
            numAdded=processInfo.srcProduct.extractMetadata(met)

            #met.setMetadataPair(metadata.METADATA_GENERATION_TIME, time.strftime('%Y-%m-%dT%H:%M:%SZ'))
            # use method in base converter
            self.getGenerationTime(met)
                

                
        #
        #
        #
        def makeBrowseChoiceBlock(self, processInfo, metadata):
            pass
           

        #
        # Override
        # make the Jpeg (or Png) browse image from the TIFF image. We want Jpeg
        # construct the browse_metadatareport footprint block: it is the rectifedBrowse for tropforest
        #
        def makeBrowses(self, processInfo, ratio=50):
            processInfo.srcProduct.makeBrowses(processInfo)


        #
        # Override
        #
        # output the Eo-Sip profuct in the destination folder
        # take the first rule and put the product in the resulting folder
        # create link for the other rules if any
        #
        def output_eoSip(self, processInfo, basePath, pathRules, overwrite=None):
                self.logger.info("  output_eoSip: basePath=%s" %  (basePath))
                #return
                # copy eoSip in first path; make links in other paths: 
                
                # now done before in base_ingester.doOneProduct
                #self.outputProductResolvedPaths = processInfo.destProduct.getOutputFolders(basePath, pathRules)

                #
                if len(self.outputProductResolvedPaths)==0:
                        raise Exception("no product resolved path")
                else:
                        # output in first path
                        firstPath=self.outputProductResolvedPaths[0]
                        processInfo.addLog("  Eo-Sip product writen in folder:%s\n" %  (firstPath))
                        self.logger.info("  Eo-Sip product writen in folder:%s\n" %  (firstPath))
                        productPath = processInfo.destProduct.writeToFolder(firstPath, overwrite)

                        # output link in other path
                        i=0
                        for item in self.outputProductResolvedPaths:
                                if i>0:
                                        otherPath="%s" % (item)
                                        self.logger.info("  eoSip product tree path[%d] is:%s" %(i, item))
                                        processInfo.destProduct.writeToFolder(basePath, overwrite)
                                        processInfo.addLog("  Eo-Sip product link writen in folder[%d]:%s\n" %  (i, otherPath))
                                        self.logger.info("  Eo-Sip product link writen in folder[%d]:%s\n" %  (i, otherPath))
                                i=i+1
                self.logger.info("  done")
                return productPath

        #
        # create a kmz, use the bounding box
        # created in the log folder
        #
        def makeKmz__UNUSED(self, processInfo):
                if not self.test_dont_write:
                        processInfo.ingester.logger.info("WILL CREATE KMZ")
                        import kmz
                        outPath = "%s/kmz" % processInfo.ingester.LOG_FOLDER
                        if not os.path.exists(outPath):
                                self.logger.info("  will make kmz folder:%s" % outPath)
                                os.makedirs(outPath)
                        kmzPath = kmz.eosipToKmz.makeKmlFromEoSip_new(False, outPath, processInfo)
                        print " KMZ created at path:%s" % kmzPath
                        if kmzPath != None:
                                processInfo.addLog("KMZ created at path:%s" % kmzPath)
                        else:
                                processInfo.addLog("KMZ was NOT CREATED!")
                                raise Exception("KMZ was NOT CREATED!")

if __name__ == '__main__':
    try:
        if len(sys.argv) > 1:
            ingester = ingester_kompsat2()
            ingester.debug=1
            exitCode = ingester.starts(sys.argv)
            #
            ingester.makeConversionReport("Kompsat-2_conversion_report", '.')

            sys.exit(exitCode)
        else:
            print "syntax: python ingester_xxx.py -c configuration_file.cfg [-l list_of_product_file]"
            sys.exit(1)
            
    except Exception, e:
        print " Error"
        exc_type, exc_obj, exc_tb = sys.exc_info()
        traceback.print_exc(file=sys.stdout)
        sys.exit(2)

f40aa86172bbd68f47e6dcf67b24c3d3e37180e4
#
# For Esa/lite dissemination project
#
# Serco 02/2015 Lavaux Gilles
#
# 03/03/2015: V: 0.1
#
#
#
#
import os, sys, inspect
import time
import zipfile
import traceback


# add converter package path to sys.path 
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parrent=os.path.dirname(currentdir)
print "##### eoSip converter package dir:%s" % parrent
sys.path.insert(0, parrent)

#
from eoSip_converter.base import ingester, reportMaker
from eoSip_converter.esaProducts import product_EOSIP, product_kompsat2
from eoSip_converter.esaProducts import metadata, browse_metadata
from eoSip_converter.esaProducts import definitions_EoSip,formatUtils
from xml_nodes import rep_rectifiedBrowse
from eoSip_converter.esaProducts.namingConvention import NamingConvention
import eoSip_converter.imageUtil as imageUtil
import eoSip_converter.fileHelper as fileHelper
import eoSip_converter.xmlHelper as xmlHelper




# minimum config version that can be use
MIN_CONFIG_VERSION=1.0
VERSION="Kompsat converter V:1.0.0"
REF_NAME="KO2_OPER_MSC_MUL_1G_20070418T101559_20070418T101601_003858_0065_1045_0001.SIP.ZIP"

class ingester_kompsat2(ingester.Ingester):

        #
        #
        #
        def getVersionImpl(self):
            return VERSION

        #
        #
        #
        def afterStarting(self, **kargs):
                pass

        #
        # config version is like: name_floatVersion
        #
        def checkConfigurationVersion(self):
                global MIN_CONFIG_VERSION
                self._checkConfigurationVersion(self.CONFIG_VERSION, MIN_CONFIG_VERSION)
                

        #
        # prepare metadata from a browse report generation
        #
        def prepareBrowseMetadata(self, processInfo):
                pass


        #
        # called before doing the various reports
        #
        def beforeReportsDone(self, processInfo):
                # alter href mapping
                processInfo.destProduct.metadata.alterMetadataMaping('href', metadata.METADATA_FULL_PACKAGENAME)


        #
        # called after having done the various reports
        #
        def afterReportsDone(self, processInfo):
                self.alterReportXml(processInfo)
        

        #
        # called at the end of the doOneProduct, before the index/shopcart creation
        #
        def afterProductDone(self, processInfo):
            #self.makeKmz(processInfo)
            pass
        
        #
        #
        def alterReportXml(self, processInfo) :
                helper=xmlHelper.XmlHelper()
                helper.setData(processInfo.destProduct.productReport);
                helper.parseData()
                processInfo.addLog(" alterReportXml: product report parsed")
                print " alterReportXml: product report parsed"
                
                # add namespace: <eop:operationalMode codeSpace="urn:esa:eop:SPOT6-7:Naomi:Mode
                aNode = helper.getFirstNodeByPath(None, 'procedure/EarthObservationEquipment/sensor/Sensor/operationalMode', None)
                helper.setNodeAttributeText(aNode, 'codeSpace', 'urn:esa:eop:KOMPSAT-2:MSC:operationalMode')

                helper2=xmlHelper.XmlHelper()
                helper2.setData(helper.prettyPrint());
                helper2.parseData()
                formattedXml = helper2.prettyPrintAll()
                if self.debug!=0:
                        print " new XML: %s " % formattedXml
                fd=open(processInfo.destProduct.reportFullPath, 'w')
                fd.write(formattedXml)
                fd.flush()
                fd.close()
                processInfo.destProduct.productReport=formattedXml
                processInfo.addLog(" alterReportXml: product report changed at path:'%s'" % processInfo.destProduct.reportFullPath)

        #
        #
        #
        def buildEoNames(self, processInfo, namingConvention=None):
            # test default in ingester
            self.buildEoNamesDefault(processInfo, namingConvention)

            # test EoSip package name
            aName = processInfo.destProduct.getSipPackageName()
            if len(aName) != len(REF_NAME):
                print "ref name:%s" % REF_NAME
                print "EoSip name:%s" % aName
                raise Exception("EoSip name has incorrect length:%s VS %s" % (len(aName), len(REF_NAME)))
            if aName.find('@') >= 0 or aName.find('#') > 0:
                raise Exception("SipProductName incomplet:%s" % aName)

    
        #
        # Override
        # this is the first function called by the base ingester
        #
        # as input we have the manifest path. Need to use his parent for the product path
        #
        def createSourceProduct(self, processInfo):
            global debug,logger
            processInfo.srcPath=processInfo.srcPath.replace('\\','/')
            product = product_kompsat2.Product_Kompsat2(processInfo.srcPath)
            product.setDebug(1)
            processInfo.srcProduct = product


        #
        # Override
        #
        def createDestinationProduct(self, processInfo):
            global debug,logger
            eosipP=product_EOSIP.Product_EOSIP()
            #eosipP.setUsePythonZipLib(False)
            eosipP.sourceProductPath = processInfo.srcPath
            processInfo.destProduct = eosipP

            # set naming convention instance
            namingConventionSip = NamingConvention(self.OUTPUT_SIP_PATTERN)
            eosipP.setNamingConventionSipInstance(namingConventionSip)
            eosipP.setNamingConventionEoInstance(namingConventionSip)
            
            processInfo.destProduct.setNamingConventionSipInstance(namingConventionSip)

            
            self.logger.info(" Eo-Sip product created")
            processInfo.addLog(" Eo-Sip product created")

                    
        #
        # Override
        #
        def verifySourceProduct(self, processInfo):
                processInfo.addLog(" verifying product:%s" % (processInfo.srcPath))
                self.logger.info(" verifying product");
                

            
        #
        # Override
        #
        def prepareProducts(self,processInfo):
                processInfo.addLog(" prepare product in:%s" % (processInfo.workFolder))
                self.logger.info(" prepare product");
                processInfo.srcProduct.extractToPath(processInfo.workFolder, processInfo.test_dont_extract)
                
                processInfo.addLog("  extracted inside:%s" % (processInfo.workFolder))
                self.logger.info("  extracted inside:%s" % (processInfo.workFolder))

                self.stretcherAppExe = self.ressourcesProvider.getRessourcePath('stretchAppExe')
                processInfo.srcProduct.stretcherAppExe = self.stretcherAppExe

        #
        # Override
        #
        def extractMetadata(self,met,processInfo):
            # fill metadata object
            numAdded=processInfo.srcProduct.extractMetadata(met)

            #met.setMetadataPair(metadata.METADATA_GENERATION_TIME, time.strftime('%Y-%m-%dT%H:%M:%SZ'))
            # use method in base converter
            self.getGenerationTime(met)
                

                
        #
        #
        #
        def makeBrowseChoiceBlock(self, processInfo, metadata):
            pass
           

        #
        # Override
        # make the Jpeg (or Png) browse image from the TIFF image. We want Jpeg
        # construct the browse_metadatareport footprint block: it is the rectifedBrowse for tropforest
        #
        def makeBrowses(self, processInfo, ratio=50):
            processInfo.srcProduct.makeBrowses(processInfo)


        #
        # Override
        #
        # output the Eo-Sip profuct in the destination folder
        # take the first rule and put the product in the resulting folder
        # create link for the other rules if any
        #
        def output_eoSip(self, processInfo, basePath, pathRules, overwrite=None):
                self.logger.info("  output_eoSip: basePath=%s" %  (basePath))
                #return
                # copy eoSip in first path; make links in other paths: 
                
                # now done before in base_ingester.doOneProduct
                #self.outputProductResolvedPaths = processInfo.destProduct.getOutputFolders(basePath, pathRules)

                #
                if len(self.outputProductResolvedPaths)==0:
                        raise Exception("no product resolved path")
                else:
                        # output in first path
                        firstPath=self.outputProductResolvedPaths[0]
                        processInfo.addLog("  Eo-Sip product writen in folder:%s\n" %  (firstPath))
                        self.logger.info("  Eo-Sip product writen in folder:%s\n" %  (firstPath))
                        productPath = processInfo.destProduct.writeToFolder(firstPath, overwrite)

                        # output link in other path
                        i=0
                        for item in self.outputProductResolvedPaths:
                                if i>0:
                                        otherPath="%s" % (item)
                                        self.logger.info("  eoSip product tree path[%d] is:%s" %(i, item))
                                        processInfo.destProduct.writeToFolder(basePath, overwrite)
                                        processInfo.addLog("  Eo-Sip product link writen in folder[%d]:%s\n" %  (i, otherPath))
                                        self.logger.info("  Eo-Sip product link writen in folder[%d]:%s\n" %  (i, otherPath))
                                i=i+1
                self.logger.info("  done")
                return productPath

        #
        # create a kmz, use the bounding box
        # created in the log folder
        #
        def makeKmz__UNUSED(self, processInfo):
                if not self.test_dont_write:
                        processInfo.ingester.logger.info("WILL CREATE KMZ")
                        import kmz
                        outPath = "%s/kmz" % processInfo.ingester.LOG_FOLDER
                        if not os.path.exists(outPath):
                                self.logger.info("  will make kmz folder:%s" % outPath)
                                os.makedirs(outPath)
                        kmzPath = kmz.eosipToKmz.makeKmlFromEoSip_new(False, outPath, processInfo)
                        print " KMZ created at path:%s" % kmzPath
                        if kmzPath != None:
                                processInfo.addLog("KMZ created at path:%s" % kmzPath)
                        else:
                                processInfo.addLog("KMZ was NOT CREATED!")
                                raise Exception("KMZ was NOT CREATED!")

if __name__ == '__main__':
    try:
        if len(sys.argv) > 1:
            ingester = ingester_kompsat2()
            ingester.debug=1
            exitCode = ingester.starts(sys.argv)
            #
            ingester.makeConversionReport("Kompsat-2_conversion_report", '.')

            sys.exit(exitCode)
        else:
            print "syntax: python ingester_xxx.py -c configuration_file.cfg [-l list_of_product_file]"
            sys.exit(1)
            
    except Exception, e:
        print " Error"
        exc_type, exc_obj, exc_tb = sys.exc_info()
        traceback.print_exc(file=sys.stdout)
        sys.exit(2)
