SciPy

scipy.io.FortranFile.read_record

FortranFile.read_record(self, *dtypes, **kwargs)[source]

Reads a record of a given type from the file.

Parameters
*dtypesdtypes, optional

Data type(s) specifying the size and endiness of the data.

Returns
datandarray

A one-dimensional array object.

Notes

If the record contains a multi-dimensional array, you can specify the size in the dtype. For example:

INTEGER var(5,4)

can be read with:

read_record('(4,5)i4').T

Note that this function does not assume the file data is in Fortran column major order, so you need to (i) swap the order of dimensions when reading and (ii) transpose the resulting array.

Alternatively, you can read the data as a 1D array and handle the ordering yourself. For example:

read_record('i4').reshape(5, 4, order='F')

For records that contain several variables or mixed types (as opposed to single scalar or array types), give them as separate arguments:

double precision :: a
integer :: b
write(1) a, b

record = f.read_record('<f4', '<i4')
a = record[0]  # first number
b = record[1]  # second number

and if any of the variables are arrays, the shape can be specified as the third item in the relevant dtype:

double precision :: a
integer :: b(3,4)
write(1) a, b

record = f.read_record('<f4', np.dtype(('<i4', (4, 3))))
a = record[0]
b = record[1].T

Numpy also supports a short syntax for this kind of type:

record = f.read_record('<f4', '(3,3)<i4')

Previous topic

scipy.io.FortranFile.read_reals

Next topic

scipy.io.FortranFile.write_record