Exemple #1
0
    def loadEmfacdb(self):
        self.sheets["emission factors"] = self.createSheet("emission factors")
        self.emfacdb = Emfacdb(self.edb)
        page = self.getPage(
            ["read", self.domain, self.edb.user, self.edb.name, "emfacdb"])
        self.emfacdb.read(fileObject=page)
        #            filename="/local_disk/dvlp/airviro/loairviro/test/emfac.out")
        nvars = [len(emfac.vars) for emfac in self.emfacdb.activities.values()]
        nvars = max(nvars)

        firstRow = 0
        for emfacInd, emfac in self.emfacdb.activities.items():
            out = []
            out.append(("Index:", emfacInd))
            out.append(("Name:", emfac.name))
            out.append(("Substance:", self.subdb[emfac.subst]))
            varIndices = []
            varNames = []
            varTypes = []
            formula = emfac.formula
            for i in range(1, len(emfac.vars) + 1):
                formula.replace("X%i" % i, emfac.vars[i].name)
                varIndices.append(str(i))
                varNames.append(emfac.vars[i].name)
                varTypes.append(emfac.vars[i].type)
            out.append(("Formula", formula))
            out.append(("Variable index:", ) + tuple(varIndices))
            out.append(("Variable names:", ) + tuple(varNames))
            out.append(("Variable types:", ) + tuple(varTypes))
            out.append(("", ))
            out.append(("", ))

            lastRow = firstRow + len(out) - 1
            lastCol = len(varIndices)

            #Make so all rows have the same length
            #Needed to use setDataArray
            for i in range(len(out)):
                completeRow = ("", ) * (lastCol - (len(out[i]) - 1))
                out[i] += completeRow

            cellRange = self.sheets["emission factors"].getCellRangeByPosition(
                0, firstRow, lastCol, lastRow)
            firstRow = lastRow + 1
            cellRange.setDataArray(tuple(out))
Exemple #2
0
    def loadEmfacdb(self):
        self.sheets["emission factors"] = self.createSheet("emission factors")
        self.emfacdb=Emfacdb(self.edb)
        page=self.getPage(["read",self.domain,self.edb.user,self.edb.name,
                           "emfacdb"])
        self.emfacdb.read(fileObject=page)
#            filename="/local_disk/dvlp/airviro/loairviro/test/emfac.out")
        nvars=[len(emfac.vars) for emfac in self.emfacdb.activities.values()]
        nvars=max(nvars)

        firstRow=0
        for emfacInd,emfac in self.emfacdb.activities.items():
            out=[]
            out.append(("Index:",emfacInd))
            out.append(("Name:",emfac.name))
            out.append(("Substance:",self.subdb[emfac.subst]))
            varIndices=[]
            varNames=[]
            varTypes=[]
            formula=emfac.formula
            for i in range(1,len(emfac.vars)+1):
                formula.replace("X%i" %i,emfac.vars[i].name)
                varIndices.append(str(i))
                varNames.append(emfac.vars[i].name)
                varTypes.append(emfac.vars[i].type)
            out.append(("Formula",formula))
            out.append(("Variable index:",)+tuple(varIndices))
            out.append(("Variable names:",)+tuple(varNames))
            out.append(("Variable types:",)+tuple(varTypes))
            out.append(("",))
            out.append(("",))

            lastRow=firstRow+len(out)-1
            lastCol=len(varIndices)

            #Make so all rows have the same length
            #Needed to use setDataArray
            for i in range(len(out)):
                completeRow=("",)*(lastCol-(len(out[i])-1))
                out[i]+=completeRow

            cellRange=self.sheets[
                "emission factors"].getCellRangeByPosition(0,firstRow,
                                                           lastCol,lastRow)
            firstRow=lastRow+1
            cellRange.setDataArray(tuple(out))
