Exemple #1
0
def main():
    var_list = ('ATPRO_HOURLY', 'ATPRO_WEEKLY', 'ATPRO_MONTHLY', 'ATREF',
                'EMISINV_A', 'SOURCE_GROUPS', 'COSTCY', 'POLY_FILE',
                'INVTABLE', 'WORK_PATH', 'GRIDDESC', 'REGION_IOAPI_GRIDNAME',
                'STATE_FIPS')
    check_env_vars(var_list)
    inv_list = get_inv_list()
    state_fips = os.environ['STATE_FIPS']
    invtable = get_invtable(os.environ['INVTABLE'])
    inv = AnnualFF10(inv_list, invtable, state_fips, use_shapes=True)
    src_groups = SourceGroups(os.environ['SOURCE_GROUPS'])
    # Read in the shapes files and drop ones that aren't needed
    shapes = read_shapes(os.environ['POLY_FILE'])
    emis = label_emis(
        inv.emis, shapes[['facid', 'shape_id',
                          'area_frac']].drop_duplicates('facid'))
    emis_id = emis[['facid', 'src_id',
                    'region_cd']].drop_duplicates(['facid', 'src_id'])
    shapes = pd.merge(shapes, emis_id, on='facid', how='left')
    shapes = shapes[shapes['src_id'].notnull()].copy()
    met_grid = Grid(os.environ['REGION_IOAPI_GRIDNAME'],
                    os.environ['GRIDDESC'])
    shapes = proc_shapes(shapes, met_grid)
    write_emis(emis.copy(), src_groups)
    write_locations(shapes.copy())
    write_parameters(shapes)
    # Temporalization
    scc_list = list(emis['scc'].drop_duplicates())
    fips_list = list(emis['region_cd'].drop_duplicates())
    temp = Temporal(os.environ['ATREF'], os.environ['ATPRO_HOURLY'],
                    os.environ['ATPRO_WEEKLY'], os.environ['ATPRO_MONTHLY'],
                    scc_list, fips_list)
    temp_profs = calc_monthly_temp(emis.drop_duplicates(['facid', 'src_id']),
                                   temp)
    write_temporal(temp_profs)
Exemple #2
0
def proc_point_sector(inv_list, grid_name, grid_desc, state_fips):
    '''
    Process a point sector
    '''
    from smk2ae.point_io import get_temp_codes, proc_points, get_grid_location
    from smk2ae.pt_ff10 import AnnualFF10
    grid_info = Grid(grid_name, grid_desc)
    var_list = ('PTPRO_HOURLY','PTPRO_WEEKLY','PTPRO_MONTHLY','PTREF','REPORT_PATH','CASE')
    check_env_vars(var_list)
    init_paths(os.environ['WORK_PATH'], ['xwalk','qa','locations','parameters','temporal'])
    inv = AnnualFF10(os.environ['SECTOR'], inv_list, os.environ['HAPS_LIST'], state_fips)
    if inv.stk.empty:
        raise ValueError, 'No facilities found containing HAPS. Check state list and inventories.'
    inv.stk['facility_name'] = '"' + inv.stk['facility_name'] + '"'
    inv.stk = insert_states(inv.stk, os.environ['COSTCY'])
    if state_fips:
        inv.state = inv.stk.ix[inv.stk['stfips'] == inv.st_fips, 'state'].drop_duplicates().values[0]
        print 'Running for state: %s' %inv.state
    from smk2ae.hourly_rep import HourlyReports 
    hrly = HourlyReports(os.environ['SECTOR'], os.environ['CASE'], os.environ['BASE_YEAR'], 
        os.environ['REPORT_PATH'], state_fips)
    from smk2ae.temporal import Temporal
    temp = Temporal(os.environ['PTREF'],os.environ['PTPRO_HOURLY'],os.environ['PTPRO_WEEKLY'],
        os.environ['PTPRO_MONTHLY'], inv.scc_list, inv.fips_list, inv.fac_list)
    inv.stk = get_temp_codes(inv.stk, temp.xref.copy())
    if not hrly.egu_units.empty:
        print inv.stk[:5]
        print hrly.egu_units[:5]
        inv.stk = pd.merge(inv.stk, hrly.egu_units, on='unit_id', how='left')
        # This is in here to keep EGU units unique temporally
        inv.stk.ix[inv.stk['uniq'] == 'Y', 'ALLDAY'] = -1
        inv.stk.ix[inv.stk['uniq'] == 'Y', 'WEEKLY'] =  inv.stk.ix[inv.stk['uniq'] == 'Y', 
          'unit_id'].astype('i')
        inv.stk.ix[inv.stk['uniq'] == 'Y', 'MONTHLY'] =  inv.stk.ix[inv.stk['uniq'] == 'Y', 
          'rel_point_id'].astype('i')
    # Process airports
    try:
        os.environ['AIRPORT_LOCS']
    except KeyError:
        print 'WARNING: No Airport Locations File defined'
    else:
        from smk2ae.airports import Airports
        air = Airports(os.environ['AIRPORT_LOCS'])
        air.match_airport_inv(inv)
        inv.drop_facilities(air.fac_list)
        if air.fac_list:
            air.calc_cell_xref(grid_info)
            air.write_runways(grid_info, temp.aermod)
            air.write_no_runways(grid_info, temp.aermod)
    # Process all other point sources
    if inv.fac_list:
        inv.stk = get_grid_location(inv.stk, grid_info)
        proc_points(inv, temp, hrly, os.environ['WORK_PATH'], grid_info)
