Utility module

Utility methods in Python.

flatten

cfpylib.util.flatten.flatten(sequence)

FortranFile

Defines a file-derived class to read/write Fortran unformatted files.

The assumption is that a Fortran unformatted file is being written by the Fortran runtime as a sequence of records. Each record consists of an integer (of the default size [usually 32 or 64 bits]) giving the length of the following data in bytes, then the data itself, then the same integer as before.

Examples

To use the default endian and size settings, one can just do::
>>> f = FortranFile('filename')
>>> x = f.readReals()
One can read arrays with varying precisions::
>>> f = FortranFile('filename')
>>> x = f.readInts('h')
>>> y = f.readInts('q')
>>> z = f.readReals('f')

Where the format codes are those used by Python’s struct module.

One can change the default endian-ness and header precision::
>>> f = FortranFile('filename', endian='>', header_prec='l')

for a file with little-endian data whose record headers are long integers.

class cfpylib.util.FortranFile.FortranFile(fname, endian='@', header_prec='i', *args, **kwargs)

File with methods for dealing with fortran unformatted data files

__init__(fname, endian='@', header_prec='i', *args, **kwargs)

Open a Fortran unformatted file for writing.

Parameters:
  • endian – character, optional Specify the endian-ness of the file. Possible values are ‘>’, ‘<’, ‘@’ and ‘=’. See the documentation of Python’s struct module for their meanings. The deafult is ‘>’ (native byte order)
  • header_prec – character, optional Specify the precision used for the record headers. Possible values are ‘h’, ‘i’, ‘l’ and ‘q’ with their meanings from Python’s struct module. The default is ‘i’ (the system’s default integer).
readInts(prec='i')

Read an array of integers.

Parameters:prec – character, optional Specify the precision of the data to be read using character codes from Python’s struct module. Possible values are ‘h’, ‘i’, ‘l’ and ‘q’
readReals(prec='f')

Read in an array of real numbers.

Parameters:prec – character, optional Specify the precision of the array using character codes from Python’s struct module. Possible values are ‘d’ and ‘f’.
readRecord()

Read a single fortran record

readString()

Read a string.

writeInts(ints, prec='i')

Write an array of integers in given precision

Parameters:
  • reals – array Data to write
  • prec – string Character code for the precision to use in writing
writeReals(reals, prec='f')

Write an array of floats in given precision

Parameters:
  • reals – array Data to write
  • prec – string Character code for the precision to use in writing
writeRecord(s)

Write a record with the given bytes.

Parameters:s – the string to write
writeString(s)

Write a string

Parameters:s – the string to write

Table Of Contents

Previous topic

Geometry module

Next topic

gas

This Page