forked from mhm-ufz/mHM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetcdf4.py
601 lines (517 loc) · 17 KB
/
netcdf4.py
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author
------
David Schaefer
Purpose
-------
A sanitizing layer for the netCDF4 library. Adds a number of convenince methods
and aims for a cleaner user interface. All classes avaliable are children of their
associated netCDF4 counterparts.
"""
import uuid
from netCDF4 import Dataset, Group, Dimension, Variable, date2num, num2date
from collections import OrderedDict
def _tupelize(arg):
if isinstance(arg,str):
return (arg,)
try:
return tuple(arg)
except TypeError:
return (arg,)
def copyGroup(ncin, group, skipdims=None, skipgroups=None, skipvars=None, skipattrs=None):
"""
Arguments
---------
ncin : Instance of an object with a createGroup method
(i.e. NcDataset, NcGroup)
group : Instance of an object with dimensions/variables/attributes/groups attributes
(i.e. NcDataset, NcGroup)
skipdims (optional) : string or list/tuple of strings
Name(s) of dimension(s) to skip
skipgroups (optional) : string or list/tuple of strings
Name(s) of group(s) to skip
skipvars (optinal) : string or list/tuple of strings
Name(s) of variable(s) to skip
skipattrs (optinal) : string or list/tuple of strings
Name(s) of attribute(s) to skip
Return
------
NcGroup
Purpose
-------
Copy the given group to ncin
"""
out = ncin.createGroup(group.name)
out.set_fill_off()
out.copyDimensions(group.dimensions, skipdims)
out.copyVariables(group.variables, skipvars)
out.copyAttributes(group.attributes, skipattrs)
out.copyGroups(group.groups, skipgroups)
return out
def copyDataset(ncin, group, skipdims=None, skipgroups=None, skipvars=None, skipattrs=None):
"""
Arguments
---------
ncin : Instance of an object with a createGroup method
(i.e. NcDataset, NcGroup)
group : Instance of an object with dimensions/variables/attributes/groups attributes
(i.e. NcDataset, NcGroup)
skipdims (optional) : string or list/tuple of strings
Name(s) of dimension(s) to skip
skipgroups (optional) : string or list/tuple of strings
Name(s) of group(s) to skip
skipvars (optinal) : string or list/tuple of strings
Name(s) of variable(s) to skip
skipattrs (optinal) : string or list/tuple of strings
Name(s) of attribute(s) to skip
Return
------
NcDataset/NcGroup
Purpose
-------
Copy the content of given group to ncin
"""
ncin.set_fill_off()
ncin.copyDimensions(group.dimensions, skipdims)
ncin.copyVariables(group.variables, skipvars)
ncin.copyAttributes(group.attributes, skipattrs)
ncin.copyGroups(group.groups, skipgroups)
return ncin
def copyGroups(ncin, groups, skip=None):
"""
Arguments
---------
ncin : Instance of an object with a createGroup method
(i.e. NcDataset, NcGroup)
groups : Dictionary
key : group name (string)
value : instance of an object with dimensions/variables/attributes/groups attributes
skip (optional) : string or list/tuple of strings
Name(s) of group(s) to skip
Return
------
None
Purpose
-------
Copy the given groups to ncin
"""
for g in groups.values():
if g.name not in _tupelize(skip):
ncin.copyGroup(g)
def copyDimension(ncin, dim):
"""
Arguments
---------
ncin : Instance of an object with a createDimension method
(i.e. NcDataset, NcGroup)
group : Instance of NcDimension
Return
------
netCDF4.Dimension
Purpose
-------
Copy the given dimension to ncin
"""
return ncin.createDimension(dim.name, None if dim.isunlimited() else len(dim))
def copyDimensions(ncin, dimensions, skip=None):
"""
Arguments
---------
ncin : Instance of an object with a createDimension method
(i.e. NcDataset, NcGroup)
dimension : Dictionary
key : dimension name (string)
value : instance of NcDimension
skip (optional) : string or list/tuple of strings
Name(s) of dimension(s) to skip
Return
------
None
Purpose
-------
Copy the given dimensions to ncin
"""
for d in dimensions.values():
if d.name not in _tupelize(skip):
ncin.copyDimension(d)
def copyAttributes(ncin, attributes, skip=None):
"""
Arguments
---------
ncin : Instance of an object with a createAttribute method
(i.e. NcDataset, NcGroup, NcVariable)
attributes : Dictionary
key : string
value : string/any numeric type
skip (optional) : string or list/tuple of strings
Name(s) of attribute(s) to skip
Return
------
None
Purpose
-------
Copy the given attributes to ncin
"""
for k, v in attributes.items():
if k not in _tupelize(skip):
if k == "missing_value":
try:
v = ncin.dtype.type(v)
except Exception:
pass
ncin.createAttribute(k, v)
def copyVariable(ncin, var, data=True, **kwargs):
"""
Arguments
---------
ncin : Instance of an object with a createCopy method
(i.e. NcDataset, NcGroup, NcVariable)
var : Instance of NcVariable
data (optional) : boolean
kwargs : will be passed to createVariable. Allows to set
parameters like chunksizes, deflate_level, ...
Return
------
NcVariable
Purpose
-------
Copy the given variables to ncin. Copy the data if data=True
"""
invardef = var.definition
if data is False:
invardef["chunksizes"] = None
invardef.update(kwargs)
invar = ncin.createVariable(
invardef.pop("name"), invardef.pop("dtype"), **invardef
)
invar.copyAttributes(var.attributes)
if data and var.shape:
invar[:] = var[:]
return invar
def copyVariables(ncin, variables, skip=None, data=True):
"""
Arguments
---------
ncin : Instance of an object with a createCopy method
(i.e. NcDataset, NcGroup, NcVariable)
variables : Dictionary
key : variables name (string)
value : instance of NcVariable
skip (optional) : string or list/tuple of strings
Name(s) of variable(s) to skip
data (optional) : boolean
Return
------
NcVariable
Purpose
-------
Copy the given variables to ncin. Copy the data if data=True
"""
for v in variables.values():
if v.name not in _tupelize(skip):
ncin.copyVariable(v, data)
def createDimensions(ncin, dim_dict):
for name, length in dim_dict.items():
ncin.createDimension(name, length)
def getVariableDefinition(ncvar):
out = ncvar.filters() if ncvar.filters() else {}
out.update({
"name" : ncvar.name,
"dtype" : ncvar.dtype,
"dimensions" : ncvar.dimensions,
"chunksizes" : ncvar.chunking() if not isinstance(ncvar.chunking(), str) else None,
"fill_value" : getattr(ncvar, "_FillValue", None),
})
return out
def getDates(ncin, timesteps=None, timevar="time", units=None, calendar=None):
"""
Arguments
---------
ncin : Instance of an object holding variables (NcDataset/NcGroup)
timesteps (optional) : list/tuple/nd.array of Numerical values.
The time_steps to return dates for. If not given the content of the
entire time variable will be returned.
units (optional) : string
time units following the CF Conventions. Needs to be given, if not
available as an attribute of the time variable.
calendar (optional) : string
calendar name following the CF conventions. Needs to be given, if not
available as an attribute of the time variable.
Return
------
List of datetime objects
Purpose
-------
Return datetime objects associated to the time variable of ncin
"""
var = ncin.variables[timevar]
if not units:
try:
units = var.units
except AttributeError:
raise AttributeError(
"Time variable does not specify an units attribute! Pass as argument."
)
if not calendar:
try:
calendar = var.calendar
except AttributeError:
calendar = "standard"
if not timesteps:
timesteps = var[:]
dates = num2date(timesteps,units,calendar)
try:
return [d.date() for d in dates]
except AttributeError:
return dates
def setFillValue(ncin, value):
"""
Arguments
---------
ncin : Instance of an object with a _FillValue attribute
(i.e. NcVariable)
Return
------
Numeric
Purpose
-------
Return the value of the attribute _FillValue
"""
ncin.setncattr("_FillValue", value)
def getFillValue(ncin):
"""
Arguments
---------
ncin : Instance of an object with a _FillValue attribute
(i.e. NcVariable)
Return
------
Numeric
Purpose
-------
Return the value of the attribute _FillValue
"""
try:
return ncin.getncattr("_FillValue")
except AttributeError:
return None
def setAttribute(ncin, name, value):
"""
Arguments
---------
ncin : Instance of an object with a setncatts method
(i.e. NcDataset/NcGroup/NcVariable)
name : string
value : string or any numeric type
Return
------
None
Purpose
-------
Set/Write the attribute given as name, value
"""
ncin.setncattr(name, value)
def setAttributes(ncin, attdict):
"""
Arguments
---------
ncin : Instance of an object with a setncatts method
(i.e. NcDataset/NcGroup/NcVariable)
attdict : dictionary
key: attribute name (string)
value: attribute value (string or any numeric type)
Return
------
None
Purpose
-------
Set/Write the attributes given in attdict
"""
ncin.setncatts(attdict)
def filterVariables(ncin, dims=None, ndim=None):
"""
Arguments
---------
ncin : Instance of an object with a variables attribute
(i.e. NcDataset/NcGroup)
dims (optional) : tuple/list of dimension strings
ndim (optional) : int number of dimensions
Return
------
OrderedDict:
key : variable name (string)
value : NcVariable instance
Purpose
-------
Return all Variables that are based on the dimension(s) given in dims
and/or have ndims dimensions.
"""
out = OrderedDict()
dims = set(dims or {})
for v in ncin.variables.values():
if dims.issubset(set(v.dimensions)):
if ndim:
if ndim == len(v.dimensions):
out[v.name] = v
else:
out[v.name] = v
return out
def filterDimensions(ncin, lengths):
"""
Arguments
---------
lengths : integer, tuple/list of integers
Return
------
OrderedDict:
key : dimension name (string)
value : NcDimension instance
Purpose
-------
Return all Dimensions with a length given in the argument lengths.
"""
try:
lengths[0]
except TypeError:
lengths = (lengths,)
return OrderedDict(
[(d.name, d) for d in ncin.dimensions.values() if len(d) in lengths]
)
def getGroups(ncin):
out = OrderedDict()
for g in getattr(ncin, "groups").values():
out[g.name] = NcGroup(ncin, g.name, id=g._grpid)
return out
def getVariables(ncin):
out = OrderedDict()
for v in getattr(ncin, "variables").values():
out[v.name] = NcVariable(ncin, v.name, v.dtype, v.dimensions, id=v._varid)
return out
def getAttributes(ncin):
out = OrderedDict()
for k in ncin.ncattrs():
if not k.startswith("_"):
out[k] = ncin.getncattr(k)
return out
def attributeSetter(ncin, name, value):
ncin.__dict__[name] = value
def attributeGetter(ncin, name):
try:
return ncin.__dict__[name]
except KeyError:
try:
return getattr(super(ncin.__class__, ncin), name)
except KeyError:
raise AttributeError("'{:}' object has no attribute '{:}'".format(ncin.__class__, name))
class NcDataset(Dataset):
def __init__(
self,
filename,
mode = "r",
clobber = True,
diskless = False,
persist = False,
weakref = False,
format = "NETCDF4",
):
if filename is None:
# in memory dataset
filename = uuid.uuid4()
diskless = True
super(NcDataset, self).__init__(
filename = filename,
mode = mode,
clobber = clobber,
diskless = diskless,
persist = persist,
weakref = weakref,
format = format,
)
for k, v in zip(self.groups, getGroups(self).values()):
self.groups[k] = v
for k, v in zip(self.variables, getVariables(self).values()):
self.variables[k] = v
def createGroup(self, name):
grp = NcGroup(self, name)
self.groups[name] = grp
return grp
def createVariable(self, *args, **kwargs):
var = NcVariable(self, *args, **kwargs)
self.variables[var.name] = var
return var
def tofile(self, fname):
# preserve dataset options
with NcDataset(fname, "w") as out:
out.copyDataset(self)
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
self.close()
copyDataset = copyDataset
copyDimension = copyDimension
copyDimensions = copyDimensions
copyAttributes = copyAttributes
copyVariable = copyVariable
copyVariables = copyVariables
copyGroup = copyGroup
copyGroups = copyGroups
createAttribute = setAttribute
createAttributes = setAttributes
createDimensions = createDimensions
filterVariables = filterVariables
filterDimensions = filterDimensions
getDates = getDates
attributes = property(fget=getAttributes)
# restore a "normal" attribute access behaviour
# __setattr__ = attributeSetter
# __getattr__ = attributeGetter
class NcGroup(Group):
def __init__(self, *args, **kwargs):
super(NcGroup,self).__init__(*args, **kwargs)
for k, v in zip(self.groups, getGroups(self).values()):
self.groups[k] = v
for k,v in zip(self.variables, getVariables(self).values()):
self.variables[k] = v
def createGroup(self, name):
grp = NcGroup(self, name)
self.groups[name] = grp
return grp
def createVariable(self, *args, **kwargs):
var = NcVariable(self, *args, **kwargs)
self.variables[var.name] = var
return var
copyDimension = copyDimension
copyDimensions = copyDimensions
copyAttributes = copyAttributes
copyVariable = copyVariable
copyVariables = copyVariables
copyGroup = copyGroup
copyGroups = copyGroups
createAttribute = setAttribute
createAttributes = setAttributes
createDimensions = createDimensions
filterVariables = filterVariables
filterDimensions = filterDimensions
getDates = getDates
attributes = property(fget=getAttributes)
# restore a "normal" attribute access behaviour
# __setattr__ = attributeSetter
# __getattr__ = attributeGetter
class NcVariable(Variable):
def __init__(self,*args,**kwargs):
super(NcVariable,self).__init__(*args,**kwargs)
copyAttributes = copyAttributes
createAttribute = setAttribute
createAttributes = setAttributes
attributes = property(fget=getAttributes)
definition = property(fget=getVariableDefinition)
fill_value = property(fget=getFillValue, fset=setFillValue)
# restore a "normal" attribute access behaviour
# __setattr__ = attributeSetter
# __getattr__ = attributeGetter
# Just to be consistent...
NcDimension = Dimension
if __name__ == "__main__":
pass