def read_sources(table_path, delimiter):
    table = DataTable()
    table.read(table_path, delimiter=delimiter)
    sources = {}
    for ind, row in enumerate(table.data):
        if row[table.colIndex['Sourcetype']] == 'Grid':
            continue
        src = Source(
            NAME=row[table.colIndex['Sourcename']],
            INFO=row[table.colIndex['SourceID']],
            X1=int(row[table.colIndex['ExtentWest']]),
            Y1=int(row[table.colIndex['ExtentSouth']]),
            CHIMNEY_HEIGHT=int(
                row[table.colIndex['Release Height (stack)']]),
            CHIMNEY_OUT=float(
                row[table.colIndex['StackOuterDim']]),
            CHIMNEY_IN=float(
                row[table.colIndex['StackInnerDim']]),
            GAS_FLOW=float(
                row[table.colIndex['GasSpeed']]),
            GASTEMPERATURE=int(
                row[table.colIndex['GasTemp']])
        )

        src.ALOB['HourlyID'] = row[table.colIndex['HourlyID']]
        src.ALOB['MonthlyID'] = row[table.colIndex['MonthlyID']]
        src.ALOB['Consumption'] = row[table.colIndex['Consumption']]
        src.ALOB['ProcessID'] = row[table.colIndex['ProcessID']]
        if int(row[table.colIndex['ExtentEast']]) != src.X1 and \
           int(row[table.colIndex['ExtentNorth']]) != src.Y1:
            src.X2 = int(row[table.colIndex['ExtentEast']])
            src.Y2 = int(row[table.colIndex['ExtentNorth']])
        sources[src.INFO] = src
    return sources
def read_emissions(sources, table_path, delimiter, substmap):
    table = DataTable()
    table.read(table_path, delimiter=delimiter)
    for row in table.data:
        try:
            src = sources[row[table.colIndex['SourceID']]]
        except KeyError:
            log.debug("Source does not exist, skipping")
            continue
        try:
            substance = substmap[int(row[table.colIndex['lSubstanceID']])]
        except KeyError:
            log.warning('Substance %i not found in substance-map')
            continue
        src.add_emission(
            SUBSTANCE=substance,
            EMISSION=float(row[table.colIndex['Emission']]),
            UNIT='ton/year'
        )
def read_timevars(sources, hour_table_path, month_table_path, delimiter):
    htable = DataTable()
    htable.read(hour_table_path, delimiter=delimiter)
    mtable = DataTable()
    mtable.read(month_table_path, delimiter=delimiter)

    hours = {}
    for row in htable.data:
        hours[row[htable.colIndex['HourID']]] = map(float, row[4:])
    months = {}
    for row in mtable.data:
        months[row[mtable.colIndex['MonthlyID']]] = map(float, row[4:])

    timevars = {}

    for src in sources.values():
        hourlyid = src.ALOB['HourlyID']
        monthlyid = src.ALOB['MonthlyID']
        name = '_'.join([monthlyid, hourlyid])
        if name in timevars:
            continue
        timevars[name] = Timevar(NAME=name)
        tvar = timevars[name]
        tvar.TYPEDAY[:, 0] = hours[hourlyid][:24]
        tvar.TYPEDAY[:, 1] = hours[hourlyid][:24]
        tvar.TYPEDAY[:, 2] = hours[hourlyid][24:48]
        tvar.TYPEDAY[:, 3] = hours[hourlyid][48:72]
        tvar.MONTH[0, :] = months[monthlyid][0:6]
        tvar.MONTH[1, :] = months[monthlyid][6:]

    return timevars
