diff --git a/ess/TraceWin.py b/ess/TraceWin.py index eda0eafac9984f370ffee927657f12d64acfc9b4..e6842124532e22058fd44998b9562754ae7cadb9 100644 --- a/ess/TraceWin.py +++ b/ess/TraceWin.py @@ -1529,16 +1529,23 @@ class project: hexlist = textwrap.wrap(f.read().hex(), 2) for key in self._refdict: o = self._refdict[key] - current = "".join(hexlist[o[0] : o[0] + o[2]]) - if o[1] == "bool": - if current == o[3]: - self._dict[key] = False - elif current == o[4]: - self._dict[key] = True - else: - raise ValueError(f"Wrong setting found for {key}, found {current}, expected one of {o[3:]}") - elif o[1] in ["d", "f", "i"]: - self._dict[key] = struct.unpack(o[1], b"".fromhex(current))[0] + if o[1] == "bool:list": + vals = [struct.unpack("?", b"".fromhex(hexlist[i]))[0] for i in o[0]] + if vals.count(True) != 1: + raise ValueError(f"Did not find {key} to be set correctly") + self._dict[key] = o[-1][vals.index(True)] + else: + current = "".join(hexlist[o[0] : o[0] + o[2]]) + if o[1] == "bool": + # TODO I think we can simplify here by using unpack of type "?" + if current == o[3]: + self._dict[key] = False + elif current == o[4]: + self._dict[key] = True + else: + raise ValueError(f"Wrong setting found for {key}, found {current}, expected one of {o[3:]}") + elif o[1] in ["d", "f", "i"]: + self._dict[key] = struct.unpack(o[1], b"".fromhex(current))[0] def print_settings(self, settings=None): """ @@ -1561,6 +1568,20 @@ class project: """ return self._dict[parameter] + def get_type(self, parameter): + """ + Get the type of parameter + as specified in the reference file + """ + return self._refdict[parameter][1] + + def get_options(self, parameter): + """ + Get the possible options for parameter + as specified in the reference file + """ + return self._refdict[parameter][-1] + def set(self, parameter, value): """ Set the new value for parameter @@ -1572,6 +1593,10 @@ class project: elif isinstance(current, (float, int)): if not isinstance(value, (float, int)): raise ValueError(f"{parameter} should be a number") + elif self.get_type(parameter) == "bool:list": + opts = self.get_options(parameter) + if value not in opts: + raise ValueError(f"{parameter} should be one of {opts}") self._dict[parameter] = value def save(self, fname=None): @@ -1592,15 +1617,25 @@ class project: for key in self._dict: o = self._refdict[key] v = self._dict[key] - if isinstance(v, bool): - if v: - v = [o[-1]] - else: - v = [o[-2]] + + if o[1] == "bool:list": + for i, val in zip(o[0], o[-1]): + if v == val: + t = True + else: + t = False + hexlist[i] = struct.pack("?", t).hex() else: - v = wrap(struct.pack(o[1], v).hex(), 2) - for i in range(len(v)): - hexlist[o[0] + i] = v[i] + if isinstance(v, bool): + # TODO Think we can simplify this by using pack of type ? + if v: + v = [o[-1]] + else: + v = [o[-2]] + else: + v = wrap(struct.pack(o[1], v).hex(), 2) + for i in range(len(v)): + hexlist[o[0] + i] = v[i] open(fname, "wb").write(bytes.fromhex("".join(hexlist))) diff --git a/ess/data/tw_project_file_reverse_engineered.json b/ess/data/tw_project_file_reverse_engineered.json index 4e22f43347be38915dac1e458cde60503cf8ce5d..f953f852bc34fe0b57920583e5fbd53fc4dd6edc 100644 --- a/ess/data/tw_project_file_reverse_engineered.json +++ b/ess/data/tw_project_file_reverse_engineered.json @@ -93,5 +93,31 @@ 12580, "d", 8 + ], + "mp:partr_sc_opt_routine": [ + [ + 119, + 125, + 12607, + 22704 + ], + "bool:list", + 1, + [ + "picnic", + "picnir", + "ce_cyl", + "my_sc" + ] + ], + "mp:partr_sc_calcstep": [ + 12616, + "i", + 4 + ], + "mp:partr_sc_scstep": [ + 9876, + "i", + 4 ] }