Exemplo n.º 1
0
def main():

    #----------Setting s for option parser functionality------
    parser=OptionParser(usage= usage, version=version)
    parser.add_option("-q", "--quiet",
                      action="store_true", dest="quiet", default=False,
                      help="Only print warnings and errors")    
    
    parser.add_option("-t", "--template",
                      action="store",dest="controlfile",default=None,
                      help="Generate default controlfile")

    parser.add_option("-l", "--logfile",
                      action="store",dest="logfile",default=None,
                      help="Writes output to logfile")
    
    parser.add_option("-d", "--debug",
                      action="store_true",dest="debug",default=False,
                      help="Writes output to logfile")
    
    parser.add_option("-c", "--console",
                      action="store_true",dest="console",default=False,
                      help="Activates console output")

    (options, args) = parser.parse_args()


    if options.controlfile!=None:
        generateCf(path.abspath(options.controlfile))
        print "Wrote default controlfile"
        sys.exit()

    if len(args)!=1:
        parser.error("Incorrect number of arguments")

    #------------Setting up logging capabilities -----------
    rootLogger=logging.getLogger('')
    reportLevel=logging.INFO
    if options.quiet:
        reportLevel=logging.WARNING
    if options.debug:
        reportLevel=logging.DEBUG
    rootLogger.setLevel(reportLevel)
    
    if options.console:        
        console=logging.StreamHandler()
        console.setLevel(reportLevel)
        formatter = logging.Formatter('%(asctime)s %(name)-12s: %(levelname)-8s %(message)s',
                                      datefmt='%a, %d %b %Y %H:%M:%S')
        console.setFormatter(formatter)    
        rootLogger.addHandler(console)
            
    if options.logfile!=None:
        logFileName=path.abspath(options.logfile)
        if not path.exists(path.dirname(logFileName)):
            print "Bad argument, directory for logfile does not exist"
            sys.exit()
        logfile=logging.FileHandler(logFileName,"w")
        logfile.setLevel(reportLevel)
        formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
        logfile.setFormatter(formatter)    
        rootLogger.addHandler(logfile)
         
    logger=logging.getLogger('bumQC')

    #-----------Starting main functionality----------------
    
    logger.info("Started script bumQC")
    

    #Creating a controlFile object from the file given as an argument
    logger.info("Reading controlfile")
    cf=controlFile.controlFile(fileName=path.abspath(args[0]))
    
    logger.info("Processing controlfile")
    outDir,rc,resDef,areaDict=processControlFile(cf)

    logger.info("Writing summary")

    fileName="bumQC_summary_"+datetime.date.today().strftime("%Y-%m-%d")+".txt"
    writeEmissionSummary(path.join(outDir,fileName),rc,resDef,areaDict)
    
    logger.info("Finished bumQC")
Exemplo n.º 2
0
def main():
    parser=OptionParser(usage= usage, version=version)    
    parser.add_option("-t", "--template",
                      action="store",dest="controlfile",default=None,
                      help="Generate default controlfile")
    (options, args) = parser.parse_args()

    if options.controlfile!=None:
        generateCf(path.abspath(options.controlfile))
        print "Wrote default controlfile"
        sys.exit()

    if len(args)!=1:
        parser.error("Incorrect number of arguments")

    cf=controlFile.controlFile(fileName=path.abspath(args[0]))
    edb=cf.findString("edb:")
    users=cf.findStringList("users:")
    substances=cf.findStringList("substances:")
    substanceIndices=cf.findStringList("substanceIndices:")
    outputDir = cf.findExistingPath("outputDir:")
    templateFile=cf.findExistingPath("templateFile:")
    log=os.path.join(outputDir,"log_runXrepedb")
    if os.path.exists(log):
        fid=open(log,'w') #Emptying log-file
        fid.close()

    if not os.path.exists(templateFile):
        report(log,"Template file does not exist")
        sys.exit("Template file does not exist")

    report(log,"Running python script for extraction of gridded emissions from Simair")


    for i in range(len(substanceIndices)):
        subst=substances[i]
        substInd=substanceIndices[i]
        for user in users:
            report(log, "Extracting substance: "+subst+" for user: "******"emisRast_"+subst+"_"+user+".asc"
                resultFilePath=os.path.join(outputDir,resultFileName)
                command="xrepedb -i "+templateFile+" > "+resultFilePath
                report( log,"Executing: "+command)

                handle=os.popen(command,'r')            

                report(log, "xrepedb output:"+string.join(handle.readlines()))
                status=handle.close()
                if status is not None:
                    report(log,"Error when running runXrepedb: the process for xrepedb did not finish")
                    sys.exit("Error when running runXrepedb: the process for xrepedb did not finish")
                else:
                    
                    gzCommand="gzip -f "+resultFilePath
                    gzhandle=os.popen(gzCommand,'r')
                    gzStatus=gzhandle.close()
                    if gzStatus!= None:
                        report(log,"Could not compress: "+resultFileName)
            else:
                report(log,"Could not modify template file")
Exemplo n.º 3
0
    controlFilePath=path.abspath(sys.argv[1])
    logFilePath=path.abspath(sys.argv[2])
                 
    if not path.exists(controlFilePath):
        sys.exit("Controlfile does not exist")
    if not path.exists(path.dirname(logFilePath)):
        sys.exit("Logfile directory does not exist")
    else:
        log=path.join(os.getcwd(),"updateSimairDB.log")
            
if path.exists(log):
    os.remove(log)

report(log,"\n"+20*"="+"Starting update procedure"+20*"=",reportToPrompt)

cf=controlFile.controlFile(controlFilePath)

#Collecting parameters from controlFile
new_edb=cf.findString("new_edb:")
report(log,"new_ed: "+new_edb,reportToPrompt)
old_edb=cf.findString("old_edb:")
report(log,"old_edb: "+old_edb,reportToPrompt)
wd=cf.findExistingPath("workingDir:")
report(log,"workingDir: "+wd,reportToPrompt)
od=cf.findExistingPath("outputDir:")
report(log,"outputDir: "+od,reportToPrompt)
parameters=cf.findStringList("parameters:")

strParams=""
i=0
for p in parameters: