Skip to content

Commit

Permalink
Smk2aermod updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bokhaeng committed Oct 13, 2020
1 parent 3270b31 commit bd36a26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
7 changes: 5 additions & 2 deletions scripts/aermod/smk2ae/scripts/aermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get_sw_corner(df, met_grid):
df['met_swcorner_lon'] = met_grid.colrow_to_ll(df['met_col'].astype('f'),
df['met_row'].astype('f'))['lon'].astype('f')
df['met_cell'] = 'G' + df['met_col'].astype(str).str.zfill(3) + 'R' + df['met_row'].astype(str).str.zfill(3)
return df
return df.drop_duplicates('met_cell')

def proc_area_sector(inv_list, grid_name, grid_desc, state_fips):
'''
Expand Down Expand Up @@ -197,7 +197,7 @@ def proc_area_sector(inv_list, grid_name, grid_desc, state_fips):
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[:3] in ('HDO','LDO','HOT','OIL'):
if run_group[:4] in ('HDON','LDON','HOTE','OILG'):
cell_grid = Grid('4%s' %grid_name[2:], grid_desc)
else:
cell_grid = met_grid
Expand Down Expand Up @@ -269,6 +269,9 @@ def write_aermod_emis(inv, cell_size, run_emis, xref):
key_cols = ['run_group','region_cd','met_cell','src_id','source_group','smoke_name']
out_cols = key_cols + ['ann_value',]
group_sccs = list(run_emis['scc'].drop_duplicates())
run_frac = run_emis[['region_cd','scc','frac']].groupby(['region_cd','scc'], as_index=False).sum()
run_emis = pd.merge(run_emis, run_frac, on=['region_cd','scc'], how='left', suffixes=['','_sum'])
run_emis['frac'] = run_emis['frac'] / run_emis['frac_sum']
# Merge in the pollutants
group_df = pd.merge(run_emis, inv.emis, on=['region_cd','scc'], how='left')
group_df['ann_value'] = group_df['ann_value'] * group_df['frac']
Expand Down
17 changes: 13 additions & 4 deletions scripts/aermod/smk2ae/smk2ae/area_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def proc_area(df, grid_info, temp, params):
write_daily_prof(df, temp)
else:
write_no_daily_prof(df, temp)
else:
run_group = df['run_group'].values[0]
fips_map = get_fips_map(df[['run_group','region_cd','met_cell','src_id',
'ann_value']])
write_county_xwalk(fips_map, run_group)

def calc_cell_corner(df, grid_info, utm):
'''
Expand Down Expand Up @@ -67,9 +72,12 @@ def write_parameters(df, grid_info, utm, params):
# The polygons are grid cells
df = calc_polygons(df, grid_info, utm)
df = pd.merge(df, params, on='run_group', how='left')
if len(df[df['rh'].isnull()]) > 0:
for col in ['rh','release_height']:
if col in list(df.columns) and 'rel_ht' not in list(df.columns):
df.rename(columns={col: 'rel_ht'}, inplace=True)
if len(df[df['rel_ht'].isnull()]) > 0:
print('Unmatched run groups in GROUP PARAMS file')
print(df.loc[df['rh'].isnull(), 'run_group'].drop_duplicates())
print(df.loc[df['rel_ht'].isnull(), 'run_group'].drop_duplicates())
raise ValueError('Unmatched run group parameters')
df['verts'] = 4
out_cols = ['run_group','met_cell','src_id','rel_ht','verts','sz','utmx','utmy','x2','y2',
Expand All @@ -87,7 +95,7 @@ def write_daily_prof(df, temp):
cols = ['region_cd','scc','run_group','met_cell','ann_value','src_id']
hierarchy = [['region_cd','scc'],['region_cd',]]
df = df[cols].copy().sort_values('ann_value').drop_duplicates(['run_group','region_cd','met_cell'],
take_last=True)
keep='last')
temp.profs.fillna(0, inplace=True)
value_cols = ['month','day','hour','factor']
run_group = df['run_group'].values[0]
Expand Down Expand Up @@ -150,6 +158,7 @@ def write_no_daily_prof(df, temp):
run_group = df['run_group'].values[0]
df.sort_values('src_id', inplace=True)
states = list(df['region_cd'].str[:2].drop_duplicates())
print(run_group)
if run_group[:3] in ('LDO','HDO','HOT'):
fips_map = get_fips_map(df[['run_group','region_cd','met_cell','src_id',
'ann_value']].copy())
Expand All @@ -175,7 +184,7 @@ def get_fips_map(df):
'''
Map county and cell by sum of emissions
'''
df.sort_values('ann_value', inplace=True)
df = df.copy().sort_values('ann_value')
df.drop_duplicates(['run_group','met_cell','src_id'], keep='last', inplace=True)
df.drop('ann_value', axis=1, inplace=True)
return df
Expand Down
2 changes: 2 additions & 0 deletions scripts/aermod/smk2ae/smk2ae/grid_surg.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _get_gref(self, gref, scc_list):
df['region_cd'] = df['region_cd'].apply(self._fix_fips)
df.ix[df['scc'].str.startswith('00'), 'scc'] = df.ix[df['scc'].str.startswith('00'),
'scc'].str[2:]
df.loc[df['scc'] != '', 'scc'] = df.loc[df['scc'] != '', 'scc'].astype(int).astype(str)
df['code'] = df['code'].str.split('!').str[0].str.strip()
return df[df['scc'].isin(scc_list)].copy()

Expand Down Expand Up @@ -164,6 +165,7 @@ def grid_sources(emis, surg, grid_info):
except KeyError:
raise KeyError('Missing surrogate for code %s' %code)
if src_surg.cell != grid_info.XCELL:
print(src_surg.cell, grid_info.XCELL)
raise ValueError('Gridding surrogate cell size does not match grid cell size')
code_emis = pd.merge(code_emis, src_surg.table, on='region_cd', how='left')
# Identify sources the cross reference successfully to a surrogate but are either missing a fraction
Expand Down

0 comments on commit bd36a26

Please sign in to comment.