-
Notifications
You must be signed in to change notification settings - Fork 0
/
pupynere.py
1313 lines (1108 loc) · 47 KB
/
pupynere.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from __future__ import division
from warnings import warn
import logging
u"""
NetCDF reader/writer module.
This module is used to read and create NetCDF files. NetCDF files are
accessed through the `netcdf_file` object. Data written to and from NetCDF
files are contained in `netcdf_variable` objects. Attributes are given
as member variables of the `netcdf_file` and `netcdf_variable` objects.
Notes
-----
NetCDF files are a self-describing binary data format. The file contains
metadata that describes the dimensions and variables in the file. More
details about NetCDF files can be found `here
<http://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html>`_. There
are three main sections to a NetCDF data structure:
1. Dimensions
2. Variables
3. Attributes
The dimensions section records the name and length of each dimension used
by the variables. The variables would then indicate which dimensions it
uses and any attributes such as data units, along with containing the data
values for the variable. It is good practice to include a
variable that is the same name as a dimension to provide the values for
that axes. Lastly, the attributes section would contain additional
information such as the name of the file creator or the instrument used to
collect the data.
When writing data to a NetCDF file, there is often the need to indicate the
'record dimension'. A record dimension is the unbounded dimension for a
variable. For example, a temperature variable may have dimensions of
latitude, longitude and time. If one wants to add more temperature data to
the NetCDF file as time progresses, then the temperature variable should
have the time dimension flagged as the record dimension.
This module implements the Scientific.IO.NetCDF API to read and create
NetCDF files. The same API is also used in the PyNIO and pynetcdf
modules, allowing these modules to be used interchangeably when working
with NetCDF files. The major advantage of this module over other
modules is that it doesn't require the code to be linked to the NetCDF
libraries.
In addition, the NetCDF file header contains the position of the data in
the file, so access can be done in an efficient manner without loading
unnecessary data into memory. It uses the ``mmap`` module to create
Numpy arrays mapped to the data on disk, for the same purpose.
Examples
--------
To create a NetCDF file:
>>> f = netcdf_file('simple.nc', 'w')
>>> f.history = 'Created for a test'
>>> f.location = u'北京'
>>> f.createDimension('time', 10)
>>> time = f.createVariable('time', 'i', ('time',))
>>> time[:] = range(10)
>>> time.units = u'µs since 2008-01-01'
>>> f.close()
Note the assignment of ``range(10)`` to ``time[:]``. Exposing the slice
of the time variable allows for the data to be set in the object, rather
than letting ``range(10)`` overwrite the ``time`` variable.
To read the NetCDF file we just created:
>>> f = netcdf_file('simple.nc', 'r')
>>> print f.history
Created for a test
>>> print f.location
北京
>>> time = f.variables['time']
>>> print time.units
µs since 2008-01-01
>>> print time.shape
(10,)
>>> print time[-1]
9
>>> f.close()
"""
__all__ = ['netcdf_file']
from functools import reduce
from operator import mul
from mmap import mmap, ACCESS_READ
from os import stat
try:
from mmap import ALLOCATIONGRANULARITY
except ImportError:
ALLOCATIONGRANULARITY = None
import numpy as np
from numpy.compat import asbytes, asstr
from numpy import frombuffer, ndarray, dtype, empty, array, asarray
from numpy import little_endian as LITTLE_ENDIAN
logger = logging.getLogger('__name__')
ABSENT = asbytes('\x00\x00\x00\x00\x00\x00\x00\x00')
ZERO = asbytes('\x00\x00\x00\x00')
NC_BYTE = asbytes('\x00\x00\x00\x01')
NC_CHAR = asbytes('\x00\x00\x00\x02')
NC_SHORT = asbytes('\x00\x00\x00\x03')
NC_INT = asbytes('\x00\x00\x00\x04')
NC_FLOAT = asbytes('\x00\x00\x00\x05')
NC_DOUBLE = asbytes('\x00\x00\x00\x06')
NC_DIMENSION = asbytes('\x00\x00\x00\n')
NC_VARIABLE = asbytes('\x00\x00\x00\x0b')
NC_ATTRIBUTE = asbytes('\x00\x00\x00\x0c')
logger = logging.getLogger(__name__)
# Map from Netcdf types and how they should be read. Netcdf is big endian.
def TYPEMAP(nctype):
static = { NC_BYTE: dtype(np.byte),
#NC_CHAR: dtype('c'),
NC_SHORT: dtype(np.int16).newbyteorder('>'),
NC_INT: dtype(np.int32).newbyteorder('>'),
NC_FLOAT: dtype(np.float32).newbyteorder('>'),
NC_DOUBLE: dtype(np.float64).newbyteorder('>'),
}
if nctype in static:
return static[nctype]
elif nctype == NC_CHAR:
return dtype('|S1')
else:
raise Exception("numpy has no corresponding type to NetCDF3 type %s" % nctype)
# Map between Numpy types and the corresponding Netcdf type.
def REVERSE(nptype):
static = { dtype(np.byte): NC_BYTE,
#dtype('c'): NC_CHAR,
dtype('<U1'): NC_INT,
dtype(np.int16): NC_SHORT,
dtype(np.int32): NC_INT,
dtype(np.int64): NC_INT, # will be converted to int32
dtype(np.float32): NC_FLOAT,
dtype(np.float64): NC_DOUBLE,
}
if nptype in static:
return static[nptype]
elif nptype.char == 'S':
return NC_CHAR
else:
raise Exception("NetCDF 3 does not support type %s" % nptype)
class netcdf_file(object):
"""
A file object for NetCDF data.
A `netcdf_file` object has two standard attributes: `dimensions` and
`variables`. The values of both are dictionaries, mapping dimension
names to their associated lengths and variable names to variables,
respectively. Application programs should never modify these
dictionaries.
All other attributes correspond to global attributes defined in the
NetCDF file. Global file attributes are created by assigning to an
attribute of the `netcdf_file` object.
Parameters
----------
filename : string or file-like
string -> filename
mode : {'r', 'w'}, optional
read-write mode, default is 'r'
mmap : None or bool, optional
Whether to mmap `filename` when reading. Default is True
when `filename` is a file name, False when `filename` is a
file-like object
version : {1, 2}, optional
version of netcdf to read / write, where 1 means *Classic
format* and 2 means *64-bit offset format*. Default is 1. See
`here <http://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Which-Format.html>`_
for more info.
maskandscale : True or False
Whether data is automagically scaled and masked.
"""
def __init__(self, filename, mode='r', mmap=None, version=1, maskandscale=False):
"""Initialize netcdf_file from fileobj (str or file-like)."""
self._dims = []
self._recs = 0
self._recsize = 0
if not filename: # Just a metadata object... no reading or writing
self.fp = self.filename = self.mode = mode = None
elif hasattr(filename, 'seek'): # file-like
self.fp = filename
self.filename = 'None'
if mmap is None:
mmap = False
elif mmap and not hasattr(filename, 'fileno'):
raise ValueError('Cannot use file object for mmap')
elif type(filename) == str: # string?
self.filename = filename
self.fp = open(self.filename, '%sb' % mode)
if mmap is None:
mmap = True
else:
raise TypeError('filename argument to netcdf_file.__init__() must be of type string, be file-like (have a "seek" attribute) or be None. Instead received', filename)
self.use_mmap = mmap
self.version_byte = version
self.maskandscale = maskandscale
# FIXME: We need an append mode
if not mode in ('r', 'w', None):
raise ValueError("Mode must be either 'r', 'w', or None.")
self.mode = mode
self.dimensions = {}
self.variables = NcOrderedDict()
self._attributes = {}
# FIXME: Really? Read the entire thing into memory? That seems like a bad choice
if mode == 'r':
self._read()
def __setattr__(self, attr, value):
# Store user defined attributes in a separate dict,
# so we can save them to file later.
try:
self._attributes[attr] = value
except AttributeError:
pass
self.__dict__[attr] = value
def close(self):
"""Closes the NetCDF file."""
if not self.fp:
return
if not self.fp.closed:
try:
self.flush()
finally:
self.fp.close()
__del__ = close
@property
def filesize(self):
"""
For files on disk, returns the size of the file (in bytes)
For virtual files to be generated, returns the expected size of
the resulting file after all data has been streamed through
"""
if self.fp:
return stat(self.fp.name).st_size
if self.recvars:
if not self._recs:
raise ValueError("The number or records is not set so it is impossible to calculate the filesize")
recvar0 = list(self.recvars.values())[0]
if not hasattr(recvar0, '_begin'):
self._calc_begins()
return int(recvar0._begin + (self._recs * self._recsize))
else:
lastvar = list(self.non_recvars.values())[-1]
if not hasattr(lastvar, '_begin'):
self._calc_begins()
return int(lastvar._begin + lastvar._vsize)
def createDimension(self, name, length):
"""
Adds a dimension to the Dimension section of the NetCDF data structure.
Note that this function merely adds a new dimension that the variables can
reference. The values for the dimension, if desired, should be added as
a variable using `createVariable`, referring to this dimension.
Parameters
----------
name : str
Name of the dimension (Eg, 'lat' or 'time').
length : int
Length of the dimension.
See Also
--------
createVariable
"""
if self.dimensions and not length:
raise ValueError('Unlimited dimension must be the first dimension of a netcdf file')
assert type(length) == int or length == None
self.dimensions[name] = length
self._dims.append(name)
def createVariable(self, name, type, dimensions=None, attributes=None):
"""
Create an empty variable for the `netcdf_file` object, specifying its data
type and the dimensions it uses.
Parameters
----------
name : str
Name of the new variable.
type : dtype or str
Data type of the variable.
dimensions : sequence of str
List of the dimension names used by the variable, in the desired order.
attributes : dict, optional
Attribute values (any type) keyed by string names. These attributes
become attributes for the netcdf_variable object.
Returns
-------
variable : netcdf_variable
The newly created ``netcdf_variable`` object.
This object has also been added to the `netcdf_file` object as well.
See Also
--------
createDimension
Notes
-----
Any dimensions to be used by the variable should already exist in the
NetCDF data structure or should be created by `createDimension` prior to
creating the NetCDF variable.
"""
if not dimensions:
dimensions = ()
for dim in dimensions:
if dim not in self.dimensions:
raise ValueError('Cannot create variable with dimension "{0}". The netcdf file\'s dimensions are {1}.'.format(dim, self.dimensions))
shape = tuple([self.dimensions[dim] for dim in dimensions])
shape_ = tuple([dim or 0 for dim in shape]) # replace None with 0 for numpy
isrec = None in shape
if None in shape and shape.index(None) != 0:
raise ValueError("Unlimited dimension must be the first dimensionn to variable %s. Instead got dimension number %d" % (name, shape.index(None)))
if isinstance(type, str):
type = dtype(type)
# Do not allocate an unpopulated numpy array for data on variable initialization.
# Allocate space for data *only* when needed,
# Justification: the standard PCIC usage of this package is solely to generate
# headers for a streamable netCDF file. Variable data is never used and,
# if initialized, will take up large volumes of unnecesary memory.
data = empty(shape_, type) if isrec else None
self.variables[name] = netcdf_variable(
data, type, shape, dimensions,
maskandscale=self.maskandscale,
attributes=attributes, isrec=isrec)
return self.variables[name]
def flush(self):
"""
Perform a sync-to-disk flush if the `netcdf_file` object is in write mode.
See Also
--------
sync : Identical function
"""
if hasattr(self, 'mode') and self.mode == 'w':
self._write()
sync = flush
def recvars(self):
return OrderedDict( filter(lambda kv: kv[1].isrec, self.variables.items()) )
recvars = property(recvars)
def non_recvars(self):
return OrderedDict( filter(lambda kv: not kv[1].isrec, self.variables.items()) )
non_recvars = property(non_recvars)
def __generate__(self):
self._calc_begins()
yield self._header()
for chunk in self._data():
yield chunk
def _header(self):
return asbytes('CDF') + \
array(self.version_byte, '>b').tobytes() + \
self._numrecs() + \
self._dim_array() + \
self._gatt_array() + \
self._var_array()
def _write(self):
self.fp.writelines(self.__generate__())
def _data(self):
if self.variables:
for var in self.variables.values():
if var.data is None:
raise ValueError("Cannot write variable with unallocated data")
if (var.data.dtype.byteorder == '<' or
(var.data.dtype.byteorder == '=' and LITTLE_ENDIAN)):
var.data = var.data.byteswap()
for var in self.non_recvars.values():
yield var.data.tobytes()
count = var.data.size * var.itemsize
yield asbytes('0') * (var._vsize - count)
# Record variables
for i in range(self._recs):
for var in self.recvars.values():
stride = var.data[i,:] if len(var.data.shape) > 1 else var.data[i]
yield stride.tobytes()
def _calc_begins(self):
'''Each netcdf variable has a metadata item named 'begin' which is an offset to the location in
the file where the data values begin. This method calculates the offset for each variable and
sets it in the variable property _begin
'''
prev = False
for name, var in self.variables.items():
if not prev:
var.__dict__['_begin'] = len(self._header())
else:
var.__dict__['_begin'] = prev._begin + prev._vsize
prev = var
def set_numrecs(self, numrecs):
assert type(numrecs) == int
self.__dict__['_recs'] = numrecs
def _numrecs(self):
if self._recs == 0:
# Get highest record count from all record variables.
self.__dict__['_recs'] = 0 if not self.recvars else max([ len(var.data) for var in self.recvars.values() ])
return self._pack_int(self._recs)
def _dim_array(self):
if self.dimensions:
buf = NC_DIMENSION + self._pack_int(len(self.dimensions))
for name in self._dims:
buf += self._pack_string(name)
length = self.dimensions[name]
buf += self._pack_int(length or 0) # replace None with 0 for record dimension
return buf
else:
return ABSENT
def _gatt_array(self):
return self._att_array(self._attributes)
def _att_array(self, attributes):
if attributes:
buf = NC_ATTRIBUTE
buf += self._pack_int(len(attributes))
for name, values in attributes.items():
buf += self._pack_string(name)
buf += self._values(values)
return buf
else:
return ABSENT
def _var_array(self):
if self.variables:
buf = NC_VARIABLE
buf += self._pack_int(len(self.variables))
# Set the metadata for all variables.
for name in self.variables.keys():
buf += self._var_metadata(name)
# Now that we have the metadata, we know the vsize of
# each record variable, so we can calculate recsize.
if self.recvars:
logger.debug([(key, v.dtype, v.shape) for key, v in self.recvars.items()])
recsize = sum([
var._vsize for var in self.recvars.values()
])
# From the spec: "A special case: Where there is
# exactly one record variable, we drop the restriction
# that each record be four-byte aligned, so in this
# case there is no record padding."
if len(self.recvars) > 1:
padding = recsize % 4
else:
padding = 0
logger.debug("Recsize %d padding %d total %d", recsize, padding, recsize + padding)
self.__dict__['_recsize'] = recsize + padding
else:
self.__dict__['_recsize'] = 0
return buf
else:
logger.debug("_var_array returning ABSENT")
return ABSENT
def _var_metadata(self, name):
'''
Returns a string representing a single 'var' from the BNF grammar here:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf/File-Format-Specification.html
'''
var = self.variables[name]
# name
buf = self._pack_string(name)
# nelems
buf += self._pack_int(len(var.dimensions))
# dimid ...
for dimname in var.dimensions:
dimid = self._dims.index(dimname)
buf += self._pack_int(dimid)
# vatt_array
buf += self._att_array(var._attributes)
# nc_type
nc_type = REVERSE(var.dtype)
buf += asbytes(nc_type)
# vsize
if not var.isrec:
vsize = var.size * var.itemsize
vsize += -vsize % 4
else: # record variable: vsize is the amount of space per record
# The record size is calculated as the sum of the vsize's of the
# record variables
try:
vsize = np.prod(var.shape[1:]) * var.itemsize
vsize += vsize % 4
except IndexError:
vsize = 0
warn("Could not determine vsize for variable", name, "so I'm defaulting to 0")
self.variables[name].__dict__['_vsize'] = vsize
# But according to "Note on vsize:" from NetCDF spec, vsize is:
# a) redundant, since it can be determined from other header info
# b) insufficient, since it's only 32 bits and files can be > 4GB
# therefore, clip it... the spec made me do it!
buf += array(min(vsize, 2**32 - 4), 'int32').tobytes()
# begin
# Pack a bogus begin, if it hasn't been calculated yet
if hasattr(self.variables[name], '_begin'):
buf += self._pack_begin(self.variables[name]._begin)
else:
buf += self._pack_begin(0)
return buf
# FIXME: This is a little messy... the try/except blocks don't really express the program logic in a good way
def _values(self, values):
# FIXME: what exactly do we do for arrays of characters? e.g. ['mary', 'had', 'a', 'little', 'lamb']
# I think that they are illegal, actually
if type(values) == list:
raise ValueError("I don't know how to handle a python list type. 'values' parameter should be a numpy array.")
try:
nc_type = REVERSE(values.dtype)
except:
# FIXME: if REVERSE(values.dtype) raises an attribute error then so will this!
types = [
(int, NC_INT),
(float, NC_FLOAT),
(str, NC_CHAR),
]
try:
sample = values[0]
except (IndexError, TypeError):
sample = values
if isinstance(sample, str):
if not isinstance(values, str):
raise ValueError("NetCDF requires that text be encoded as UTF-8")
values = values.encode('utf-8')
for class_, nc_type in types:
if isinstance(sample, class_): break
# FIXME: raise exception here? If we get to this point... nc_type is undefined
# Special case for character types
# numpy.asarray will detect the length for character types and set dtype accordingly
if nc_type == NC_CHAR:
values = asarray(values)
nelems = values.itemsize
else:
values = asarray(values, TYPEMAP(nc_type))
nelems = values.size
buf = asbytes(nc_type)
buf += self._pack_int(nelems)
if not values.shape and (values.dtype.byteorder == '<' or
(values.dtype.byteorder == '=' and LITTLE_ENDIAN)):
values = values.byteswap()
buf += values.tobytes()
count = values.size * values.itemsize
buf += asbytes('0') * (-count % 4) # pad
return buf
def _read(self):
# Check magic bytes and version
magic = self.fp.read(3)
if not magic == asbytes('CDF'):
raise TypeError("Error: %s is not a valid NetCDF 3 file" %
self.filename)
self.__dict__['version_byte'] = frombuffer(self.fp.read(1), '>b')[0]
# Read file headers and set data.
self._read_numrecs()
self._read_dim_array()
self._read_gatt_array()
self._read_var_array()
def _read_numrecs(self):
self.__dict__['_recs'] = self._unpack_int()
def _read_dim_array(self):
header = self.fp.read(4)
if not header in [ZERO, NC_DIMENSION]:
raise ValueError("Unexpected header.")
count = self._unpack_int()
for dim in range(count):
name = asstr(self._unpack_string())
length = self._unpack_int() or None # None for record dimension
self.dimensions[name] = length
self._dims.append(name) # preserve order
def _read_gatt_array(self):
for k, v in self._read_att_array().items():
self.__setattr__(k, v)
def _read_att_array(self):
header = self.fp.read(4)
if not header in [ZERO, NC_ATTRIBUTE]:
raise ValueError("Unexpected header.")
if header == ZERO:
more = self.fp.read(4)
assert more == ZERO
return {}
count = self._unpack_int()
attributes = {}
for attr in range(count):
name = asstr(self._unpack_string())
attributes[name] = self._read_values()
return attributes
def _read_var_array(self):
header = self.fp.read(4)
if not header in [ZERO, NC_VARIABLE]:
raise ValueError("Unexpected header.")
if header == ZERO:
more = self.fp.read(4)
assert more == ZERO
return
records = 0
dtypes = {'names': [], 'formats': []}
rec_vars = []
count = self._unpack_int()
for var in range(count):
name, dimensions, shape, attributes, type, start, vsize = self._read_var()
# http://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html
# Note that vsize is the product of the dimension lengths
# (omitting the record dimension) and the number of bytes
# per value (determined from the type), increased to the
# next multiple of 4, for each variable. If a record
# variable, this is the amount of space per record. The
# netCDF "record size" is calculated as the sum of the
# vsize's of all the record variables.
#
# The vsize field is actually redundant, because its value
# may be computed from other information in the header. The
# 32-bit vsize field is not large enough to contain the size
# of variables that require more than 2^32 - 4 bytes, so
# 2^32 - 1 is used in the vsize field for such variables.
isrec = shape and shape[0] is None
if isrec: # record variable
rec_vars.append(name)
# The netCDF "record size" is calculated as the sum of
# the vsize's of all the record variables.
self.__dict__['_recsize'] += vsize
# Store the position where record arrays start.
if records == 0:
records = start
dtypes['names'].append(name)
dtypes['formats'].append(str(shape[1:]) + '>' + type.char)
# Handle padding with a virtual variable.
if type.char in 'bch':
actual_size = reduce(mul, (1,) + shape[1:]) * type.itemsize
padding = -actual_size % 4
if padding:
dtypes['names'].append('_padding_%d' % var)
dtypes['formats'].append('(%d,)>b' % padding)
# Data will be set later.
data = None
else: # not a record variable
# Calculate size to avoid problems with vsize (above)
size = reduce(mul, shape, 1) * type.itemsize
pos = self.fp.tell()
if self.use_mmap:
if ALLOCATIONGRANULARITY:
pages = start // ALLOCATIONGRANULARITY
offset = pages * ALLOCATIONGRANULARITY
start = start % ALLOCATIONGRANULARITY
mm = mmap(self.fp.fileno(), start+size, access=ACCESS_READ, offset=offset)
else:
mm = mmap(self.fp.fileno(), start+size, access=ACCESS_READ)
data = ndarray.__new__(ndarray, shape, dtype=type,
buffer=mm, offset=start, order='C')
else:
self.fp.seek(start)
data = frombuffer(self.fp.read(size), type)
data.shape = shape
self.fp.seek(pos)
# Add variable.
self.variables[name] = netcdf_variable(
data, type, shape, dimensions, attributes,
maskandscale=self.maskandscale, isrec=isrec)
if rec_vars:
dtypes['formats'] = [f.replace('()', '').replace(' ', '') for f in dtypes['formats']]
# Remove padding when only one record variable.
if len(rec_vars) == 1:
dtypes['names'] = dtypes['names'][:1]
dtypes['formats'] = dtypes['formats'][:1]
# Build rec array.
pos = self.fp.tell()
if self.use_mmap:
if ALLOCATIONGRANULARITY:
pages = records // ALLOCATIONGRANULARITY
offset = pages * ALLOCATIONGRANULARITY
records = records % ALLOCATIONGRANULARITY
mm = mmap(self.fp.fileno(), records+self._recs*self._recsize, access=ACCESS_READ, offset=offset)
else:
mm = mmap(self.fp.fileno(), records+self._recs*self._recsize, access=ACCESS_READ)
rec_array = ndarray.__new__(ndarray, (self._recs,), dtype=dtypes,
buffer=mm, offset=records, order='C')
else:
self.fp.seek(records)
rec_array = frombuffer(self.fp.read(self._recs*self._recsize), dtype=dtypes)
rec_array.shape = (self._recs,)
self.fp.seek(pos)
for var in rec_vars:
self.variables[var].__dict__['data'] = rec_array[var]
def _read_var(self):
name = asstr(self._unpack_string())
dimensions = []
shape = []
dims = self._unpack_int()
for i in range(dims):
dimid = self._unpack_int()
dimname = self._dims[dimid]
dimensions.append(dimname)
dim = self.dimensions[dimname]
shape.append(dim)
dimensions = tuple(dimensions)
shape = tuple(shape)
attributes = self._read_att_array()
nc_type = self.fp.read(4)
vsize = int(frombuffer(self.fp.read(4), 'int32')[0])
start = [self._unpack_int, self._unpack_int64][self.version_byte-1]()
type = TYPEMAP(nc_type)
return name, dimensions, shape, attributes, type, start, vsize
def _read_values(self):
nc_type = self.fp.read(4)
n = self._unpack_int()
type = TYPEMAP(nc_type)
count = n*type.itemsize
values = self.fp.read(int(count))
self.fp.read(-count % 4) # read padding
if type.char not in ('S', 'a'):
values = frombuffer(values, type)
if values.shape == (1,): values = values[0]
else:
## text values are encoded via UTF-8, per NetCDF standard
values = values.rstrip(asbytes('\x00')).decode('utf-8', 'replace')
return values
def _pack_begin(self, begin):
if self.version_byte == 1:
return self._pack_int(begin)
elif self.version_byte == 2:
return self._pack_int64(begin)
def _pack_int(self, value):
return array(value, '>i').tobytes()
_pack_int32 = _pack_int
def _unpack_int(self):
return int(frombuffer(self.fp.read(4), '>i')[0])
_unpack_int32 = _unpack_int
def _pack_int64(self, value):
return array(value, '>q').tobytes()
def _unpack_int64(self):
return frombuffer(self.fp.read(8), '>q')[0]
def _pack_string(self, s):
count = len(s)
pad = asbytes('0') * (-count % 4)
return self._pack_int(count) + asbytes(s) + pad
def _unpack_string(self):
count = self._unpack_int()
s = self.fp.read(count).rstrip(asbytes('\x00'))
self.fp.read(-count % 4) # read padding
return s
class netcdf_variable(object):
"""
A data object for the `netcdf` module.
`netcdf_variable` objects are constructed by calling the method
`netcdf_file.createVariable` on the `netcdf_file` object. `netcdf_variable`
objects behave much like array objects defined in numpy, except that their
data resides in a file. Data is read by indexing and written by assigning
to an indexed subset; the entire array can be accessed by the index ``[:]``
or (for scalars) by using the methods `getValue` and `assignValue`.
`netcdf_variable` objects also have attribute `shape` with the same meaning
as for arrays, but the shape cannot be modified. There is another read-only
attribute `dimensions`, whose value is the tuple of dimension names.
All other attributes correspond to variable attributes defined in
the NetCDF file. Variable attributes are created by assigning to an
attribute of the `netcdf_variable` object.
Parameters
----------
data : array_like
The data array that holds the values for the variable.
Not initialized until data is assigned to it, to save memory.
type: numpy dtype
Desired data-type for the data array.
shape : sequence of ints
The shape of the array. This should match the lengths of the
variable's dimensions.
dimensions : sequence of strings
The names of the dimensions used by the variable. Must be in the
same order of the dimension lengths given by `shape`.
attributes : dict, optional
Attribute values (any type) keyed by string names. These attributes
become attributes for the netcdf_variable object.
maskandscale: True or False
Whether data is automagically scaled and masked.
isrec: True or False
Whether this is a record variable (first dimension unlimited)
Attributes
----------
dimensions : list of str
List of names of dimensions used by the variable object.
isrec, shape
Properties
See also
--------
isrec, shape
"""
def __init__(self, data, type, shape, dimensions, attributes=None, maskandscale=False, isrec=False):
self.dtype = type
self._shape = shape
self.dimensions = dimensions
self.maskandscale = maskandscale
self._isrec = isrec
self.__dict__["data"] = data
self._attributes = attributes or {}
for k, v in self._attributes.items():
self.__dict__[k] = v
def __setattr__(self, attr, value):
# Data isn't allocated until an assignment is made
# (for memory-saving purposes)
# so if we're assigning data, we need to allocate it first.
if attr == "data" and not self._data_allocated():
if not self.isrec:
# we know what size data array we'll need, allocate it.
self._allocate_data()
else:
# record variable has unknown shape, since new records can be
# added. But the user did variable[:] = array or similar
# so the contents of the variable are just the value argument.
self.__dict__["data"] = value
return
# Store user defined attributes in a separate dict,
# so we can save them to file later.
try:
self._attributes[attr] = value
except AttributeError:
pass
self.__dict__[attr] = value
def isrec(self):
"""Returns whether the variable has a record dimension or not.
A record dimension is a dimension along which additional data could be
easily appended in the netcdf data structure without much rewriting of
the data file. This attribute is a read-only property of the
`netcdf_variable`.
"""
return self._isrec
isrec = property(isrec)
def shape(self):
"""Returns the shape tuple of the data variable.
This is a read-only attribute and can not be modified in the
same manner of other numpy arrays.
"""
return self._shape if not self._data_allocated() else self.data.shape
shape = property(shape)
def getValue(self):
"""
Retrieve a scalar value from a `netcdf_variable` of length one.
Raises
------
ValueError
If the netcdf variable is an array of length greater than one,
this exception will be raised.
"""
if not self._data_allocated():
raise ValueError("Cannot access unallocated data")
return self.data.item()
def assignValue(self, value):
"""
Assign a scalar value to a `netcdf_variable` of length one.
Parameters
----------
value : scalar
Scalar value (of compatible type) to assign to a length-one netcdf
variable. This value will be written to file.
Raises
------
ValueError
If the input is not a scalar, or if the destination is not a length-one
netcdf variable.
"""
if not self._data_allocated():
self._allocate_data()
if not self.data.flags.writeable:
# Work-around for a bug in NumPy. Calling itemset() on a read-only
# memory-mapped array causes a seg. fault.
# See NumPy ticket #1622, and SciPy ticket #1202.
# This check for `writeable` can be removed when the oldest version
# of numpy still supported by scipy contains the fix for #1622.
raise RuntimeError("variable is not writeable")
self.data.itemset(value)
def typecode(self):
"""
Return the typecode of the variable.
Returns
-------