mirror of https://gitlab.com/pamhyr/pamhyr2
Results: Refacto read csv.
parent
c63c776989
commit
4e1acfecdc
|
|
@ -1231,7 +1231,6 @@ class ResultsWindow(PamhyrWindow):
|
||||||
return
|
return
|
||||||
|
|
||||||
def import_data(self):
|
def import_data(self):
|
||||||
|
|
||||||
file_types = [
|
file_types = [
|
||||||
self._trad["file_csv"],
|
self._trad["file_csv"],
|
||||||
self._trad["file_all"],
|
self._trad["file_all"],
|
||||||
|
|
@ -1248,29 +1247,38 @@ class ResultsWindow(PamhyrWindow):
|
||||||
if filename == "":
|
if filename == "":
|
||||||
return
|
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):
|
def read_csv_file_data(self, filename):
|
||||||
if string.replace(".", "").isnumeric():
|
sep = ","
|
||||||
return True
|
x = []
|
||||||
else:
|
y = []
|
||||||
return False
|
|
||||||
|
|
||||||
with open(filename, 'r', newline='') as f:
|
with open(filename, 'r', newline='') as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
x = []
|
|
||||||
y = []
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line[0] != "*" and line[0] != "#" and line[0] != "$":
|
if line[0] != "*" and line[0] != "#" and line[0] != "$":
|
||||||
row = line.split(sep)
|
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_lst = ['Q(t)', 'Z(t)', 'Z(x)']
|
||||||
data_type, ok = QInputDialog.getItem(
|
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:
|
if not ok:
|
||||||
return
|
return
|
||||||
|
|
@ -1291,25 +1299,37 @@ class ResultsWindow(PamhyrWindow):
|
||||||
tmp_unit = {'Z': ' (m)',
|
tmp_unit = {'Z': ' (m)',
|
||||||
'Q': ' (m³/s)'}
|
'Q': ' (m³/s)'}
|
||||||
|
|
||||||
data = {'type_x': tmp_dict[data_type[2]],
|
data = {
|
||||||
'type_y': tmp_dict[data_type[0]],
|
'type_x': tmp_dict[data_type[2]],
|
||||||
'legend': legend,
|
'type_y': tmp_dict[data_type[0]],
|
||||||
'unit': tmp_unit[data_type[0]],
|
'legend': legend,
|
||||||
'x': x,
|
'unit': tmp_unit[data_type[0]],
|
||||||
'y': y}
|
'x': x, 'y': y
|
||||||
|
}
|
||||||
|
|
||||||
if data_type == 'Z(x)':
|
return data
|
||||||
line = self.canvas_2.axes.plot(x, y, marker="+",
|
|
||||||
label=legend + ' (m)')
|
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.canvas.draw_idle()
|
||||||
self.plot_rkc.update_idle()
|
self.plot_rkc.update_idle()
|
||||||
if data_type == 'Q(t)':
|
if data['type_x'] == 'discharge' and data['type_y'] == 'time':
|
||||||
line = self.canvas_4.axes.plot(x, y, marker="+",
|
line = self.canvas_4.axes.plot(
|
||||||
label=legend + ' (m³/s)')
|
x, y, marker="+",
|
||||||
|
label=legend + ' ' + unit
|
||||||
|
)
|
||||||
self.plot_h._line.append(line)
|
self.plot_h._line.append(line)
|
||||||
self.plot_h.enable_legend()
|
self.plot_h.enable_legend()
|
||||||
self.plot_h.canvas.draw_idle()
|
self.plot_h.canvas.draw_idle()
|
||||||
self.plot_h.update_idle
|
self.plot_h.update_idle()
|
||||||
|
|
||||||
for p in self._additional_plot:
|
for p in self._additional_plot:
|
||||||
self._additional_plot[p].add_imported_plot(data)
|
self._additional_plot[p].add_imported_plot(data)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue