21 """Defines a file-derived class to read/write Fortran unformatted files. 23 The assumption is that a Fortran unformatted file is being written by 24 the Fortran runtime as a sequence of records. Each record consists of 25 an integer (of the default size [usually 32 or 64 bits]) giving the 26 length of the following data in bytes, then the data itself, then the 27 same integer as before. 32 To use the default endian and size settings, one can just do:: 33 >>> f = FortranFile('filename') 36 One can read arrays with varying precisions:: 37 >>> f = FortranFile('filename') 38 >>> x = f.readInts('h') 39 >>> y = f.readInts('q') 40 >>> z = f.readReals('f') 41 Where the format codes are those used by Python's struct module. 43 One can change the default endian-ness and header precision:: 44 >>> f = FortranFile('filename', endian='>', header_prec='l') 45 for a file with little-endian data whose record headers are long 49 __docformat__ =
"restructuredtext en" 56 """File with methods for dealing with fortran unformatted data files""" 58 def _get_header_length(self):
60 _header_length = property(fget=_get_header_length)
62 def _set_endian(self,c):
63 """Set endian to big (c='>') or little (c='<') or native (c='@') 67 The endian-ness to use when reading from this file. 72 raise ValueError(
'Cannot set endian-ness')
73 def _get_endian(self):
75 ENDIAN = property(fset=_set_endian,
77 doc=
"Possible endian values are '<', '>', '@', '='" 80 def _set_header_prec(self, prec):
84 raise ValueError(
'Cannot set header precision')
85 def _get_header_prec(self):
87 HEADER_PREC = property(fset=_set_header_prec,
88 fget=_get_header_prec,
89 doc=
"Possible header precisions are 'h', 'i', 'l', 'q'" 93 """Open a Fortran unformatted file for writing. 97 endian : character, optional 98 Specify the endian-ness of the file. Possible values are 99 '>', '<', '@' and '='. See the documentation of Python's 100 struct module for their meanings. The deafult is '>' (native 102 header_prec : character, optional 103 Specify the precision used for the record headers. Possible 104 values are 'h', 'i', 'l' and 'q' with their meanings from 105 Python's struct module. The default is 'i' (the system's 109 if 'header_prec' in kwargs:
110 header_prec = kwargs[
'header_prec']
111 del(kwargs[
'header_prec'])
114 if 'endian' in kwargs:
115 endian = kwargs[
'endian']
116 del(kwargs[
'endian'])
119 file.__init__(self, fname, *args, **kwargs)
123 def _read_exactly(self, num_bytes):
124 """Read in exactly num_bytes, raising an error if it can't be done.""" 131 read_data = self.read(num_bytes - l)
133 raise IOError(
'Could not read enough data.' 134 ' Wanted %d bytes, got %d.' % (num_bytes, l))
137 def _read_check(self):
142 def _write_check(self, number_of_bytes):
143 """Write the header for the given number of bytes""" 148 """Read a single fortran record""" 153 raise IOError(
'Error reading record from data file')
157 """Write a record with the given bytes. 161 s : the string to write 164 length_bytes = len(s)
178 s : the string to write 183 _real_precisions =
'df' 186 """Read in an array of real numbers. 190 prec : character, optional 191 Specify the precision of the array using character codes from 192 Python's struct module. Possible values are 'd' and 'f'. 196 _numpy_precisions = {
'd': numpy.float64,
201 raise ValueError(
'Not an appropriate precision')
204 num = len(data_str)/struct.calcsize(prec)
205 numbers =struct.unpack(self.
ENDIAN+str(num)+prec,data_str)
206 return numpy.array(numbers, dtype=_numpy_precisions[prec])
209 """Write an array of floats in given precision 216 Character code for the precision to use in writing 219 raise ValueError(
'Not an appropriate precision')
223 length_bytes = len(reals)*struct.calcsize(prec)
227 self.write(struct.pack(_fmt,r))
230 _int_precisions =
'hilq' 233 """Read an array of integers. 237 prec : character, optional 238 Specify the precision of the data to be read using 239 character codes from Python's struct module. Possible 240 values are 'h', 'i', 'l' and 'q' 244 raise ValueError(
'Not an appropriate precision')
247 num = len(data_str)/struct.calcsize(prec)
248 return numpy.array(struct.unpack(self.
ENDIAN+str(num)+prec,data_str))
251 """Write an array of integers in given precision 258 Character code for the precision to use in writing 261 raise ValueError(
'Not an appropriate precision')
265 length_bytes = len(ints)*struct.calcsize(prec)
269 self.write(struct.pack(_fmt,item))
def writeReals(self, reals, prec='f')
def readInts(self, prec='i')
def readReals(self, prec='f')
def __init__(self, fname, args, kwargs)
def _write_check(self, number_of_bytes)
def _read_exactly(self, num_bytes)
def writeInts(self, ints, prec='i')