diff --git a/src/Model/Geometry/ProfileXYZ.py b/src/Model/Geometry/ProfileXYZ.py index 82ef682c..41381a04 100644 --- a/src/Model/Geometry/ProfileXYZ.py +++ b/src/Model/Geometry/ProfileXYZ.py @@ -749,8 +749,8 @@ class ProfileXYZ(Profile, SQLSubModel): if zz[i] >= z and zz[i+1] < z: y = np.interp( z, - [zz[i], zz[i+1]], - [station[i], station[i+1]] + [zz[i+1], zz[i]], + [station[i+1], station[i]] ) line.append([y, z]) @@ -839,11 +839,8 @@ class ProfileXYZ(Profile, SQLSubModel): for i in range(self.number_points-1): if zz[i] > z and zz[i+1] <= z: - y = np.interp( - z, - [zz[i], zz[i+1]], - [station[i], station[i+1]] - ) + fact = (z - zz[i]) / (zz[i+1] - zz[i]) + y = station[i] + fact * (station[i+1] - station[i]) start.append(y) end = [] @@ -852,11 +849,8 @@ class ProfileXYZ(Profile, SQLSubModel): for i in reversed(range(self.number_points-1)): if zz[i] <= z and zz[i+1] > z: - y = np.interp( - z, - [zz[i], zz[i+1]], - [station[i], station[i+1]] - ) + fact = (z - zz[i]) / (zz[i+1] - zz[i]) + y = station[i] + fact * (station[i+1] - station[i]) end.append(y) if len(start) != len(end): @@ -887,33 +881,25 @@ class ProfileXYZ(Profile, SQLSubModel): # Interpolate points at river left side if (i_left > 0): - x = np.interp( - z, - [self.point(i_left).z, self.point(i_left - 1).z], - [self.point(i_left).x, self.point(i_left - 1).x] - ) - y = np.interp( - z, - [self.point(i_left).z, self.point(i_left - 1).z], - [self.point(i_left).y, self.point(i_left - 1).y] - ) - pt_left = PointXYZ(x, y, z, name="wl_left") + fact = (z - self.point(i_left).z) / (self.point(i_left - 1).z + - self.point(i_left).z) + x = self.point(i_left).x + fact * (self.point(i_left - 1).x + - self.point(i_left).x) + y = self.point(i_left).y + fact * (self.point(i_left - 1).y + - self.point(i_left).y) + pt_left = PointXYZ(x=x, y=y, z=z, name="wl_left") else: pt_left = self.point(0) # Interpolate points at river right side if (i_right < self.number_points - 1): - x = np.interp( - z, - [self.point(i_right).z, self.point(i_right + 1).z], - [self.point(i_right).x, self.point(i_right + 1).x] - ) - y = np.interp( - z, - [self.point(i_right).z, self.point(i_right + 1).z], - [self.point(i_right).y, self.point(i_right + 1).y] - ) - pt_right = PointXYZ(x, y, z, name="wl_right") + fact = (z - self.point(i_right).z) / (self.point(i_right + 1).z - + self.point(i_right).z) + x = self.point(i_right).x + fact * (self.point(i_right + 1).x - + self.point(i_right).x) + y = self.point(i_right).y + fact * (self.point(i_right + 1).y - + self.point(i_right).y) + pt_right = PointXYZ(x=x, y=y, z=z, name="wl_right") else: pt_right = self.point(self.number_points - 1) diff --git a/src/View/Results/CustomPlot/Plot.py b/src/View/Results/CustomPlot/Plot.py index 64e09f66..e30e5442 100644 --- a/src/View/Results/CustomPlot/Plot.py +++ b/src/View/Results/CustomPlot/Plot.py @@ -599,13 +599,7 @@ class CustomPlot(PamhyrPlot): self.lines["wet_area"] = line # Legend - lns = reduce( - lambda acc, line: acc + line, - map(lambda line: self.lines[line], self.lines), - [] - ) - labs = list(map(lambda line: self._trad[line], self.lines)) - self.canvas.axes.legend(lns, labs, loc="best") + self.update_legend() def _redraw_rk(self): results = self.data[self._current_res_id] @@ -1103,13 +1097,7 @@ class CustomPlot(PamhyrPlot): self._customize_x_axes_time(ts) # Legend - lns = reduce( - lambda acc, line: acc + line, - map(lambda line: self.lines[line], self.lines), - [] - ) - labs = list(map(lambda line: self._trad[line], self.lines)) - self.canvas.axes.legend(lns, labs, loc="best") + self.update_legend() def _redraw_time(self): @@ -1419,3 +1407,25 @@ class CustomPlot(PamhyrPlot): x = self._timestamp self._current.set_data([x, x], self.canvas.axes.get_ylim()) self.canvas.draw_idle() + + def update_legend(self): + lns = reduce( + lambda acc, line: acc + line, + map(lambda line: self.lines[line], self.lines), + [] + ) + labs = list(map(lambda line: self._trad[line], self.lines)) + self.canvas.axes.legend(lns, labs, loc="best") + + def add_imported_plot(self, data): + if (data["type_x"] == self._x and + data["type_y"] in self._y): + while data["legend"] in self.lines: + data["legend"] += '*' + self._trad._dict[data["legend"]] = data["legend"] + data["unit"] + self.lines[data["legend"]] = self.canvas.axes.plot(data["x"], + data["y"], + marker="+", + linestyle="--") + self.update_legend() + self.idle() diff --git a/src/View/Results/CustomPlot/Translate.py b/src/View/Results/CustomPlot/Translate.py index d341732f..a6edcff7 100644 --- a/src/View/Results/CustomPlot/Translate.py +++ b/src/View/Results/CustomPlot/Translate.py @@ -55,6 +55,9 @@ class CustomPlotTranslate(ResultsTranslate): self._dict['wet_perimeter'] = self._dict["unit_wet_perimeter"] self._dict['hydraulic_radius'] = self._dict["unit_hydraulic_radius"] self._dict['froude'] = self._dict["unit_froude"] + self._dict['user_imported'] = _translate( + "CustomPlot", "user_imported" + ) # Unit corresponding long name (plot axes display) diff --git a/src/View/Results/Table.py b/src/View/Results/Table.py index e5c6785e..668619f0 100644 --- a/src/View/Results/Table.py +++ b/src/View/Results/Table.py @@ -160,8 +160,6 @@ class TableModel(PamhyrTableModel): self._lst = _river.reachs elif self._opt_data == "profile" or self._opt_data == "raw_data": self._lst = _river.reach(reach).profiles - # self._lst = list(compress(_river.reach(reach).profiles, - # _river.reach(reach).profile_mask)) elif self._opt_data == "solver": self._lst = self._parent._solvers diff --git a/src/View/Results/Window.py b/src/View/Results/Window.py index 7b1902f7..c2a17f25 100644 --- a/src/View/Results/Window.py +++ b/src/View/Results/Window.py @@ -31,6 +31,8 @@ except Exception as e: print(f"Module 'rasterio' is not available: {e}") _rasterio_loaded = False +from functools import reduce + from numpy import sqrt from datetime import datetime @@ -54,7 +56,7 @@ from PyQt5.QtWidgets import ( QFileDialog, QTableView, QAbstractItemView, QUndoStack, QShortcut, QAction, QItemDelegate, QComboBox, QVBoxLayout, QHeaderView, QTabWidget, - QSlider, QLabel, QWidget, QGridLayout, QTabBar + QSlider, QLabel, QWidget, QGridLayout, QTabBar, QInputDialog ) from View.Tools.Plot.PamhyrCanvas import MplCanvas @@ -347,6 +349,7 @@ class ResultsWindow(PamhyrWindow): "action_add": self._add_custom_plot, "action_export": self._export, # "action_export": self.export_current, + "action_import_data": self.import_data } if _rasterio_loaded: actions["action_Geo_tiff"] = self.import_geotiff @@ -715,7 +718,7 @@ class ResultsWindow(PamhyrWindow): select_file="AnyFile", callback=lambda f: self.export_to(f[0], x, y, envelop, solver_id), default_suffix=".csv", - file_filter=["CSV (*.csv)"], + file_filter=[self._dict["file_csv"]], ) def export_to(self, filename, x, y, envelop, solver_id): @@ -1225,3 +1228,83 @@ class ResultsWindow(PamhyrWindow): self.plot_xy.idle() self.canvas.axes.set_xlim(xlim) self.canvas.axes.set_ylim(ylim) + return + + def import_data(self): + + file_types = [ + self._trad["file_csv"], + self._trad["file_all"], + ] + + self.file_dialog( + select_file="Existing_file", + callback=lambda f: self.read_csv_file(f[0]), + default_suffix=".csv", + file_filter=file_types, + ) + + def read_csv_file(self, filename): + if filename == "": + return + + sep = " " + 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: + try: + x.append(float(row[0])) + y.append(float(row[1])) + except: + pass + + 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) + + if not ok: + return + + legend, ok = QInputDialog.getText(self, 'Legend', 'Legend:') + + if not ok: + return + + if legend.strip() == '': + legend = '*' + + tmp_dict = {'Z': 'water_elevation', + 'Q': 'discharge', + 'x': 'rk', + 't': 'time'} + + 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} + + if data_type == 'Z(x)': + line = self.canvas_2.axes.plot(x, y, marker="+", + label=legend + ' (m)') + 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)') + self.plot_h._line.append(line) + self.plot_h.enable_legend() + self.plot_h.canvas.draw_idle() + self.plot_h.update_idle + + for p in self._additional_plot: + self._additional_plot[p].add_imported_plot(data) diff --git a/src/View/Results/translate.py b/src/View/Results/translate.py index cd6f46d4..7a1e7e9b 100644 --- a/src/View/Results/translate.py +++ b/src/View/Results/translate.py @@ -56,6 +56,8 @@ class ResultsTranslate(MainTranslate): self._dict["file_all"] = _translate("Results", "All files (*)") self._dict["file_geotiff"] = _translate( "Results", "GeoTIFF file (*.tiff *.tif)") + self._dict["file_csv"] = _translate( + "Results", "CSV file (*.csv)") self._dict["ImageCoordinates"] = _translate( "Results", "Image coordinates" ) diff --git a/src/View/ui/Results.ui b/src/View/ui/Results.ui index 3fcf700a..4e33b042 100644 --- a/src/View/ui/Results.ui +++ b/src/View/ui/Results.ui @@ -239,6 +239,7 @@ + @@ -288,6 +289,18 @@ Import background image + + + + ressources/import.pngressources/import.png + + + Import data + + + Import data from SCV file + +