diff --git a/src/View/Results/Window.py b/src/View/Results/Window.py index ac1f358574b5f89f3f201f909b1813c402c94db3..146847ae3787ebf7216edd75d8adfbe629621355 100644 --- a/src/View/Results/Window.py +++ b/src/View/Results/Window.py @@ -303,7 +303,8 @@ class ResultsWindow(PamhyrWindow): actions = { "action_reload": self._reload, "action_add": self._add_custom_plot, - "action_export": self.export, + # "action_export": self.export, + "action_export": self.export_current, } for action in actions: @@ -591,12 +592,15 @@ class ResultsWindow(PamhyrWindow): ) def export_to(self, directory): + timestamps = sorted(self._results.get("timestamps")) for reach in self._results.river.reachs: - self.export_reach(reach, directory) + self.export_reach(reach, directory, timestamps) - def export_reach(self, reach, directory): + def export_reach(self, reach, directory, timestamps): name = reach.name name = name.replace(" ", "-") + if len(timestamps) == 1: + name = f"{name}_t{timestamps[0]}" file_name = os.path.join( directory, @@ -606,28 +610,40 @@ class ResultsWindow(PamhyrWindow): with open(file_name, 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) - writer.writerow(["name", "rk", "data-file"]) - for profile in reach.profiles: - p_file_name = os.path.join( - directory, - f"cs_{profile.geometry.id}.csv" - ) - - writer.writerow([ - profile.name, - profile.rk, - p_file_name - ]) - - self.export_profile(reach, profile, p_file_name) - - def export_profile(self, reach, profile, file_name): + if len(timestamps) > 1: + writer.writerow(["name", "rk", "data-file"]) + for profile in reach.profiles: + p_file_name = os.path.join( + directory, + f"cs_{profile.geometry.id}.csv" + ) + + writer.writerow([ + profile.name, + profile.rk, + p_file_name + ]) + + self.export_profile(reach, + profile, + p_file_name, + timestamps) + else: + ts = timestamps[0] + writer.writerow(self._table["raw_data"]._headers) + for row in range(self._table["raw_data"].rowCount()): + line = [] + for column in range(self._table["raw_data"].columnCount()): + index = self._table["raw_data"].index(row, column) + line.append(self._table["raw_data"].data(index)) + writer.writerow(line) + + def export_profile(self, reach, profile, file_name, timestamps): with open(file_name, 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) writer.writerow(["timestamp", "z", "q"]) - timestamps = sorted(self._results.get("timestamps")) for ts in timestamps: writer.writerow([ @@ -635,3 +651,13 @@ class ResultsWindow(PamhyrWindow): profile.get_ts_key(ts, "Z"), profile.get_ts_key(ts, "Q"), ]) + + def export_current(self): + self.file_dialog( + select_file=False, + callback=lambda d: self.export_current_to(d[0]) + ) + + def export_current_to(self, directory): + reach = self._results.river.reachs[self._get_current_reach()] + self.export_reach(reach, directory, [self._get_current_timestamp()])