Exemple #4
0
def regionalizePS(dd, code):
    psIndices = dd["psIndices"]
    bu_sources = dd["bu_sources"]
    regdef = dd["regdef"]
    regstat = dd["regstat"]
    subdb = dd["subdb"]
    rsrc = dd["rsrc"]
    trace = dd["trace"]

    # Table to record all remaning emissions after subtracting point source emissions
    # The table is only meant for quality control
    
    restTable = DataTable(desc=[{"id": "Code", "type": int}])
    restTable.setKeys(["Code"])

    
    # Listing substances and corresponding units in regstat
    units = {}
    for desc in regstat.desc:
        if desc["id"].lower() not in ["geocode", "year"]:
            units[desc["id"]] = desc["units"]

            # Adding columns for all relevant substances in rest table
            restTable.addCol({"id": desc["id"],
                              "units": "%",
                              "type": float})
            
            

    # Finding the regId for each pointsource
    for psInd in psIndices:
        ps = bu_sources[psInd]
        try:
            (row, col) = regdef.getIndex(ps.par["X1"].val, ps.par["Y1"].val)
        except ValueError, e:
            raise ValueError("While extracting gc from regdef: " + str(e))
        ps.regId = int(regdef.data[row,col])
def read_subgrps(comb_table_path, comb_def_table_path, delimiter, substmap):
    combtbl = DataTable()
    combtbl.read(comb_table_path, delimiter=delimiter)

    if path.exists(comb_def_table_path):
        deftbl = DataTable()
        deftbl.read(comb_def_table_path, delimiter=delimiter)
        processes = {}
        for row in deftbl.data:
            processes[int(row[deftbl.colIndex['CombTypeID']])] = \
                row[deftbl.colIndex['Combustion process name']]
    else:
        processes = None

    subgrps = {}
    for row in combtbl.data:
        processid = int(row[combtbl.colIndex['ProcessID']])
        log.debug('Processid: %i' % processid)
        if processid not in subgrps:
            if processes is not None:
                name = processes[processid]
            else:
                name = 'unknown'
            subgrps[processid] = Subgrp(NAME=name)
        subgrp = subgrps[processid]

        enviman_subst_key = int(
            row[combtbl.colIndex['lSubstanceID']]
        )

        try:
            substance = substmap[enviman_subst_key]
        except KeyError:
            log.warning(
                "Substance " +
                "%i not found in substance mapping" % enviman_subst_key
            )
            continue

        subgrp.add_substance(
            substance=substance,
            slope=100 * float(row[combtbl.colIndex['FracEmission']]),
            unit='%'
        )
    return subgrps
