Example #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("-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!")
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()

        source.par["X1"].val = xc
        source.par["Y1"].val = yc
        source.par["X2"].val = 0
        source.par["Y2"].val = 0



sourcedb.write(filename=outfilename)

Example #3
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
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