_fortran.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. """
  2. Module to read / write Fortran unformatted sequential files.
  3. This is in the spirit of code written by Neil Martinsen-Burrell and Joe Zuntz.
  4. """
  5. from __future__ import division, print_function, absolute_import
  6. import warnings
  7. import numpy as np
  8. __all__ = ['FortranFile']
  9. class FortranFile(object):
  10. """
  11. A file object for unformatted sequential files from Fortran code.
  12. Parameters
  13. ----------
  14. filename : file or str
  15. Open file object or filename.
  16. mode : {'r', 'w'}, optional
  17. Read-write mode, default is 'r'.
  18. header_dtype : dtype, optional
  19. Data type of the header. Size and endiness must match the input/output file.
  20. Notes
  21. -----
  22. These files are broken up into records of unspecified types. The size of
  23. each record is given at the start (although the size of this header is not
  24. standard) and the data is written onto disk without any formatting. Fortran
  25. compilers supporting the BACKSPACE statement will write a second copy of
  26. the size to facilitate backwards seeking.
  27. This class only supports files written with both sizes for the record.
  28. It also does not support the subrecords used in Intel and gfortran compilers
  29. for records which are greater than 2GB with a 4-byte header.
  30. An example of an unformatted sequential file in Fortran would be written as::
  31. OPEN(1, FILE=myfilename, FORM='unformatted')
  32. WRITE(1) myvariable
  33. Since this is a non-standard file format, whose contents depend on the
  34. compiler and the endianness of the machine, caution is advised. Files from
  35. gfortran 4.8.0 and gfortran 4.1.2 on x86_64 are known to work.
  36. Consider using Fortran direct-access files or files from the newer Stream
  37. I/O, which can be easily read by `numpy.fromfile`.
  38. Examples
  39. --------
  40. To create an unformatted sequential Fortran file:
  41. >>> from scipy.io import FortranFile
  42. >>> f = FortranFile('test.unf', 'w')
  43. >>> f.write_record(np.array([1,2,3,4,5], dtype=np.int32))
  44. >>> f.write_record(np.linspace(0,1,20).reshape((5,4)).T)
  45. >>> f.close()
  46. To read this file:
  47. >>> f = FortranFile('test.unf', 'r')
  48. >>> print(f.read_ints(np.int32))
  49. [1 2 3 4 5]
  50. >>> print(f.read_reals(float).reshape((5,4), order="F"))
  51. [[0. 0.05263158 0.10526316 0.15789474]
  52. [0.21052632 0.26315789 0.31578947 0.36842105]
  53. [0.42105263 0.47368421 0.52631579 0.57894737]
  54. [0.63157895 0.68421053 0.73684211 0.78947368]
  55. [0.84210526 0.89473684 0.94736842 1. ]]
  56. >>> f.close()
  57. Or, in Fortran::
  58. integer :: a(5), i
  59. double precision :: b(5,4)
  60. open(1, file='test.unf', form='unformatted')
  61. read(1) a
  62. read(1) b
  63. close(1)
  64. write(*,*) a
  65. do i = 1, 5
  66. write(*,*) b(i,:)
  67. end do
  68. """
  69. def __init__(self, filename, mode='r', header_dtype=np.uint32):
  70. if header_dtype is None:
  71. raise ValueError('Must specify dtype')
  72. header_dtype = np.dtype(header_dtype)
  73. if header_dtype.kind != 'u':
  74. warnings.warn("Given a dtype which is not unsigned.")
  75. if mode not in 'rw' or len(mode) != 1:
  76. raise ValueError('mode must be either r or w')
  77. if hasattr(filename, 'seek'):
  78. self._fp = filename
  79. else:
  80. self._fp = open(filename, '%sb' % mode)
  81. self._header_dtype = header_dtype
  82. def _read_size(self):
  83. return int(np.fromfile(self._fp, dtype=self._header_dtype, count=1))
  84. def write_record(self, *items):
  85. """
  86. Write a record (including sizes) to the file.
  87. Parameters
  88. ----------
  89. *items : array_like
  90. The data arrays to write.
  91. Notes
  92. -----
  93. Writes data items to a file::
  94. write_record(a.T, b.T, c.T, ...)
  95. write(1) a, b, c, ...
  96. Note that data in multidimensional arrays is written in
  97. row-major order --- to make them read correctly by Fortran
  98. programs, you need to transpose the arrays yourself when
  99. writing them.
  100. """
  101. items = tuple(np.asarray(item) for item in items)
  102. total_size = sum(item.nbytes for item in items)
  103. nb = np.array([total_size], dtype=self._header_dtype)
  104. nb.tofile(self._fp)
  105. for item in items:
  106. item.tofile(self._fp)
  107. nb.tofile(self._fp)
  108. def read_record(self, *dtypes, **kwargs):
  109. """
  110. Reads a record of a given type from the file.
  111. Parameters
  112. ----------
  113. *dtypes : dtypes, optional
  114. Data type(s) specifying the size and endiness of the data.
  115. Returns
  116. -------
  117. data : ndarray
  118. A one-dimensional array object.
  119. Notes
  120. -----
  121. If the record contains a multi-dimensional array, you can specify
  122. the size in the dtype. For example::
  123. INTEGER var(5,4)
  124. can be read with::
  125. read_record('(4,5)i4').T
  126. Note that this function does **not** assume the file data is in Fortran
  127. column major order, so you need to (i) swap the order of dimensions
  128. when reading and (ii) transpose the resulting array.
  129. Alternatively, you can read the data as a 1D array and handle the
  130. ordering yourself. For example::
  131. read_record('i4').reshape(5, 4, order='F')
  132. For records that contain several variables or mixed types (as opposed
  133. to single scalar or array types), give them as separate arguments::
  134. double precision :: a
  135. integer :: b
  136. write(1) a, b
  137. record = f.read_record('<f4', '<i4')
  138. a = record[0] # first number
  139. b = record[1] # second number
  140. and if any of the variables are arrays, the shape can be specified as
  141. the third item in the relevant dtype::
  142. double precision :: a
  143. integer :: b(3,4)
  144. write(1) a, b
  145. record = f.read_record('<f4', np.dtype(('<i4', (4, 3))))
  146. a = record[0]
  147. b = record[1].T
  148. Numpy also supports a short syntax for this kind of type::
  149. record = f.read_record('<f4', '(3,3)<i4')
  150. See Also
  151. --------
  152. read_reals
  153. read_ints
  154. """
  155. dtype = kwargs.pop('dtype', None)
  156. if kwargs:
  157. raise ValueError("Unknown keyword arguments {}".format(tuple(kwargs.keys())))
  158. if dtype is not None:
  159. dtypes = dtypes + (dtype,)
  160. elif not dtypes:
  161. raise ValueError('Must specify at least one dtype')
  162. first_size = self._read_size()
  163. dtypes = tuple(np.dtype(dtype) for dtype in dtypes)
  164. block_size = sum(dtype.itemsize for dtype in dtypes)
  165. num_blocks, remainder = divmod(first_size, block_size)
  166. if remainder != 0:
  167. raise ValueError('Size obtained ({0}) is not a multiple of the '
  168. 'dtypes given ({1}).'.format(first_size, block_size))
  169. if len(dtypes) != 1 and first_size != block_size:
  170. # Fortran does not write mixed type array items in interleaved order,
  171. # and it's not possible to guess the sizes of the arrays that were written.
  172. # The user must specify the exact sizes of each of the arrays.
  173. raise ValueError('Size obtained ({0}) does not match with the expected '
  174. 'size ({1}) of multi-item record'.format(first_size, block_size))
  175. data = []
  176. for dtype in dtypes:
  177. r = np.fromfile(self._fp, dtype=dtype, count=num_blocks)
  178. if dtype.shape != ():
  179. # Squeeze outmost block dimension for array items
  180. if num_blocks == 1:
  181. assert r.shape == (1,) + dtype.shape
  182. r = r[0]
  183. data.append(r)
  184. second_size = self._read_size()
  185. if first_size != second_size:
  186. raise IOError('Sizes do not agree in the header and footer for '
  187. 'this record - check header dtype')
  188. # Unpack result
  189. if len(dtypes) == 1:
  190. return data[0]
  191. else:
  192. return tuple(data)
  193. def read_ints(self, dtype='i4'):
  194. """
  195. Reads a record of a given type from the file, defaulting to an integer
  196. type (``INTEGER*4`` in Fortran).
  197. Parameters
  198. ----------
  199. dtype : dtype, optional
  200. Data type specifying the size and endiness of the data.
  201. Returns
  202. -------
  203. data : ndarray
  204. A one-dimensional array object.
  205. See Also
  206. --------
  207. read_reals
  208. read_record
  209. """
  210. return self.read_record(dtype)
  211. def read_reals(self, dtype='f8'):
  212. """
  213. Reads a record of a given type from the file, defaulting to a floating
  214. point number (``real*8`` in Fortran).
  215. Parameters
  216. ----------
  217. dtype : dtype, optional
  218. Data type specifying the size and endiness of the data.
  219. Returns
  220. -------
  221. data : ndarray
  222. A one-dimensional array object.
  223. See Also
  224. --------
  225. read_ints
  226. read_record
  227. """
  228. return self.read_record(dtype)
  229. def close(self):
  230. """
  231. Closes the file. It is unsupported to call any other methods off this
  232. object after closing it. Note that this class supports the 'with'
  233. statement in modern versions of Python, to call this automatically
  234. """
  235. self._fp.close()
  236. def __enter__(self):
  237. return self
  238. def __exit__(self, type, value, tb):
  239. self.close()