Exemple #6
0
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option("-u",'--user',
                      action="store",dest="user",
                      help="Name of target edb user")

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

    parser.add_option("-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("-d",
                      "--doc",
                      action="store_true",
                      dest="doc",
                      help="Prints more detailed documentation and exit")

    parser.add_option("-v",
                      action="store_const",
                      const=logging.DEBUG,
                      dest="loglevel",
                      default=get_loglevel(),
                      help="Produce verbose output")

    parser.add_option("-o",
                      "--output",
                      action="store",
                      dest="outfileName",
                      default=None,
                      help="Output file")

    parser.add_option("-i",
                      "--i",
                      action="store",
                      dest="infile",
                      help="Raster to be regionalized")

    parser.add_option("-r",
                      "--regdef",
                      action="store",
                      dest="regdef",
                      help="Raster defining the regions found " +
                      "in statistics table")

    parser.add_option("-s",
                      "--stat",
                      action="store",
                      dest="stat",
                      help="Tab-separated statistics table, " +
                      "with 1st column representing geocode and value " +
                      "representing region sum")

    parser.add_option("-c",
                      "--colname",
                      action="store",
                      dest="colname",
                      help="Header of column in stat table to fetch " +
                      "region sums from")

    (options, args) = parser.parse_args()

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

    # ------------Process and validate options---------------
    if options.doc:
        print __doc__
        sys.exit()

    if len(args) > 0:
        parser.error("Incorrect number of arguments")

    regdef = Raster()
    if options.regdef is not None:
        regdef.read(options.regdef)
    else:
        log.error("No region definition raster specified")
        sys.exit(1)

    key = Raster()
    if options.infile is not None:
        key.read(options.infile)
    else:
        log.info("No initial distribution raster given, using " +
                 "homogenious distribution")
        key.assign(regdef)
        key.data = np.where(regdef.data != regdef.nodata, 1, regdef.nodata)

    if options.stat is None:
        log.error("No statistics table specified")
        sys.exit(1)

    if (regdef.xll != key.xll or regdef.yll != key.yll):
        log.error("The raster domain size is differs between key raster " +
                  "and region raster")

    stat = DataTable()
    stat.read(options.stat, defaultType=float)
    print stat.desc
    stat.convertCol(stat.desc[0]["id"], int)
    print stat.desc
    # Set first column as unique id for rows
    stat.setKeys([stat.desc[0]["id"]])

    # Remove nodata in rasters
    key.nodataToZero()
    regdef.nodataToZero()

    log.info("Consistency och completeness check for codes " +
             "in regdef and statistics")
    # Create list of codes in raster
    regdefCodes = regdef.unique()
    regdefCodes = map(int, regdefCodes)

    if 0.0 in regdefCodes:
        regdefCodes.remove(0.0)

    statRegCodes = [row[0] for row in stat.data]

    if options.colname is not None:
        try:
            col = stat.colIndex[options.colname]
        except KeyError:
            log.error("No column named " +
                      "%s found in statistics table" % options.colname)
            sys.exit(1)
    else:
        col = 1
    colId = stat.desc[col]["id"]

    errFound = False
    for code in statRegCodes:
        # Assuring that the regional codes are present in regdef
        if code not in regdefCodes:
            log.error("Input Error: code:" + str(int(code)) +
                      "in reg totals is not represented in regional raster")
            errFound = True

    # Assuring that the regional raster IDs are present
    # in the regional statistics
    for code in regdefCodes:
        if code not in statRegCodes:
            log.error("Input Error: ID:" + str(code) +
                      " in region raster is not represented in " +
                      "regional totals!")
            errFound = True

    if errFound:
        sys.exit(1)

    # For all regions, calculate the regional sum,
    # set the key values to key/regSum
    res = np.zeros(key.data.shape)
    log.info("Regionalizing key raster")
    for code in regdefCodes:
        emis = stat.lookup(colId, code)
        log.debug("Statistics for reg %i: %f" % (code, emis))
        mask = np.array(regdef.data == code)
        regKey = mask * key.data
        regSum = np.sum(np.sum(regKey))
        if regSum > 0:
            res = res + emis * regKey / regSum
        else:
            log.warning("Distribution key is zero for geocode" +
                        " %i, using homogenuous distribution for this region" %
                        code)
            res = res + emis * mask / np.sum(np.sum(mask))

    key.data = res

    # resultRast=(totFractionRast*key)*regionalTotals.regSum(substance)
    key.write(options.outfileName)
    log.info("Result with sum: %f written" % res.sum())
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option("-u",'--user',
                      action="store",dest="user",
                      help="Name of target edb user")

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

    parser.add_option("--begin",
                      action="store",dest="begin",
                      help="YYMMDDHH to begin calculation")

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

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

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

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

    (options, args) = parser.parse_args()


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

    if options.user is None:
        log.error("Need to specify -u <user>")
        return 1
    if options.edb is None:
        log.error("Need to specify -e <edb>")
        return 1
    if options.begin is None:
        log.error("Need to specify --begin <YYMMDDHH>")
        return 1
    if options.end is None:
        log.error("Need to specify --end <YYMMDDHH>")
        return 1
    if options.MMSIfile is None:
        log.error("Need to specify --MMSI <mmsi-file>")
        return 1        
    if options.macro is None:
        log.error("Need to specify --macro <macro-file>")
        return 1
        
    dmn=Domain()
    try:
        edb = Edb(dmn,options.user,options.edb)
    except:
        log.error("No such edb found")
        return 1

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


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

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

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

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

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

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

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

    log.info("Finished!")
    return 0
