source: palm/trunk/SCRIPTS/palm_gf_files/palm_gf_tools.py

Last change on this file was 4869, checked in by schwenkel, 3 years ago

update palm_gf to python3

  • Property svn:keywords set to Id
File size: 18.0 KB
Line 
1#!/usr/bin/env python
2from PyQt5 import QtWidgets, uic
3from PyQt5.QtWidgets import QFileDialog
4import sys
5import sqlite3
6import os
7
8palm_dir = os.getcwd()
9palm_bin = palm_dir + '/trunk/SCRIPTS'
10
11
12class MyTable(QtWidgets.QDialog):
13
14    class MyTableWidgetItem(QtWidgets.QTableWidgetItem):
15        def __init__(self, text, sortKey):
16            QtWidgets.QTableWidgetItem.__init__(self, text, QtWidgets.QTableWidgetItem.UserType)
17            self.sortKey = sortKey
18
19        def __lt__(self, other):
20            return self.sortKey < other.sortKey
21
22    def __init__(self):
23        super(MyTable, self).__init__()
24        uic.loadUi(palm_bin + '/palm_gf_files/palm_gf_table.ui', self)
25        self.center_window()
26        self.load_limits()
27        self.exec_button.clicked.connect(self.filter_data)
28        # connects labels of table to sorting subroutine
29        self.nx_table.clicked.connect(lambda: self.sort_table(str("nx")))
30        self.ny_table.clicked.connect(lambda: self.sort_table(str("ny")))
31        self.nz_table.clicked.connect(lambda: self.sort_table(str("nz")))
32        self.npex_table.clicked.connect(lambda: self.sort_table(str("npex")))
33        self.npey_table.clicked.connect(lambda: self.sort_table(str("npey")))
34        self.npexnpey_table.clicked.connect(lambda: self.sort_table(str("pxpy")))
35        self.np_table.clicked.connect(lambda: self.sort_table(str("np")))
36        self.ngpts_table.clicked.connect(lambda: self.sort_table(str("ngpts")))
37        self.nxpex_table.clicked.connect(lambda: self.sort_table(str("nxnpex")))
38        self.nypey_table.clicked.connect(lambda: self.sort_table(str("nynpey")))
39        # connects main buttons to subroutines
40        self.load_all_button.clicked.connect(self.load_all)
41        self.reset_button.clicked.connect(self.load_limits)
42        self.save_button.clicked.connect(self.save)
43
44    def center_window(self):
45        # centers window in the middle of screen
46        frame = self.frameGeometry()
47        center = QtWidgets.QDesktopWidget().availableGeometry().center()
48        frame.moveCenter(center)
49        self.move(frame.topLeft())
50
51    def load_limits(self):
52        # reads out the .db and sets limits of results as Min/Max
53        con = sqlite3.connect('palm_gf_data.db')
54        c = con.cursor()
55        c.execute("SELECT * FROM " + 'grid_limits')
56        Min = c.fetchone()
57        Max = c.fetchone()
58
59        self.nx_min.setValue(Min[0])
60        self.nx_min.setMinimum(Min[0])
61        self.nx_max.setValue(Max[0])
62        self.nx_max.setMaximum(Max[0])
63
64        self.ny_min.setValue(Min[1])
65        self.ny_min.setMinimum(Min[1])
66        self.ny_max.setValue(Max[1])
67        self.ny_max.setMaximum(Max[1])
68
69        self.nz_min.setValue(Min[2])
70        self.nz_min.setMinimum(Min[2])
71        self.nz_max.setValue(Max[2])
72        self.nz_max.setMaximum(Max[2])
73
74        self.npex_min.setValue(Min[3])
75        self.npex_min.setMinimum(Min[3])
76        self.npex_max.setValue(Max[3])
77        self.npex_max.setMaximum(Max[3])
78
79        self.npey_min.setValue(Min[4])
80        self.npey_min.setMinimum(Min[4])
81        self.npey_max.setValue(Max[4])
82        self.npey_max.setMaximum(Max[4])
83
84        self.npxnpy_min.setValue(Min[5])
85        self.npxnpy_min.setMinimum(Min[5])
86        self.npxnpy_max.setValue(Max[5])
87        self.npxnpy_max.setMaximum(Max[5])
88
89        self.np_min.setValue(Min[6])
90        self.np_min.setMinimum(Min[6])
91        self.np_max.setValue(Max[6])
92        self.np_max.setMaximum(Max[6])
93        # try statement, as ngpts could be too huge to handle for PyQt5 (>10^8)
94        try:
95            self.ngpts_min.setValue(Min[7])
96            self.ngpts_min.setMinimum(Min[7])
97            self.ngpts_max.setValue(Max[7])
98            self.ngpts_max.setMaximum(Max[7])
99
100        except OverflowError:
101            self.ngpts_min.setEnabled(False)
102            self.ngpts_max.setEnabled(False)
103            self.ngpts_table.setEnabled(False)
104            self.ngpts_label.setStyleSheet('color: grey')
105            self.exec_button.setEnabled(False)
106            self.status_label.setText('ngpts overflow')
107
108        self.nxpex_min.setValue(Min[8])
109        self.nxpex_min.setMinimum(Min[8])
110        self.nxpex_max.setValue(Max[8])
111        self.nxpex_max.setMaximum(Max[8])
112
113        self.nypey_min.setValue(Min[9])
114        self.nypey_min.setMinimum(Min[9])
115        self.nypey_max.setValue(Max[9])
116        self.nypey_max.setMaximum(Max[9])
117
118        con.commit()
119        c.close()
120        con.close()
121
122    def load_all(self):
123        # shows all results found in .db file
124        con = sqlite3.connect('palm_gf_data.db')
125        c = con.cursor()
126        c.execute("SELECT * FROM " + 'grid_current')
127        data = c.fetchall()
128
129        self.tableWidget.setRowCount(len(data))
130        row_cnt = -1
131        for i in range(0, len(data)):
132            line = data[i]
133            row_cnt += 1
134            for j in range(0, 10):
135                value = line[j]
136
137                if j == 7:
138                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
139                        str("%.1e" % value), i))
140
141                else:
142                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
143                        str(value), i))
144
145        c.close()
146        con.close()
147        self.tableWidget.setRowCount(row_cnt + 1)
148        self.status_label.setText(str(row_cnt + 1) + ' results loaded')
149
150    def filter_data(self):
151        # filters data according to set Min/Max. Reads out .db to find all fitting data
152        con = sqlite3.connect('palm_gf_data.db')
153        c = con.cursor()
154        c.execute("SELECT * FROM " + 'grid_current')
155        data = c.fetchall()
156
157        self.tableWidget.setRowCount(len(data))
158
159        row_cnt = -1
160        for i in range(0, len(data)):
161            line = data[i]
162            if self.nx_min.value() <= line[0] <= self.nx_max.value():
163                if self.ny_min.value() <= line[1] <= self.ny_max.value():
164                    if self.nz_min.value() <= line[2] <= self.nz_max.value():
165                        if self.npex_min.value() <= line[3] <= self.npex_max.value():
166                            if self.npey_min.value() <= line[4] <= self.npey_max.value():
167                                if self.npxnpy_min.value() <= line[5] <= self.npxnpy_max.value():
168                                    if self.np_min.value() <= line[6] <= self.np_max.value():
169                                        if self.ngpts_min.value() <= line[7] <= self.ngpts_max.value():
170                                            if self.nxpex_min.value() <= line[8] <= self.nxpex_max.value():
171                                                if self.nypey_min.value() <= line[9] <= self.nypey_max.value():
172                                                    row_cnt += 1
173                                                    for j in range(0, 10):
174                                                        value = line[j]
175
176                                                        if j == 7:
177                                                            self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
178                                                                str("%.1e" % value), i))
179                                                        else:
180                                                            self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
181                                                                str(value), i))
182
183        c.close()
184        con.close()
185        self.tableWidget.setRowCount(row_cnt + 1)
186        self.status_label.setText(str(row_cnt + 1) + ' results found')
187
188    def sort_table(self, column):
189        # with argument as chosen column, read not from table but from .db and reloads sorted data into table
190        fnx_mn = self.nx_min.value()
191        fnx_mx = self.nx_max.value()
192        fny_mn = self.ny_min.value()
193        fny_mx = self.ny_max.value()
194        fnz_mn = self.nz_min.value()
195        fnz_mx = self.nz_max.value()
196        fnpex_mn = self.npex_min.value()
197        fnpex_mx = self.npex_max.value()
198        fnpey_mn = self.npex_min.value()
199        fnpey_mx = self.npey_max.value()
200        fnpxnpy_mn = self.npxnpy_min.value()
201        fnpxnpy_mx = self.npxnpy_max.value()
202        fnp_mn = self.np_min.value()
203        fnp_mx = self.np_max.value()
204        fngpts_mn = self.ngpts_min.value()
205        fngpts_mx = self.ngpts_max.value()
206        nxpex_mn = self.nxpex_min.value()
207        nxpex_mx = self.nxpex_max.value()
208        nypey_mn = self.nypey_min.value()
209        nypey_mx = self.nypey_max.value()
210
211        if column == 'nx':
212            sorted_col = 'nx'
213            if self.nx_table.isChecked() is True:
214                order = " DESC"
215            else:
216                order = " ASC"
217            self.ny_table.setChecked(False)
218            self.nz_table.setChecked(False)
219            self.npex_table.setChecked(False)
220            self.npey_table.setChecked(False)
221            self.np_table.setChecked(False)
222            self.ngpts_table.setChecked(False)
223            self.nxpex_table.setChecked(False)
224            self.nypey_table.setChecked(False)
225
226        if column == str("ny"):
227            sorted_col = "ny"
228            if self.ny_table.isChecked() is True:
229                order = " DESC"
230            else:
231                order = " ASC"
232
233            self.nx_table.setChecked(False)
234            self.nz_table.setChecked(False)
235            self.npex_table.setChecked(False)
236            self.npey_table.setChecked(False)
237            self.npexnpey_table.setChecked(False)
238            self.np_table.setChecked(False)
239            self.ngpts_table.setChecked(False)
240            self.nxpex_table.setChecked(False)
241            self.nypey_table.setChecked(False)
242
243        if column == str("nz"):
244            sorted_col = "nz"
245            if self.nz_table.isChecked() is True:
246                order = " DESC"
247            else:
248                order = " ASC"
249
250            self.nx_table.setChecked(False)
251            self.ny_table.setChecked(False)
252            self.npex_table.setChecked(False)
253            self.npey_table.setChecked(False)
254            self.npexnpey_table.setChecked(False)
255            self.np_table.setChecked(False)
256            self.ngpts_table.setChecked(False)
257            self.nxpex_table.setChecked(False)
258            self.nypey_table.setChecked(False)
259
260        if column == str("npex"):
261            sorted_col = "npex"
262            if self.npex_table.isChecked() is True:
263                order = " DESC"
264            else:
265                order = " ASC"
266
267            self.ny_table.setChecked(False)
268            self.nz_table.setChecked(False)
269            self.nx_table.setChecked(False)
270            self.npey_table.setChecked(False)
271            self.npexnpey_table.setChecked(False)
272            self.np_table.setChecked(False)
273            self.ngpts_table.setChecked(False)
274            self.nxpex_table.setChecked(False)
275            self.nypey_table.setChecked(False)
276
277        if column == str("npey"):
278            sorted_col = "npey"
279            if self.npey_table.isChecked() is True:
280                order = " DESC"
281            else:
282                order = " ASC"
283
284            self.ny_table.setChecked(False)
285            self.nz_table.setChecked(False)
286            self.npex_table.setChecked(False)
287            self.nx_table.setChecked(False)
288            self.npexnpey_table.setChecked(False)
289            self.np_table.setChecked(False)
290            self.ngpts_table.setChecked(False)
291            self.nxpex_table.setChecked(False)
292            self.nypey_table.setChecked(False)
293
294        if column == str("pxpy"):
295            sorted_col = "pxpy"
296            if self.npexnpey_table.isChecked() is True:
297                order = " DESC"
298            else:
299                order = " ASC"
300
301            self.ny_table.setChecked(False)
302            self.nz_table.setChecked(False)
303            self.npex_table.setChecked(False)
304            self.npey_table.setChecked(False)
305            self.nx_table.setChecked(False)
306            self.np_table.setChecked(False)
307            self.ngpts_table.setChecked(False)
308            self.nxpex_table.setChecked(False)
309            self.nypey_table.setChecked(False)
310
311        if column == str("np"):
312            sorted_col = "np"
313            if self.np_table.isChecked() is True:
314                order = " DESC"
315            else:
316                order = " ASC"
317
318            self.ny_table.setChecked(False)
319            self.nz_table.setChecked(False)
320            self.npex_table.setChecked(False)
321            self.npey_table.setChecked(False)
322            self.npexnpey_table.setChecked(False)
323            self.nx_table.setChecked(False)
324            self.ngpts_table.setChecked(False)
325            self.nxpex_table.setChecked(False)
326            self.nypey_table.setChecked(False)
327
328        if column == str("ngpts"):
329            sorted_col = "ngpts"
330            if self.ngpts_table.isChecked() is True:
331                order = " DESC"
332            else:
333                order = " ASC"
334
335            self.ny_table.setChecked(False)
336            self.nz_table.setChecked(False)
337            self.npex_table.setChecked(False)
338            self.npey_table.setChecked(False)
339            self.npexnpey_table.setChecked(False)
340            self.np_table.setChecked(False)
341            self.nx_table.setChecked(False)
342            self.nxpex_table.setChecked(False)
343            self.nypey_table.setChecked(False)
344
345        if column == str("nxnpex"):
346            sorted_col = "nxnpex"
347            if self.nxpex_table.isChecked() is True:
348                order = " DESC"
349            else:
350                order = " ASC"
351
352            self.ny_table.setChecked(False)
353            self.nz_table.setChecked(False)
354            self.npex_table.setChecked(False)
355            self.npey_table.setChecked(False)
356            self.npexnpey_table.setChecked(False)
357            self.np_table.setChecked(False)
358            self.nx_table.setChecked(False)
359            self.ngpts_table.setChecked(False)
360            self.nypey_table.setChecked(False)
361
362        if column == str("nynpey"):
363            sorted_col = "nynpey"
364            if self.nypey_table.isChecked() is True:
365                order = " DESC"
366            else:
367                order = " ASC"
368
369            self.ny_table.setChecked(False)
370            self.nz_table.setChecked(False)
371            self.npex_table.setChecked(False)
372            self.npey_table.setChecked(False)
373            self.npexnpey_table.setChecked(False)
374            self.np_table.setChecked(False)
375            self.nx_table.setChecked(False)
376            self.ngpts_table.setChecked(False)
377            self.nxpex_table.setChecked(False)
378
379        conn = sqlite3.connect('palm_gf_data.db')
380        c = conn.cursor()
381        c.execute("SELECT * FROM grid_current  WHERE nx <= " + str(fnx_mx) + " AND nx >= " + str(
382            fnx_mn) + " AND ny <= " + str(fny_mx) + " AND ny >= " + str(fny_mn) + " AND nz <= " + str(fnz_mx) +
383                  " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
384                  str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
385                                                                                                      "pxpy <= " + str(
386            fnpxnpy_mx) + " AND pxpy >= " + str(fnpxnpy_mn) + " AND np <= " + str(fnp_mx) + " AND np >= " + str(
387            fnp_mn) + " AND ngpts <= " + str(fngpts_mx) + " AND ngpts >= " + str(fngpts_mn) +
388                  " AND nxnpex <= " + str(nxpex_mx) + " AND nxnpex >= " + str(nxpex_mn) + " AND nynpey <= " + str(
389            nypey_mx) + " AND nynpey >= " + str(nypey_mn) +
390                  " ORDER BY " + str(sorted_col) + str(order))
391
392        sorted = c.fetchall()
393        c.close()
394        conn.close()
395        self.tableWidget.setRowCount(len(sorted))
396        for row_indx in range(0, len(sorted)):
397            for col_indx in range(0, 10):
398                row = sorted[row_indx]
399                value = row[col_indx]
400                if col_indx == 7:
401                    self.tableWidget.setItem(row_indx, col_indx, self.MyTableWidgetItem(str("%.1e" % value), col_indx))
402                else:
403                    self.tableWidget.setItem(row_indx, col_indx, self.MyTableWidgetItem(str(value), col_indx))
404
405        self.status_label.setText(str(len(sorted)) + ' results found')
406
407    def save_dialog(self):
408        # calling native PyQt5 Dialog to get desired save path
409        options = QFileDialog.Options()
410        options |= QFileDialog.DontUseNativeDialog
411        fileName, _ = QFileDialog.getSaveFileName(self, "Save .txt File", ".txt",
412                                                  "All Files (*.txt);;Text Files (*.txt)", options=options)
413        if fileName:
414            return fileName
415
416    def save(self):
417        # saves the file
418        # try because QFileDialog could be closed without choosing path
419        try:
420            if len(self.tableWidget.selectedItems()) != 0:
421                location = self.save_dialog()
422                data2safe = self.tableWidget.selectedItems()
423                if location is not None:
424                    with open(location, 'w') as file:
425                        file.write('nx  ' + 'ny  ' + 'nz  ' + 'npex  ' + 'npey' + "\n")
426                        for row in range(0, int(len(data2safe)), 10):
427                            file.write(str(data2safe[0+row].text()) + '  ' + str(data2safe[1+row].text()) + '  ' +
428                                       str(data2safe[2+row].text()) + '  ' + str(data2safe[3+row].text()) + '  ' +
429                                       str(data2safe[4+row].text()) + "\n")
430                    file.close()
431            else:
432                self.status_label.setText('Select to save')
433        except AttributeError:
434            pass
435
436
437if __name__ == "__main__":
438    app = QtWidgets.QApplication(sys.argv)
439    window = MyTable()
440    window.setWindowTitle('palm_gf Table')
441    window.show()
442    sys.exit(app.exec_())
Note: See TracBrowser for help on using the repository browser.