-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncutil.py
More file actions
335 lines (287 loc) · 10.5 KB
/
ncutil.py
File metadata and controls
335 lines (287 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# Last modified: Time-stamp: <2011-05-05 15:31:37 haines>
"""
Create, update and load utilities for netcdf files
"""
from pycdf import *
import os
import numpy
def nc_create(ncFile, (global_atts, var_atts, dim_inits, var_inits, var_data)):
"""
Create new netcdf file
:Parameters:
ncFile : string
Path and name of file to create
(global_atts, var_atts, dim_inits, var_inits, var_data) : tuple
Global Attributes, Variable Attributes, Dimensions, Variable Dimensions, and Data
Everything you need to create a netCDF file.
"""
try:
# Open new netCDF file, overwrite if it exists, create if does not
nc = CDF(ncFile, NC.WRITE|NC.CREATE|NC.TRUNC)
# Automatically set define and data modes.
nc.automode()
#
# GLOBALS
for attrName in global_atts.keys():
setattr(nc, attrName, global_atts[attrName])
# DIMENSIONS
for dim in dim_inits:
dimName, dimValue = dim
# print '%s = %d' % (dimName, dimValue)
ncdim = nc.def_dim(dimName, dimValue)
# VARIABLES
for var in var_inits:
varName, varType, varDim = var
ncvar = nc.def_var(varName, varType, varDim)
# add attributes
for attrName in var_atts[varName].keys():
setattr(ncvar, attrName, var_atts[varName][attrName])
# setattr(ncvar, '_FillValue', numpy.nan)
# add data
nrecs = nc.inq_unlimlen()
for var in var_data:
varName, varData = var
# print varName
# print varData
# print varData.shape
ncvar = nc.var(varName)
# e.g. lat = array(var_data['lat'])
# if an array
if type(varData) == numpy.ndarray:
if ncvar.isrecord():
# time, ens, u, v
ncvar[nrecs:nrecs+len(varData)] = varData.tolist()
else:
ncvar[:] = varData.tolist() # z
else:
# if tuple, sequence or scalar
ncvar[:] = varData
nc.close()
except CDFError, msg:
print "CDFError:", msg
# if nc:
# nc.close()
# del(nc)
def nc_update(ncFile, (global_atts, var_atts, var_data)):
"""
Create new netcdf file
:Parameters:
ncFile : string
Path and name of file to create
(global_atts, var_atts, var_data) : tuple
Global Attributes, Variable Attributes and Data
Everything you need to update a netCDF file.
"""
try:
# Open netCDF in write mode
nc = CDF(ncFile, NC.WRITE)
# Automatically set define and data modes.
nc.automode()
#
# GLOBALS
for attrName in global_atts.keys():
setattr(nc, attrName, global_atts[attrName])
# VARIABLES
# update attributes
for var in var_atts:
varName, atts = var
ncvar = nc.var(varName)
for attrName in atts.keys():
setattr(ncvar, attrName, atts[attrName])
# update data
nrecs = nc.inq_unlimlen()
for var in var_data:
varName, varData = var
ncvar = nc.var(varName)
# e.g. lat = array(var_data['lat'])
# if an array
if type(varData) == numpy.ndarray:
if ncvar.isrecord():
# time, ens, u, v (with unlimited dimension)
ncvar[nrecs:nrecs+len(varData)] = varData.tolist()
else:
ncvar[:] = varData.tolist() # z (limited dimension)
else:
# if tuple, sequence or scalar
ncvar[:] = varData
nc.close()
except CDFError, msg:
print "CDFError:", msg
# if nc:
# nc.close()
# del(nc)
def nc_get_time(ncFile):
"""Get time array from file """
try:
nc = CDF(ncFile)
ncvars = nc.variables()
if 'time' in ncvars.keys():
es = nc.var('time')[:]
units = nc.var('time').units
else:
print "time variable not found in ", ncFile
nc.close()
return (es, units)
except CDFError, msg:
print "CDFError:", msg
def nc_find_record_vars(ncFile):
"""Find which variable are record variables"""
try:
nc = CDF(ncFile)
ncvars = nc.variables()
# list which variables is a record variable
var_list = [varName for varName in ncvars.keys() if nc.var(varName).isrecord()]
nc.close()
return var_list
except CDFError, msg:
print "CDFError:", msg
def nc_replace_fillvalue(ncFile, newfillvalue=-99999.0):
"""
Replaces any occurrence of old _FillValue with new one
This function is useful for replacing the _FillValue global
attribute and then searching the data for the old value and
replacing it with the new one.
:Parameters:
ncFile : string
Path and name of file to create
:Other Parameters:
newfillvalue : type match to data (generally float)
By default is -99999.0
"""
try:
nc = CDF(ncFile, NC.WRITE)
nc.automode()
oldfillvalue = nc._FillValue
nc._FillValue = newfillvalue
for v in nc.variables().keys():
vd = nc.var(v)[:]
if numpy.isnan(oldfillvalue):
idx = numpy.isnan(vd)
else:
idx = vd == oldfillvalue
if idx.any():
vd[idx] = nc._FillValue
nc.var(v)[:] = vd
nc.close()
except CDFError, msg:
print "CDFError:", msg
def nc_rename_dimension(ncFile, oldname, newname):
""" Rename dimension name """
try:
nc = CDF(ncFile, NC.WRITE)
nc.definemode()
for d in nc.dimensions().keys():
if d==oldname: nc.dim(d).rename(newname)
nc.close()
except CDFError, msg:
print "CDFError:", msg
def nc_file_check(fns):
"""Check file or list of files to ensure it is a netcdf file
If it is not, remove a file or files from the list"""
if isinstance(fns, str):
try:
nc = CDF(fns)
nc.close()
new_fns = fns
except CDFError, msg:
print "CDFError:", msg, fns
new_fns = None
else:
new_fns = []
for fn in fns:
try:
nc = CDF(fn)
nc.close()
new_fns.append(fn)
except CDFError, msg:
print "CDFError:", msg, fn
return tuple(new_fns)
def nc_load(ncFile, varsLoad='all', nameType='variable_name',
ga_flag=True, va_flag=True):
"""
Load netcdf file
:Parameters:
ncFile : string or list of strings
Path and name of file to load
If list, then CDFMF
:Other Parameters:
nameType : string 'variable_name' (default) or 'standard_name'
Defines naming convention to use for variable names as data
are loaded. Variable name is the name used to store data
in file. 'standard_name' means use variable name based on
variable attribute called 'standard_name' of netcdf variable.
varLoad : string or tuple of strings
specific variable names to be loaded into a sequence or scalar
in python following specification set in nameType
By default, all variables will be loaded.
ga_flag : boolean flag
By default, load the global file attributes
va_flag : boolean flag
By default, load the variable file attributes
:Returns:
(global_atts, var_atts, dim_inits, var_inits, var_data) : tuple
Global Attributes, Variable Attributes, Dimensions, Variable Dimensions, and Variable Data
Everything you need to create a netCDF file.
"""
try:
if isinstance(ncFile, str):
# if only one file and it is a string
nc = CDF(ncFile)
else:
# if multiple filenames
nc = CDFMF(tuple(set(ncFile)))
ncdims = nc.dimensions(full=1)
ncvars = nc.variables()
# GLOBAL ATTRIBUTES (global_atts)
if ga_flag:
global_atts = nc.attributes()
else:
global_atts = {}
# DIMENSIONS (dim_inits)
dim_inits = [None for j in range(len(ncdims))]
if len(ncdims)>0:
for dimName,dimValue in ncdims.items():
val,idx,isUN = dimValue
if isUN:
dim_inits[idx] = (dimName, NC.UNLIMITED)
else:
dim_inits[idx] = (dimName, val)
if varsLoad == 'all':
varNames = ncvars.keys()
else:
varNames = varsLoad
# VARIABLE DIMENSIONS (var_inits)
# gets init info for requested variables and original order
# initialize with same number of original variables
# so order can be preserved by idx
var_inits = [None for j in range(len(ncvars))]
if len(ncvars)>0:
for varName in varNames:
val,shape,typ,idx = ncvars[varName]
var_inits[idx] = (varName, typ, val)
# remove the None values from the list to preserve original order
var_inits = [v for v in var_inits if v != None]
# VARIABLE ATTRIBUTES (var_atts)
# gets attributes of requested variables
var_atts = {}
if len(ncvars)>0 and va_flag:
for var in varNames:
varAttrs = nc.var(var).attributes()
var_atts[var] = varAttrs
# VARIABLE DATA (var_data)
# loads requested variables, original order preserved as with var_inits
var_data = [None for j in range(len(ncvars))]
if len(ncvars)>0:
for varName in varNames:
val,shape,typ,idx = ncvars[varName]
var_data[idx] = (varName, nc.var(varName)[:])
var_data = [v for v in var_data if v != None]
# type cast lists into tuples
dim_inits = tuple(dim_inits)
var_inits = tuple(var_inits)
var_data = tuple(var_data)
nc.close()
return (global_atts, var_atts, dim_inits, var_inits, var_data)
except CDFError, msg:
print "CDFError:", msg