def main():
    # -----------Setting up and unsing option parser-----------------------
    parser = OptionParser(usage=usage, version=version)

    parser.add_option("-d", "--doc",
                      action="store_true", dest="doc",
                      help="Prints more detailed documentation and exit")

    parser.add_option("-v",
                      action="store_const", const=logging.DEBUG,
                      dest="loglevel", default=get_loglevel(),
                      help="Produce verbose output")

    parser.add_option("-o", "--output",
                      action="store", dest="outfileName", default=None,
                      help="Output file")

    parser.add_option("-i", "--i",
                      action="store", dest="infile",
                      help="Raster to be regionalized")

    parser.add_option("-r", "--regdef",
                      action="store", dest="regdef",
                      help="Raster defining the regions found " +
                      "in statistics table")

    parser.add_option("-s", "--stat",
                      action="store", dest="stat",
                      help="Tab-separated statistics table, " +
                      "with 1st column representing geocode and value " +
                      "representing region sum")

    parser.add_option("-c", "--colname",
                      action="store", dest="colname",
                      help="Header of column in stat table to fetch " +
                      "region sums from")

    (options, args) = parser.parse_args()

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

    # ------------Process and validate options---------------
    if options.doc:
        print __doc__
        sys.exit()

    if len(args) > 0:
        parser.error("Incorrect number of arguments")

    regdef = Raster()
    if options.regdef is not None:
        regdef.read(options.regdef)
    else:
        log.error("No region definition raster specified")
        sys.exit(1)

    key = Raster()
    if options.infile is not None:
        key.read(options.infile)
    else:
        log.info("No initial distribution raster given, using " +
                 "homogenious distribution")
        key.assign(regdef)
        key.data = np.where(regdef.data != regdef.nodata, 1, regdef.nodata)

    if options.stat is None:
        log.error("No statistics table specified")
        sys.exit(1)

    if (regdef.xll != key.xll or regdef.yll != key.yll):
        log.error("The raster domain size is differs between key raster " +
                  "and region raster")

    stat = DataTable()
    stat.read(options.stat, defaultType=float)
    print stat.desc
    stat.convertCol(stat.desc[0]["id"], int)
    print stat.desc
    # Set first column as unique id for rows
    stat.setKeys([stat.desc[0]["id"]])

    # Remove nodata in rasters
    key.nodataToZero()
    regdef.nodataToZero()

    log.info("Consistency och completeness check for codes " +
             "in regdef and statistics")
    # Create list of codes in raster
    regdefCodes = regdef.unique()
    regdefCodes = map(int, regdefCodes)

    if 0.0 in regdefCodes:
        regdefCodes.remove(0.0)

    statRegCodes = [row[0] for row in stat.data]

    if options.colname is not None:
        try:
            col = stat.colIndex[options.colname]
        except KeyError:
            log.error("No column named " +
                      "%s found in statistics table" % options.colname)
            sys.exit(1)
    else:
        col = 1
    colId = stat.desc[col]["id"]

    errFound = False
    for code in statRegCodes:
        # Assuring that the regional codes are present in regdef
        if code not in regdefCodes:
            log.error("Input Error: code:" + str(int(code)) +
                     "in reg totals is not represented in regional raster")
            errFound = True

    # Assuring that the regional raster IDs are present
    # in the regional statistics
    for code in regdefCodes:
        if code not in statRegCodes:
            log.error("Input Error: ID:" + str(code) +
                     " in region raster is not represented in " +
                      "regional totals!")
            errFound = True

    if errFound:
        sys.exit(1)

    # For all regions, calculate the regional sum,
    # set the key values to key/regSum
    res = np.zeros(key.data.shape)
    log.info("Regionalizing key raster")
    for code in regdefCodes:
        emis = stat.lookup(colId, code)
        log.debug("Statistics for reg %i: %f" % (code, emis))
        mask = np.array(regdef.data == code)
        regKey = mask * key.data
        regSum = np.sum(np.sum(regKey))
        if regSum > 0:
            res = res + emis * regKey / regSum
        else:
            log.warning(
                "Distribution key is zero for geocode" +
                " %i, using homogenuous distribution for this region" % code)
            res = res + emis * mask / np.sum(np.sum(mask))

    key.data = res

    # resultRast=(totFractionRast*key)*regionalTotals.regSum(substance)
    key.write(options.outfileName)
    log.info("Result with sum: %f written" % res.sum())
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!")
Exemple #11
0
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")
def main():
    #-----------Setting up and using option parser-----------------------
    parser=OptionParser(usage= usage, version=version)

    parser.add_option(
        '-v',
        action='store_const', dest='loglevel',
        const=logging.DEBUG, default=get_loglevel(),
        help='produce verbose output'
    )
