From 374360150f641eae79e69d8d78f68bf3b8cad455 Mon Sep 17 00:00:00 2001 From: CPD Date: Mon, 17 Mar 2025 08:35:38 +0100 Subject: [PATCH] Add option to save as pkl --- cpdctrl_gui/ui/main_window.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/cpdctrl_gui/ui/main_window.py b/cpdctrl_gui/ui/main_window.py index 2116a71..f70e630 100644 --- a/cpdctrl_gui/ui/main_window.py +++ b/cpdctrl_gui/ui/main_window.py @@ -359,14 +359,33 @@ class MainWindow(QMainWindow): """ if self.data_collector is None: raise RuntimeError("Can not save, not data collector initialized") - csv = self.data_collector.to_csv() - # open file dialog - filename = self.data_collector.dirname + ".csv" - file_name, _ = QFileDialog.getSaveFileName(self, "Save File", filename, "CSV Files (*.csv)") - if file_name: - with open(file_name, "w") as f: - f.write(csv) - self.set_status(f"Saved data to {file_name}") + # if not loaded already, this will load the data into memory, which might take a while + # this should be done before the dialog + self.data_collector.get_data() + # open file dialog with previous in directory with previous file extension + last_dir = AppConfig.MAIN_CFG.get_or("tmp_last_measurement_save_dir", "") + ext = AppConfig.MAIN_CFG.get_or("tmp_last_measurement_save_ext", "csv") + if ext not in ["csv", "pkl"]: + ext = "csv" + # data_collector.dirname gets the name, not path + init_path = os.path.join(last_dir, self.data_collector.dirname + "." + ext) + file_path, _ = QFileDialog.getOpenFileName(self, "Save File", init_path, "CSV Files (*.csv);;Pickle Files (*.pkl)") + if file_path: + AppConfig.MAIN_CFG.set("tmp_last_measurement_load_dir", os.path.dirname(file_path)) + if file_path.endswith(".csv"): + AppConfig.MAIN_CFG.set("tmp_last_measurement_save_ext", "csv") + csv = self.data_collector.to_csv() + with open(file_path, "w") as f: + f.write(csv) + elif file_path.endswith(".pkl"): + AppConfig.MAIN_CFG.set("tmp_last_measurement_save_ext", "pkl") + data = self.data_collector.get_data() + import pickle + with open(file_path, "wb") as f: + pickle.dump(data, f) + else: + raise ValueError(f"Unknown file extension in path: '{file_path}'") + self.set_status(f"Saved data to {file_path}") else: self.set_status(f"Aborted saving data, no file selected")