def write_runways(self, grid, temp): ''' Write the files for airports with runways ''' inv_cols = [ 'facility_id', 'facility_name', 'fac_col', 'fac_row', 'state' ] df = pd.merge(self.locs, self.inv[inv_cols].drop_duplicates(), on='facility_id', how='left') if not df.empty: print 'Airports facilities with runways: %s' % len( df['facility_id'].drop_duplicates()) df['length'] = df.apply(lambda row: grid.calc_dist(\ row['start_x'],row['start_y'],row['end_x'],row['end_y']), axis=1) df['area'] = df['length'] * df['width'] df['lnemis'] = 1000. / df['area'] df['relhgt'] = 3. df['szinit'] = 3. df['type'] = 'LINE' df['centroid_lon'] = grid.colrow_to_ll( df['fac_col'].astype('f') + 0.5, df['fac_row'].astype('f') + 0.5)['lon'].astype('f') utm = UTM() df['utm_zone'] = df['centroid_lon'].apply(utm.get_zone) df[['xs1','ys1']] = df[['start_x','start_y','utm_zone']].apply(lambda x: pd.Series(\ utm.get_coords(x['start_x'],x['start_y'],x['utm_zone'])), axis=1) df[['xs2','ys2']] = df[['end_x','end_y','utm_zone']].apply(lambda x: pd.Series(\ utm.get_coords(x['end_x'],x['end_y'],x['utm_zone'])), axis=1) self._write_runway_locs(df) self._write_runway_params(df) self._write_temp_prof(df, temp, 'line')
def write_no_runways(self, grid, temp): ''' Write the files for airports without runways ''' inv_cols = [ 'facility_id', 'facility_name', 'fac_col', 'fac_row', 'state', 'latitude', 'longitude' ] df = self.inv.ix[~self.inv['facility_id'].isin(self.runways), inv_cols].copy() df.drop_duplicates(inplace=True) if not df.empty: print 'Airports facilities without runways: %s' % len( df['facility_id'].drop_duplicates()) df['len_x'] = 10. df['len_y'] = 10. df['src_id'] = 'AP1' df['relhgt'] = 3. df['szinit'] = 3. df['angle'] = 0 df['centroid_lon'] = grid.colrow_to_ll( df['fac_col'].astype('f') + 0.5, df['fac_row'].astype('f') + 0.5)['lon'].astype('f') utm = UTM() df['utm_zone'] = df['centroid_lon'].apply(utm.get_zone) df[['utm_x','utm_y']] = df[['longitude','latitude','utm_zone']].apply(lambda x: pd.Series(\ utm.get_coords(x['longitude'],x['latitude'],x['utm_zone'])), axis=1) df[['grid_x','grid_y']] = df[['longitude','latitude']].apply(lambda x: pd.Series(\ grid.get_coords(x['longitude'],x['latitude'])), axis=1) self._write_nonrunway_locs(df) self._write_nonrunway_params(df) self._write_temp_prof(df, temp, 'nonrunway')
def main(): var_list = ('GRIDDESC', 'REGION_IOAPI_GRIDNAME', 'EMISINV_A', 'HAPS_LIST', 'BASE_YEAR', 'SECTOR', 'WORK_PATH', 'POLY_FILE', 'STATE_FIPS') check_env_vars(var_list) inv_list = get_inv_list() state_fips = os.environ['STATE_FIPS'] emis = read_emissions(inv_list, os.environ['HAPS_LIST'], state_fips) shapes = read_shapes(os.environ['POLY_FILE'], state_fips) # Drop shapes that have no emissions shapes = pd.merge(shapes, emis[['facid', 'src_id', 'scc']].drop_duplicates(['facid', 'src_id']), on=['facid', 'src_id'], how='left') shapes = shapes[shapes['scc'].notnull()].copy() shapes.drop('scc', axis=1, inplace=True) met_grid = Grid(os.environ['REGION_IOAPI_GRIDNAME'], os.environ['GRIDDESC']) cell_poly_df = get_met_cell(shapes, met_grid) shapes.drop(['col', 'row', 'cell'], axis=1, inplace=True) shapes = pd.merge(shapes, cell_poly_df, on=['facid', 'src_id'], how='left') shapes['centroid_lon'] = met_grid.colrow_to_ll( shapes['col'].astype('f') + 0.5, shapes['row'].astype('f') + 0.5)['lon'].astype('f') utm = UTM() shapes['utm_zone'] = shapes['centroid_lon'].apply(utm.get_zone) 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) shapes['type'] = 'AREAPOLY' state_rh = calc_state_rh(emis[['region_cd', 'scc', 'ann_value', 'ure']].copy()) ''' Ports and underway get separate output files except for the emissions files ''' shapes['state'] = shapes['region_cd'].str[:2] for source_type in ('ports', 'underway'): print 'Running for %s' % source_type shapes_type = shapes[shapes['facid'].str.startswith( source_type[0].upper())].copy() if shapes_type.empty: print 'WARNING: No %s sources found' % source_type else: emis_type = emis[emis['facid'].str.startswith( source_type[0].upper())].copy() write_emis(emis_type.copy(), source_type) write_locations( shapes_type.copy().drop_duplicates([ 'region_cd', 'facid', 'src_id', 'type', 'col', 'row', 'utm_zone' ]), source_type) write_parameters(shapes_type, state_rh, source_type) write_temporal(emis_type, shapes_type, source_type)
def get_utm_location(df, grid_info): ''' Select UTM zone based on centroid of met grid cell. Calc UTM X and Y. ''' utm = UTM() df['centroid_lon'] = grid_info.colrow_to_ll(df['col'].astype('f') + 0.5, df['row'].astype('f') + 0.5)['lon'].astype('f') utm = UTM() df['utm_zone'] = df['centroid_lon'].apply(utm.get_zone) df[['utm_x','utm_y']] = df[['longitude','latitude','utm_zone']].apply(lambda x: \ pd.Series(utm.get_coords(x['longitude'],x['latitude'],x['utm_zone'])), axis=1) return df
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)
def proc_shapes(shapes, met_grid): ''' Process the shapes into the correct cells and reproject to UTM ''' cell_poly_df = get_met_cell(shapes, met_grid) shapes.drop(['col', 'row', 'cell'], axis=1, inplace=True) shapes = pd.merge(shapes, cell_poly_df, on=['facid', 'src_id'], how='left') shapes['centroid_lon'] = met_grid.colrow_to_ll( shapes['col'].astype('f'), shapes['row'].astype('f'))['lon'].astype('f') utm = UTM() shapes['utm_zone'] = shapes['centroid_lon'].apply(utm.get_zone) 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) shapes['type'] = 'AREAPOLY' shapes['state'] = shapes['region_cd'].str[:2] return shapes