Skip to content

mfile_to_csv

Code to read from a PROCESS MFILE and write values into a csv

Input files: mfile (default MFILE.DAT) as output from PROCESS variable list (default mfile_to_csv_vars.json) as defined by user

Instructions: - command line call: python mfile_to_csv.py -f -v

Output file: .csv will be saved to the directory of the input file

parse_args(args)

Parse supplied arguments.

Parameters:

Name Type Description Default
args (list, None)

arguments to parse

required

Returns:

Type Description
Namespace

parsed arguments

Source code in process/core/io/mfile_to_csv.py
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
54
55
56
57
58
59
def parse_args(args):
    """Parse supplied arguments.

    Parameters
    ----------
    args : list, None
        arguments to parse

    Returns
    -------
    Namespace
        parsed arguments
    """
    parser = argparse.ArgumentParser(
        description="Read from a PROCESS MFILE and write values into a csv."
    )
    parser.add_argument(
        "-f",
        "--mfile",
        type=str,
        default="MFILE.DAT",
        help="Specify input mfile name, default = MFILE.DAT",
    )
    parser.add_argument(
        "-v",
        "--varfile",
        type=str,
        default="mfile_to_csv_vars.json",
        help="Specify file holding variable names, default = mfile_to_csv_vars.json",
    )

    return parser.parse_args(args)

get_vars(vfile='mfile_to_csv_vars.json')

Returns variable names from identified file.

Parameters:

Name Type Description Default
args string

input JSON filename

required
vfile

(Default value = "mfile_to_csv_vars.json")

'mfile_to_csv_vars.json'

Returns:

Type Description
list

variable names

Source code in process/core/io/mfile_to_csv.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def get_vars(vfile="mfile_to_csv_vars.json"):
    """Returns variable names from identified file.

    Parameters
    ----------
    args : string
        input JSON filename
    vfile :
         (Default value = "mfile_to_csv_vars.json")

    Returns
    -------
    list
        variable names
    """
    print("Fetching list of variables from", vfile)

    return json.loads(Path(vfile).read_text())["vars"]

read_mfile(mfilename='MFILE.DAT', variables=None)

Returns specified variable values from identified file.

Parameters:

Name Type Description Default
args (string, list)

input filename, variable names

required
mfilename

(Default value = "MFILE.DAT")

'MFILE.DAT'
variables

(Default value = None)

None

Returns:

Type Description
list of tuples

variable descriptions, names, and values

Source code in process/core/io/mfile_to_csv.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def read_mfile(mfilename="MFILE.DAT", variables=None):
    """Returns specified variable values from identified file.

    Parameters
    ----------
    args : string, list
        input filename, variable names
    mfilename :
         (Default value = "MFILE.DAT")
    variables :
         (Default value = None)

    Returns
    -------
    list of tuples
        variable descriptions, names, and values
    """
    if variables is None:
        variables = []
    print("Reading from MFILE:", mfilename)

    m_file = MFile(mfilename)

    output_vars = []

    # for each variable named in the input varfile, get the description and data value
    for var_name in variables:
        if var_name not in m_file.data:
            print(f"Variable '{var_name}' not in MFILE. Skipping and moving on...")
        else:
            # In case of a file containing multiple scans, (scan = -1) uses the last scan value
            var_val = m_file.data[var_name].get_scan(-1)
            description = m_file.data[var_name].var_description
            var_data = (description, var_name, var_val)
            output_vars.append(var_data)

    return output_vars

get_savenamepath(mfilename='MFILE.DAT')

Returns path/filename.csv for file saving.

Parameters:

Name Type Description Default
args string

input filename

required
mfilename

(Default value = "MFILE.DAT")

'MFILE.DAT'

Returns:

Type Description
PurePosixPath

output filename

Source code in process/core/io/mfile_to_csv.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def get_savenamepath(mfilename="MFILE.DAT"):
    """Returns path/filename.csv for file saving.

    Parameters
    ----------
    args : string
        input filename
    mfilename :
         (Default value = "MFILE.DAT")

    Returns
    -------
    pathlib.PurePosixPath
        output filename
    """

    # Either save it locally or output the csv file to the directory of the input file
    dirname = Path.cwd() if mfilename == "MFILE.DAT" else PurePath(mfilename).parent

    csv_filename = PurePath(mfilename).stem
    return PurePath(dirname, csv_filename + ".csv")

write_to_csv(csv_outfile, output_data=None)

Write to csv file.

Parameters:

Name Type Description Default
args string, list of tuples

input filename, variable data

required
csv_outfile
required
output_data

(Default value = None)

None
Source code in process/core/io/mfile_to_csv.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def write_to_csv(csv_outfile, output_data=None):
    """Write to csv file.

    Parameters
    ----------
    args : string, list of tuples
        input filename, variable data
    csv_outfile :

    output_data :
         (Default value = None)
    """
    if output_data is None:
        output_data = []
    with open(csv_outfile, "w") as csv_file:
        print("Writing to csv file:", csv_outfile)
        writer = csv.writer(csv_file, delimiter=",")
        writer.writerow(["Description", "Varname", "Value"])

        for vardesc in output_data:
            writer.writerow(vardesc)

main(args=None)

Extract certain variables from an MFILE.DAT and output to CSV.

Parameters:

Name Type Description Default
args list

optional command-line args for testing, defaults to None

None
Source code in process/core/io/mfile_to_csv.py
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
def main(args=None):
    """Extract certain variables from an MFILE.DAT and output to CSV.

    Parameters
    ----------
    args : list, optional
        optional command-line args for testing, defaults to None
    """
    # read from command line inputs
    args = parse_args(args)

    # read list of required variables from input json file
    jvars = get_vars(args.varfile)

    # read required data from input mfile
    output_data = read_mfile(args.mfile, jvars)

    # identify save location
    output_file = get_savenamepath(args.mfile)

    # write to csv
    write_to_csv(output_file, output_data)

    # write final line to screen
    print("Complete.")