From 747b62411d1254ebf6f90c109e6d7fe82d26cf3a Mon Sep 17 00:00:00 2001 From: "Lange, Christian M" Date: Mon, 28 Apr 2025 21:01:41 -0400 Subject: [PATCH] Update TerascanLogic.save_dage() I added a function written by ChatGPT. I have not tested it yet. --- src/qudi/logic/terascan_logic.py | 49 +++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/qudi/logic/terascan_logic.py b/src/qudi/logic/terascan_logic.py index b2cb5e8..9a32fa3 100644 --- a/src/qudi/logic/terascan_logic.py +++ b/src/qudi/logic/terascan_logic.py @@ -1,4 +1,6 @@ +import os +import re import numpy as np import time from PySide2 import QtCore @@ -324,22 +326,43 @@ def clear_data(self): self._curr_wavelength = 0.0 self._time_since_last_wl_change = time.perf_counter() - - # TODO: Clean this up. I don't like the logic. + @QtCore.Slot() def save_data(self) -> None: - ds = TextDataStorage( - root_dir=self.save_dir, # Use the configurable save directory - column_formats='.15e' - ) - array = np.column_stack(( + """ + Save self.valid_wavelength_data and self.valid_counts_data to a new + 8-digit, zero-padded .dat file in self.save_dir, e.g. '00000001.dat'. + """ + # 1) Make sure directory exists + os.makedirs(self.save_dir, exist_ok=True) + + # 2) Find existing files of form '########.dat' + existing = [] + for fn in os.listdir(self.save_dir): + if re.fullmatch(r'\d{8}\.dat', fn): + existing.append(int(fn[:8])) + # 3) Determine next index + next_idx = max(existing) + 1 if existing else 1 + filename = f"{next_idx:08d}.dat" + + # 4) Stack wavelength and counts into an NĂ—2 array + data = np.vstack([ self.valid_wavelength_data, - self.valid_counts_data, - self.ttl_data, - self.wavelength_data, - self.counts_data, - )) - ds.save_data(array) + self.valid_counts_data + ]).T # shape (N, 2) + + # 5) Instantiate storage and save + storage = TextDataStorage( + root_dir=self.save_dir, + comments='# ', + delimiter='\t', + file_extension='.dat', + column_formats=('.8f', '.15e'), + include_global_metadata=False + ) + # save_data returns (file_path, timestamp, (rows, columns)) + storage.save_data(data, filename=filename) + def __update_data(self):