Example #1
0
def loadTemplate(edb,acIndex=None):
    """
    Reads sources from template edb and stores them in 
    dictionary with join attribute as keys
    
    @param edb: edb object
    """
     
    template={}
    sourcedb=Sourcedb(edb)
    sourcedb.read()    
    for source in sourcedb.sources:
        if acIndex is None:
            attr=source["SEARCHKEY4"]
            if attr is None:
                log.error("SEARCHKEY4 is not specified for source "+ 
                          "X1=%i,Y1=%i" %(source["X1"],source["Y1"]))
        else:
            try:
                attr=source["ACTIVITYCODE"][acIndex-1] #acIndex starts from 1
            except TypeError:
                log.error("No activity codes found for source"+
                          "X1=%i,Y1=%i" %(source["X1"],source["Y1"]))
                return 1
            except IndexError:
                log.error("No activity code tree found with index %i" %acIndex)
            
        template[attr]=source

    return template  
Example #2
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("-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
        
    dmn=Domain()
    try:
        edb = Edb(dmn,options.user,options.edb)
    except:
        log.error("No such edb found")
        return 1

    sourcedb = Sourcedb(edb)

    log.info("Reading ship sources")
    sourcedb.read()

    for src in sourcedb.sources:
        src.acitivity_emis={}

    sourcedb.write()

    log.info("Finished!")
from pyAirviro.basic.domain import Domain
from pyAirviro.edb.edb import Edb
from pyAirviro.edb import point
from pyAirviro.edb.sourcedb import Sourcedb

user = sys.argv[1]
startedb =sys.argv[2]


outfilename = user+startedb+"_PointSources.out"
domainName=os.environ["AVDBNAME"]                                                                                             
dmn = Domain(domainName)


edb = Edb(dmn,user,startedb)
sourcedb = Sourcedb(edb)

sourcedb.read()


for source in sourcedb.sources:
    print source
    if source.isArea():
        x1 = source.par["X1"].val
        x2 = source.par["X2"].val
        y1 = source.par["Y1"].val
        y2 = source.par["Y2"].val
        area = abs( (x1-x2)*(y1-y2) )
        radius = math.sqrt( area/math.pi )
        source.par["CHIMNEY IN"].val = 2 * radius
        (xc,yc) = source.centre()
Example #4
0
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option('--src_user',
                      action="store",dest="src_user",
                      help="Name of target edb user")

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

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

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

    parser.add_option("--codeIndex",
                      action="store",dest="codeIndex",
                      help="Activity code index to use for filter")

    parser.add_option("--codeValue",
                      action="store",dest="codeValue",
                      help="Activity code value to filter by")

    parser.add_option("-l", "--loglevel",
                      action="store",dest="loglevel",default=2,
                      help="Sets the loglevel (0-3 where 3=full logging)")

    parser.add_option("-o", "--outfile",
                      action="store",dest="outfile",
                      help="Output filenam, leave out to write directly to target edb")

    (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.src_user is None:
        log.error("Need to specify --src_user <user>")
        return 1
    if options.src_edb is None:
        log.error("Need to specify --src_edb <edb>")
        return 1

    if options.tgt_user is None:
        log.error("Need to specify --tgt_user <user>")
        return 1
    if options.tgt_edb is None:
        log.error("Need to specify --tgt_edb <edb>")
        return 1


    if len(args)!=0:
        log.error("No argument expected")
        return 1

    dmn=Domain()
    src_edb=Edb(dmn,options.src_user,options.src_edb)
    tgt_edb=Edb(dmn,options.tgt_user,options.tgt_edb)

    if not src_edb.exists():
        log.error("Source edb %s does not exist") %options.src_edb
        return 1

    if not tgt_edb.exists():
        log.error("Target edb %s does not exist") %options.tgt_edb
        return 1

    if options.codeIndex is None:
        log.error("No code index specified")
        return 1

    if options.codeValue is None:
        log.error("No code value specified")
        return 1

    log.info("Reading subdb...")
    src_subdb=Subdb(src_edb)
    src_subdb.read()

    log.info("Reading sourcedb...")
    src_sourcedb=Sourcedb(src_edb)
    src_sourcedb.read()
    

    tgt_sourcedb=Sourcedb(tgt_edb)
    tgt_sourcedb.read()

    #log.info("Reading subgrpdb")
    #src_subgrpdb=Subgrpdb(src_edb)
    #src_subgrpdb.read()

    log.info("Reading edb.rsrc")
    src_rsrc=Rsrc(src_edb.rsrcPath())
    tgt_rsrc=Rsrc(tgt_edb.rsrcPath())

    log.info("Reading target sourcedb...")


    acCodeTables=[]
    for i in range(tgt_rsrc.numberOfCodeTrees("ac")):
        acCodeTables.append(CodeTable(tgt_rsrc.path,codeType="ac",codeIndex=i+1))

    gcCodeTables=[]
    for i in range(tgt_rsrc.numberOfCodeTrees("gc")):
        gcCodeTables.append(CodeTable(tgt_rsrc.path,codeType="gc",codeIndex=i+1))



    copiedSources=0
    readSources=0
    for src in src_sourcedb.sources:
        hasEmis=False
        src["PX"]=0
        src["PY"]=0
                      
        toBeRemoved=[]
        for substInd,emis in src.subst_emis.iteritems():
            ac= emis["ACTCODE"][int(options.codeIndex)-1]
            ac=[c for c in ac if c!="-"]
            emis["ACTCODE"][int(options.codeIndex)-1]=ac #Remove '-'from code

            ac=".".join(ac)
            
            if ac!=options.codeValue:
                toBeRemoved.append(substInd)

        for key in toBeRemoved:
            src.removeSubstEmis(key)

        if src.subst_emis!={}:
            hasEmis=True
        
        toBeRemoved=[]
        for subgrpInd,emis in src.subgrp_emis.iteritems():
            ac= emis["ACTCODE"][int(options.codeIndex)-1]
            ac=[c for c in ac if c!="-"]
            emis["ACTCODE"][int(options.codeIndex)-1]=ac #Remove '-'from code
            ac=".".join(ac)

            
            if ac!=options.codeValue:
                toBeRemoved.append(subgrpInd)

        for key in toBeRemoved:
            src.removeSubgrpEmis(key)

        if src.subgrp_emis!={}:
            hasEmis=True


        toBeRemoved=[]
        for actInd,emis in src.activity_emis.iteritems():
            ac= emis["ACTCODE"][int(options.codeIndex)-1]
            ac=[c for c in ac if c!="-"]
            emis["ACTCODE"][int(options.codeIndex)-1]=ac #Remove '-'from code
            ac=".".join(ac)
            
            if ac!=options.codeValue:
                toBeRemoved.append(actInd)

        for key in toBeRemoved:
            src.removeActivityEmis(key)

        if src.activity_emis!={}:
            hasEmis=True

        readSources+=1
        if hasEmis:
            copiedSources+=1
            tgt_sourcedb.sources.append(src)

    if options.outfile is None:
        log.info("Writing sources to target edb")
    else:
        log.info("Writing sources to file")

    tgt_sourcedb.write(filename=options.outfile,force=True)

    log.info("Successfully Copied %i out of %i sources" %(copiedSources,readSources))
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("--begin",
                      action="store",dest="begin",
                      help="YYMMDDHH to begin calculation")

    parser.add_option("--end",
                      action="store",dest="end",
                      help="YYMMDDHH to end calculation")
    
    parser.add_option("-l", "--loglevel",
                      action="store",dest="loglevel",default=2,
                      help="Sets the loglevel, 0-3 where 3=full logging")
    
    parser.add_option("-o","--outfile",
                      action="store",dest="outfile",
                      default=None,
                      help="Name of output file")

    parser.add_option("--MMSI",action="store",
                      dest="MMSIfile",default=None,
                      help="File with MMSI for ships process")

    parser.add_option("--macro",action="store",
                      dest="macro",default=None,
                      help="Macro of search")

    parser.add_option("-p","--parameterTable",action="store",
                      dest="parameterTable",
                      help="Tab-sep ascii table with one row for each run and one column for each search parameter to set")

    (options, args) = parser.parse_args()


    
    #--------------------Init logger-----------------------
    rootLogger = logger.RootLogger(level=options.loglevel)
    global log
    log = rootLogger.getStreamLogger(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.begin is None:
        log.error("Need to specify --begin <YYMMDDHH>")
        return 1
    if options.end is None:
        log.error("Need to specify --end <YYMMDDHH>")
        return 1
    if options.MMSIfile is None:
        log.error("Need to specify --MMSI <mmsi-file>")
        return 1        
    if options.macro is None:
        log.error("Need to specify --macro <macro-file>")
        return 1
        
    dmn=Domain()
    try:
        edb = Edb(dmn,options.user,options.edb)
    except:
        log.error("No such edb found")
        return 1

    parTable = DataTable()
    parTable.read(options.parameterTable)


    mmsiList=open(options.MMSIfile,"r").read().split("\n")
    
    #log.error("Could not read mmsi file")
    #return 1
    
    tableConds=[]
    tableVars= []
    for colId in parTable.listIds():
        if colId.startswith("var."):
            tableVars.append(colId)
        elif colId.startswith("cond."):
            tableConds.append(colId)
        
        
    emfacdb = Emfacdb(edb)
    emfacdb.read()

    sourcedb = Sourcedb(edb)
    
    macro = ControlFile(options.macro,removeComments=False)
    macro.setParam("FROM","      : "+options.begin)
    macro.setParam("TO","        : "+options.end)
    macro.setParam("edb.user:"******"edb.edb:",options.edb)
    #Write header to result file
    outfile = open(options.outfile,"w")
    outfile.write("MMSI"+"\t")
    for header in parTable.listIds():
        outfile.write("header"+"\t")
    outfile.write("Result\n")

    emisPattern = re.compile("#EMIS (\d*\.\d*)")
    command="gifmap -T -i "+options.macro+" -o /usr/airviro/tmp/res.tmp"
    
    for mmsi in mmsiList:
        if mmsi=="":
            continue
        sourcedb.read(X1=SHIPFLAG,Y1=int(mmsi))
        if len(sourcedb.sources) !=1:
            log.warning("No ship with MMSI %s found" %mmsi)
            continue
        source = sourcedb.sources[0]

        macro.setParam("SEARCHSTR","  : "+mmsi+",*")

        for rowInd,row in enumerate(parTable.data):
            for tableVar in tableVars:
                for actIndex, act  in  source.activity_emis.items():
                    ef = emfacdb[actIndex]
                    for varInd,var in ef.vars.items():
                        tableVarName=tableVar[4:]
                        if "var."+var.name==tableVarName:
                            break
                    colInd=parTable.colIndex("var."+var.name)
                    varVal=row[colInd]
                    act["VARLIST"][varInd-1]=(varInd,varVal)
            if len(tableVars)>0:
                sourcedb.write()

            for condPar in tableConds:
                colIndex = parTable.colIndex(condPar)
                condVal=row[colIndex]
                macroKey=condPar[5:]
                macro.setParam(macroKey,"  : "+condVal)

            macro.write()
            p=subprocess.Popen(command,stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
            retCode=p.wait()
            res=p.stdout.read()
            match=emisPattern.search(res)
            emis = float(match.groups()[0])
            outfile.write(mmsi+"\t")
            for val in row:
                outfile.write(val+"\t")
            outfile.write(str(emis)+"\n")

    log.info("Finished!")
    return 0
Example #6
0
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
                      
    parser.add_option("-l", "--loglevel",
                      action="store",dest="loglevel",default=2,
                      help="Sets the loglevel (0-3 where 3=full logging)")

    parser.add_option("-e","--edb",
                      action="store",dest="edb",
                      help="Edb to fetch ship data from")

    parser.add_option("-u","--user",
                      action="store",dest="user",default=None,
                      help="Owner of edb to fetch ship data from")
    
    parser.add_option("-o","--outfile",
                      action="store",dest="outfile",default=None,
                      help="Output file")
    
    parser.add_option("--MMSI",
                      action="store",dest="MMSI",default=None,
                      help="Text-file with mmsi to process")
    
    parser.add_option("--begin",
                      action="store",dest="begin",default=None,
                      help="Begin processing at 'YYMMDDhhmm'")
    
    parser.add_option("--end",
                      action="store",dest="end",default=None,
                      help="End processing at 'YYMMDDhhmm'")
    

    parser.add_option("-s","--subst",
                      action="store",dest="substance",
                      help="Substance to process")
    

    (options, args) = parser.parse_args()
    
    #--------------------Init logger-----------------------
    rootLogger=logger.RootLogger(int(options.loglevel))
    global log
    log = rootLogger.getLogger(sys.argv[0])

    #-----------------Validating options-------------------
    if options.MMSI is None:
        log.error("Need to specify '--MMSI <MMSI>'")
        return 1

    if options.begin is None:
        log.error("Need to specify '--begin <YYMMDDhh>'")
        return 1
    else:
        begin=string2date(options.begin)

    if options.end is None:
        log.error("Need to specify '--end <YYMMDDhh>'")
        return 1
    else:
        end=string2date(options.end)
    
    if options.outfile is None:
        log.error("Need to specify -o <output file>")
        return 1    

    if options.edb is None:
        log.error("Need to specify -e <edb>")
        return 1
    
    if options.user is None:
        log.error("Need to specify -u <user>")
        return 1
    
    dmn=Domain()
    edb = Edb(dmn,options.user,options.edb)

    sourcedb=Sourcedb(edb)
    sourcedb.read(X1=SHIPFLAG,Y1=int(options.MMSI))

    src=sourcedb.sources[0]
    emfacdb= Emfacdb(edb)
    emfacdb.read()

    subdb=Subdb(edb)
    subdb.readSubstances()

    foundEfForSubst=False
    v={}
    for efInd,ef in emfacdb.activities.items():
        if subdb[ef.subst]!=options.substance:
            continue
        
        foundEfForSubst=True
        for varInd,var in ef.vars.items():
            if efInd not in src.activity_emis:
                log.error("Emission factor with index %i for substance %s not used by source" %(efInd,options.substance))
                return 1
            ind,varVal=src.activity_emis[efInd]["VARLIST"][varInd-1]
            v[var.name]=float(varVal)
    
    if not foundEfForSubst:
        log.error("Did not find any emission factor for substance %s" %options.substance)
        
    output=open(path.abspath(options.outfile),"w")
    output.write("MMSI: %s\n" %options.MMSI)
    output.write("Substance: %s\n" %options.substance)
    output.write(str(v)+"\n")
    output.write("Datetime\tSpeed [knots]\tStatus\tEmis [g/s]\tAirviro plot emis [g/s]\tEmis [g]\n")    

    ts=series.ShipTs(begin=begin,end=end,res=300)
    ts.read(int(options.MMSI))
    if ts.nvals()==0:
        log.info("No data for ship in specified interval")
        return 0

    if options.substance!="Veh km":
        k=(0.85*v["P_ME_inst"])/pow(v["Vmax"]*0.94,3)

    totEmis=0

    #init coordinates to first record in ts
    initRec=0
    while ts.data[initRec,3] is np.ma.masked:
        initRec+=1        
    x=ts.data[initRec,3]
    y=ts.data[initRec,4]

    gap=0
    for i in range(initRec,ts.nvals()):
        #set last valid coordinate to x1
        if x is not np.ma.masked and y is not np.ma.masked:
            x1=x
            y1=y
        x=ts.data[i,3]
        y=ts.data[i,4]

        if x is np.ma.masked  or y is np.ma.masked:
            gap+=5
            continue

        if gap>MAXGAP:
            log.debug(" %s - AIS gap %i minutes > max (%i minutes), no emis calculated" %(ts.toDatetime(i).strftime("%y%m%d %H:%M"),gap,MAXGAP))
            gap=0
            continue
        
        dist=np.sqrt(pow(x-x1,2)+pow(y-y1,2))
        
        speed=(dist/300.0)/ KNOTS2MPERS
        if speed>MAXKNOTS:
            log.debug(" %s - speed %f knots > max (%f knots), no emis calculated" %(ts.toDatetime(i).strftime("%y%m%d %H:%M"),speed,MAXKNOTS))
            continue
        status=ts.data[i,5]        
        emis=None

        if options.substance=="Fuel consumption":
            if status == 1: #cruise
                P_ME_cruise=k*pow(speed,3)
                if P_ME_cruise>v["P_ME_inst"]:
                    P_ME_cruise=v["P_ME_inst"]

                emis=(P_ME_cruise*v["SFC_ME"]+
                      v["P_AE_inst"]*v["AE_usage_cruise"]*v["SFC_AE"]
                      )/3600.0

            elif status==2: #man
                emis=(v["P_ME_inst"]*v["ME_usage_man"]*v["SFC_ME"]+
                      v["P_AE_inst"]*0.5)/3600.0

            elif status==3: #hotelling
                emis=(v["P_AE_inst"]*v["AE_usage_hotel"]*v["SFC_AE"])/3600.0

            elif status==4: #ops
                emis=0
        elif options.substance=="Veh km":
            #emis=speed*KNOTS2MPERS
            emis=dist*0.001
            totEmis+=dist/1000.0        
        
        timediff=end-begin
        nhours=timediff.days*24+timediff.seconds/3600
        if emis is not None:
            output.write("%s\t%f\t%i\t%f\t%f\t%f\n" %(ts.toDatetime(i).strftime("%Y%m%d %H:%M"),speed,status,emis,emis/(nhours*12),emis*300))
        gap=0

    print "Total emission is %f" %totEmis            
Example #7
0
    def loadSources(self):
        self.sheets["sources"] = self.createSheet("sources")
        sourcedb = Sourcedb(self.edb)

        page = self.getPage(
            ["read", self.domain, self.edb.user, self.edb.name, "sourcedb"])
        #filename="/local_disk/dvlp/airviro/loairviro/test/TR_ships.out"

        substEmis = {}
        substAlob = {}
        subgrpEmis = {}
        subgrpAlob = {}
        activityEmis = {}
        activityAlob = {}
        srcAlobs = {}

        #Reading sources
        batchInd = 0
        while sourcedb.read(fileObject=page, accumulate=True, batchSize=1):
            self.updateProgressDlg("Loading point and area sources",
                                   "read batch %i" % batchInd)
            #print "Read batch %i" %batchInd
            batchInd += 1

        self.updateProgressDlg("Loading point and area sources",
                               "writing sources to sheet")
        #To present sources in a table with a nice header, it is necessary
        #to list all substances, alobs, subgroups, emfacs and variables that are
        #used in the edb. This is because each alob, substance etc. should be
        #shown only once in the header
        for src in sourcedb.sources:
            #Accumulate all alobs into a list
            for alob in src.ALOBOrder:
                srcAlobs[alob] = None

            #store all substances and their alobs in a dict
            for substInd, emis in src.subst_emis:
                if substInd not in substEmis:
                    substEmis[substInd] = {"alob": {}}
                for alob in emis.ALOBOrder:
                    substEmis[substInd]["alob"][alob] = None

            #store all substance groups and their alobs in a dict
            for subgrpInd, emis in src.subgrp_emis.items():
                if subgrpInd not in subgrpEmis:
                    subgrpEmis[subgrpInd] = {"alob": {}}
                for alob in emis.ALOBOrder:
                    subgrpEmis[subgrpInd]["alob"][alob] = None

            #Accumulate all activities and included alobs
            for emfacInd, emis in src.activity_emis.items():
                if emfacInd not in activityEmis:
                    activityEmis[emfacInd] = {"alob": {}, "var": {}}
                for varInd, varVal in emis["VARLIST"]:
                    #vars should also be indexed
                    activityEmis[emfacInd]["var"][varInd] = None
                for alob in emis.ALOBOrder:
                    activityEmis[emfacInd]["alob"][alob] = None

        #Writing header
        header0 = ()
        header = ()
        srcAlobInd = {}

        for parName in src.parOrder:
            header += (parName, )
        header0 += ("Static parameters", )
        header0 += (len(header) - len(header0)) * ("", )
        alobKeys = srcAlobs.keys()
        alobKeys.sort()
        for alobKey in alobKeys:
            header += (alobKey, )
        if src["ALOB"] > 0:
            header0 += ("ALOBs", )
            header0 += (len(header) - len(header0)) * ("", )

        for substInd in substEmis:
            substName = self.subdb.substNames[substInd]
            header += ("Emission", "Time variation", "Unit", "Macro",
                       "Activity code")
            header0 += ("Substance:", substName)
            header0 += (len(header) - len(header0)) * ("", )
            alobKeys = substEmis[substInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                row += [src.ALOB.get(alobKey, "")]

            for alob in alobKeys:
                header += (alob, )
            if len(alobKeys) > 0:
                header0 += ("ALOBs", )
                header0 += (len(header) - len(header0)) * ("", )

        for subgrpInd in subgrpEmis:
            subgrp = self.subgrpdb.subgrps[subgrpInd]
            header += ("Activity", "Time variation", "Unit", "Activity code")
            header0 += ("Substance group:", subgrp.name)
            header0 += (len(header) - len(header0)) * ("", )

            alobKeys = subgrpEmis[subgrpInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                header += (alobKey, )
            if subgrpEmis[subgrpInd]["alob"] > 0:
                header0 += ("ALOBs", )
                header0 += (len(header) - len(header0)) * ("", )

        for emfacInd in activityEmis:
            emfac = self.emfacdb[emfacInd]
            header += ("Time variation", )
            for varInd, var in emfac.vars.items():
                header += (var.name, )
            header0 += ("Emfac:", emfac.name)
            header0 += (len(header) - len(header0)) * ("", )

            header += ("Activity code", )
            alobKeys = activityEmis[emfacInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                header += (alobKey, )
            if len(alobKeys):
                header0 += ("ALOBs", )
                header0 += (len(header) - len(header0)) * ("", )
        header0 += (len(header) - len(header0)) * ("", )

        firstCol = 0
        for colInd, val in enumerate(header0[1:]):
            if val != "":
                bottomCellRange = self.sheets[
                    "sources"].getCellRangeByPosition(firstCol, 1, colInd - 2,
                                                      1)

                bottomBorder = bottomCellRange.BottomBorder
                bottomBorder.OuterLineWidth = 30
                bottomCellRange.BottomBorder = bottomBorder
                firstCol = colInd - 2

        out = [header0, header]
        for src in sourcedb.sources:
            row = []
            for par in src.parOrder:
                row += [unicode(src[par])]

            #Write alobs for sources
            alobKeys = srcAlobs.keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                row += [src.ALOB.get(alobKey, "")]

            #write substance emissions with alobs
            for substInd in substEmis:
                alobKeys = substEmis[substInd]["alob"].keys()
                alobKeys.sort()
                if substInd in src.subst_emis:
                    emis = src.subst_emis[substInd]
                    row += [
                        emis["EMISSION"], emis["TIMEVAR"], emis["UNIT"],
                        emis["MACRO"], emis["ACTCODE"]
                    ]
                    for alobKey in alobKeys:
                        row += [emis.ALOB.get(alobKey, "")]
                else:
                    row += ["", "", "", "", ""]  #empty cells for substance
                    row += [""] * len(alobKeys)

            #write substance group emissions with alobs
            for subgrpInd in subgrpEmis:
                alobKeys = subgrpEmis[subgrpInd]["alob"].keys()
                alobKeys.sort()
                if subgrpInd in src.subst_emis:
                    emis = src.subgrp_emis[subgrpInd]
                    row += [
                        emis["ACTIVITY"], emis["TIMEVAR"], emis["UNIT"],
                        emis["ACTCODE"]
                    ]
                    for alobKey in alobKeys:
                        row += [emis.ALOB.get(alobKey, "")]
                else:
                    row += ["", "", "", ""]  #empty cells for substance group
                    row += [""] * len(alobKeys)

            #write emfac emissions with variables and alobs
            for emfacInd in activityEmis:
                alobKeys = activityEmis[emfacInd]["alob"].keys()
                alobKeys.sort()
                varKeys = activityEmis[emfacInd]["var"].keys()
                varKeys.sort()
                if emfacInd in src.activity_emis:
                    emis = src.activity_emis[emfacInd]
                    row += [emis["TIMEVAR"]]
                    varVals = [var[1] for var in emis["VARLIST"]]
                    row += varVals
                    row += [emis["ACTCODE"]]
                    for alobKey in alobKeys:
                        row += [emis.ALOB.get(alobKey, "")]
                else:
                    row += 2 * [""] + [""] * len(varKeys) + [""
                                                             ] * len(alobKeys)
            out.append(tuple(row))
        cellRange = self.sheets["sources"].getCellRangeByPosition(
            0, 0,
            len(header) - 1,
            len(out) - 1)
        cellRange.setDataArray(tuple(out))
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("-v","--viewports",
                      action="store",dest="viewports",
                      help="Comma-separated list of area id's to be cut out, default is all")

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

    parser.add_option("-s","--suffix",
                      action="store",dest="suffix",default="v1",
                      help="Sets suffix to names of generated edb's to support version management, default is 'v1'")

    (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 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()
    viewports=[]
    if options.viewports is not None:        
        viewportIds=options.viewports.split(",")
    else:
        viewportIds=dmn.listViewports()
    for vpId in viewportIds:
        vp=ViewPort()
        vp.read(path.join(dmn.wndPath(),"modell.par"),vpId)
        viewports.append(vp)
    
    edb=Edb(dmn,options.user,options.edb)

    log.info("Reading sourcedb...")
    sourcedb=Sourcedb(edb)
    sourcedb.read()

    log.info("Reading emfacdb...")
    emfacdb=Emfacdb(edb)
    emfacdb.read()

    log.info("Reading subdb...")
    subdb=Subdb(edb)
    subdb.read()
    
    edbDotRsrc=edb.rsrcPath()
    
    for vpInd,vp in enumerate(viewports):        
        targetEdbName=vp.code+"_"+options.year+"_"+options.suffix     
        tEdb=Edb(dmn,options.user,targetEdbName)
        if tEdb.exists():
            log.info("Edb %s already exists, remove first to update" %targetEdbName)
            continue
        tEdb.create(edbRsrc=edbDotRsrc)
        log.info("Created empty edb %s" %targetEdbName)

        subdb.setEdb(tEdb)
        subdb.write()
        log.info("Wrote searchkeys")

        emfacdb.setEdb(tEdb)        
        emfacdb.write()
        log.info("Wrote emfacdb")
        
        tSourcedb=Sourcedb(tEdb)        
        
        log.info("Cutting out viewport %s (%i/%i)" %(vp.code,vpInd+1,len(viewports)))
        for srcInd,src in enumerate(sourcedb.sources):
            if includeShip(src,vp.code,src["Y1"],options.year):
                log.debug("Ship %i/%i included in %s" %(srcInd+1,len(sourcedb.sources),tEdb.name))
                tSourcedb.sources.append(src)
        tSourcedb.write()
        log.info("Wrote exatracted sources to %s" %tEdb.name)
        tEdb.setDesc("This edb has been extracted from %s under user %s, " %(edb.name,edb.user)+
                            "and includes all ships that have visited the map area %s (%s) during %s\n" %(vp.code,vp.name,options.year))
    log.info("Finished!")
    return 0
Example #9
0
    def loadSources(self):        
        self.sheets["sources"] = self.createSheet("sources")
        sourcedb=Sourcedb(self.edb)

        page=self.getPage(["read",self.domain,self.edb.user,self.edb.name,
                           "sourcedb"])
        #filename="/local_disk/dvlp/airviro/loairviro/test/TR_ships.out"
        
        substEmis={}
        substAlob={}
        subgrpEmis={}
        subgrpAlob={}
        activityEmis={}
        activityAlob={}
        srcAlobs={}

        #Reading sources
        batchInd=0
        while sourcedb.read(fileObject=page,accumulate=True,batchSize=1):
            self.updateProgressDlg(
                "Loading point and area sources","read batch %i" %batchInd)
            #print "Read batch %i" %batchInd
            batchInd+=1

        self.updateProgressDlg(
            "Loading point and area sources","writing sources to sheet")
        #To present sources in a table with a nice header, it is necessary
        #to list all substances, alobs, subgroups, emfacs and variables that are
        #used in the edb. This is because each alob, substance etc. should be
        #shown only once in the header
        for src in sourcedb.sources:
            #Accumulate all alobs into a list
            for alob in src.ALOBOrder:
                srcAlobs[alob]=None

            #store all substances and their alobs in a dict
            for substInd,emis in src.subst_emis:
                if substInd not in substEmis:
                    substEmis[substInd]={"alob":{}}
                for alob in emis.ALOBOrder:
                    substEmis[substInd]["alob"][alob]=None

            #store all substance groups and their alobs in a dict
            for subgrpInd,emis in src.subgrp_emis.items():
                if subgrpInd not in subgrpEmis:
                    subgrpEmis[subgrpInd]={"alob":{}}
                for alob in emis.ALOBOrder:
                    subgrpEmis[subgrpInd]["alob"][alob]=None

            #Accumulate all activities and included alobs
            for emfacInd,emis in src.activity_emis.items():
                if emfacInd not in activityEmis:
                    activityEmis[emfacInd]={"alob":{},"var":{}}
                for varInd,varVal in emis["VARLIST"]:
                    #vars should also be indexed
                    activityEmis[emfacInd]["var"][varInd]=None
                for alob in emis.ALOBOrder:
                    activityEmis[emfacInd]["alob"][alob]=None

        #Writing header
        header0=()
        header=()
        srcAlobInd={}

        for parName in src.parOrder:
            header+=(parName,)
        header0+=("Static parameters",)
        header0+=(len(header)-len(header0))*("",)
        alobKeys=srcAlobs.keys()
        alobKeys.sort()
        for alobKey in alobKeys:
            header+=(alobKey,)
        if src["ALOB"]>0:
            header0+=("ALOBs",)
            header0+=(len(header)-len(header0))*("",)

        for substInd in substEmis:
            substName=self.subdb.substNames[substInd]
            header+=("Emission","Time variation","Unit","Macro","Activity code")
            header0+=("Substance:",substName)
            header0+=(len(header)-len(header0))*("",)
            alobKeys=substEmis[substInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                row+=[src.ALOB.get(alobKey,"")]

            for alob in alobKeys:
                header+=(alob,)
            if len(alobKeys)>0:
                header0+=("ALOBs",)
                header0+=(len(header)-len(header0))*("",)

        for subgrpInd in subgrpEmis:
            subgrp=self.subgrpdb.subgrps[subgrpInd]
            header+=("Activity","Time variation","Unit","Activity code")
            header0+=("Substance group:",subgrp.name)
            header0+=(len(header)-len(header0))*("",)

            alobKeys=subgrpEmis[subgrpInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                header+=(alobKey,)
            if subgrpEmis[subgrpInd]["alob"]>0:
                header0+=("ALOBs",)
                header0+=(len(header)-len(header0))*("",)

                
        for emfacInd in activityEmis:
            emfac=self.emfacdb[emfacInd]
            header+=("Time variation",)
            for varInd,var in emfac.vars.items():
                header+=(var.name,)
            header0+=("Emfac:",emfac.name)
            header0+=(len(header)-len(header0))*("",)
                
            header+=("Activity code",)
            alobKeys=activityEmis[emfacInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                header+=(alobKey,)
            if len(alobKeys):
                header0+=("ALOBs",)
                header0+=(len(header)-len(header0))*("",)
        header0+=(len(header)-len(header0))*("",)

        firstCol=0
        for colInd,val in enumerate(header0[1:]):
            if val !="":
                bottomCellRange=self.sheets[
                    "sources"].getCellRangeByPosition(firstCol,1,colInd-2,1)
                
                bottomBorder = bottomCellRange.BottomBorder
                bottomBorder.OuterLineWidth = 30
                bottomCellRange.BottomBorder = bottomBorder
                firstCol=colInd-2
                
        out=[header0,header]
        for src in sourcedb.sources:
            row=[]
            for par in src.parOrder:
                row+=[unicode(src[par])]

            #Write alobs for sources
            alobKeys=srcAlobs.keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                row+=[src.ALOB.get(alobKey,"")]

            #write substance emissions with alobs
            for substInd in substEmis:
                alobKeys=substEmis[substInd]["alob"].keys()
                alobKeys.sort()
                if substInd in src.subst_emis:
                    emis=src.subst_emis[substInd]
                    row+=[emis["EMISSION"],
                          emis["TIMEVAR"],
                          emis["UNIT"],
                          emis["MACRO"],
                          emis["ACTCODE"]]
                    for alobKey in alobKeys:
                        row+=[emis.ALOB.get(alobKey,"")]
                else:
                    row+=["","","","",""] #empty cells for substance
                    row+=[""]*len(alobKeys)

            #write substance group emissions with alobs
            for subgrpInd in subgrpEmis:
                alobKeys=subgrpEmis[subgrpInd]["alob"].keys()
                alobKeys.sort()
                if subgrpInd in src.subst_emis:
                    emis=src.subgrp_emis[subgrpInd]
                    row+=[emis["ACTIVITY"],
                          emis["TIMEVAR"],
                          emis["UNIT"],
                          emis["ACTCODE"]]
                    for alobKey in alobKeys:
                        row+=[emis.ALOB.get(alobKey,"")]
                else:
                    row+=["","","",""] #empty cells for substance group
                    row+=[""]*len(alobKeys)

            #write emfac emissions with variables and alobs
            for emfacInd in activityEmis:
                alobKeys=activityEmis[emfacInd]["alob"].keys()
                alobKeys.sort()
                varKeys=activityEmis[emfacInd]["var"].keys()
                varKeys.sort()
                if emfacInd in src.activity_emis:
                    emis=src.activity_emis[emfacInd]
                    row+=[emis["TIMEVAR"]]
                    varVals = [var[1] for var in emis["VARLIST"]]
                    row+=varVals
                    row+=[emis["ACTCODE"]]
                    for alobKey in alobKeys:
                        row+=[emis.ALOB.get(alobKey,"")]
                else:
                    row+=2*[""]+[""]*len(varKeys)+[""]*len(alobKeys)
            out.append(tuple(row))
        cellRange=self.sheets[
            "sources"].getCellRangeByPosition(0,0,len(header)-1,len(out)-1)
        cellRange.setDataArray(tuple(out))