#     parser.add_option("-l", "--loglevel",
#                       action="store",dest="loglevel",default=2,
#                       help="Sets the loglevel (0-3 where 3=full logging)")
    
    parser.add_option("-t", "--template",
                      action="store",dest="cf",default=None,
                      help="Generate default controlfile") 
    
    (options, args) = parser.parse_args()

    #------------Setting up logging capabilities -----------
    logging.basicConfig(
        format='%(levelname)s:%(name)s: %(message)s',
        level=options.loglevel,
    )
    global log
    log = logging.getLogger(parser.prog)
    
    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")

    #Opening controlfile
    cf=ControlFile(fileName=path.abspath(args[0]))
    domain = Domain()
    edbName=cf.findString("edb:")
    userName=cf.findString("user:"******"Edb "+edbName+" does not exist for user "+userName+" in domain "+domain.name)
        sys.exit()

    year=cf.findInt("year:")
    substList=cf.findStringList("substances:")

    #Remove doubles from substance list
    substances={'nat':[]}
    for subst in substList:
        if subst not in substances['nat']:
            substances['nat'].append(subst)


    #Make dictionary for substance,units couples
    unitDict={}
    defaultUnit=cf.findString("nationalTotals.defaultUnit:")
    unitList=cf.findStringList("nationalTotals.units:")
    for i in range(len(unitList)):
        unitSubstances=cf.findStringList("nationalTotals.unit."+str(i)+":")
        for s in unitSubstances:
            unitDict[s]=unitList[i]
    for subst in substances['nat']:
        if subst not in unitDict.keys():
            unitDict[subst]=defaultUnit

    nationalTotalsTablePath=cf.findString("nationalTotalsTable:")
    nationalTotalsFile=codecs.open(nationalTotalsTablePath,'w',encoding="latin6")

    nationalTotalsTreePath=cf.findString("nationalTotalsTree:")
    nationalTotalsTreePath=path.abspath(nationalTotalsTreePath)
    nationalTotalTreeFile=open(nationalTotalsTreePath,'w')
    
    log.info("Reading emissions from CRF")
    CRFTree=CodeEmisTree(name="CRF")
    CRFPath=cf.findExistingPath("CRF.path:")
    CRFTree.parseCRFXML(CRFPath,year)
    CRFTree.calculateSums(byAttribute="Category")
    #CRFTree.write(sys.stdout)
    CRFTable=CRFTree.createTable(keys=["Category","Classification","AWMS"])
    HFC={
        "HFC-23":11700,
        "HFC-32":650,
        "HFC-41":150,
        "HFC-43-10 mee":1300,
        "HFC-125":2800,
        "HFC-134":1000,
        "HFC-134a":1300,
        "HFC-152a":140,
        "HFC-143":300,
        "HFC-143a":3800,
        "HFC-227ea":2900,
        "HFC-236fa":6300,
        "HFC-245ca":560
        }
    
    PFC={
        "CF4":6500,
        "C2F6":9200,
        "C3F8":7000,
        "C4F10":7000,
        "c-C4F8":8700,
        "C5F12":7500,
        "C6F14":7400
        }
    
    CRFTable.aggregateCols({"id":"HFC-tot","type":"str","units":"Gg"},HFC)
    CRFTable.aggregateCols({"id":"PFC-tot","type":"str","units":"Gg"},PFC)
    CRFTable.sortRows()
    CRFTableKeys=cf.findStringList("CRF.tableKeys:")
    CRFTable.keys=CRFTableKeys
    #CRFTable.write(sys.stdout)
    
    log.info("Reading emissions from NFR")
    NFRTree=CodeEmisTree(name="NFR")
    NFRPath=cf.findExistingPath("NFR.path:")
