Ejemplo n.º 1
0
def main():
    # Create the domain
    domain = Domain()

    # Parse command line arguments
    parser = argparse.ArgumentParser(description=__doc__)
    utils.add_standard_command_options(parser)
    utils.add_edb_command_options(parser)

    parser.add_argument(
        '-F',
        action="store_true", dest='force',
        help="Force - overwrite target edb"
    )

    parser.add_argument(
        '--delimiter', default='\t',
        action="store", dest='delimiter',
        help='Delimiter used in enviman csv-tables'
    )

    parser.add_argument(
        '-i',
        action="store", dest='indir',
        help=("Directory containing enviman tables in .csv format," +
              " named the same as Enviman MS Excel sheets, " +
              "but with space replaced by '_'")
    )

    parser.add_argument(
        '--substancemap',
        action=MappingAction, dest='substancemap',
        help=("File mapping substance indices from enviman to Airviro")
    )

    parser.add_argument(
        '--source_srs', metavar="EPSG", type=int,
        action='store', dest='source_srs',
        help='Coordinate system of enviman data'
    )

    parser.add_argument(
        '--target_srs', metavar="EPSG", type=int,
        action='store', dest='target_srs',
        help='Coordinate system of Airviro data'
    )

    args = parser.parse_args()
    edb = Edb(domain, args.user, args.edb)

    PAG_Source_path = path.join(args.indir, 'PAG_Source.csv')
    AnEm_Table_path = path.join(args.indir, 'AnEm_Table.csv')
    Mon_Table_path = path.join(args.indir, 'Mon_Table.csv')
    Hour_Table_path = path.join(args.indir, 'Hour_Table.csv')
    Comb_Table_path = path.join(args.indir, 'Comb_Table.csv')
    Comb_Def_path = path.join(args.indir, 'Comb_Def.csv')

    subgrp_ind = get_max_subgrp_index(edb) + 1
    timevar_ind = get_max_timevar_index(edb) + 1

    if path.exists(PAG_Source_path):
        sources = read_sources(
            PAG_Source_path,
            args.delimiter
        )
    else:
        sources = None
        log.warning('PAG_Source.csv not found')

    if args.source_srs is not None and args.target_srs is not None:
        log.debug("Preparing spatial reference transformation")
        target_srs = osr.SpatialReference()
        source_srs = osr.SpatialReference()
        target_srs.ImportFromEPSG(args.target_srs)
        source_srs.ImportFromEPSG(args.source_srs)
        srs_transform = osr.CoordinateTransformation(source_srs, target_srs)
        for sourceid, source in sources.iteritems():
            transform(source, srs_transform)

    else:
        log.debug("No spatial reference transformation will be performed")

    if path.exists(AnEm_Table_path) and sources is not None:
        read_emissions(
            sources,
            AnEm_Table_path,
            args.delimiter,
            args.substancemap
        )
    else:
        log.warning('AnEmi.csv not found')

    if path.exists(Comb_Table_path):
        subgrps = read_subgrps(
            Comb_Table_path,
            Comb_Def_path,
            args.delimiter,
            args.substancemap
        )

    else:
        log.warning('Comb_Table.csv not found')

    if path.exists(Hour_Table_path) and path.exists(Mon_Table_path):
        timevars = read_timevars(
            sources,
            Hour_Table_path,
            Mon_Table_path,
            args.delimiter
        )
    else:
        timevars = None

    for key, subgrp in subgrps.iteritems():
        subgrp.INDEX = subgrp_ind
        subgrp_ind += 1

    for key, tvar in timevars.iteritems():
        tvar.INDEX = subgrp_ind
        timevar_ind += 1

    for src in sources.values():
        if timevars is not None:
            timevarid = '_'.join([src.ALOB['MonthlyID'], src.ALOB['HourlyID']])
            timevarind = timevars[timevarid].INDEX
        else:
            timevarind = 1

        unwritten_subgroups = False
        if src.ALOB['Consumption'] != '0':
            if subgrps is not None:
                subgrp = subgrps[int(src.ALOB['ProcessID'])]
                src.add_subgrp(
                    SUBGRP=subgrp.INDEX,
                    ACTIVITY=float(src.ALOB['Consumption']),
                    TIMEVAR=timevarind,
                    UNIT='ton/year'
                )
            elif not unwritten_subgroups:
                log.warning(
                    "Could not add substance group emissions " +
                    "due to missing Comb Table"
                )
                unwritten_subgroups = True

        for emis in src.EMISSION:
            emis.TIMEVAR = timevarind

    if timevars is not None:
        with TimevarStream(edb, mode='w', sourcetype='point') as outstream:
            log.info('Writing timevars...')
            writer = ModelWriter(outstream)
            for tvar in timevars.values():
                log.debug('Writing timevar %i' % tvar.INDEX)
                writer.write(tvar)

    if subgrps is not None:
        with codecs.open(
            '/usr/airviro/data/SCAC/prod/industry/enviman_subgrps.txt',
            mode='w',
            encoding='HP Roman8') as outstream:
