from pylab import *
import netCDF4
# open NetCDF input files
f = netCDF4.MFDataset('/usgs/data2/rsignell/models/ncep/narr/air.2m.19??.nc')
# print variables
f.variables.keys()
[u'lat', u'lon', u'x', u'y', u'Lambert_Conformal', u'time', u'time_bnds', u'air']
print f.variables['time']
<class 'netCDF4._Variable'> float64 time('time',) units: hours since 1800-1-1 00:00:0.0 long_name: Time axis: T standard_name: time coordinate_defines: start delta_t: 0000-00-01 00:00:00 actual_range: [ 1569072. 1577808.] avg_period: 0000-00-01 00:00:00 unlimited dimensions = ('time',) current size = (731,)
atemp = f.variables['air']
print atemp
ntimes, ny, nx = shape(atemp)
cold_days = zeros((ny,nx),dtype=int)
# create output NetCDF file
nco = netCDF4.Dataset('/usgs/data2/notebook/cold_days3.nc','w',clobber=True)
nco.createDimension('x',nx)
nco.createDimension('y',ny)
nco.createDimension('time',ntimes)
cold_days_v = nco.createVariable('cold_days', 'i4', ( 'time', 'y', 'x'))
cold_days_v.units='days'
cold_days_v.long_name='total number of days below 0 degC'
cold_days_v.grid_mapping = 'Lambert_Conformal'
timeo = nco.createVariable('time','f8',('time'))
lono = nco.createVariable('lon','f4',('y','x'))
lato = nco.createVariable('lat','f4',('y','x'))
xo = nco.createVariable('x','f4',('x'))
yo = nco.createVariable('y','f4',('y'))
lco = nco.createVariable('Lambert_Conformal','i4')
# copy all the variable attributes from original file
for var in ['lon','lat','x','y','Lambert_Conformal','time']:
for att in f.variables[var].ncattrs():
setattr(nco.variables[var],att,getattr(f.variables[var],att))
# copy variable data for lon,lat,x and y
lono[:]=f.variables['lon'][:]
lato[:]=f.variables['lat'][:]
xo[:]=f.variables['x'][:]
yo[:]=f.variables['y'][:]
timeo[:]=f.variables['time'][:]
for i in xrange(ntimes):
cold_days += atemp[i,:,:].data-273.15 < 0
# write the cold_days data
cold_days_v[i,:,:]=cold_days
# copy Global attributes from original file
for att in f.ncattrs():
setattr(nco,att,getattr(f,att))
nco.Conventions='CF-1.6'
nco.close()
f.close()
<class 'netCDF4._Variable'> int16 air('time', 'y', 'x') units: K long_name: Daily Air Temperature at 2 m unpacked_valid_range: [ 151. 400.] precision: 0.00379979 actual_range: [ 219.79893494 312.32373047] missing_value: 32767 valid_range: [-32765 32765] _FillValue: -32767 GRIB_name: TMP GRIB_id: 11 var_desc: Air temperature standard_name: air_temperature level_desc: 2 m dataset: NARR Daily Averages statistic: Mean parent_stat: Individual Obs grid_mapping: Lambert_Conformal coordinates: lat lon add_offset: 275.5 scale_factor: 0.00379979 cell_methods: time: mean (of 8 3-hourly values in one day) unlimited dimensions = ('time',) current size = (731, 277, 349)
f2v = netCDF4.Dataset('/usgs/data2/notebook/cold_days3.nc').variables
print f2v['cold_days']
<type 'netCDF4.Variable'> int32 cold_days(u'time', u'y', u'x') units: days long_name: total number of days below 0 degC grid_mapping: Lambert_Conformal unlimited dimensions = () current size = (731, 277, 349)
pcolor(f2v['cold_days'][100,:,:])
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x2bf08c0>