Processing RCP emissions in SMOKE

Hi,

The specifics of what needs to be done depend upon what is in the CSV file. The easiest way starts with a pre-existing IO/API file, but that isn’t strictly necessary. The code below is just something a colleague threw together very quickly – it is probably not complete but it should be close:

– START PYTHON CODE

import pandas as pd

import PseudoNetCDF as pnc

pathtoexistingemisfile = ‘/path/to/ioapi.nc’

outpath = ‘test.nc’

pathtocsv = ‘/path/to/RCP.csv’

read and copy an existing file

iofile = pnc.pncopen(pathtoexistingemisfile).copy()

zero out

for k, v in iofile.variables.items():

if k != ‘TFLAG’:

v[:] = 0.

Assuming a specific structure of columns and spatial/temporal consistency

Datetime_UTC in the form YYYY-MM-DD HH:MM:SS+0000

Latitude in the form of decimal degrees north

Longitude in the form of decimal degrees east

Species in the form of the name expected by CMAQ

Value in the unit expected by CMAQ

csvdata = pd.read_csv(pathtocsv, parse_dates=[‘Datetime_UTC’])

Convert lon/lat to i/j

i, j = iofile.ll2ij(csvdata[‘Longitude’], csvdata[‘Latitude’])

csvdata[‘I’] = csvdata[‘Longitude’].astype(‘i’) * 0 + i

csvdata[‘J’] = csvdata[‘I’] * 0 + j

Group on date/i/j/spc and sum masses

grouped = csvdata.groupby([‘Species’, ‘Datetime_UTC’, ‘I’, ‘J’]).sum()

times = iofile.getTimes()

for ridx, row in grouped.iterrows():

spck, date, i, j = ridx

h = ((date - times[0]).total_seconds() // 3600).astype(‘i’)

iofile.variables[spck][h, 0, j, i] = row[‘Value’]

iofile.save(outpath, format=‘NETCCDF_CLASSIC’)

– END PYTHON CODE