yt.utilities.fortran_utils module

Utilities for reading Fortran files.

class yt.utilities.fortran_utils.file[source]

Bases: object

yt.utilities.fortran_utils.peek_record_size(f, endian='=')[source]

This function accept the file handle and returns the size of the next record and then rewinds the file to the previous position.

Parameters:
  • f (File object) – An open file object. Should have been opened in mode rb.
  • endian (str) – ‘=’ is native, ‘>’ is big, ‘<’ is little endian
Returns:

Return type:

Number of bytes in the next record

yt.utilities.fortran_utils.read_attrs(f, attrs, endian='=')[source]

This function accepts a file pointer and reads from that file pointer 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, see read_record.

Parameters:
  • f (File object) – An open file object. Should have been opened in mode rb.
  • 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’]
  • endian (str) – ‘=’ is native, ‘>’ is big, ‘<’ is little endian
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 = open("fort.3", "rb")
>>> rv = read_attrs(f, header)
yt.utilities.fortran_utils.read_cattrs(f, attrs, endian='=')[source]

This function accepts a file pointer to a C-binary file and reads from that file pointer according to a definition of attributes, returning a dictionary.

This function performs very similarly to read_attrs, except it does not add on any record padding. It is thus useful for using the same header types as in read_attrs, but for C files rather than Fortran.

Parameters:
  • f (File object) – An open file object. Should have been opened in mode rb.
  • 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’]
  • endian (str) – ‘=’ is native, ‘>’ is big, ‘<’ is little endian
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 = open("cdata.bin", "rb")
>>> rv = read_cattrs(f, header)
yt.utilities.fortran_utils.read_record(f, rspec, endian='=')[source]

This function accepts a file pointer and reads from that file pointer a single “record” with different components.

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.

Parameters:
  • f (File object) – An open file object. Should have been opened in mode rb.
  • rspec (iterable of iterables) – This object should be an iterable of the format [ (attr_name, count, struct type), ... ].
  • endian (str) – ‘=’ is native, ‘>’ is big, ‘<’ is little endian
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 = open("fort.3", "rb")
>>> rv = read_record(f, header)
yt.utilities.fortran_utils.read_vector(f, d, endian='=')[source]

This function accepts a file pointer and reads from that file pointer a vector of values.

Parameters:
  • f (File object) – An open file object. Should have been opened in mode rb.
  • d (data type) – This is the datatype (from the struct module) that we should read.
  • endian (str) – ‘=’ is native, ‘>’ is big, ‘<’ is little endian
Returns:

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

Return type:

numpy.ndarray

Examples

>>> f = open("fort.3", "rb")
>>> rv = read_vector(f, 'd')
yt.utilities.fortran_utils.skip(f, n=1, endian='=')[source]

This function accepts a file pointer and skips a Fortran unformatted record. Optionally check that the skip was done correctly by checking the pad bytes.

Parameters:
  • f (File object) – An open file object. Should have been opened in mode rb.
  • n (int) – Number of records to skip.
  • endian (str) – ‘=’ is native, ‘>’ is big, ‘<’ is little endian
Returns:

skipped

Return type:

The number of elements in the skipped array

Examples

>>> f = open("fort.3", "rb")
>>> skip(f, 3)