From 0bf410e14f4906793a8c9091e2c05026ee31f6fd Mon Sep 17 00:00:00 2001 From: CPD Date: Mon, 17 Mar 2025 16:06:35 +0100 Subject: [PATCH] Add store led script after measurement --- cpdctrl/led_script.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/cpdctrl/led_script.py b/cpdctrl/led_script.py index df509b8..85405cb 100644 --- a/cpdctrl/led_script.py +++ b/cpdctrl/led_script.py @@ -292,6 +292,8 @@ class LedScript: raise InvalidScriptUpdate(f"Cannot update row {idx+1} the script only has {n_rows} rows") if not (led >= 0 and led <= 100): raise InvalidScriptUpdate(f"Cannot update row {idx+1} because the led value {led}% is not in the range of 0%-100%") + if not (dt > 0): + raise InvalidScriptUpdate(f"Cannot update row {idx+1} because the dt value '{dt}' <= 0") if self.current_dt: current_idx = self.get_current_index(self.current_dt) if current_idx < idx: @@ -453,4 +455,38 @@ class LedScript: if ignore_errors: return states, errors return states + + @staticmethod + def script_to_file(script: np.ndarray, add_default_header=True, add_extra_header=[]): + """Convert a script array to a script text file.""" + lines = [] + if add_default_header: + lines.append("# Led script file automatically generated at the end of the measurement.") + lines.append("# It may contain changes made during the measurement, which means") + lines.append("# it might not represented the actual values during the measurement.") + lines.append(f"# Duration: {int(script['dtsum'][-1])} s") + lines.append(f"# Number of steps: {script['dtsum'].shape[0]}") + for line in add_extra_header: + if line.startswith("#"): + lines.append(line) + else: + lines.append(f"# {line}") + lines.append("# Time LED") + for i in range(script['dt'].shape[0]): + lines.append(f"{split_seconds_into_units_str(int(script['dt'][i]))} {int(script['led'][i])}") + return '\n'.join(lines) + + def to_file(self, add_default_header=True, add_extra_header=[]): + """Convert the script to a script text file.""" + return LedScript.script_to_file(self.script, add_default_header, add_extra_header) +def split_seconds_into_units_str(t: int): + """Split seconds into hours, minutes, seconds.""" + h = t // 3600 + m = (t % 3600) // 60 + s = t % 60 + ret = "" + if h != 0: ret += f"{h}h" + if m != 0: ret += f"{m}m" + if len(ret) == 0 or s != 0: ret += f"{s}s" + return ret \ No newline at end of file