#     pdb.set_trace()
    NFRTree.parseNFRTable(filename=NFRPath)
    NFRTree.calculateSums()
    NFRTable=NFRTree.createTable()
    NFRTableKeys=cf.findStringList("NFR.tableKeys:")
    NFRTable.keys=NFRTableKeys
    colIndex=NFRTable.getColIndex("Code")
    for row in NFRTable.data:
        if isinstance(row[colIndex],str):
            row[colIndex]=row[colIndex].replace(" ","")

    log.info("Reading emissions from Mobile")
    MobilePath=cf.findExistingPath("Mobile.path:")
    MobileTable=DataTable()
    MobileTable.read(MobilePath,units=True)
    MobileTable=MobileTable.filtered({"Year":str(year)})
    MobileTableKeys=cf.findStringList("Mobile.tableKeys:")
    MobileTable.keys=MobileTableKeys
    #Remove biogenic emissions of CO2
    #Remove GHG from Bunkers, except for international aviation LTO
    for row in MobileTable.data:
#        if "Bio" in row[MobileTable.getColIndex("Codename")]:
        if "Bio" in row[MobileTable.getColIndex("Fueltype")]:
            row[MobileTable.getColIndex("CO2")]=None

#        if row[MobileTable.getColIndex("Localcodename")]=="Bunkers" and row[MobileTable.getColIndex("Localsubcodename")]!="LTO":
        if row[MobileTable.getColIndex("Subsector")]=="Bunkers" and row[MobileTable.getColIndex("Region")]!="LTO":
            row[MobileTable.getColIndex("CO2")]=None
            row[MobileTable.getColIndex("CH4")]=None
            row[MobileTable.getColIndex("N2O")]=None


    log.info("Reading emissions from Manual")
    ManualPath=cf.findExistingPath("Manual.path:")
    ManualTable=DataTable()
    ManualTable.read(ManualPath,units=True)
    ManualTableKeys=cf.findStringList("Manual.tableKeys:")
    ManualTable.keys=ManualTableKeys



    srcTables={'NFR':NFRTable,
               'CRF':CRFTable,
               'Mobile':MobileTable,
               'Manual':ManualTable}

    for tKey in srcTables.keys():
        substList=cf.findStringList(tKey+".substances:")
        substances[tKey]=substList    
    
    log.info("Reading dataSourceTable")
    dsPath=cf.findExistingPath("sourceTable:")
    dsTable=DataTable()
    dsTable.read(dsPath)

    acIndex=cf.findInt("nationalTotals.acIndex:")

    log.info("Reading activity codes from edb")
    natTree=CodeEmisTree("National totals","Gg")
    natTree.readActivityCodes(path.join(edb.edbPath(),"edb.rsrc"),acIndex)    

    log.info("Identifying column indices for sources in dataSourceTable")

    #codeHeaders={"Mobile":"Mobile CRFnodeprefix",
    #             "NFR":"NFR Code",
    #             "CRF":"CRF Code",
    #             "nat":"Code",
    #             "Manual":"Code"
    #             }

    codeHeaders={"Mobile":"Mobile UNFCCC",
                 "NFR":"NFR Code",
                 "CRF":"CRF Code",
                 "nat":"Code",
                 "Manual":"Code"
                 }
    
    codeInd={}
    for key,colName in codeHeaders.iteritems():        
        try:
            codeInd[key]=dsTable.getColIndex(colName)
        except DataTableException:
            log.error("Could not find '"+colName+"' in dataSourceTable")
            sys.exit()
        
    log.info("Collecting data from data sources")
    prevNatCode=""
    for row in dsTable.data:
        rowId={}
        srcRow={}
        for key in srcTables:
            rowId[key]=[]
            srcRow[key]=srcTables[key].ncols*[0]

        natCode=row[codeInd['nat']]
        if natCode!=prevNatCode:
            log.debug("\nProcessing: "+natCode)
            prevNatCode=natCode


        #Get row-id for each src table
        for tKey in srcTables.keys():
            colInd=codeInd[tKey]
            if row[colInd]!=None:
                for key in srcTables[tKey].keys:
                    try:
                        idPart=row[dsTable.getColIndex(tKey+" "+key)]
                    except ValueError:
                        log.error("No column named '"+tKey+" "+key+"' found in dataSourceTable")
                        sys.exit()
                    rowId[tKey].append(idPart)
                #If not all key values = None
                nonNones=[val for val in rowId[tKey] if val !=None]
                if len(nonNones)==0:
                    rowId[tKey]=None
                    srcRow[tKey]=None
                else:
