SciPy

scipy.io.FortranFile.read_record

FortranFile.read_record(dtype=None)[source]

Reads a record of a given type from the file.

Parameters:

dtype : dtype, optional

Data type specifying the size and endiness of the data.

Returns:

data : ndarray

A one-dimensional array object.

See also

read_reals, read_ints

Notes

If the record contains a multi-dimensional array, calling reshape or resize will restructure the array to the correct size. Since Fortran multidimensional arrays are stored in column-major format, this may have some non-intuitive consequences. If the variable was declared as ‘INTEGER var(5,4)’, for example, var could be read with ‘read_record(dtype=np.integer).reshape( (4,5) )’ since Python uses row-major ordering of indices.

One can transpose to obtain the indices in the same order as in Fortran.

For records that contain several variables or mixed types (as opposed to single scalar or array types), it is possible to specify a dtype with mixed types:

record = f.read_record([('a', '<f4'), ('b', '<i4')])
record['a']  # access the variable 'a'

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

record = f.read_record([('a', '<f4'), ('b', '<i4', (3,3))])

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

record = f.read_record('<f4,(3,3)<i4')
record['f0']  # variables are called f0, f1, ...