#        with SubgrpStream(edb, mode='w') as outstream:
            log.info('Writing subgrps...')
            writer = ModelWriter(outstream)
            for subgrp in subgrps.values():
                log.debug('Writing subgrps %i' % subgrp.INDEX)
                writer.write(subgrp)
    with codecs.open(
        '/usr/airviro/data/SCAC/prod/industry/enviman_sources.txt',
        mode='w',
        encoding='HP Roman8') as outstream:

#    with SourceStream(edb, mode='w') as outstream:
        log.info('Writing sources...')
        writer = ModelWriter(outstream)
        for src in sources.values():
            log.debug('Writing source %s' % src.NAME)
            writer.write(src)
Ejemplo n.º 2
0
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser = argparse.ArgumentParser(description=__doc__)
    utils.add_standard_command_options(parser)
    utils.add_edb_command_options(parser)

    parser.add_argument("-o","--outfile",
                      action="store",dest="filename",default=None,
                      help="Name of output file with sources that fail the verification")

    args = parser.parse_args()

    if args.filename is None:
        parser.error("Need to specify output file for verification errors")

    outfile=codecs.open(path.abspath(args.filename),"w","HP Roman8")

    #Get current domain
    domainName=os.environ["AVDBNAME"]
    dmn = domain.Domain(domainName)
    
    if args.source_edb ==None:
        parser.error("Need to specify edb")
    if args.source_user ==None:
        parser.error("Need to specify user")

    #Check if edb exist
    if not dmn.edbExistForUser(args.source_edb,args.source_user):
        log.error("Edb "+args.source_edb+" does not exist for user "+args.source_user+" in domain "+domainName)    
        sys.exit()

    edb=Edb(dmn,args.source_user,args.source_edb)
    rsrc = edb.rsrc
    source_stream=SourceStream(edb,mode='r')

    source_reader = ModelReader(source_stream)

    nerrors=0
    for src in source_reader:
        log.debug("Looking at src")
        err=False       
        gc=src.GEOCODE

        if len(gc) !=len(rsrc.gc):
            log.info(
                "Wrong number of geocodes, source: %s" %src.NAME)
            err=True
            nerrors+=1
        
        else:
            #Check if each code exists in its corresponding code tree
            for i,code in enumerate(gc):                
                if not rsrc.gc[i].hasCode(code):
                    log.info("Code: %s in source %s not found in edb.rsrc" %(
                            code,src.NAME))
                    nerrors+=1
                    err=True
                
        for emis in chain(src.EMISSION,src.SUBGRP,src.ACTIVITY):
            ac=emis.ACTCODE
            log.debug("AC: "+str(ac))
            
            if len(ac) !=len(rsrc.ac):
                log.info(
                    "Wrong number of activity codes, source: %s" %src.NAME)
                err=True
                nerrors+=1

            else:
                #Check if each code exists in its corresponding code tree
                for i,code in enumerate(ac):
                    if code[-1] == '.':
                        code=code[:-1]
                    if not rsrc.ac[i].hasCode(code):
                        log.info("Code: %s in source %s not found in rsrc" %(
                                code,src.NAME))
                        nerrors+=1
                        err=True            

        if err:
            outfile.write(src.NAME)
            outfile.write("\n")

    log.info("Number of errors found: %i" %nerrors)