Skip to content

process_output

OutputFileManager

Source code in process/core/process_output.py
 9
10
11
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
class OutputFileManager:
    @classmethod
    def open_files(cls, output_prefix: str, *, mode="w"):
        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):
        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):
        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):
        cls._outfile.close()
        cls._mfile.close()

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

Source code in process/core/process_output.py
10
11
12
13
14
15
16
17
@classmethod
def open_files(cls, output_prefix: str, *, mode="w"):
    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

Source code in process/core/process_output.py
19
20
21
22
23
24
25
26
27
28
29
@classmethod
def open_idempotence_files(cls, output_prefix: str):
    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

Source code in process/core/process_output.py
31
32
33
34
35
36
37
@classmethod
def close_idempotence_files(cls, output_prefix: str):
    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

Source code in process/core/process_output.py
39
40
41
42
@classmethod
def finish(cls):
    cls._outfile.close()
    cls._mfile.close()

write(file, string)

Source code in process/core/process_output.py
45
46
47
48
49
50
51
def write(file, string: str):
    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)

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
125
126
127
128
129
130
131
132
133
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
136
137
138
139
140
141
142
143
144
145
146
147
148
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
151
152
153
154
155
156
157
158
159
160
161
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='')

Source code in process/core/process_output.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def ovarre(file, descr: str, varnam: str, value, output_flag: str = ""):
    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, bytes):
        # TODO: remove when Fortran is gone
        value = value.decode().strip()
    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
198
199
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
202
203
def ovarrf(file, descr: str, varnam: str, value, output_flag: str = ""):
    ovarre(file, descr, varnam, value, output_flag)

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

Source code in process/core/process_output.py
206
207
def ovarin(file, descr: str, varnam: str, value, output_flag: str = ""):
    ovarre(file, descr, varnam, value, output_flag)

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

Source code in process/core/process_output.py
210
211
def ovarst(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
214
215
def obuild(file, descr: str, thick: float, total: float, variable_name: str = ""):
    write(file, f"{descr:<50}{thick:.3e}{' ':<10}{total:.3e}  {variable_name}")