#                     if tKey=="CRF":
#                         pdb.set_trace()
                    try:
                        srcRow[tKey]=srcTables[tKey].accumulate(rowId[tKey])
                    except DataTableException, err:
#                         import pdb; pdb.set_trace()
                        log.error("While trying to get data from "+tKey+" table: "+str(err))
                        sys.exit()


        #Creating code tree path for the current code
        codeParts=natCode.split(".")
        natPath=""
        for i in range(1,len(codeParts)+1):
            natPath+=".".join(codeParts[:i])
            if i < len(codeParts):
                natPath+="/"               
        natNode=natTree.root.find(natPath)
        if natNode is None:
#             import pdb; pdb.set_trace()
            log.error("Could not find national code path : "+natCode)
            sys.exit()

        for subst in substances['nat']:
            log.debug("Substance: "+subst)
            storedSubstances=[]
            foundSubstances=[]
            rec=None

            #Looping over all sourceTables
            for tKey in srcTables:
                st=srcTables[tKey]
                if rowId[tKey]!=None and subst in substances[tKey]:
                    try:
                        colIndex=st.getColIndex(subst)
                    except DataTableException:
                        log.error("Could not find column %s in table %s" %(subst,tKey)) 
                    emis=srcRow[tKey][colIndex]
                    unit=st.desc[colIndex].get('units',None)
                    storedSubstances.append(subst)
                    nodeExists=False
                    if emis!=0 and emis!=None:
                        if subst not in foundSubstances:
                            foundSubstances.append(subst)
                        else:
                            log.error("Substance found in multiple datasources for "+natCode)
                            sys.exit()
                        for node in natNode:
                            if node.tag=="Record" and node.attrib.get("substance",None)==subst:
                                rec=node
                                nodeExists=True
                                break
                        if not nodeExists:                        
                            rec=ET.SubElement(natNode,"Record")
                        break
                    if unit==None:
                        log.error("No units set for substance "+subst+" in source table "+tKey)
                        sys.exit()
            if storedSubstances==[]:
                log.error("No data source specified for substance "+subst)
                sys.exit()

            if rec!=None:
                if emis!=None and emis!=0:
                    try:
                        emis=convertUnits(fromUnit=unit,toUnit=unitDict[subst],val=emis)
                    except TypeError:
                        emis=0
                        
                    if not nodeExists:
                        rec.attrib["substance"]=subst
                    else:
                        emis+=float(rec.attrib["emission"])
                    rec.attrib["emission"]=str(emis)