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 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 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("-i","--infile", action="store",dest="infile", help="Input csv file") # parser.add_option("-y","--year", # action="store",dest="year", # help="Only store sources for given year") parser.add_option("-v",dest='loglevel', action="store_const",default=get_loglevel(), help="produce verbose output") parser.add_option("-t", "--template", action="store",dest="cf",default=None, help="Generate default controlfile") parser.add_option("-o", "--outfile", action="store",dest="outfile",default=None, help="Name of outfiles (without extension)") parser.add_option("-d","--delimiter", action="store",dest="delimiter",default="\t", help="Delimiter used in csv-file") parser.add_option("-c","--filterCol", action="store",dest="filterCol", help="Header of column to use as filter") parser.add_option("-f","--filterVal", action="store",dest="filterVal", help="Value to use in filter") # parser.add_option("-g", "--geocodeRasterDir", # action="store",dest="geocodeRasterDir",default=None, # help="Directory with geocode rasters") (options, args) = parser.parse_args() #--------------------Init logger----------------------- # rootLogger = logger.RootLogger(level=options.loglevel) logging.basicConfig( format='%(levelname)s:%(name)s: %(message)s', level=options.loglevel, ) global log # log = rootLogger.getLogger(sys.argv[0]) log = logging.getLogger(parser.prog) #-----------------Validating options------------------- if options.cf is not None: generateCf(path.abspath(options.cf),controlFileTemplate) log.info("Wrote default controlfile") return 1 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.year)!=4: # log.error("Year should be given with four digits") # return 1 if len(args)!=1: log.error("Controlfile should be given as argument") return 1 dmn=Domain() edb=Edb(dmn,options.user,options.edb) if not edb.exists(): log.error("Edb %s does not exist" %options.edb) return 1 log.info("Parsing controlfile") cf=ControlFile(args[0]) cdbPars=re.compile("companydb\.par\.(\w*?):").findall(cf.content) fdbPars=re.compile("facilitydb\.par\.(\w*?):").findall(cf.content) sdbPars=re.compile("sourcedb\.par\.(\w*?):").findall(cf.content) substEmisNr=re.compile("sourcedb\.subst_emis\.([0-9]*)\.emis").findall(cf.content) subgrpEmisNr=re.compile("sourcedb\.subgrp_emis\.([0-9]*)\.emis").findall(cf.content) cdbCols={} cdbDefaults={} for par in cdbPars: cdbCols[par]=cf.findString("companydb.par.%s:" %par) cdbDefaults[par]=cf.findString("companydb.par.%s.default:" %par, optional=True,default=None) fdbCols={} fdbDefaults={} for par in fdbPars: fdbCols[par]=cf.findString("facilitydb.par.%s:" %par) fdbDefaults[par]=cf.findString("facilitydb.par.%s.default:" %par, optional=True,default=None) sdbCols={} sdbDefaults={} for par in sdbPars: sdbCols[par]=cf.findString("sourcedb.par.%s:" %par) sdbDefaults[par]=cf.findString("sourcedb.par.%s.default:" %par, optional=True,default=None) substEmisCols={} substEmisDefaults={} if substEmisNr is not None: for emisNr in substEmisNr: cols={} defaults={} emisPars=re.compile("sourcedb\.subst_emis\.%s\.(\w*?):" %(emisNr)).findall(cf.content) emisDefaultPars=re.compile( "sourcedb\.subst_emis\.%s\.(\w*?)\.default:" %(emisNr)).findall(cf.content) if emisPars is not None: for par in emisPars: cols[par]=cf.findString("sourcedb.subst_emis.%s.%s:" %(emisNr,par)) if emisDefaultPars is not None: for par in emisDefaultPars: defaults[par]=cf.findString("sourcedb.subst_emis.%s.%s.default:" %(emisNr,par), optional=True,default=None) substEmisCols[emisNr]=cols substEmisDefaults[emisNr]=defaults subgrpEmisCols={} subgrpEmisDefaults={} if subgrpEmisNr is not None: for emisNr in subgrpEmisNr: cols={} defaults={} emisPars=re.compile("sourcedb\.subgrp_emis\.%s\.(\w*?):" %(emisNr)).findall(cf.content) emisDefaultPars=re.compile( "sourcedb\.subgrp_emis\.%s\.(\w*?)\.default:" %(emisNr)).findall(cf.content) if emisPars is not None: for par in emisPars: cols[par]=cf.findString("sourcedb.subgrp_emis.%s.%s:" %(emisNr,par)) if emisDefaultPars is not None: for par in emisDefaultPars: defaults[par]=cf.findString("sourcedb.subgrp_emis.%s.%s.default:" %(emisNr,par), optional=True,default=None) subgrpEmisCols[emisNr]=cols subgrpEmisDefaults[emisNr]=defaults log.info("Reading subdb...") subdb=Subdb(edb) subdb.read() log.info("Reading companydb...") companydb=Companydb(edb) companydb.read() log.info("Reading sourcedb...") # source_stream = SourceStream(edb, 'w') source_stream = open(options.outfile, 'w') source_writer = ModelWriter(source_stream,encoding="HP Roman8") log.info("Reading facilitydb...") facilitydb=Facilitydb(edb) facilitydb.read() log.info("Reading subgrpdb") subgrpdb=Subgrpdb(edb) subgrpdb.read() log.info("Reading edb.rsrc") rsrc=Rsrc(edb.rsrcPath()) acCodeTables=[] for i in range(rsrc.numberOfCodeTrees("ac")): acCodeTables.append(CodeTable(rsrc.path,codeType="ac",codeIndex=i+1)) gcCodeTables=[] for i in range(rsrc.numberOfCodeTrees("gc")): gcCodeTables.append(CodeTable(rsrc.path,codeType="gc",codeIndex=i+1)) geocodeRasters=[] rast1=Raster() rast1.read("/usr/airviro/data/geo/topdown/dynamicRasters/dynamic__GEOCODE__1.txt") rast2=Raster() rast2.read("/usr/airviro/data/geo/topdown/dynamicRasters/dynamic__GEOCODE__2.txt") geocodeRasters.append(rast1) geocodeRasters.append(rast2) log.info("Reading csv-file") table=DataTable() table.read(options.infile,delimiter=options.delimiter,encoding="ISO-8859-15") if options.filterCol is not None: if options.filterCol not in table.colIndex: log.error("Filter column header not found in table") sys.exit(1) invalid=False nFiltered=0 nRows=0 log.info("Processing rows") for rowInd,row in enumerate(table.data): nRows+=1 if options.filterCol is not None: filterVal=row[table.colIndex[options.filterCol]] if options.filterVal!=str(filterVal): nFiltered+=1 continue comp = Company() for par in comp.parOrder: val=cdbDefaults.get(par,None) if par in cdbCols: colId=cdbCols[par] try: tableVal=row[table.colIndex[colId]] except KeyError: log.error( "No column with header %s, columns: %s" %( colId,str(table.listIds()))) if tableVal is not None: val = tableVal if val is not None: #Too long names are truncated if par=="NAME" and len(val)>45: val=val[:45] comp[par]=val fac = Facility() for par in fac.parOrder: val=fdbDefaults.get(par,None) if par in fdbCols: colId=fdbCols[par] tableVal=row[table.colIndex[colId]] if tableVal is not None: val = tableVal if val is not None: #Too long names are truncated if par=="NAME" and len(val)>45: val=val[:45] fac[par]=val src = Source() for par in ["X1", "Y1","X2","Y2", "PX","PY","NAME","INFO","INFO2","DATE","CHANGED", "CHIMNEY HEIGHT","GASTEMPERATURE","GAS FLOW", "SEARCHKEY1","SEARCHKEY2","SEARCHKEY3", "SEARCHKEY4","SEARCHKEY5","CHIMNEY OUT","CHIMNEY IN", "HOUSE WIDTH","HOUSE HEIGHT","NOSEGMENTS","BUILD_WIDTHS", "BUILD_HEIGHTS","BUILD_LENGTHS","BUILD_DISTFARWALL", "BUILD_CENTER","GEOCODE","FORMULAMACRO","ALOB"]: val=sdbDefaults.get(par,None) if par in sdbCols: colId=sdbCols[par] tableVal=row[table.colIndex[colId]] if tableVal is not None: val = tableVal if val is not None: #validate code if par=="GEOCODE" and val is not None: gcList=val.split() for codeIndex,code in enumerate(gcList): if not gcCodeTables[codeIndex].hasCode(code): log.error("Invalid geo code %s on row %i" %(code,rowInd)) invalid=True #Too long names are truncated if par=="NAME" and len(val)>45: val=val[:45] #Store in src object and convert to correct type src._fieldvalues[par] = lazy_parse( src, par, val) gc1=geocodeRasters[0].getVal(src.get_coord()[0],src.get_coord()[1]) gc2=geocodeRasters[1].getVal(src.get_coord()[0],src.get_coord()[1]) src.GEOCODE = [str(int(gc1)) + "." + str(int(gc2))] for emisNr,emis in substEmisCols.items(): substEmis={"unit":None,"ac":None,"substance":None,"emis":None} for par in substEmis.keys(): if par in emis: substEmis[par]=row[table.colIndex[emis[par]]] else: try: substEmis[par]=substEmisDefaults[emisNr][par] except KeyError: log.error( "Need to specify column or default value for subgrp emis %i" %emisNr) substInd=subdb.substIndex(substEmis["substance"]) if substInd is None: log.error("Invalid substance name %s on row %i" %( substEmis["substance"],rowInd)) sys.exit(1) try: unit=rsrc.sub[substEmis["unit"]] except KeyError: log.error("Invalid unit name %s on row %i" %(emis["unit"],rowInd)) sys.exit(1) acList=substEmis["ac"].split('\\')[0].split() for codeIndex,code in enumerate(acList): # if code == "2.A.4.2": # import pdb; pdb.set_trace() refCode = acCodeTables[codeIndex].checkCode(code) if refCode == "-": log.error("Invalid activity code %s on row %i" %(code,rowInd)) sys.exit(1) if refCode != code: acList[codeIndex] = refCode substEmis["ac"] = acList if substEmis["emis"] is not None and substEmis["emis"]!="0": try: emis = src.add_emission() emis.UNIT = substEmis["unit"] emis.ACTCODE = substEmis["ac"] # needs re-formatting emis.EMISSION = float(substEmis["emis"]) emis.SUBSTANCE = substInd emis.auto_adjust_unit(edb) except: # print substEmis # log.error("Invalid substance emission on row %i" %rowInd) invalid=True src.EMISSION=src.EMISSION[:-1] for emis in subgrpEmisCols.values(): subgrpEmis={"unit":None,"ac":None,"name":None,"emis":None} for par in subgrpEmis.keys(): if par in emis: subgrpEmis[par]=row[table.colIndex[emis[par]]] else: try: subgrpEmis[par]=subgrpEmisDefaults[emisNr][par] except KeyError: log.error( "Need to specify column or default value for subgrp emis %i" %emisNr) #validating subgrp name try: subgrp=subgrpdb.getByName(subgrpEmis["name"]) except KeyError: log.error("Invalid subgrp name %s on row %i" %(subgrpEmis["name"],rowInd)) invalid=True #validating subgrp emis unit try: unitFactor=rsrc.subGrpEm[subgrpEmis["unit"]] except KeyError: log.error("Invalid unit %s for subgrp emission on row %i" %( subgrpEmis["unit"],rowInd)) invalid=True #validating subgrp activity code acList=subgrpEmis["ac"].split() for codeIndex,code in enumerate(acList): refCode = acCodeTables[codeIndex].checkCode(code) if refCode == "-": log.error("Invalid activity code %s on row %i" %(code,rowInd)) invalid=True break if refCode != code: acList[codeIndex] = refCode substEmis["ac"] = acList try: src.addSubgrpEmis(subgrp.index,emis=subgrpEmis["emis"],unit=subgrpEmis["unit"], ac=subgrpEmis["ac"]) except: log.error("Invalid subgrp emission on row %i" %rowInd) invalid=True companydb.append(comp,force=True) facilitydb.append(fac,force=True) source_writer.write(src) # sourcedb.append(src) if invalid: log.info("No output written due to validation errors") sys.exit(0) if len(companydb.items)>0: if options.outfile is None: log.info("Writing companydb") else: log.info("Writing company db to file") companydb.write(filename=options.outfile+".companydb") if len(facilitydb.items)>0: if options.outfile is None: log.info("Writing facilitydb") else: log.info("Writing facilitydb to file") facilitydb.write(filename=options.outfile+".facilitydb") # if len(sourcedb.sources)>0: # if options.outfile is None: # log.info("Writing sourcedb") # else: # log.info("Writing sourcedb to file") # sourcedb.write(filename=options.outfile+".sourcedb") if options.filterCol is not None: log.info("Filtered out %i out of %i" %(nFiltered,nRows))
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("-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
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 parser parser = argparse.ArgumentParser(description=__doc__) utils.add_standard_command_options(parser) parser.add_argument( "-e","--edbs", action="store",dest="edbList", help="List of 'user/edb' pairs separated by :" ) parser.add_argument( "-L","--labels", action="store", dest="labels", help="List of edb labels separated by :" ) parser.add_argument( "-s","--substances", action="store",dest="substances", help="List of substance names separated by :" ) parser.add_argument( "-t","--title", action="store",dest="title", help="Report title" ) parser.add_argument( "-g","--gc-filter", action="store",dest="gcfilter", help="Filter on Geo codes, separated by :" ) parser.add_argument( "-o","--outfile", action="store",dest="outfile", help="Output filename" ) parser.add_argument( "-f","--format", action="store",dest="format", help="Output in 'excel','csv' or 'raw' " + "(Excel-format requires xlwt python module)" ) parser.add_argument("--substMapping", action="store",dest="substMapping", help="File with tab separated mappings of substance names") parser.add_argument("--markerTable", action="store",dest="markerTable", help="Table of codes to be formatted and commented") parser.add_argument( "macro",metavar="MACRO", help="A macro to use" ) args = parser.parse_args() if args.markerTable is not None: keys=["Year","GC","AC","note_1","note_2"] markerTable = DataTable(keys=keys,desc=[{"id":"Year","type":str},{"id":"GC","type":str},{"id":"AC","type":str},{"id":"note_1","type":str},{"id":"note_2","type":str}]) markerTable.read(args.markerTable) else: markerTable=None substMapping={} if args.substMapping is not None: with codecs.open(args.substMapping,encoding="HP Roman8",mode="r") as f: for line in f: oldName,newName = line.split(":") substMapping[oldName.strip()]=newName.strip() dmn = Domain() if args.gcfilter is not None: args.gcfilter = args.gcfilter.split(":") # Read original macro with codecs.open(args.macro, encoding="HP Roman8", mode="r") as f: originalContent = f.read() # Create a tmp copy of the macro, write content from the original macro macroTempFile = tempfile.NamedTemporaryFile( suffix=".sedb", dir=dmn.tmpDir() ) tmpMacro = codecs.open( macroTempFile.name, encoding="HP Roman8",mode="w" ) tmpMacro.write(originalContent) tmpMacro.flush() # Create a ControlFile obj to simplify reading and modifying macro macro = ControlFile(macroTempFile.name, removeComments=False) ebd = macro.findString("edb.edb:") user = macro.findString("edb.user:"******"edb.reportgeocode:")[-1]) acIndex = int(macro.findString("edb.reportactcode:")[-1]) if args.edbList is None: ebds = [[user, edb]] else: edbs = args.edbList.split(":") edbs = [e.split("/") for e in edbs] nedbs = len(edbs) if args.labels is None: labels = ["No label"] * len(edbs) else: labels = args.labels.split(":") if len(labels) != nedbs: log.error("Number of labels specified should match number of edb:s") sys.exit(1) if args.substances is None: log.error("Need to specify substances") sys.exit(1) else: substances = args.substances.split(":") if args.format not in ('excel','csv','raw'): log.error( "Invalid format specifier : %s, should be one of 'excel'" + ", 'csv' or 'raw'" %args.format ) sys.exit(1) elif args.format == "excel": try: import xlwt except: log.error( "trendReport.py requires python module xlwt to write excel-files") sys.exit(1) # first edb # import pdb; pdb.set_trace() edb = Edb(dmn, edbs[0][0], edbs[0][1]) # assume same code definitions in all edbs to be processed, read from first rsrc = edb.rsrc nrsubstances = len(substances) unitIndex = int(macro.findString("UNIT :")) units = rsrc.search[unitIndex] subdb = Subdb(edb) subdb.read() #decode input title using stdin encoding title=args.title.decode(sys.stdin.encoding) rawOutput = "" rawMeta = u"name: %s\nnrmacros: %i\nnrsub: %i\nunit: %s\n" %( title, nedbs, nrsubstances, units) emissions = [] for ind, edbUser in enumerate(edbs): label = labels[ind] userName = edbUser[0] edbName = edbUser[1] macro.setParam("edb.user:"******"edb.edb:", edbName) macro.setParam("USER :"******"EDB :", edbName) rawMeta += "macro.%i.edbuser: %s\n" %(ind, userName) rawMeta += "macro.%i.edbname: %s\n" %(ind, edbName) rawMeta += "macro.%i.desc: %s\n" %(ind, label) for subst in substances: log.info( "User: %s, edb: %s, substance %s" %( userName, edbName, subst) ) substanceIndex = subdb.substIndex(subst) macro.setParam("ELEMENT :", substanceIndex) macro.write() command = "xrepedb -i " + macro.name log.info("Running xrepedb for substance %s" % subst) # import pdb; pdb.set_trace() (returnCode, errMsg, outMsg) = utilities.execute(command) if returnCode != 0: log.error("Could not run %s\nstdout: %s\nstderr:%s" %( command,outMsg,errMsg)) sys.exit(1) if len(outMsg) < 10: log.error("Invalid output from xrepedb: %s" % outMsg) sys.exit(1) rawOutput += "#MACRO %i \"%s\" \"%s\"\n" % (ind, subst, labels[ind]) rawOutput += outMsg lines = outMsg.split("\n")[:-1] for lineInd, line in enumerate(lines): vals = line.split() ac = vals[1].split(".") gc = vals[3].split(".") if len(ac) == 1: if ac[0] == "<all>": acLev1 = "alla" else: acLev1 = ac[0] acLev2 = "alla" else: acLev1 = ac[0] acLev2 = ac[1] if len(gc) == 1: if gc[0] == "<all>": gcLev1 = "alla" else: gcLev1 = gc[0] gcLev2 = "alla" else: gcLev1 = gc[0] gcLev2 = gc[1] emis = float(vals[4]) if acLev1 == "alla": acLev1Name = "alla" acLev2Name = "alla" else: node = rsrc.ac[acIndex - 1].root.find(acLev1) acLev1Name = node.attrib["name"] if acLev2 == "alla": acLev2Name = "alla" else: node = rsrc.ac[acIndex-1].root.find( acLev1 + "/" + acLev2 ) acLev2Name = node.attrib["name"] if gcLev1 == "alla": gcLev1Name = "alla" gcLev2Name = "alla" else: node = rsrc.gc[gcIndex-1].root.find(gcLev1) gcLev1Name = node.attrib["name"] if gcLev2 == "alla": gcLev2Name = "alla" else: node = rsrc.gc[gcIndex - 1].root.find( gcLev1 + "/" + gcLev2 ) gcLev2Name = node.attrib["name"] if args.gcfilter is not None: if gc[0] not in args.gcfilter: # if args.gcfilter != gcLev1: continue emissions.append({"label": label, "substance": subst, "ac": '.'.join(ac), "gc": '.'.join(gc), "gcLev1": gcLev1Name, "gcLev2": gcLev2Name, "acLev1": acLev1Name, "acLev2": acLev2Name, "acLev1Code": acLev1, "acLev2Code": acLev2, "val": emis, "edbIndex": ind}) #Close tempfile to automatically remove it tmpMacro.close() if args.format == "raw": outfile = codecs.open(args.outfile,"w","HP Roman8") outfile.write(rawMeta) outfile.write(rawOutput) outfile.close() elif args.format == "csv": outfile = open(args.outfile,"w") desc = [ {'id': 'gc', 'type': unicode}, {'id': 'ac', 'type': unicode}, {'id': 'label', 'type': unicode}, {'id': 'user', 'type': unicode}, {'id': 'edb', 'type': unicode} ] for subst in substances: desc.append({'id': subst, 'type': float}) keys = ['gc', 'ac', 'label'] table = DataTable(desc=desc, keys=keys) log.info("Adding emissions to csv-table") for emis in emissions: row = [None] * len(desc) user = edbs[emis['edbIndex']][0] edb = edbs[emis['edbIndex']][1] row[table.colIndex['gc']] = emis['gc'] row[table.colIndex['ac']] = emis['ac'] row[table.colIndex['label']] = emis['label'] row[table.colIndex['user']] = user row[table.colIndex['edb']] = edb row[table.colIndex[emis['substance']]] = emis['val'] # data is appended to the correct row, or a new row is added if the # table keys do not match any existing row log.debug( "Adding row for substance %s, gc %s, ac %s" %( emis['substance'], emis['gc'], emis['ac']) ) table.addRow(row, append=True) table.write(outfile) outfile.close() else: # Create style objects for excel output header1Style = xlwt.easyxf( 'font: name Times New Roman,color-index black, bold on', num_format_str='0.000E+00' ) markerStyle1 = xlwt.easyxf( 'font: name Times New Roman,color-index red, bold off, italic on', num_format_str='0.000E+00') markerStyle2 = xlwt.easyxf( 'font: name Times New Roman,color-index orange, bold off, italic on', num_format_str='0.000E+00') normalStyle = xlwt.easyxf( 'font: name Times New Roman,color-index black, bold off', num_format_str='0.000E+00' ) excelBook = xlwt.Workbook() # Creating info sheet infoWs = excelBook.add_sheet("Info") infoWs.col(0).width = 256*20 infoWs.col(1).width = 256*25 infoWs.col(2).width = 256*20 infoWs.col(3).width = 256*200 infoWs.write(0,0,u"Rapportnamn:",header1Style) infoWs.write(0,1,title,header1Style) infoWs.write(1,0,u"Beskrivning av dataunderlaget",header1Style) infoWs.write(3,0,u"Makron (specificerar utsökningar ur databasen)",header1Style) infoWs.write(4,0,u"Etikett",header1Style) infoWs.write(4,1,u"Ägare till EDB",header1Style) infoWs.write(4,2,u"EDB (emissiondatabas)",header1Style) infoWs.write(4,3,u"Beskrivning",header1Style) for i,edbUser in enumerate(edbs): userName=edbUser[0] edbName=edbUser[1] label=labels[i] infoWs.write(5+i,0,label) infoWs.write(5+i,1,userName) infoWs.write(5+i,2,edbName) #reading edb description file (if it exists) edb=Edb(dmn,userName,edbName) infoWs.write(5+i,3,edb.desc().replace("\n"," ")) #split substances in green house gases and air quality related ghgList=[s for s in substances if s in ghgs] aqList=[s for s in substances if s not in ghgs] #Write air quality headers firstRow=4 #Add two rows for marker comments if markerTable is not None: firstRow+=2 if len(aqList)>0: aqWs = excelBook.add_sheet(u"Luftföroreningar") aqWs.col(0).width = 256*25 aqWs.col(1).width = 256*30 aqWs.col(2).width = 256*20 aqWs.col(3).width = 256*15 for col in range(nrsubstances*nedbs): aqWs.col(col+4).width=256*15 aqWs.write(0,0,u"Rapportnamn:",header1Style) aqWs.write(0,1,title,header1Style) aqWs.write(1,0,u"Emissioner av luftföroreningar",header1Style) aqWs.write(1,1,u"Enhet: "+units,header1Style) if markerTable is not None: aqWs.write(2,0,u"OBS! Röd kursiv text anger osäkra värden p.g.a. att en stor del av emissionen är fördelad med schabloner inom kommungruppen. Granska underkategorin \"Energiförsörjning via el-värmeverk samt inom industrin\" för att se eventuella misstänkta värden.",markerStyle1) aqWs.write(3,0,u"OBS! Orange kursiv text anger osäkra värden p.g.a. att trenden varierar kraftigt och eventuellt felaktigt, ytterligare verifiering krävs. Granska underkategorin \"Energiförsörjning via el-värmeverk samt inom industrin\" för att se eventuella misstänkta värden.",markerStyle2) aqWs.write(firstRow,0,"Huvudsektor",header1Style) aqWs.write(firstRow,1,"Undersektor",header1Style) aqWs.write(firstRow,2,u"Län",header1Style) aqWs.write(firstRow,3,"Kommun",header1Style) #Write ghg headers if len(ghgList)>0: ghgWs = excelBook.add_sheet(u"Växthusgaser") ghgWs.col(0).width = 256*25 ghgWs.col(1).width = 256*30 ghgWs.col(2).width = 256*20 ghgWs.col(3).width = 256*15 for col in range(nrsubstances*nedbs): ghgWs.col(col+4).width=256*15 ghgWs.write(0,0,u"Rapportnamn:",header1Style) ghgWs.write(0,1,title,header1Style) ghgWs.write(1,0,u"Emissioner av Växthusgaser",header1Style) ghgWs.write(2,0,u"CO2-ekv. efter ämnesnamn innebär att emissionen är uttryckt i CO2-ekvivalenter",header1Style) if markerTable is not None: ghgWs.write(3,0,u"OBS! Röd kursiv text anger osäkra värden p.g.a. att en stor del av emissionen är fördelad med schabloner inom kommungruppen. Granska underkategorin \"Energiförsörjning via el-värmeverk samt inom industrin\" för att se eventuella misstänkta värden.",markerStyle1) ghgWs.write(4,0,u"OBS! Orange kursiv text anger osäkra värden p.g.a. att trenden varierar kraftigt och eventuellt felaktigt, ytterligare verifiering krävs. Granska underkategorin \"Energiförsörjning via el-värmeverk samt inom industrin\" för att se eventuella misstänkta värden.",markerStyle2) ghgWs.write(1,1,u"Enhet: "+units,header1Style) ghgWs.write(firstRow,0,"Huvudsektor",header1Style) ghgWs.write(firstRow,1,"Undersektor",header1Style) ghgWs.write(firstRow,2,u"Län",header1Style) ghgWs.write(firstRow,3,"Kommun",header1Style) def getColInd(nmacros, substances,macroInd,subst): #gets the column index in excel file sInd=substances.index(subst) #Including extra columns to write CO2-equivalents nSubstWithCO2equivalents=0 for s in substances[:sInd+1]: if s in doubleColumns: nSubstWithCO2equivalents+=1 return 4 + macroInd+sInd*nmacros+nSubstWithCO2equivalents*(macroInd+1) #write macro labels and substance headers for air quality sheet for sInd,subst in enumerate(aqList): for i,edbUser in enumerate(edbs): col=getColInd(nedbs,aqList,i,subst) aqWs.write(firstRow-1,col,labels[i],header1Style) #If a substance name is given in mapping this is used, otherwise #The substance bname from the airviro substance list is used aqWs.write(firstRow,col,substMapping.get(subst,subst),header1Style) #write macro labels and substance headers for ghg sheet for sInd,subst in enumerate(ghgList): for i,edbUser in enumerate(edbs): col=getColInd(nedbs,ghgList,i,subst) #If CO2-equivalents are calculated, an extra column is needed if subst in doubleColumns: ghgWs.write(firstRow-1,col-1,labels[i],header1Style) ghgWs.write(firstRow-1,col,labels[i],header1Style) #If CO2-equivalents are calculated, an extra column is needed if subst in doubleColumns: #debug statement #print "writing subst %s in col %i and %i" %(subst,col-1,col) ghgWs.write(firstRow,col-1,substMapping.get(subst,subst),header1Style) ghgWs.write(firstRow,col,substMapping.get(subst,subst)+"CO2-ekv.",header1Style) elif subst in storedAsCO2equivalents: #debug statement #print "writing subst %s in col %i" %(subst,col) ghgWs.write(firstRow,col,substMapping.get(subst,subst)+"CO2-ekv.",header1Style) else: #debug statement #print "writing subst %s in col %i" %(subst,col) ghgWs.write(firstRow,col,substMapping.get(subst,subst),header1Style) #looping over all emissions, writing them to the correct column and row ghgRow=[] aqRow=[] for m in range(nedbs*nrsubstances+4+3*nedbs): ghgRow.append(firstRow+1) for m in range(nedbs*nrsubstances+4): aqRow.append(firstRow+1) for emis in emissions: subst = emis["substance"] emisVal=emis["val"] edbInd=emis["edbIndex"] #Check if gc, ac and year can be found in the error list #debugging marker style if markerTable is not None: TableRowInd=markerTable.rowIndices([labels[edbInd], emis["gc"], emis["ac"], "ja","*"]) if len(TableRowInd) >0: valueStyle=markerStyle1 else: TableRowInd=markerTable.rowIndices([labels[edbInd], emis["gc"], emis["ac"], "*","ja"]) if len(TableRowInd)>0: valueStyle=markerStyle2 else: valueStyle=normalStyle else: valueStyle=normalStyle if subst in ghgList: col=getColInd(nedbs,ghgList,edbInd,subst) row=ghgRow[col] if ghgRow[0]<=+row: ghgWs.write(row,0,emis["acLev1"],normalStyle) ghgWs.write(row,1,emis["acLev2"],normalStyle) ghgWs.write(row,2,emis["gcLev1"],normalStyle) ghgWs.write(row,3,emis["gcLev2"],normalStyle) ghgRow[0]+=1 #converts the emission to CO2-ekquivalents if subst in doubleColumns: ghgWs.write(row,col-1,float(emisVal),valueStyle) ghgWs.write(row,col,float(emisVal)*float(ekvFactors[subst]),valueStyle) else: ghgWs.write(row,col,float(emisVal),valueStyle) ghgRow[col]+=1 else: col=getColInd(nedbs,aqList,edbInd,subst) row=aqRow[col] if aqRow[0]<=+row: aqWs.write(row,0,emis["acLev1"],normalStyle) aqWs.write(row,1,emis["acLev2"],normalStyle) aqWs.write(row,2,emis["gcLev1"],normalStyle) aqWs.write(row,3,emis["gcLev2"],normalStyle) aqRow[0]+=1 aqWs.write(row,col,float(emisVal),valueStyle) aqRow[col]+=1 excelBook.save(args.outfile) log.info("Finished!")
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
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(): actionList = [ 'addSubstance', 'removeSubstance', 'mapSubstances', 'addCodeTree', 'removeCodeTree', 'moveCodeTree', 'extractByGeocode', 'uniteGrids', 'scale' ] # setting up parser parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter ) utils.add_standard_command_options(parser) parser.add_argument("-d", "--doc", action="store_true", dest="doc", help="Prints more detailed documentation and exit") parser.add_argument("-u", "--user", action="store", dest="user", default=None, help="Specify user manually") parser.add_argument("-e", "--edb", action="store", dest="edb", default=None, help="Name of target edb") parser.add_argument("-s", "--substance", action="store", dest="substance", help="Name of substance to add or remove") parser.add_argument("--substanceFactorFile", action="store", dest="substanceFactorFile", help="File with substance factors") parser.add_argument("--substanceMapping", action="store", dest="substanceMapping", help="File with mapping for substance indices") parser.add_argument("--codeMapping", action="store", dest="codeMapping", help="File with mapping from old codes to new") parser.add_argument("--edbMapping", action="store", dest="edbMapping", help="File with mapping from gc to edb") parser.add_argument("-f", "--force", action="store_true", dest="force", default=False, help="Start the process without confirming the domain") parser.add_argument("--factor", action="store", type=float, dest="factor", help="Factor used to scale emissions") parser.add_argument( "-n", "--names", action="store", dest="sourceNames", default=['.*'], nargs='*', help="Space separated list of regular expressions" ) parser.add_argument( "--sourceTypes", action="store", dest="sourceTypes", nargs='*', default=['griddb', 'sources'], choices=["griddb", "sources", "subgrpdb", "svehdb", "vehdb"], help="Source types to process" ) parser.add_argument( "--codeType", action="store", dest="codeType", default='ac', help="Code type to process" ) parser.add_argument( "--codeIndex", action="store", dest="codeIndex", default=1, type=int, help="Index of code tree to use in processing" ) parser.add_argument( "--newIndex", action="store", dest="newIndex", default=None, help="New index when moving an already defined code tree" ) parser.add_argument( "--codeLevel", action="store", dest="codeLevel", default=1, type=int, help="Level of code tree to use in processing") parser.add_argument( "--actions", action="store", dest="actions", nargs='*', choices=actionList, help="Action list" ) args = parser.parse_args() if args.doc: print doc sys.exit() # if len(args) > 0: # log.error("Incorrect number of arguments") # sys.exit(1) if args.edb is None: log.error("Need to specify edb") sys.exit(1) if args.user is None: log.error("Need to specify user") sys.exit(1) dmn = Domain() edb = Edb(dmn, args.user, args.edb) subdb = Subdb(edb) subdb.read() rsrc = Rsrc(edb.rsrcPath()) # Check if edb exist if not edb.exists(): log.error("Edb " + args.edb + " does not exist for user " + args.user + " in domain " + dmn.name) sys.exit(1) if args.codeType.lower() == "ac": codeType = "ACTIVITYCODE" elif args.codeType.lower() == "gc": codeType = "GEOCODE" else: parser.error("codeType should be either 'gc' or 'ac'") if args.codeIndex < 1: raise ValueError("Minimum code index is 1") if args.codeIndex > 1 and codeType == "gc": raise OSError("Multiple geocode trees not implemented") for a in args.actions: if a not in actionList: parser.error("Unknown action %s" % a) log.info("User: "******"Edb: " + args.edb) loadGridData = False # Set values in argsDict argDict = { "edb": edb, "user": args.user, "domain": dmn, "grids": [], "filters": args.sourceNames, "codeType": codeType, "codeIndex": args.codeIndex, "codeLevel": args.codeLevel, "sources": None, "griddb": None, "subgrpdb": None, "roaddb": None, "vehdb": None, "svehdb": None, "rsrc": rsrc } # ----- Reading sources from edb ------------------------------- if 'griddb' in args.sourceTypes: grids = [] gridNames = edb.listGrids() for name in gridNames: grd = egrid.Egrid(edb, name) grd.readAsc() grids.append(grd) argDict["grids"] = grids if 'sources' in args.sourceTypes: argDict["sources"] = list(ModelReader(SourceStream(edb, mode='r'))) if 'roaddb' in args.sourceTypes: log.error("Not implemented with new roaddb-structure") sys.exit(1) if 'subgrpdb' in args.sourceTypes: subgrpdb = Subgrpdb(edb) subgrpdb.read() argDict["subgrpdb"] = subgrpdb # emfacdbdb = Emfacdb(edb) # emfacdb.read() # argDict["subgrpdb"] = subgrpdb if 'svehdb' in args.sourceTypes: svehdb = Svehdb(edb) svehdb.read() argDict["svehdb"] = svehdb # TODO: add option for vehdb # parse additional args # -----addSubstance-------------------------------------- if "addSubstance" in args.actions: if args.substance is None: parser.error("Action addSubstance needs" + "--substance to be specified") if args.substanceFactorFile is None: parser.error("Option addSubstance also needs" + "--substanceFactorFile to be specified") if args.substance not in subdb.substIndices: raise ValueError( "Substance %s not in substance list" % args.substance) substanceNameFactorDict = parseMapping( path.abspath(args.substanceFactorFile), valueType=float) substanceFactorDict = {} # Converts mapping between substance name and factor to # mapping between substance index and factor for name, factor in substanceNameFactorDict.iteritems(): if name not in subdb.substIndices: raise KeyError( "Substance : %s not found in substance list" % name ) ind = subdb.substIndices[name] substanceFactorDict[ind] = factor argDict["substanceFactorDict"] = substanceFactorDict argDict["substance"] = subdb.substIndices[args.substance] # ----removeSubstance------------------------------------ if "removeSubstance" in args.actions: if args.substance is None: parser.error("Action removeSubstance needs" + "--substance to be specified") if args.substance not in subdb.substIndices: log.error("Substance %s not in substance list" % args.substance) sys.exit(1) argDict["substance"] = subdb.substIndices[args.substance] # ----mapSubstances-------------------------------------- if "mapSubstances" in args.actions: if args.substanceMapping is None: parser.error("Action mapSubstances needs" + "--substanceMapping to be specified") substanceMappingFile = path.abspath(args.substanceMapping) substanceMapping = parseMapping( substanceMappingFile, keyType=int, valueType=int ) argDict["substanceMapping"] = substanceMapping # ----addCodeTree----------------------------------------- if "addCodeTree" in args.actions: if args.codeMapping is None: parser.error("Action addCodeTree needs" + "--codeMapping to be specified") if args.newIndex is None: parser.error("Action addCodeTree needs" + "--newIndex to be specified") codeMappingFile = path.abspath(args.codeMapping) codeMapping = parseMapping(codeMappingFile) argDict["codeMapping"] = codeMapping argDict["newIndex"] = int(args.newIndex) # ----removeCodeTree------------------------------------- # Only arguments with default values needed, no validation is needed # ----moveCodeTree--------------------------------------- if "moveCodeTree" in args.actions: if args.newIndex is None: parser.error("Action moveCodeTree needs" + "--newIndex to be specified") try: argDict["newIndex"] = int(args.newIndex) except ValueError: log.error("newIndex must be an integer value") sys.exit(1) # ----extractByGeocode----------------------------------- if "extractByGeocode" in args.actions: if args.edbMapping is None: parser.error("Action extractByGeocode needs" + "--edbMapping to be specified") log.info("Slicing edb by geocode") edbMappingFile = path.abspath(args.edbMapping) # Geocode has to be int edbMapping = parseMapping(edbMappingFile, keyType=int) argDict["edbMapping"] = edbMapping if 'scale' in args.actions: argDict['substance'] = subdb.substIndices[args.substance] argDict['factor'] = args.factor if args.factor is None: log.error("Must specify --factor") sys.exit(1) if args.substance is None: log.error("Must specify --substance") sys.exit(1) # Processing actions for action in args.actions: log.info("Running action: %s" % action) argDict = eval(action)(argDict) # Writes each processed grid to edb if loadGridData: for grd in argDict["grids"]: if not match(grd["NAME"], argDict["filters"]) or grd.hasSubgrp(): continue else: log.debug( "Wrote grid %s to edb: %s, user: %s" % ( grd.par["NAME"].val, grd.edb.name, grd.user) ) grd.load() else: for grd in argDict["grids"]: if not match(grd["NAME"], argDict["filters"]) or grd.hasSubgrp(): continue else: log.debug( "Wrote .asc-file for grid %s to edb: %s, user: %s" % ( grd.par["NAME"].val, grd.edb.name, grd.user) ) grd.writeAsc(grd.getAscPath()) # Writes each processed database to edb if argDict["sources"] is not None: with SourceStream(edb, mode="w") as stream: model_writer = ModelWriter(stream) for src in argDict["sources"]: log.debug('Wrote source %s' % src.NAME) model_writer.write(src) for dbName in ["roaddb", "subgrpdb", "svehdb", "vehdb"]: if argDict[dbName] is not None: argDict[dbName].write() log.info( "Wrote %s to edb: %s, user: %s" % ( dbName, edb.name, edb.user) )
def main(): # Parse command line arguments parser = argparse.ArgumentParser(description=__doc__) utils.add_standard_command_options(parser) parser.add_argument("controlfile", metavar='CONTROLFILE', action="store", help="Controlfile for topdown processing") parser.add_argument("-t", "--template", metavar='TEMPLATEFILE', action="store",dest="cf",default=None, help="Generate default controlfile") args = parser.parse_args() if args.cf is not None: generateCf(args.cf) log.info("Wrote default controlfile") sys.exit(0) log.info("Starting topdown processing") # Opening controlfile cf = ControlFile(args.controlfile) dmn = Domain() log.info("Reading topdown table") tdTableName = cf.findExistingPath("topDownTable:") tdTable = DataTable() tdTable.keys.append("Code") tdTable.read(tdTableName,delimiter=";") log.info("Reading national totals table") natTotalTableName = cf.findExistingPath("nationalTotalTable:") natTable = DataTable(desc=[{"id": "Code", "type":unicode}, {"id": "description", "type":unicode}]) natTable.keys.append("Code") natTable.read(natTotalTableName, units=True, defaultType=str) notationKeys = ["NE", "NO", "NA", "IE"] log.debug("Remove notation keys from national totals table") for row in natTable.data: for i in range(len(row)): if row[i] in notationKeys: row[i] = None log.debug("Convert all emission columns in national totals to float") for colId in natTable.listIds(): if colId not in ["Code","description"]: natTable.convertCol(colId,float) log.debug("Store units from national totals for each substance in dict") natUnits={} for col in natTable.desc: if col.get("units",None)!=None: natUnits[col["id"]]=col["units"] log.debug("Read remaining data from control file") bottomupEdbName = cf.findString("bottomUpEdb:") topDownEdbName = cf.findString("topDownEdb:") emissionsEdbName = cf.findString("emissionsEdb:") userName = cf.findString("user:"******"year:") #initialize edb objects buEdb = Edb(dmn,userName,bottomupEdbName) tdEdb = Edb(dmn,userName,topDownEdbName) eEdb = Edb(dmn,userName,emissionsEdbName) log.info("Reading/preparing EDB:s") log.info("Reading subdb") subdb = Subdb(eEdb) subdb.read() log.info("Reading subgrpdb") subgrpdb = SubgrpStream(buEdb) subgrpdb.read() log.info("Reading facilitydb") facilityIn = FacilityStream(buEdb) log.info("Reading companydb") companyIn = CompanyStream(buEdb) facilityOut = FacilityStream(eEdb,mode="w") companyOut = CompanyStream(eEdb,mode="w") log.info("Writing company db to result edb") companyOut.write(companyIn.read()) log.info("Writing facility db to result edb") facilityOut.write(facilityIn.read()) if not buEdb.exists(): log.error("Edb " + buEdb.name + " does not exist for user " + userName + " in domain " + dmn.name) sys.exit(1) if not tdEdb.exists(): log.error("Edb " + tdEdb.name + " does not exist for user " + userName + " in domain " + dmn.name) sys.exit(1) if not eEdb.exists(): log.error("Edb " + eEdb.name + " does not exist for user " + userName + " in domain " + dmn.name) sys.exit(1) keys = tdEdb.listGrids() msg = "%i keys found in edb: %s" % (len(keys), tdEdb.name) log.info(msg) # sourcedb from bottom-up edb with SourceStream(buEdb, mode='rb') as source_instream: source_reader = ModelReader(source_instream) bu_sources = list(source_reader) log.info( "%i point sources found in edb: %s" % ( len(bu_sources), buEdb.name) ) # Empty sourcedb of the result edb if cf.findBoolean("emptyEmissionSourcedb:"): eEdb.empty_sourcedb() e_sources = [] log.info("Removed point sources from edb: %s" % (eEdb.name)) else: # sourcedb from emission edb (result edb) with SourceStream(eEdb, mode='rb') as source_instream: source_reader = ModelReader(source_instream) e_sources = list(source_reader) msg = "%i point sources found in edb: %s" % (len(e_sources), eEdb.name) log.info(msg) if not path.exists(eEdb.rsrcPath()): log.error("No edb.rsrc exists for emission edb") sys.exit() else: rsrc = Rsrc(eEdb.rsrcPath()) acIndex = cf.findInt("acIndex:") codeDepth = rsrc.ac[acIndex-1].depth substances = cf.findStringList("substances:") for subst in substances: if subst not in subdb.substIndices: log.error("Substance: " + subst + " not in Airviro substance list") sys.exit() # Initialize trace for debug and additional logging if cf.findBoolean("trace:") == True: log.info("Initializing trace for detailed logging") trace = TraceDef( active=True, substances=cf.findStringList("trace.substances:"), logfile=cf.findString("trace.logfile:"), regdefgc=cf.findIntList("trace.regdef.gc:", optional=True, default=None), gcDefRaster=cf.findExistingPath("trace.gcraster:") ) else: trace = TraceDef(active=False) log.info("Initializing result table") resTablePath = cf.findString("resTable:") resTable = DataTable(desc=[{"id": "Code", "type": unicode}]) resTable.keys.append("Code") for subst in substances: resTable.addCol({"id": subst, "type": float, "unit": "%"}) # Create emission grid template (with geocodes) log.info("Reading emission grid template") eGridTemplatePath = cf.findExistingPath("emisGridTemplatePath:") eGridTemplate = Egrid(eEdb,"name") if eGridTemplatePath[-4:] == ".asc": eGridTemplatePath=eGridTemplatePath[:-4] eGridTemplate.readData(eGridTemplatePath) eGridTemplate.substances = {} eGridTemplate.par["SUBSTANCE"].val = [] dd = {"key": None, "regstat": None, "regdef": None, "bu_sources": bu_sources, "psIndices": [], "units": natUnits, "rsrc": rsrc, "subdb": subdb, "trace": trace, "subgrpdb": subgrpdb } # Process all rows in the topdown table for row in tdTable.data: code = row[tdTable.colIndex["Code"]] active = row[tdTable.colIndex["Active"]] statType = row[tdTable.colIndex["Stat_type"]] if active == "no": continue log.info("Code: "+code) distributed=False # Add '-' to the code to reach max length (fix for a GUI bug) airviroCode = code # while len(airviroCode.split(".")) < codeDepth: # airviroCode += ".-" tdrow = tdTable.data[tdTable.rowIndex([code])] nrow = natTable.data[natTable.rowIndex([code])] # Create a resTable row to fill with data resrow = [None] * resTable.ncols resrow[0] = code # Check if national totals are non-zero nonZero = False for val in nrow: if val != None: if val > 0: nonZero = True break # Filter out indices for pointsources with the current ac # Also including sources coded with sub-codes # This allows to estimate top-down emissions on a higher code-level psIndices = [] for i, ps in enumerate(bu_sources): codeMatch = False for emis in ps.EMISSION: # It is assumed that the first code is used while processing topdown ac = emis.ACTCODE[0] if ac[-1] == ".": ac=ac[:-1] # if ac[:len(code)] == code: if ac == code: codeMatch = True break if not codeMatch: for emis in ps.SUBGRP: # It is assumed that the first code is used while processing topdown ac = emis.ACTCODE[0] if ac[:len(code)] == code: codeMatch = True break if codeMatch: psIndices.append(i) dd["psIndices"] = psIndices keyName = row[tdTable.colIndex["Key"]] #If no distribution key specified and no ps in bottom-up edb - cont. if keyName is None and psIndices == []: log.debug("No key and no point sources found for code: %s, skipping..." % code) resTable.addRow(resrow) continue if psIndices!=[]: msg = "--Found %i pointsources" % len(psIndices) log.info(msg) if keyName is not None: if keyName not in keys: log.error("No such key: " + keyName) sys.exit() msg = "--Key: %s" % keyName log.info(msg) keyGrid = Egrid(tdEdb, keyName) keyGrid.readData() log.debug("Read key: " + keyName + " from topdownEdb") # create emission grid to store distributed emissions eGrid = deepcopy(eGridTemplate) eGrid.name = code.replace(".", "_") eGrid.par["NAME"].val = code eGrid.par["INFO2"].val = "Distribution key: " + keyGrid.par["NAME"].val eGrid.par["ACTIVITYCODE"].val = [airviroCode.split(".")] regstatName = row[tdTable.colIndex["Regstat"]] regdefName = row[tdTable.colIndex["Regdef"]] if regstatName is not None: if regdefName is None: log.error("No region definition given for regional statistics: " + regstatName) sys.exit(1) regstatPath = path.join(dmn.domainPath(), "topdown", "regstat", regstatName) regstat = DataTable() log.info("regstatPath: "+regstatPath) regstat.read(regstatPath, units=True, defaultType=float, delimiter=";") if not "Geocode" in regstat.listIds(): log.error("No Geocode column found in regstat") sys.exit(1) regstat.convertCol("Geocode", int) regstat.keys.append("Geocode") # Making Geocode the primary key # create list of unique geo codes geocodes = [row[regstat.colIndex["Geocode"]] for row in regstat.data] geocodes = unique(geocodes) for colId in regstat.listIds(): if colId.lower() == "year": rows = [] regstat.convertCol(colId, int) # Make it possible to accumulate year regstat.setKeys(regstat.keys + [colId]) # Calculates the total emission for each geocode # in case there are multiple rows for different fuels etc colsToSum = regstat.listIds() colsToSum.remove(colId) colsToSum.remove("Geocode") for gc in geocodes: # sums all numeric values in colsToSum for # rows matching row id [gc,year] #returns an accumulated row and appends it to rows rowId = regstat.dict2RowId({"Geocode": gc, colId: year}) rows.append(regstat.accumulate(rowId, "sum", colsToSum)) regstat.data = rows # replace original rows with accumulated rows regstat.keys.remove(colId) break # dd["regstat"] = regstat regdef = Raster() regdefPath = path.join(dmn.domainPath(), "topdown", "regdef", regdefName) regdef.read(regdefPath) dd["regstat"] = regstat dd["regdef"] = regdef else: dd["regstat"] = None dd["regdef"] = None if dd["regstat"] is not None and len(bu_sources) > 0 and statType == "fixed": log.info("--Regionalizing pointsources") dd = regionalizePS(dd, code) if keyName is not None and nonZero: regionalizedDefault = False # Spatial distribution of emissions for subst in substances: sInd = subdb.substIndices[subst] toUnit = dd["units"][subst] + "/year" ntot = nrow[natTable.colIndex[subst]] pstot = 0 for i in dd["psIndices"]: source = dd["bu_sources"][i] # TODO: should give reference to subgrps to include emis from them pstot += source.get_emis( sInd, toUnit, eEdb, actcodes=[code] ) if ntot is None or ntot == 0: if pstot > 0: # 9999 is used as marker for no national total resrow[resTable.colIndex[subst]] = 9999.0 log.warning( "Nattot is 0 but ps tot is: %f %s" % (pstot, toUnit)) continue nrest = ntot - pstot resrow[resTable.colIndex[subst]] = 100.0 if abs(nrest / ntot) < 0.0001: nrest = 0 log.info( "--Rest is < 0.01 % of national total, rounded to zero" ) continue elif nrest < 0: log.warning( "--National rest is below zero, %4.2f proc for %s" % ( -1 * nrest / ntot * 100, subst) ) dd["trace"].write() # continue log.info( "---Substance: "+subst+ ", rest is: " + str(nrest) + toUnit + " = " + str(nrest / ntot * 100.0) + "%" ) try: keyRast = keyGrid.substances[sInd] except KeyError: keyRast = keyGrid.substances[subdb.substIndices["all"]] dd["key"] = keyRast if dd["regstat"] is not None: if (subst not in regstat.colIndex and sInd not in keyGrid.substances and not regionalizedDefault): dd = regionalizeKey(dd, subst, code) regionalizedDefault = True else: dd = regionalizeKey(dd, subst, code) emisRast = distribute(dd["key"], nrest) emisRast = emisRast * unitConvFac(toUnit, "ton/year") eGrid.addData(emisRast, dd["subdb"].substIndices[subst]) distributed = True else: # resTable is filled # In case all national totals are zero but there are ps for subst in substances: sInd = dd["subdb"].substIndices[subst] toUnit = dd["units"][subst] + "/year" ntot = nrow[natTable.colIndex[subst]] pstot = 0 for i in dd["psIndices"]: source = dd["bu_sources"][i] # subgrps are not used! pstot += source.get_emis(sInd, toUnit, buEdb, actcodes=[code]) if ntot!=0 and ntot is not None: resrow[resTable.colIndex[subst]] = pstot / ntot * 100.0 else: resrow[resTable.colIndex[subst]] = -999.0 if len(dd["psIndices"]) > 0: tmp_sources = (bu_sources[i] for i in dd["psIndices"]) with SourceStream(eEdb, mode='wb') as out_source_stream: source_writer = ModelWriter(out_source_stream) for source in tmp_sources: source_writer.write(source) log.debug("Wrote ps to emission edb") if distributed: eGrid.load() log.debug("Wrote emission grid to emission edb") dd["trace"].write() resTable.addRow(resrow) resTableFile = open(resTablePath,"w") resTable.write(resTableFile) log.info("Finished topdown process")
break for key in templateEmfacdb.activities: if not targetEmfacdb[key] == templateEmfacdb[key]: log.error("Edb's have incompatible emfacdb,"+ " cannot perform join including emission factors") if "activity_emis" not in skipPars: skipPars.append("activity_emis") break if not "activity_emis" in skipPars: log.debug("Emfacdb are identical in both target and template") log.debug("Reading searchkeys from target edb") targetSubdb=Subdb(targetEdb) targetSubdb.read() log.debug("Reading searchkeys from template edb") templateSubdb=Subdb(templateEdb) templateSubdb.read() if templateSubdb.undefined(): log.debug("No searchkeys defined in template edb, no transfer is done") elif targetSubdb.undefined(): log.info("Searchkey definition transfered from template to target edb") templateSubdb.setEdb(targetEdb) templateSubdb.write() elif targetSubdb!=templateSubdb: skipPars+=["SEARCHKEY1","SEARCHKEY2","SEARCHKEY3","SEARCHKEY4","SEARCHKEY5"] log.warning("Searchkey definitions are not identical in target"+