def main():
    #-----------Setting up and using the 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("-u", "--user",
                      action="store", dest="user", default=None,
                      help="Specify user manually")    

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

    parser.add_option("-t", "--template",
                      action="store",dest="cf",default=None,
                      help="Generate default controlfile")

    parser.add_option("-f", "--force",
                      action="store_true",dest="force",default=False,
                      help="To start the process without confirming the domain")
    
    parser.add_option("-w","--write",
                      action="store_true",dest="write",default=False,
                      help="To write directly to emfacdb")
    
    parser.add_option("-o", "--output",
                      action="store",dest="outputFile",default=None,
                      help="To write output in ascii format")

        
    (options, args) = parser.parse_args()

    #------------Setting up logging capabilities -----------
    rootLogger=logger.RootLogger(int(options.loglevel))
    log=rootLogger.getLogger(sys.argv[0])

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

    if len(args)!=1:
        parser.error("Incorrect number of arguments")
    
    if options.edb ==None:
        parser.error("Need to specify edb using flag -e")
    if options.user ==None:
        parser.error("Need to specify user using flag -u")        

    domainName=os.environ["AVDBNAME"]
    dmn = domain.Domain(domainName)        

    if not options.force:
        answer=raw_input("Chosen dbase is: "+domainName+",continue(y/n)?")    
        if answer=="y":
            dmn=domain.Domain()            
        else:
            log.info("Interrupted by user")
            sys.exit(1)
    
    if not dmn.edbExistForUser(options.edb,options.user):
        log.error("Edb "+options.edb+" does not exist for user "+
                     options.user+" in domain "+domainName)    
        sys.exit(1)

    #---Creating edb and rsrc objects------------------
    edb=Edb(dmn.name,options.user,options.edb)
    emfacdb=Emfacdb(edb)

    substIndexLookupDict = dmn.listSubstanceIndices()
    
    #Opening controlfile
    #---retrieving data from control file----
    cf=controlfile.ControlFile(fileName=path.abspath(args[0]))

    substances = cf.findStringList("substances:")
    variables = cf.findStringList("variables:")

    actIndex = 1
    for subst in substances:
        try:
            substanceIndex = substIndexLookupDict[subst]
        except KeyError:
            log.error("Substance %s could not be found in domain substance list" %subst)     
        alias = cf.findString(subst + ".alias:", optional = True, default = subst)

        varDict = {}
        for var in variables:
            defaultVarValue = cf.findString("var."+var+":",
                                     optional = True,
                                     default = None)
            varValue=None
            if "{substance}" in var:
                varId = var.replace("{substance}", alias)
                varValue = cf.findString("var."+varId+":", optional = True,
                                         default = None)
            if varValue is None and defaultVarValue is None:
                log.error("variable %s not specified for substance %s" %(var,
                                                                            alias))
                sys.exit(1)
            if varValue is None:
                varValue = defaultVarValue

            varValue = varValue.replace("{substance}", alias)
            varDict[var.replace("{substance}", alias)] = varValue

        #Replace variable names with variable values
        #Done twice to make sure nested dependencies are handled
        for i in range(2):
            for var in varDict:
                varValue = varDict[var]
                pattern = re.compile("{(.*?)}")
                usedVars = pattern.findall(varDict[var])
                for usedVar in usedVars:
                    if usedVar not in varDict:
                        log.error("Variable %s not defined in controlfile" %usedVar)
                        sys.exit(1)
                    subPattern = re.compile("{" + usedVar + "}")
                    varValue = subPattern.sub(varDict[usedVar], varValue)
                varDict[var] = varValue        

        #The value of the variable named formula contains the formula definition
        formula = varDict["formula"]
        #The variables in the formula are indexed
        try:
            varList = listVars(formula)
        except ValueError,e:
            log.error(e)
            sys.exit(1)
        formula = cleanFormula(formula)
        formula=formula.replace(" ","")
        actName=("SEI_" + subst)[:13].replace(" ","_")
        emfacdb.activities[actIndex] = Emfac(actName,
                                             actIndex,
                                             subst = substanceIndex,
                                             formula = formula,
                                             varList = varList)
        actIndex+=1
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
Exemple #5
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            
Exemple #6
0
class LoadEdb(XJobExecutor, unohelper.Base):
    """
    Load an Airviro edb into a number of spreadsheets
    """
    def __init__(self, ctx):
        self.ctx = ctx
        self.desktop = self.ctx.ServiceManager.createInstanceWithContext(
            "com.sun.star.frame.Desktop", self.ctx)
        self.doc = self.desktop.getCurrentComponent()
        self.sheets = {
            "sources": None,
            "substance groups": None,
            "emfac": None,
            "substances": None,
            "units": None
        }
        self.edb = None
        self.subdb = None
        self.subgrpdb = None
        self.host = None
        self.auth = None
        self.user = None
        self.edb = None
        self.domain = None

    def login(self, host, domain, user, pw):
        if "http://" not in host:
            host = "http://" + host

        #todo: get authenticator via https
        self.auth = "gmX=QxTLA3Cz3=9aacZk0k9Zae_FW8Kznq+9B_7_i3i0"
        self.domain = domain
        self.user = user
        self.host = host

    def setEdb(self, domainName, userName, edbName):
        self.edb = Edb(domainName, userName, edbName)

    def getPage(self, argList):
        req = "%s/cgi-bin/iairviro/do.cgi?%s&loAirviro.cgi&%s" % (
            self.host, self.auth, "&".join(argList))
        page = urllib2.urlopen(req)
        return page

    def loadSubdb(self):
        self.sheets["substances"] = self.createSheet("substances")
        self.subdb = Subdb(self.edb)
        page = self.getPage(
            ["read", self.domain, self.edb.user, self.edb.name, "subdb_0"])

        #        self.subdb.readSubstances(
        #            filename="/local_disk/dvlp/airviro/loairviro/test/substances.out",
        #            keepEmpty=True)
        self.subdb.readSubstances(fileObject=page, keepEmpty=True)
        out = (("Index", "Substance name"), )
        indices = sorted(self.subdb.substNames.keys())
        for i, ind in enumerate(indices):
            substName = self.subdb.substNames[ind]
            out += ((ind, substName), )

        cellRange = self.sheets["substances"].getCellRangeByPosition(
            0, 0, 1,
            len(out) - 1)
        cellRange.setDataArray(out)

    def loadSubgrpdb(self):
        self.sheets["substance groups"] = self.createSheet("substance groups")
        self.subgrpdb = Subgrpdb(self.edb)
        page = self.getPage(
            ["read", self.domain, self.edb.user, self.edb.name, "subgrpdb"])
        self.subgrpdb.read(fileObject=page)
        #            filename="/local_disk/dvlp/airviro/loairviro/test/subgrp.out")

        substances = []
        for subgrpInd, subgrp in self.subgrpdb.subgrps.items():
            for subst in subgrp.substances:
                substName = self.subdb.substNames[subst]
                if substName not in substances:
                    substances.append(substName)

        header1 = ("", "")
        header2 = ("Index", "Name")
        for substName in substances:
            header1 += (substName, "", "")
            header2 += ("Slope", "Offset", "Unit")

        out = (header1, header2)
        for subgrpInd, subgrp in self.subgrpdb.subgrps.items():
            #list names of substances in subgrp
            substInSubgrp = [self.subdb[s] for s in subgrp.substances]
            row = (subgrp.index, subgrp.name)
            for substName in substances:
                if substName in substInSubgrp:
                    substIndex = self.subdb.substIndices[substName]
                    slope = subgrp.substances[substIndex]["slope"]
                    offset = subgrp.substances[substIndex]["offset"]
                    unit = subgrp.substances[substIndex]["unit"]
                else:
                    slope = ""
                    offset = ""
                    unit = ""
                row += (slope, offset, unit)
            out += (row, )

        cellRange = self.sheets["substance groups"].getCellRangeByPosition(
            0, 0,
            len(header1) - 1,
            len(out) - 1)
        cellRange.setDataArray(out)

    def loadEmfacdb(self):
        self.sheets["emission factors"] = self.createSheet("emission factors")
        self.emfacdb = Emfacdb(self.edb)
        page = self.getPage(
            ["read", self.domain, self.edb.user, self.edb.name, "emfacdb"])
        self.emfacdb.read(fileObject=page)
        #            filename="/local_disk/dvlp/airviro/loairviro/test/emfac.out")
        nvars = [len(emfac.vars) for emfac in self.emfacdb.activities.values()]
        nvars = max(nvars)

        firstRow = 0
        for emfacInd, emfac in self.emfacdb.activities.items():
            out = []
            out.append(("Index:", emfacInd))
            out.append(("Name:", emfac.name))
            out.append(("Substance:", self.subdb[emfac.subst]))
            varIndices = []
            varNames = []
            varTypes = []
            formula = emfac.formula
            for i in range(1, len(emfac.vars) + 1):
                formula.replace("X%i" % i, emfac.vars[i].name)
                varIndices.append(str(i))
                varNames.append(emfac.vars[i].name)
                varTypes.append(emfac.vars[i].type)
            out.append(("Formula", formula))
            out.append(("Variable index:", ) + tuple(varIndices))
            out.append(("Variable names:", ) + tuple(varNames))
            out.append(("Variable types:", ) + tuple(varTypes))
            out.append(("", ))
            out.append(("", ))

            lastRow = firstRow + len(out) - 1
            lastCol = len(varIndices)

            #Make so all rows have the same length
            #Needed to use setDataArray
            for i in range(len(out)):
                completeRow = ("", ) * (lastCol - (len(out[i]) - 1))
                out[i] += completeRow

            cellRange = self.sheets["emission factors"].getCellRangeByPosition(
                0, firstRow, lastCol, lastRow)
            firstRow = lastRow + 1
            cellRange.setDataArray(tuple(out))

    def createSheet(self, name):
        try:
            sheets = self.doc.getSheets()
        except Exception:
            raise TypeError("Model retrived was not a spreadsheet")

        #TODO: warning dialogue before removing sheet
        if sheets.hasByName(name):
            sheets.removeByName(name)

        pos = sheets.getCount()
        sheets.insertNewByName(name, pos)
        return sheets.getByIndex(pos)

    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 trigger(self, args=''):
        """Called by addon's UI controls or service:URL"""
        self.createProgressDlg()
        self.setEdb("shipair", "sjov", "BS_2011_e1")
        self.updateProgressDlg("Loading edb: authenticating", "")
        self.login("www.shipair.smhi.se", "shipair", "airviro", "dummypw")
        self.updateProgressDlg("Loading substance list and searchkeys", "")
        self.loadSubdb()
        print "Loaded subdb"
        self.updateProgressDlg("Loading substance groups", "")
        self.loadSubgrpdb()
        print "Loaded subgrpdb"
        self.updateProgressDlg("Loading emission factors", "")
        self.loadEmfacdb()
        print "Loaded emfacdb"
        self.updateProgressDlg("Loading point and area sources", "")
        self.loadSources()
        self.updateProgressDlg("Edb loaded successfully!", "")
        sleep(1)
        print "Loaded sources"
        self.progressContainer.dispose()

    def createProgressDlg(self):
        parentwin = self.doc.CurrentController.Frame.ContainerWindow
        dialogModel = self.ctx.ServiceManager.createInstanceWithContext(
            'com.sun.star.awt.UnoControlDialogModel', self.ctx)
        FramePosSize = self.doc.CurrentController.Frame.ContainerWindow.PosSize
        WindowWidth = FramePosSize.Width
        dialogModel.Width = 200
        dialogModel.Height = 100
        dialogModel.PositionX = (WindowWidth / 2.2) - 105
        dialogModel.PositionY = 30
        dialogModel.Title = 'Loading sources'

        statusTxtModel = addControl(
            'com.sun.star.awt.UnoControlFixedTextModel', dialogModel, 6, 2,
            190, 20, 'Initializing', 'statusLabel')

        counterTxtModel = addControl(
            'com.sun.star.awt.UnoControlFixedTextModel', dialogModel, 6, 30,
            190, 50, '0', 'counterLabel')

        self.progressContainer = self.ctx.ServiceManager.createInstanceWithContext(
            'com.sun.star.awt.UnoControlDialog', self.ctx)

        self.progressContainer.setModel(dialogModel)
        self.progressContainer.setVisible(True)

        self.statusControl = self.progressContainer.getControl(
            'statusLabel').getModel()
        self.counterControl = self.progressContainer.getControl(
            'counterLabel').getModel()

    def updateProgressDlg(self, statusStr, counterStr):
        self.statusControl.Label = statusStr
        self.counterControl.Label = counterStr
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
Exemple #8
0
class LoadEdb(XJobExecutor,unohelper.Base):
    """
    Load an Airviro edb into a number of spreadsheets
    """
    def __init__(self,ctx):
        self.ctx = ctx
        self.desktop=self.ctx.ServiceManager.createInstanceWithContext(
            "com.sun.star.frame.Desktop", self.ctx )
        self.doc=self.desktop.getCurrentComponent()
        self.sheets={"sources":None,"substance groups":None,
                     "emfac":None,"substances":None, "units":None}
        self.edb=None
        self.subdb=None
        self.subgrpdb=None
        self.host=None
        self.auth=None
        self.user=None
        self.edb=None
        self.domain=None

    def login(self,host,domain,user,pw):
        if "http://" not in host:
            host="http://"+host
                    
        #todo: get authenticator via https        
        self.auth ="gmX=QxTLA3Cz3=9aacZk0k9Zae_FW8Kznq+9B_7_i3i0"
        self.domain=domain
        self.user=user
        self.host=host

    def setEdb(self,domainName,userName,edbName):
        self.edb=Edb(domainName,userName,edbName)

    def getPage(self,argList):
        req = "%s/cgi-bin/iairviro/do.cgi?%s&loAirviro.cgi&%s"%(self.host,
                                                                self.auth,
                                                                "&".join(argList))
        page = urllib2.urlopen(req)
        return page
        
    def loadSubdb(self):
        self.sheets["substances"] = self.createSheet("substances")
        self.subdb=Subdb(self.edb)
        page=self.getPage(["read",self.domain,self.edb.user,self.edb.name,
                           "subdb_0"])
        
