_fileutils#

The file _fileutils.py contains utility functions for reading/writing EQDSK files. This reference material is intended for developers, and users are not recommended to use these functions directly.

_fileutils.read_array(fh, fmt)#

Reads from a Fortran formatted ASCII data file. It is assumed that the array is flattened and stored in Fortran order (column-major). Information is read from a file handle until the requested array is filled.

Parameters:
  • shape (Union[int, str, _SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, float, complex, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – The shape of the array to return. If provided as an int, a 1D array is returned of length shape. If passed the string "all", will read until the end of the file.

  • fh (TextIO) – File handle. Should be in a text read mode, i.e. open(filename, "r").

  • fmt (str) – A Fortran format string, such as '(5e16.9)'

Raises:
  • ValueError – If shape is not a scalar, a 1D iterable, or the string "all". Also raised if attempting to read a line which does not match the supplied format.

  • EOFError – If encountering end-of-file while reading an array, and shape if not "all"

Warns:

UserWarning – When reading an array over multiple lines, raises a warning if the array reaches the requested size but there are still elements on the last line. These elements will be discarded, and the filehandle will move on the next line.

Return type:

ndarray

_fileutils.write_array(fh, fmt)#

Writes to a Fortran formatted ASCII data file. The provided array is flattened and written in Fortran order (column-major). Information is written to a file handle until the requested array is written. The file handle will be left on a newline.

Parameters:
  • arr (Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – The array to write.

  • fh (TextIO) – File handle. Should be in a text write mode, i.e. open(filename, "w").

  • fmt (str) – A Fortran IO format string, such as '(5e16.9)'.

Return type:

None

class freeqdsk._fileutils.ChunkOutput(filehandle, chunksize=5, extraspaces=0)#

Outputs values in lines, inserting newlines when needed.

Parameters:
  • filehandle (TextIO) – Output to write to.

  • chunksize (int (default: 5)) – Number of values per line.

  • extraspaces (int (default: 0)) – Number of extra spaces between outputs

__enter__()#

Entry point for with statements.

__exit__(exc_type, exc_value, traceback)#

Exit point for with statements.

Ensures that the chunk finishes with a new line.

__init__(filehandle, chunksize=5, extraspaces=0)#
__weakref__#

list of weak references to the object (if defined)

endblock()#

Make sure next block of data is on new line.

Return type:

None

newline()#

Ensure that the file is at the start of a new line. If the file is already at a newline, does nothing.

Return type:

None

write(value)#

Write a value to the output, adding a newline if needed

Distinguishes between: - list : Iterates over the list and writes each element - int : Converts using str - float : Converts using f2s to Fortran-formatted string

Parameters:

value (Union[int, float, List[Any]]) – Either a single int or float, or a list to output. If provided with a list, the function is called recursively on each element.

Return type:

None

_fileutils.f2s()#

Format a string containing a float.

Positive floats require a extra space in front to ensure that positive and negative values have the same width.

Parameters:

f (float) – A single float to be converted to a string.

Return type:

str

_fileutils.write_1d(out)#

Writes a 1D variable to a ChunkOutput file handle.

Parameters:
  • values (Iterable[Any]) – List of values to write.

  • out (ChunkOutput) – File handle managed by a ChunkOutput object.

Return type:

None

_fileutils.write_2d(out)#

Writes a 2D array to a ChunkOutput file handle.

Note that this transposes the array, looping over the first index fastest

Parameters:
  • values (Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) – List of values to write.

  • out (ChunkOutput) – File handle managed by a ChunkOutput object.

Raises:

ValueError – If values is not a 2D array.

Return type:

None

_fileutils.next_value()#

A generator which yields values from a file handle.

Checks if the value is a float or int, returning the correct type depending on whether ‘.’ is in the string.

Parameters:

fh (TextIO) – File handle for text file to be read.

Yields:

Union[int, float] – Yields either an int or a float, depending on whether the string contains a decimal point.

Return type:

Generator[Union[int, float], None, None]