# yt.utilities.cython_fortran_utils module¶

class yt.utilities.cython_fortran_utils.FortranFile

Bases: object

This class provides facilities to interact with files written in fortran-record format. Since this is a non-standard file format, whose contents depend on the compiler and the endianness of the machine, caution is advised. This code will assume that the record header is written as a 32bit (4byte) signed integer. The code also assumes that the records use the system’s local endianness.

Notes

Since the assumed record header is an signed integer on 32bit, it will overflow at 2**31=2147483648 elements.

This module has been inspired by scipy’s FortranFile, especially the docstrings.

close()

Close the file descriptor.

This method has no effect if the file is already closed.

read_attrs()

This function reads from that file according to a definition of attributes, returning a dictionary.

Fortran unformatted files provide total bytesize at the beginning and end of a record. By correlating the components of that record with attribute names, we construct a dictionary that gets returned. Note that this function is used for reading sequentially-written records. If you have many written that were written simultaneously.

Parameters

attrs (iterable of iterables) –

This object should be an iterable of one of the formats: [ (attr_name, count, struct type), … ]. [ ((name1,name2,name3), count, vector type] [ ((name1,name2,name3), count, ‘type type type’] [ (attr_name, count, struct type, optional)]

optionalboolean.

If True, the attribute can be stored as an empty Fortran record.

Returns

values – This will return a dict of iterables of the components of the values in the file.

Return type

dict

Examples

>>> header = [ ("ncpu", 1, "i"), ("nfiles", 2, "i") ]
>>> f = FortranFile("fort.3")

read_int()

Reads a single int32 from the file and return it.

Returns

data – The value.

Return type

int32

Examples

>>> f = FortranFile("fort.3")
>>> rv = f.read_vector("d")  # Read a float64 array
>>> rv = f.read_vector("i")  # Read an int32 array

read_vector()

Reads a record from the file and return it as numpy array.

Parameters

d (data type) – This is the datatype (from the struct module) that we should read.

Returns

tr – This is the vector of values read from the file.

Return type

numpy.ndarray

Examples

>>> f = FortranFile("fort.3")
>>> rv = f.read_vector("d")  # Read a float64 array
>>> rv = f.read_vector("i")  # Read an int32 array

seek()

Change stream position.

Parameters
• pos (int) – Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence.

• whence (int) – Determine how pos is interpreted. Can by any of * 0 – start of stream (the default); offset should be zero or positive * 1 – current stream position; offset may be negative * 2 – end of stream; offset is usually negative

Returns

pos – The new absolute position.

Return type

int

skip()

Skip records.

Parameters

n (-) – The number of records to skip

Returns

value – Returns 0 on success.

Return type

int

tell()

Return current stream position.