From 4e1acfecdcce7fd732bb65be2b34df3a9f299e76 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Mon, 3 Nov 2025 14:49:12 +0100 Subject: [PATCH] Results: Refacto read csv. --- src/View/Results/Window.py | 74 ++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/src/View/Results/Window.py b/src/View/Results/Window.py index 0004152c..a5b2d89d 100644 --- a/src/View/Results/Window.py +++ b/src/View/Results/Window.py @@ -1231,7 +1231,6 @@ class ResultsWindow(PamhyrWindow): return def import_data(self): - file_types = [ self._trad["file_csv"], self._trad["file_all"], @@ -1248,29 +1247,38 @@ class ResultsWindow(PamhyrWindow): if filename == "": return - sep = " " + x, y = self.read_csv_file_data(filename) + data = self.read_csv_file_format(x, y) + self.read_csv_file_update_plot(data) - def is_float(string): - if string.replace(".", "").isnumeric(): - return True - else: - return False + def read_csv_file_data(self, filename): + sep = "," + x = [] + y = [] with open(filename, 'r', newline='') as f: lines = f.readlines() - x = [] - y = [] for line in lines: if line[0] != "*" and line[0] != "#" and line[0] != "$": row = line.split(sep) - if len(row) >= 2: - if is_float(row[0]) and is_float(row[1]): - x.append(float(row[0])) - y.append(float(row[1])) + if len(row) >= 2: + try: + fx, fy = float(row[0]), float(row[1]) + x.append(fx) + y.append(fy) + except: + continue + + return x, y + + def read_csv_file_format(self, x, y): data_type_lst = ['Q(t)', 'Z(t)', 'Z(x)'] data_type, ok = QInputDialog.getItem( - self, 'Data type', 'Chose the type of data:', data_type_lst) + self, 'Data type', + 'Chose the type of data:', + data_type_lst + ) if not ok: return @@ -1291,25 +1299,37 @@ class ResultsWindow(PamhyrWindow): tmp_unit = {'Z': ' (m)', 'Q': ' (m³/s)'} - data = {'type_x': tmp_dict[data_type[2]], - 'type_y': tmp_dict[data_type[0]], - 'legend': legend, - 'unit': tmp_unit[data_type[0]], - 'x': x, - 'y': y} + data = { + 'type_x': tmp_dict[data_type[2]], + 'type_y': tmp_dict[data_type[0]], + 'legend': legend, + 'unit': tmp_unit[data_type[0]], + 'x': x, 'y': y + } - if data_type == 'Z(x)': - line = self.canvas_2.axes.plot(x, y, marker="+", - label=legend + ' (m)') + return data + + def read_csv_file_update_plot(self, data): + x, y = data['x'], data['y'] + legend = data['legend'] + unit = data['unit'] + + if data['type_x'] == 'water_elevation' and data['type_y'] == 'time': + line = self.canvas_2.axes.plot( + x, y, marker="+", + label=legend + ' ' + unit + ) self.plot_rkc.canvas.draw_idle() self.plot_rkc.update_idle() - if data_type == 'Q(t)': - line = self.canvas_4.axes.plot(x, y, marker="+", - label=legend + ' (m³/s)') + if data['type_x'] == 'discharge' and data['type_y'] == 'time': + line = self.canvas_4.axes.plot( + x, y, marker="+", + label=legend + ' ' + unit + ) self.plot_h._line.append(line) self.plot_h.enable_legend() self.plot_h.canvas.draw_idle() - self.plot_h.update_idle + self.plot_h.update_idle() for p in self._additional_plot: self._additional_plot[p].add_imported_plot(data)