import glob
import os
import netCDF4
os.chdir('/usgs/data2/emontgomery/stellwagen/Data')
os.chdir('ARGO_MERCHANT')
# find netcdf files
types = ('*.cdf', '*.nc') # the tuple of file types
files = []
for file in types:
files.extend(glob.glob(file))
print files
['1211-A1H.cdf', '1211-AA.cdf', '1211P-A.cdf', '1211P-A1H.cdf', '1211TR-A.cdf', '1211TR-A1H.cdf']
nc = netCDF4.Dataset('1211-A1H.cdf')
vars=nc.variables.keys()
coord_vars = ['time','time2','depth','lat','lon']
# find data variables by removing coordinate variables from the variable list
data_vars = [var for var in vars if var not in coord_vars]
print data_vars
[u'u_1205', u'v_1206', u'CD_310', u'CS_300', u'upr_4001']
nt = len(nc.dimensions['time'])
nz = len(nc.dimensions['depth'])
print nt,nz
250 1
# create dimensions
nco = netCDF4.Dataset('/usgs/data2/notebook/test.nc','w',clobber=True)
nco.createDimension('time',nt)
nco.createDimension('station',1)
if nz > 1:
nco.createDimension('depth',nz)
nchar=20
nco.createDimension('nchar',nchar)
<netCDF4.Dimension at 0x39e4a50>
# create coordinate variables
time_v = nco.createVariable('time', 'f8', ('time'))
lon_v = nco.createVariable('lon','f4',('station'))
lat_v = nco.createVariable('lat','f4',('station'))
depth_v = nco.createVariable('depth','f4',('station'))
station_v = nco.createVariable('site','S1',('station','nchar'))
# write global attributes
g_attdict = nc.__dict__
g_attdict['Conventions'] = 'CF-1.6'
g_attdict['featureType'] = 'timeSeries'
nco.setncatts(g_attdict)
# write station variable
station_v.cf_role = 'timeseries_id'
station_v.standard_name = 'station_id'
data = numpy.empty((1,),'S'+repr(nchar))
data[0] = 'mystation'
station_v[:] = netCDF4.stringtochar(data)
# write time variable
time_v.units = 'seconds since 1858-11-17 00:00:00 +0:00'
time_v.standard_name = 'time'
time_v.calendar = 'gregorian'
time_v[:] = (nc.variables['time'][:]-2400001.)*3600.*24. + nc.variables['time2'][:]/1000.
# write lon variable
lon_v.units = 'degree_east'
lon_v.standard_name = 'longitude'
lon_v[:] = nc.variables['lon'][:]
# write lat variable
lat_v.units = 'degree_north'
lat_v.standard_name = 'latitude'
lat_v[:] = nc.variables['lat'][:]
# write depth variable
depth_v.units = 'm'
depth_v.standard_name = 'depth'
depth_v.positive = 'down'
depth_v.axis = 'Z'
depth_v[:] = nc.variables['depth'][:]
for varname in data_vars:
print nc.variables[varname].long_name
Eastward Velocity Northward Velocity CURRENT DIRECTION (T) CURRENT SPEED (CM/S) UPPER ROTOR SPEED
# variable mapping
d={}
d['eastward_sea_water_velocity']=['eastward velocity', 'east']
d['northward_sea_water_velocity']=['northward velocity', 'north']
d['sea_water_temperature']=['t_20']
d['sea_water_salinity'] = ['s_40']
# create the data variables
var_v=[]
for varname in data_vars:
ncvar = nc.variables[varname]
# if time series variable
if len(ncvar) == nt:
var = nco.createVariable(varname,ncvar.dtype,('time','station'))
else:
var = nco.createVariable(varname,ncvar.dtype,('time','depth','station'))
# load old variable attributes and modify if necessary
attdict = ncvar.__dict__
# if dounpackshort and 'scale_factor' in attdict: del attdict['scale_factor']
attdict['coordinates'] = 'time lon lat depth'
# assign standard_name if in dictionary
a =[k for (k, v) in d.iteritems() if attdict['long_name'].strip().lower() in v]
if len(a)==1: attdict['standard_name']=a[0]
# write variable attributes
var.setncatts(attdict)
# write the data
var[:] = ncvar[:]
nco.close()
d={}
d['eastward_sea_water_velocity']=['u_1205', 'u']
d['northward_sea_water_velocity']=['v_1206', 'v']
a =[k for (k, v) in d.iteritems() if 'u_1205' in v]
a =[k for (k, v) in d.iteritems() if 'u_2205' in v]
a
[]
empty(a)
array(0.0)
isempty(a)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-288-bc8b6da86051> in <module>() ----> 1 isempty(a) NameError: name 'isempty' is not defined
a.isempty()
a
a.empty()
len(a)