#        self.subdb.readSubstances(
#            filename="/local_disk/dvlp/airviro/loairviro/test/substances.out",
#            keepEmpty=True)
        self.subdb.readSubstances(fileObject=page,keepEmpty=True)
        out=(("Index","Substance name"),)
        indices=sorted(self.subdb.substNames.keys())
        for i,ind in enumerate(indices):
            substName=self.subdb.substNames[ind]
            out+=((ind,substName),)

        cellRange=self.sheets[
            "substances"].getCellRangeByPosition(0,0,1,len(out)-1)
        cellRange.setDataArray(out)

    def loadSubgrpdb(self):
        self.sheets["substance groups"] = self.createSheet("substance groups")
        self.subgrpdb=Subgrpdb(self.edb)      
        page=self.getPage(["read",self.domain,self.edb.user,self.edb.name,
                           "subgrpdb"])
        self.subgrpdb.read(fileObject=page)
#            filename="/local_disk/dvlp/airviro/loairviro/test/subgrp.out")
        
        substances=[]
        for subgrpInd,subgrp in self.subgrpdb.subgrps.items():
            for subst in subgrp.substances:
                substName=self.subdb.substNames[subst]
                if substName not in substances:
                    substances.append(substName)

        header1=("","")
        header2=("Index","Name")
        for substName in substances:
            header1+=(substName,"","")
            header2+=("Slope","Offset","Unit")

        out=(header1,header2)
        for subgrpInd,subgrp in self.subgrpdb.subgrps.items():
            #list names of substances in subgrp
            substInSubgrp=[self.subdb[s] for s in subgrp.substances]
            row=(subgrp.index,subgrp.name)
            for substName in substances:
                if substName in substInSubgrp:
                    substIndex=self.subdb.substIndices[substName]
                    slope=subgrp.substances[substIndex]["slope"]
                    offset=subgrp.substances[substIndex]["offset"]
                    unit=subgrp.substances[substIndex]["unit"]
                else:
                    slope=""
                    offset=""
                    unit=""
                row+=(slope,offset,unit)
            out+=(row,)
                    
        cellRange=self.sheets[
            "substance groups"].getCellRangeByPosition(0,0,
                                                       len(header1)-1,len(out)-1)
        cellRange.setDataArray(out)

    def loadEmfacdb(self):
        self.sheets["emission factors"] = self.createSheet("emission factors")
        self.emfacdb=Emfacdb(self.edb)
        page=self.getPage(["read",self.domain,self.edb.user,self.edb.name,
                           "emfacdb"])
        self.emfacdb.read(fileObject=page)
#            filename="/local_disk/dvlp/airviro/loairviro/test/emfac.out")
        nvars=[len(emfac.vars) for emfac in self.emfacdb.activities.values()]
        nvars=max(nvars)

        firstRow=0
        for emfacInd,emfac in self.emfacdb.activities.items():
            out=[]
            out.append(("Index:",emfacInd))
            out.append(("Name:",emfac.name))
            out.append(("Substance:",self.subdb[emfac.subst]))
            varIndices=[]
            varNames=[]
            varTypes=[]
            formula=emfac.formula
            for i in range(1,len(emfac.vars)+1):
                formula.replace("X%i" %i,emfac.vars[i].name)
                varIndices.append(str(i))
                varNames.append(emfac.vars[i].name)
                varTypes.append(emfac.vars[i].type)
            out.append(("Formula",formula))
            out.append(("Variable index:",)+tuple(varIndices))
            out.append(("Variable names:",)+tuple(varNames))
            out.append(("Variable types:",)+tuple(varTypes))
            out.append(("",))
            out.append(("",))

            lastRow=firstRow+len(out)-1
            lastCol=len(varIndices)

            #Make so all rows have the same length
            #Needed to use setDataArray
            for i in range(len(out)):
                completeRow=("",)*(lastCol-(len(out[i])-1))
                out[i]+=completeRow

            cellRange=self.sheets[
                "emission factors"].getCellRangeByPosition(0,firstRow,
                                                           lastCol,lastRow)
            firstRow=lastRow+1
            cellRange.setDataArray(tuple(out))
        
                
    def createSheet(self,name):
        try:
            sheets = self.doc.getSheets()
        except Exception:
            raise TypeError("Model retrived was not a spreadsheet")

        #TODO: warning dialogue before removing sheet
        if sheets.hasByName(name):
            sheets.removeByName(name)
            
        pos = sheets.getCount()
        sheets.insertNewByName(name, pos)
        return sheets.getByIndex(pos)
        
    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 trigger(self,args=''):
        """Called by addon's UI controls or service:URL"""
        self.createProgressDlg()
        self.setEdb("shipair","sjov","BS_2011_e1")
        self.updateProgressDlg("Loading edb: authenticating","")
        self.login("www.shipair.smhi.se","shipair","airviro","dummypw")
        self.updateProgressDlg("Loading substance list and searchkeys","")
        self.loadSubdb()
        print "Loaded subdb"
        self.updateProgressDlg("Loading substance groups","")
        self.loadSubgrpdb()
        print "Loaded subgrpdb"
        self.updateProgressDlg("Loading emission factors","")
        self.loadEmfacdb()
        print "Loaded emfacdb"
        self.updateProgressDlg("Loading point and area sources","")
        self.loadSources()
        self.updateProgressDlg("Edb loaded successfully!","")
        sleep(1)
        print "Loaded sources"
        self.progressContainer.dispose()
        
    def createProgressDlg(self):
        parentwin=self.doc.CurrentController.Frame.ContainerWindow
        dialogModel = self.ctx.ServiceManager.createInstanceWithContext(
            'com.sun.star.awt.UnoControlDialogModel', self.ctx)
        FramePosSize = self.doc.CurrentController.Frame.ContainerWindow.PosSize
        WindowWidth = FramePosSize.Width
        dialogModel.Width = 200
        dialogModel.Height = 100
        dialogModel.PositionX = (WindowWidth/2.2) - 105
        dialogModel.PositionY = 30
        dialogModel.Title = 'Loading sources'

        statusTxtModel = addControl('com.sun.star.awt.UnoControlFixedTextModel',
                               dialogModel, 6, 2, 190, 20, 'Initializing',
                               'statusLabel')

        counterTxtModel = addControl('com.sun.star.awt.UnoControlFixedTextModel',
                             dialogModel, 6, 30, 190, 50, '0',
                             'counterLabel')
        
        self.progressContainer = self.ctx.ServiceManager.createInstanceWithContext('com.sun.star.awt.UnoControlDialog', self.ctx)
        
        self.progressContainer.setModel(dialogModel)
        self.progressContainer.setVisible(True)

        self.statusControl = self.progressContainer.getControl('statusLabel').getModel()
        self.counterControl = self.progressContainer.getControl('counterLabel').getModel()

    def updateProgressDlg(self,statusStr,counterStr):
        self.statusControl.Label=statusStr
        self.counterControl.Label=counterStr
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option("-u",'--targetUser',
                      action="store",dest="targetUser",
                      help="Name of target edb user")

    parser.add_option("-e","--targetEdb",
                      action="store",dest="targetEdb",
                      help="Name of target edb")
    
    parser.add_option("--templateUser",
                      action="store",dest="templateUser",
                      help="Name of template edb user")
    
    parser.add_option("--templateEdb",
                      action="store",dest="templateEdb",
                      help="Name of template edb")
    
    parser.add_option("-f","--force",
                      action="store_true",dest="force",
                      help="Overwrite existing data")

    parser.add_option("--templateACIndex",
                      action="store",dest="templateACIndex",
                      help="Attribute on template to join by")
    
    parser.add_option("--targetACIndex",
                      action="store",dest="targetACIndex",
                      help="Attribute on target to join 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("--ALOBFilter",
                      action="store",dest="ALOBFilter",
                      help="Exclude ships with specified ALOB"+
                      " (specify as '<key>=<value>')")

    parser.add_option("--MMSI",
                      action="store",dest="MMSI",
                      help="Specify single MMSI to fetch")

    parser.add_option("--logfile",
                      action="store",dest="logfile",
                      help="Send output to logfile") 
    
    (options, args) = parser.parse_args()

    #---------Init logger------------------    
    rootLogger = logger.RootLogger(level=options.loglevel)
    global log
    if options.logfile is not None:
        log = rootLogger.getFileLogger(sys.argv[0],options.logfile,format="%(message)s")
    else:
        log = rootLogger.getStreamLogger(sys.argv[0],infoFormat="%(message)s")
    
    #---------Validating and processing options-------------------
    
    if len(args)!=0:
        log.error("No arguments wanted, only options")
        log.info(str(args))
        
    if options.targetUser is None:
        log.error("Need to specify target user -u <user>")
        return 1
    if options.targetEdb is None:
        log.error("Need to specify target edb -e <edb>")
        return 1
    if options.templateUser is None:
        log.error("Need to specify template user --templateUser <user>")
        return 1
    if options.templateEdb is None:
        log.error("Need to specify template edb --templateEdb <edb>")
        return 1

    if options.MMSI is not None:
        X1=2147483647 #ship source identifier
        coords=options.MMSI.split(",")
        if len(coords)>1: #If X1,Y1 is used as identifier as in sourcedb
            Y1=int(coords[1])
        else:
            Y1=int(coords[0])
    else:
        X1=None
        Y1=None
            
    if options.ALOBFilter is not None:
        ALOBFilter=options.ALOBFilter.split("=")
        if len(ALOBFilter)!=2:
            log.error("Wrong format used for ALOBFilter,"+
                      " should be specified as <key>=<value>")
            return 1
    else:
        ALOBFilter=None

    #------------------------------------------------------

    msg=50*"-"+"\nUpdating ships with source templates\n"
    if options.MMSI is not None:
        msg+="Processing selected with MMSI: %s\n" %options.MMSI
    else:
        msg+="Processing all sources"

    if ALOBFilter is not None:
            msg+="Ships with ALOB %s=%s excluded\n" %(ALOBFilter[0],ALOBFilter[1])
    if options.force:
        msg+="Already existing data will be overwritten"
    else:
        msg+="Already existing data will not be overwritten"
        
    msg+="\n"
    log.info(msg)
    
    #Parameters that will not be transferred from template to target
    skipPars=["NAME","INFO","INFO2","ALOB","X1","Y1","X2","Y2","PX","PY"]
    
    #Set domain
    dmn=Domain()

    #Create edb objects
    targetEdb=Edb(dmn,options.targetUser,options.targetEdb)
    templateEdb=Edb(dmn,options.templateUser,options.templateEdb)

    #Read sourcedb from target edb
    log.debug("Reading sources from target edb")
    sourcedb=Sourcedb(targetEdb)

    templateACIndex=options.templateACIndex
    if templateACIndex is not None:
        try:
            templateACIndex=int(templateACIndex)
        except:
            log.error("Invalid templateACIndex, should be an integer")
            return 1
        
    targetACIndex=options.targetACIndex
    if targetACIndex is not None:
        try:
            targetACIndex=int(targetACIndex)
        except:
            log.error("Invalid targetACIndex, should be an integer")
            return 1

    log.debug("Reading sources from target edb")
    templates=loadTemplate(templateEdb,templateACIndex)
    log.info("Read %i unique sources from template edb" %len(templates.keys()))

    templateEmfacdb = Emfacdb(templateEdb)
    templateEmfacdb.read()
    targetEmfacdb = Emfacdb(targetEdb)
    targetEmfacdb.read()

    #Set templateEmfacdb to targetEdb (preparation to transfer emfacs)
    templateEmfacdb.setEdb(targetEdb)
    if templateEmfacdb.activities!={} and options.force:
        log.info("Overwriting emfacdb of target")
        try:
            templateEmfacdb.write(force=True)
        except PyAirviroOSException,err:
            log.error(str(err))
            return 1
        log.debug("Loaded emfacdb from template to target edb")