Exemple #3
0
def proc_area_sector(inv_list, grid_name, grid_desc, state_fips):
    '''
    Process an area sector
    '''
    from smk2ae.ar_ff10 import AnnualFF10
    from smk2ae.grid_surg import GridSurg, match_surrogate, grid_sources
    from smk2ae.hem_groups import HEMGroups
    var_list = ('ATPRO_HOURLY','ATPRO_WEEKLY','ATPRO_MONTHLY','ATREF','SRGPRO','AGREF','SRGDESC',
      'HEM_GROUPS','COSTCY','RUN_GROUPS','CELL_GRID')
    check_env_vars(var_list)
    init_paths(os.environ['WORK_PATH'], ['emis','qa','locations','parameters','temporal','xwalk'])
    inv = AnnualFF10(inv_list, os.environ['HAPS_LIST'], state_fips)
    surg = GridSurg(os.environ['AGREF'], os.environ['SRGPRO'], os.environ['SRGDESC'], inv.scc_list)
    hem = HEMGroups(os.environ['HEM_GROUPS'])
    inv.emis = pd.merge(inv.emis, hem.xref, on='scc', how='left')
    if not inv.emis[(inv.emis['run_group'].isnull()) | (inv.emis['run_group'] == '')].empty:
        print 'WARNING: Unmatched SCCs to HEM groups'
        print inv.emis.ix[inv.emis['run_group'].isnull(),'scc'].drop_duplicates()
    # Narrow down to just the selected run groups.
    if os.environ['RUN_GROUPS']:
        run_groups = [rg.strip() for rg in os.environ['RUN_GROUPS'].split(',')]
        inv.emis = inv.emis[inv.emis['run_group'].isin(run_groups)].copy()
        inv.update_lists()
    else:
        run_groups = list(inv.emis['run_group'].drop_duplicates())
    inv.emis = match_surrogate(inv.emis, surg.xref)
    met_grid = Grid(grid_name, grid_desc)
    cell_grid = Grid(os.environ['CELL_GRID'], grid_desc)
    grid_emis = grid_sources(inv.emis, surg, cell_grid)
    if (met_grid.gdnam == cell_grid.gdnam) and (met_grid.xcell == 12000.):
        grid_emis['met_col'] = grid_emis['col']
        grid_emis['met_row'] = grid_emis['row']
        grid_emis['src_id'] = '12_1'
    elif (cell_grid.xcell == 4000.) and (met_grid.xorig == cell_grid.xorig) and (met_grid.xcell == 12000.):
        grid_emis = define_met_cell(grid_emis, cell_grid, met_grid)
    else:
        raise ValueError, 'Unsupported grid cell size'
    centroids = grid_emis[['met_col','met_row']].copy().drop_duplicates()
    centroids['met_centroid_lon'] = met_grid.colrow_to_ll(centroids['met_col'].astype('f') + 0.5, 
      centroids['met_row'].astype('f') + 0.5)['lon'].astype('f')
    centroids['met_cell'] = 'G' + centroids['met_col'].astype(str).str.zfill(3) + 'R' + centroids['met_row'].astype(str).str.zfill(3)
    grid_emis = pd.merge(grid_emis, centroids, on=['met_col','met_row'], how='left')
    grid_keys = ['region_cd','scc','src_id','met_cell']
    write_aermod_emis(inv, cell_grid.xcell, run_groups, 
      grid_emis[grid_keys + ['frac',]].drop_duplicates(grid_keys), hem.xref)
    # Temporalization
    from smk2ae.temporal import Temporal
    temp = Temporal(os.environ['ATREF'],os.environ['ATPRO_HOURLY'],os.environ['ATPRO_WEEKLY'],
        os.environ['ATPRO_MONTHLY'], inv.scc_list, inv.fips_list)
    from smk2ae.area_io import proc_area
    emis_keys = ['region_cd','scc','run_group','col','row','met_cell','src_id']
    grid_emis = grid_emis[emis_keys+['ann_value','met_centroid_lon']].copy().drop_duplicates(emis_keys)
    proc_area(grid_emis, cell_grid, temp)
