Skip to content
16 changes: 13 additions & 3 deletions process/io/mfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,16 @@ def add_to_mfile_variable(self, des, name, value, unit, flag, scan=None):
self.data[var_key] = var
self.data[var_key].set_scan(1, value)

def write_to_json(self, keys_to_write={}, scan=-1, verbose=False):
def write_to_json(self, keys_to_write=None, scan=-1, verbose=False):
"""Write MFILE object to JSON file"""

if keys_to_write == {}:
if keys_to_write is None:
keys_to_write = self.data.keys()

filename = f"{self.filename}.json"

dict_to_write = dict()

if scan == 0:
for i in range(self.data["rmajor"].get_number_of_scans()):
sub_dict = {}
Expand All @@ -363,6 +364,13 @@ def write_to_json(self, keys_to_write={}, scan=-1, verbose=False):
dict_to_write[f"scan-{i+1}"] = sub_dict
else:
for item in keys_to_write:
# Initialize dat_key properly based on the number of scans
if self.data[item].get_number_of_scans() == 1:
dat_key = -1
else:
dat_key = (
scan if scan > 0 else 1
) # Default to scan 1 if not specified
data = self.data[item].get_scan(dat_key)
des = self.data[item].var_description.replace("_", " ")
if verbose:
Expand Down Expand Up @@ -392,8 +400,10 @@ def sort_value(value_words: List[str]) -> Union[str, float]:
# Attempt float conversion of first word
return float(value_words[0])
except ValueError:
# Log the exception with details
logger.exception(f"Can't parse value in MFILE: {value_words}")
raise
# Return the original string as a fallback
return " ".join(value_words).strip()


def sort_brackets(var):
Expand Down
19 changes: 16 additions & 3 deletions process/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from process.buildings import Buildings
from process.costs import Costs
from process.io import plot_proc
from process.io import mfile
from process.plasma_geometry import PlasmaGeom
from process.pulse import Pulse
from process.scan import Scan
Expand Down Expand Up @@ -184,6 +185,12 @@ def parse_args(self, args):
default="MFILE.DAT",
help="mfile for post-processing/plotting",
)
parser.add_argument(
"-mj",
"--mfilejson",
action="store_true",
help="Produce a filled json from --mfile arg in working dir",
)

# If args is not None, then parse the supplied arguments. This is likely
# to come from the test suite when testing command-line arguments; the
Expand All @@ -209,14 +216,20 @@ def post_process(self):
# run, for example.
if self.args.plot:
# Check mfile exists, then plot
mfile = Path(self.args.mfile)
mfile_str = str(mfile.resolve())
if mfile.exists():
mfile_path = Path(self.args.mfile)
mfile_str = str(mfile_path.resolve())
if mfile_path.exists():
# TODO Get --show arg to work: actually show the plot, don't
# just save it
plot_proc.main(args=["-f", mfile_str])
else:
logger.error("mfile to be used for plotting doesn't exist")
if self.args.mfilejson:
# Produce a json file containing mfile output, useful for VVUQ work.
mfile_path = Path(self.args.mfile)
mfile_data = mfile.MFile(filename=mfile_path)
mfile_data.open_mfile()
mfile_data.write_to_json()


class VaryRun:
Expand Down
Loading