API-Doc

resfo.lazy_read(filelike, fileformat: Format | None = None) Iterator[ResArray]

Reads the contents of an res file and generates the entries of that file. Each entry has a entry.read_keyword() and entry.read_array() method which will return the corresponding data, but only upon request. This requires the user to pay attention to when values are read as it should happen before the file is closed.

When lazy_read is given a path or filename, the file will be closed once the generator has ran out of elements.

For greater control, one can pass an opened file so that close can be called at the correct time.

Parameters:
  • filelike – Either filename, pathlib.Path or stream to write file to. For fileformat=Format.UNFORMATTED the stream must be in binary mode and for fileformat=Format.FORMATTED in text mode.

  • fileformat – Either resfo.Format.FORMATTED for ascii format, resfo.Format.UNFORMATTED for binary formatted files or None for guess.

Raises:

resfo.ResfoParsingError – If the file is not a valid res file.

Note

If given a file to be open (as opposed to a stream), the errors (various IOError s) associated with the default behavior of the built-in open() function may be raised.

When given a stream, the exceptions associated with the stream will pass through.

resfo.read(*args, **kwargs) List[Tuple[str, ReadArrayValue]]

Read the contents of a res file and return a list of tuples (keyword, array). Takes the same parameters as lazy_read, but differs in return type

resfo.write(filelike, contents: Sequence[Tuple[str, ArrayLike | MESS]] | Dict[str, ArrayLike | MESS], fileformat: Format = Format.UNFORMATTED)

Write the given contents to the given file in res format.

Parameters:
  • filelike – Either filename, pathlib.Path or stream to write file to. For fileformat=Format.UNFORMATTED the stream must be in binary mode and for fileformat=Format.FORMATTED in text mode.

  • contents – list or iterable of tuples (kw, arr) where keyword is the keyword, and arr is a numpy arraylike of values. The keyword must have exactly 8 characters and the type of the array will be converted according to resfo.types.to_np_type

  • fileformat – Either resfo.Format.FORMATTED for ascii format or resfo.Format.UNFORMATTED for binary format.

Raises:

resfo.ResfoWriteError – If the given contents cannot be written to an res file.

Note

If given a file to be open (as opposed to a stream), the errors (various IOError s) associated with the default behavior of the built-in open() function may be raised.

When given a stream, the exceptions associated with the stream will pass through.

class resfo.Format(value)

The format of an res file, either FORMATTED for ascii or UNFORMATTED for binary.

class resfo.MESS

The MESS value is a sentinell object used to signal that the type of the array that is to be written should be an empty array of type MESS.

class resfo.array_entry.ResArray(stream)

An array entry in a res file.

This class is not ment to be constructed directly, but rather generated e.g. resfo.lazy_read().

abstract read_array() ReadArrayValue

Read the array from the unformatted res file.

Returns:

numpy array of values.

read_keyword() str

Read the keyword from the res file.

Returns:

The keyword as a 8 character string.

read_length() int

Read the length from the res file.

Returns:

The length of the array in number of entries.

read_type() str | bytes

The type given in the header of the array. In case of unformatted file this will be bytes, for unformatted files it is str.

abstract update(*, keyword: str | None = None, array: ArrayLike | None = None)

Updates the entry with the given new data.

Parameters:
  • new_keyword – The new keyword, must be 8 characters, defaults to no change.

  • new_array – The new array, must be of same type and size as existing array, defaults to no change.

Raises:
  • ValueError – If array does not have the same type or length as existing array.

  • io.UnsupportedOperation – If the underlying stream has been opened with the wrong mode (file must be opened with “r+” in order to correctly update)