Skip to content

Instantly share code, notes, and snippets.

@is
Created April 23, 2016 08:49
Show Gist options
  • Select an option

  • Save is/b8b9b9fc9b81b63b96c6971566f62857 to your computer and use it in GitHub Desktop.

Select an option

Save is/b8b9b9fc9b81b63b96c6971566f62857 to your computer and use it in GitHub Desktop.
cut CMAQ CONC file python script. require numpy and netCDF4
#!/usr/bin/python
import netCDF4 as nc
import numpy as np
import glob
species = ['SO2']
def convert(ifn, ofn):
ds0 = nc.Dataset(ifn)
ds1 = nc.Dataset(ofn, 'w')
for dname in ['TSTEP', 'LAY', 'ROW', 'COL']:
dim = ds0.dimensions[dname]
if dname == 'LAY':
ds1.createDimension(dname, 1)
elif dim.isunlimited():
ds1.createDimension(dname, 0)
else:
ds1.createDimension(dname, len(dim))
for v in species:
ds1.createVariable(v, 'f4', ['TSTEP', 'LAY', 'ROW', 'COL'])
for v in species:
av = ds1.variables[v]
na = ds0.variables[v][:][:,0:1,:,:]
av[:] = na
ds0.close()
ds1.close()
def fnmap(ifn):
return ifn[-7:]
if __name__ == '__main__':
fns = glob.glob('data/*.CONC.*')
for f in fns:
ofn = fnmap(f)
print f, ofn
convert(f, ofn)
# vim: expandtab ts=2 sts=2 ai filetype=python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment