Skip to content

process_output

Handles writing output to PROCESS MFile/OUTFile.

OutputFileManager

Manages the opening of regular/idempotence output files.

Source code in process/core/process_output.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class OutputFileManager:
    """Manages the opening of regular/idempotence output files."""

    @classmethod
    def open_files(cls, output_prefix: str, *, mode="w"):
        """Setup the handlers for MFile and OUTFile for writing."""
        cls._outfile = open(  # noqa: SIM115
            Path(output_prefix + "OUT.DAT"), mode
        )
        cls._mfile = open(  # noqa: SIM115
            Path(output_prefix + "MFILE.DAT"), mode
        )

    @classmethod
    def open_idempotence_files(cls, output_prefix: str):
        """Setup the handlers for idempotence MFile and OUTFile for writing."""
        cls._outfile.close()
        cls._mfile.close()

        cls._outfile = open(  # noqa: SIM115
            Path(output_prefix + "IDEM_OUT.DAT"), "w"
        )
        cls._mfile = open(  # noqa: SIM115
            Path(output_prefix + "IDEM_MFILE.DAT"), "w"
        )

    @classmethod
    def close_idempotence_files(cls, output_prefix: str):
        """Removes idempotence output files, closes the handler,
        and opens the main output files.
        """
        Path(cls._outfile.name).unlink()
        Path(cls._mfile.name).unlink()
        cls._outfile.close()
        cls._mfile.close()
        cls.open_files(mode="a", output_prefix=output_prefix)

    @classmethod
    def finish(cls):
        """Closes the file handlers."""
        cls._outfile.close()
        cls._mfile.close()

open_files(output_prefix, *, mode='w') classmethod

Setup the handlers for MFile and OUTFile for writing.

Source code in process/core/process_output.py
15
16
17
18
19
20
21
22
23
@classmethod
def open_files(cls, output_prefix: str, *, mode="w"):
    """Setup the handlers for MFile and OUTFile for writing."""
    cls._outfile = open(  # noqa: SIM115
        Path(output_prefix + "OUT.DAT"), mode
    )
    cls._mfile = open(  # noqa: SIM115
        Path(output_prefix + "MFILE.DAT"), mode
    )

open_idempotence_files(output_prefix) classmethod

Setup the handlers for idempotence MFile and OUTFile for writing.

Source code in process/core/process_output.py
25
26
27
28
29
30
31
32
33
34
35
36
@classmethod
def open_idempotence_files(cls, output_prefix: str):
    """Setup the handlers for idempotence MFile and OUTFile for writing."""
    cls._outfile.close()
    cls._mfile.close()

    cls._outfile = open(  # noqa: SIM115
        Path(output_prefix + "IDEM_OUT.DAT"), "w"
    )
    cls._mfile = open(  # noqa: SIM115
        Path(output_prefix + "IDEM_MFILE.DAT"), "w"
    )

close_idempotence_files(output_prefix) classmethod

Removes idempotence output files, closes the handler, and opens the main output files.

Source code in process/core/process_output.py
38
39
40
41
42
43
44
45
46
47
@classmethod
def close_idempotence_files(cls, output_prefix: str):
    """Removes idempotence output files, closes the handler,
    and opens the main output files.
    """
    Path(cls._outfile.name).unlink()
    Path(cls._mfile.name).unlink()
    cls._outfile.close()
    cls._mfile.close()
    cls.open_files(mode="a", output_prefix=output_prefix)

finish() classmethod

Closes the file handlers.

Source code in process/core/process_output.py
49
50
51
52
53
@classmethod
def finish(cls):
    """Closes the file handlers."""
    cls._outfile.close()
    cls._mfile.close()

write(file, string)

Writes a string to the given file identifier.

Raises:

Type Description
ProcessValueError

The file is not recognised as an MFile, OUTFile, or terminal.

Source code in process/core/process_output.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def write(file, string: str):
    """Writes a string to the given file identifier.

    Raises
    ------
    ProcessValueError
        The file is not recognised as an MFile, OUTFile, or terminal.
    """
    if file == constants.MFILE:
        OutputFileManager._mfile.write(f"{string}\n")
    elif file == constants.NOUT:
        OutputFileManager._outfile.write(f"{string}\n")
    elif file == constants.IOTTY:
        print(string)
    else:
        error_msg = f"Unknown file identifier {file}, it is not recognised as either an MFile, OUTFile, or terminal."
        raise ProcessValueError(error_msg)

ocentr(file, string, width, *, character='*')

Write a centred header within a line of characters to a file

Parameters:

Name Type Description Default
file

the integer unit of the file

required
string str

the heading text

required
width int

the desired with of the header

required
character

the character to pad the heading with () (Default value = "")

'*'
Source code in process/core/process_output.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def ocentr(file, string: str, width: int, *, character="*"):
    """Write a centred header within a line of characters to a file

    Parameters
    ----------
    file :
        the integer unit of the file
    string :
        the heading text
    width :
        the desired with of the header
    character :
        the character to pad the heading with (*) (Default value = "*")

    """
    write(file, f"{f' {string} ':{character}^{width}}")
    write(constants.MFILE, f"# {string} #")

ostars(file, width, *, character='*')

Write a line of characters to a file

Parameters:

Name Type Description Default
file

the integer unit of the file

required
width int

the desired with of the line

required
character

the character to fill the line with () (Default value = "")

'*'
Source code in process/core/process_output.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def ostars(file, width: int, *, character="*"):
    """Write a line of characters to a file

    Parameters
    ----------
    file :
        the integer unit of the file
    width :
        the desired with of the line
    character :
        the character to fill the line with (*) (Default value = "*")

    """
    write(file, character * width)

oheadr(file, string, *, width=110, character='*')

Write a centred header within a line of characters between two blank lines

Parameters:

Name Type Description Default
file

the integer unit of the file

required
string str

the heading text

required
width int

the desired with of the header

110
character

the character to pad the heading with () (Default value = "")

'*'
Source code in process/core/process_output.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def oheadr(file, string: str, *, width: int = 110, character="*"):
    """Write a centred header within a line of characters between two blank lines

    Parameters
    ----------
    file :
        the integer unit of the file
    string :
        the heading text
    width :
        the desired with of the header
    character :
        the character to pad the heading with (*) (Default value = "*")
    """
    oblnkl(file)
    ocentr(file, string, width, character=character)
    oblnkl(file)

oshead(file, string, *, width=80, character='*')

Write a short centred header within a line of characters between two blank lines

Parameters:

Name Type Description Default
file

the integer unit of the file

required
string str

the heading text

required
width int

the desired with of the header

80
character

the character to pad the heading with () (Default value = "")

'*'
Source code in process/core/process_output.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def oshead(file, string: str, *, width: int = 80, character="*"):
    """Write a short centred header within a line of characters between two blank lines

    Parameters
    ----------
    file :
        the integer unit of the file
    string :
        the heading text
    width :
        the desired with of the header
    character :
        the character to pad the heading with (*) (Default value = "*")
    """
    oheadr(file, string, width=width, character=character)

oblnkl(file)

Write a blank line to a file

Parameters:

Name Type Description Default
file

the integer unit of the file

required
Source code in process/core/process_output.py
146
147
148
149
150
151
152
153
154
def oblnkl(file):
    """Write a blank line to a file

    Parameters
    ----------
    file :
        the integer unit of the file
    """
    write(file, " ")

osubhd(file, string)

Write a subheading between two blank lines

Parameters:

Name Type Description Default
file

the integer unit of the file

required
string

the heading text

required
Source code in process/core/process_output.py
157
158
159
160
161
162
163
164
165
166
167
168
169
def osubhd(file, string):
    """Write a subheading between two blank lines

    Parameters
    ----------
    file :
        the integer unit of the file
    string :
        the heading text
    """
    oblnkl(file)
    write(file, string)
    oblnkl(file)

ocmmnt(file, string)

Write a comment to a file

Parameters:

Name Type Description Default
file

the integer unit of the file

required
string str

the comment text

required
Source code in process/core/process_output.py
172
173
174
175
176
177
178
179
180
181
182
def ocmmnt(file, string: str):
    """Write a comment to a file

    Parameters
    ----------
    file :
        the integer unit of the file
    string :
        the comment text
    """
    write(file, string)

ovarre(file, descr, varnam, value, output_flag='')

Write out a variable to a file via its identifier.

Source code in process/core/process_output.py
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
def ovarre(file, descr: str, varnam: str, value, output_flag: str = ""):
    """Write out a variable to a file via its identifier."""
    replacement_character = "_"
    if file != constants.MFILE:
        replacement_character = " "

    description = f"{descr:<72}".replace(" ", replacement_character)
    varname = f"{varnam:<30}".replace(" ", replacement_character)

    if isinstance(value, np.ndarray):
        value = value.item()
    if isinstance(value, str):
        # try and convert the value to a float
        # if it fails, leave as a string
        with suppress(ValueError):
            value = float(value)

    format_value = f"{value:.17e}" if isinstance(value, float) else f"{value: >12}"

    # TODO need to find a way to identify iteration variables at a higher level
    # in the data structure
    # if varnam.strip("()") in numerics.name_xc:
    #     # MDK add ITV label if it is an iteration variable
    #     # The ITV flag overwrites the output_flag
    #     output_flag = "ITV"

    line = f"{description}{replacement_character} {varname}{replacement_character} {format_value} {output_flag}"
    write(file, line)
    if file != constants.MFILE:
        ovarre(constants.MFILE, descr, varnam, value, output_flag)

ocosts(file, varnam, descr, value)

Source code in process/core/process_output.py
217
218
def ocosts(file, varnam: str, descr: str, value):
    ovarre(file, descr, varnam, value)

ovarrf(file, descr, varnam, value, output_flag='')

Source code in process/core/process_output.py
221
222
def ovarrf(file, descr: str, varnam: str, value, output_flag: str = ""):
    ovarre(file, descr, varnam, value, output_flag)

obuild(file, descr, thick, total, variable_name='')

Source code in process/core/process_output.py
225
226
def obuild(file, descr: str, thick: float, total: float, variable_name: str = ""):
    write(file, f"{descr:<50}{thick:.3e}{' ':<10}{total:.3e}  {variable_name}")