Exemplo n.º 1
0
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option("-u",'--user',
                      action="store",dest="user",
                      help="Name of target edb user")

    parser.add_option("-e","--edb",
                      action="store",dest="edb",
                      help="Name of target edb")

    parser.add_option("-y","--year",
                       action="store",dest="year",
                       help="Cut out for given year")
    
    parser.add_option("-f","--file",
                      action="store",dest="file",
                      help="File to get data from")
    
    parser.add_option("-l", "--loglevel",
                      action="store",dest="loglevel",default=2,
                      help="Sets the loglevel (0-3 where 3=full logging)")

    (options, args) = parser.parse_args()


    
    #--------------------Init logger-----------------------
    rootLogger = logger.RootLogger(level=options.loglevel)
    global log
    log = rootLogger.getLogger(sys.argv[0])

    #-----------------Validating options-------------------

    if options.user is None:
        log.error("Need to specify -u <user>")
        return 1
    if options.edb is None:
        log.error("Need to specify -e <edb>")
        return 1
    if options.year is None:
         log.error("Need to specify -y <year>")
         return 1
    if options.file is None:
        log.error("Need to specify -f <file>")
        return 1
#     if len(options.suffix)>4:
#         log.error("Limit your suffix length to 4 characters")
#         return 1

    if len(options.year)!=4:
        log.error("Year should be given with four digits")
        return 1

    dmn=Domain()
    edb=Edb(dmn,options.user,options.edb)
    subgrpdb=Subgrpdb(edb)
    rf = codecs.open(options.file,"r","ISO-8859-15")
    species={}
    tmp=tempfile.NamedTemporaryFile(suffix=".tmp",dir=dmn.tmpDir())
    emisKey={}
    try:
        cmd="subdb -0 -o "+tmp.name
        returnCode,outMsg,errMsg=utilities.execute(cmd)
        if returnCode!=0:
            raise IOError("Error while running:\n" + cmd + "\nstdout: "+outMsg+"\nstderr: "+errMsg)
        f=codecs.open(tmp.name,"r","HP Roman8")
#         content=f.read()
        for line in f.read().split('\n'):
            if line == "":
                break
            line=line.split()
            if line[1] == '""' or line[1] == '"-"':
                continue
            emisKey[line[1][1:-1].lower()] = line[0]
        f.close()
    except IOError:
        msg="stdout: "+outMsg+"\nstderr: "+errMsg
        raise IOError("Could not run: "+cmd+"\n"+msg)

    for line in rf:
        if "Average of em" in line:
            line = line.split("\t")
            line[-1]=line[-1][:-1]
            i=0
            for spec in line[4:]:
                i+=1
                if len(spec) < 13:
                    continue
                if spec[13:].lower() == "pm25":
                    species[i]="pm2.5"
                elif spec[13:].lower() == "bap":
                    species[i]="benzo_a_pyrene"
                elif spec[13:].lower() == "diox":
                    species[i]="dioxine"
                elif spec[13:].lower() == "pah":
                    species[i]="pah-4"
                else:
                    species[i]=spec[13:].lower()
            continue
        line = line[:-1].split('\t')
        if line[3] != options.year:
            continue
        name=line[0]

        i=0
        substances={}
        for emFactor in line[4:]:
            i+=1
            if emFactor == "":
                continue
            if species[i] in emisKey:
#                print "Now doing species "+species[i]
                index=int(emisKey[species[i]])
                emFactor = float(emFactor)
                if species[i] == "pah-4" or species[i] == "benzo_a_pyrene":
                    emFactor *= 10** -6
                if species[i]=="dioxine":
                    emFactor *= 10** -9
                if emFactor < 10** -5:
                    unit="g/TJ"
                    emFactor*= 10**6
                elif emFactor < 10** -2:
                    unit="kg/TJ"
                    emFactor*= 10**3
                elif emFactor < 10:
                    unit="ton/TJ"
                else:
                    unit="Gg/TJ"
                    emFactor*= 10** -3

                substances[index]={"slope":emFactor,"offset":0,"unit":unit}
            else:
                print species[i] + " missing"
        subgrpdb.add_subgrp(name,substances)


#     subgrpdb.read()
    subgrpdb.write_to_file("test.txt")