Exemple #4
0
def main():
    var_list = ('EMISINV_A','HAPS_LIST','BASE_YEAR','WORK_PATH','POLY_FILE','STATE_FIPS',
      'HEM_GROUPS','SRG_XREF','SRG_PATH','SRG_PREFIX','RUN_GROUPS')
    check_env_vars(var_list)
    inv_list = get_inv_list()
    state_fips = os.environ['STATE_FIPS']
    inv = AnnualFF10(inv_list, os.environ['HAPS_LIST'], state_fips)
    hem = HEMGroups(os.environ['HEM_GROUPS'])
    inv.emis = pd.merge(inv.emis, hem.xref, on='scc', how='left')
    # Narrow down to just the selected run groups.
    if os.environ['RUN_GROUPS']:
        run_groups = [rg.strip() for rg in os.environ['RUN_GROUPS'].split(',')]
        inv.emis = inv.emis[inv.emis['run_group'].isin(run_groups)].copy()
        inv.update_lists()
    else:
        run_groups = list(inv.emis['run_group'].drop_duplicates())
    if inv.emis.empty:
        raise ValueError, 'No emissions found for given states and run groups.'
    surgs = nonconus_surgs(os.environ['SRG_PATH'], os.environ['SRG_PREFIX'])
    surgs.read_xref(os.environ['SRG_XREF'])
    inv.emis = surgs.match_emis(inv.emis)
    if not inv.emis[inv.emis['tract'].isnull()].empty:
        print inv.emis[inv.emis['tract'].isnull()].drop_duplicates()
        raise ValueError, 'Missing tract cross-reference'
    shapes = read_shapes(os.environ['POLY_FILE'], inv.fips_list)
    utm_zones = shapes[['facid','lon']].copy().drop_duplicates('facid')
    utm = UTM()
    utm_zones['utm_zone'] = utm_zones['lon'].apply(utm.get_zone)
    shapes = pd.merge(shapes, utm_zones[['facid','utm_zone']], on='facid', how='left')
    shapes[['utmx','utmy']] = shapes[['lon','lat','utm_zone']].apply(lambda x: \
      pd.Series(utm.get_coords(x['lon'],x['lat'],x['utm_zone'])), axis=1)
    temp = Temporal(os.environ['ATREF'],os.environ['ATPRO_HOURLY'],os.environ['ATPRO_WEEKLY'],
        os.environ['ATPRO_MONTHLY'], inv.scc_list, inv.fips_list)
    for run_group in list(inv.emis['run_group'].drop_duplicates()):
        for state in list(inv.emis['region_cd'].str[1:3].drop_duplicates()):
            write_aermod_emis(inv, surgs, shapes, hem.xref, run_group, state)
            emis_group = inv.emis.ix[(inv.emis['run_group'] == run_group) & \
             (inv.emis['region_cd'].str[1:3] == state), ['region_cd','run_group','tract']].copy()
            emis_group.drop_duplicates(inplace=True)
            write_locations(emis_group, shapes.copy().drop_duplicates('facid'), run_group, state)
            write_parameters(emis_group, shapes, run_group, state) 
            temp_group = pd.merge(inv.emis[(inv.emis['run_group'] == run_group) & \
              (inv.emis['region_cd'].str[1:3] == state)], shapes[['region_cd','facid','tract',
              'polynumber','src_id']].drop_duplicates(), on=['region_cd','tract'], how='left')
            if temp.use_daily:
                write_daily_prof(temp_group, temp, run_group, state)
            else:
                write_no_daily_prof(temp_group, temp, run_group, state)
Exemple #5
0
def proc_area_sector(inv_list, grid_name, grid_desc, state_fips):
    '''
    Process an area sector or run group
    '''
    from smk2ae.ar_ff10 import AnnualFF10
    from smk2ae.grid_surg import GridSurg, match_surrogate, grid_sources
    from smk2ae.source_groups import SourceGroups
    # Check for area specific environment variables
    var_list = ('ATPRO_HOURLY', 'ATPRO_WEEKLY', 'ATPRO_MONTHLY', 'ATREF',
                'SRGPRO', 'AGREF', 'SRGDESC', 'SOURCE_GROUPS', 'COSTCY',
                'RUN_GROUPS', 'GROUP_PARAMS', 'RUN_GROUP_SUFFIX')
    check_env_vars(var_list)
    # Setup the output paths
    init_paths(os.environ['WORK_PATH'],
               ['emis', 'qa', 'locations', 'parameters', 'temporal', 'xwalk'])
    invtable = get_invtable(os.environ['INVTABLE'])
    # Read the nonpoint FF10s
    inv = AnnualFF10(inv_list, invtable, state_fips)
    scc_list = list(inv.emis['scc'].drop_duplicates())
    # Load all of the appropriate gridding surrogates
    surg = GridSurg(os.environ['AGREF'], os.environ['SRGPRO'],
                    os.environ['SRGDESC'], scc_list)
    src_groups = SourceGroups(os.environ['SOURCE_GROUPS'])
    src_groups.xref['run_group'] = src_groups.xref['run_group'] + os.environ[
        'RUN_GROUP_SUFFIX']
    inv.merge_rungroups(src_groups.xref)
    # Narrow down to just the selected run groups.
    if os.environ['RUN_GROUPS']:
        run_groups = [rg.strip() for rg in os.environ['RUN_GROUPS'].split(',')]
    else:
        run_groups = list(inv.emis['run_group'].drop_duplicates())
    # Setup the met grid. This is the IOAPI grid specified, but may not be the same as the cell grid
    #  if this is a sector with 4 km cells such as HDON, OILGAS, etc.
    met_grid = Grid(grid_name, grid_desc)
    for run_group in run_groups:
        print('NOTE: Running for %s' % run_group)
        # Gridding
        run_emis = match_surrogate(
            inv.emis[inv.emis['run_group'] == run_group], surg.xref)
        if grid_name.startswith('12'):
            # Grid at 4 km for HDON LDON HDOFF LDOFF HOTEL and OILGAS
            if run_group[:4] in ('HDON', 'LDON', 'HOTE', 'OILG'):
                cell_grid = Grid('4%s' % grid_name[2:], grid_desc)
            else:
                cell_grid = met_grid
        else:
            cell_grid = met_grid
        run_emis = grid_sources(run_emis, surg, cell_grid)
        # If the cell and met grid are the same, then set up the parameters to be equivalent
        if cell_grid.GDNAM == met_grid.GDNAM:
            run_emis['met_col'] = run_emis['col']
            run_emis['met_row'] = run_emis['row']
            run_emis['src_id'] = '%s_1' % int(met_grid.XCELL / 1000.)
        # Otherwise find the met cell based on the cell grid
        elif (cell_grid.XCELL
              == 4000.) and (met_grid.XORIG
                             == cell_grid.XORIG) and (met_grid.XCELL
                                                      == 12000.):
            run_emis = define_met_cell(run_emis, cell_grid, met_grid)
        else:
            raise ValueError('Unsupported grid cell size')
        swcorners = get_sw_corner(
            run_emis[['met_col', 'met_row']].copy().drop_duplicates(),
            met_grid)
        run_emis = pd.merge(run_emis,
                            swcorners,
                            on=['met_col', 'met_row'],
                            how='left')
        grid_keys = ['region_cd', 'scc', 'src_id', 'met_cell']
        write_aermod_emis(
            inv, cell_grid.XCELL, run_emis[grid_keys + [
                'frac',
            ]].drop_duplicates(grid_keys), src_groups.xref)
        # Onroad run groups have temporal helper files calculated outside of this processor
        if run_group[:3] in ('HDO', 'LDO', 'HOT'):
            temp = None
        else:
            from smk2ae.temporal import Temporal
            # Temporalization
            scc_list = list(run_emis['scc'].drop_duplicates())
            fips_list = list(run_emis['region_cd'].drop_duplicates())
            temp = Temporal(os.environ['ATREF'], os.environ['ATPRO_HOURLY'],
                            os.environ['ATPRO_WEEKLY'],
                            os.environ['ATPRO_MONTHLY'], scc_list, fips_list)
        from smk2ae.area_io import proc_area
        emis_keys = [
            'region_cd', 'scc', 'run_group', 'col', 'row', 'met_cell', 'src_id'
        ]
        run_emis = run_emis[emis_keys +
                            ['ann_value', 'met_swcorner_lon']].copy(
                            ).drop_duplicates(emis_keys)
        params = release_params(os.environ['GROUP_PARAMS'],
                                os.environ['RUN_GROUP_SUFFIX'])
        proc_area(run_emis, cell_grid, temp, params)