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

Last change on this file since 2716 was 2309, checked in by gronemeier, 7 years ago

some further bugfixes for palm_gf

File size: 22.7 KB
Line 
1import sys
2import os
3import sqlite3
4from PyQt4 import QtCore, QtGui, uic
5import subprocess as sub
6import palm_gf_conf as configwr
7   
8out = sub.check_output("echo $PALM_BIN", shell=True, stderr=sub.STDOUT)
9palm_bin = out.rstrip()
10
11qtCreatorFile = palm_bin + '/palm_gf_files/palm_gf_table.ui'
12
13Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
14
15class MyApp3(QtGui.QMainWindow, Ui_MainWindow):
16
17    class MyTableWidgetItem(QtGui.QTableWidgetItem):
18        def __init__(self, text, sortKey):
19            QtGui.QTableWidgetItem.__init__(self, text, QtGui.QTableWidgetItem.UserType)
20            self.sortKey = sortKey
21
22        def __lt__(self, other):
23            return self.sortKey < other.sortKey
24
25    def __init__(self):   #     def __init__(self, parent=None):
26        QtGui.QMainWindow.__init__(self)
27        Ui_MainWindow.__init__(self)
28
29        framegm = self.frameGeometry()
30        screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
31        centerpoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
32        framegm.moveCenter(centerpoint)
33
34        #centerpoint = str(centerpoint)
35        #xcenter = centerpoint.split('(')[1].split(',')[0]
36        #ycenter = centerpoint.split('(')[1].split(',')[1].split(')')[0]
37        ##print xcenter, ycenter
38        #centerpoint = QtCore.QPoint(int(xcenter) + 418, int(ycenter))
39        #framegm.moveCenter(centerpoint)
40        self.move(framegm.topLeft())
41
42
43
44        self.setupUi(self)
45        self.pushButton_3.clicked.connect(self.load_result)
46        #self.nx_min.valueChanged.connect(self.check)
47        self.load_trigger()
48        self.pushButton.clicked.connect(self.filter_results)
49        #self.Sortnow_button.clicked.connect(self.sort_order)
50        self.pushButton_2.clicked.connect(self.load_trigger)
51        self.tableWidget.horizontalHeader().setClickable(True)
52
53
54        self.nx_table.clicked.connect(lambda: self.sort_table(str("nx")))
55        self.ny_table.clicked.connect(lambda: self.sort_table(str("ny")))
56        self.nz_table.clicked.connect(lambda: self.sort_table(str("nz")))
57        self.npex_table.clicked.connect(lambda: self.sort_table(str("npex")))
58        self.npey_table.clicked.connect(lambda: self.sort_table(str("npey")))
59        self.npexnpey_table.clicked.connect(lambda: self.sort_table(str("npexnpey")))
60        self.np_table.clicked.connect(lambda: self.sort_table(str("np")))
61        self.ngpts_table.clicked.connect(lambda: self.sort_table(str("ngpts")))
62        self.nxpex_table.clicked.connect(lambda: self.sort_table(str("nxnpex")))
63        self.nypey_table.clicked.connect(lambda: self.sort_table(str("nynpey")))
64
65        self.instant()
66
67        self.save_to_file_button.clicked.connect(self.get_path)
68
69    def instant(self):
70        checkfile = open(".palm_gf_tmp", "r")
71        # self.result_label.setText(str(checkfile.readline()))
72        res_text = str(checkfile.readline())  # XXX
73        result_nr = res_text.split(' ')[2]
74        checkfile.close()
75        if int(result_nr) < 100000:
76            self.load_result()
77
78
79
80    def load_trigger(self):
81        pathx = configwr.read_config()
82        pathx = pathx[19]
83
84
85        dtb = str('.palm_gf_data.db')
86        #con = sqlite3.connect("/localdata/.palm_gf_data.db")
87
88        pathx = pathx + '/.palm_gf_data.db'
89        con = sqlite3.connect(pathx)
90        c = con.cursor()
91        c.execute("SELECT * FROM " + 'grid_limits')
92        mini = c.fetchone()
93        max = c.fetchone()
94        self.nx_min.setValue(mini[0])
95        self.nx_max.setValue(max[0])
96
97        self.ny_min.setValue(mini[1])
98        self.ny_max.setValue(max[1])
99        self.nz_min.setValue(mini[2])
100        self.nz_max.setValue(max[2])
101        self.npex_min.setValue(mini[3])
102        self.npex_max.setValue(max[3])
103        self.npey_min.setValue(mini[4])
104        self.npey_max.setValue(max[4])
105        self.npxnpy_min.setValue(mini[5])
106        self.npxnpy_max.setValue(max[5])
107        self.np_min.setValue(mini[6])
108        self.np_max.setValue(max[6])
109        self.ngpts_min.setValue(mini[7])
110        self.ngpts_max.setValue(max[7])
111        self.nxpex_min.setValue(mini[8])
112        self.nxpex_max.setValue(max[8])
113        self.nypey_min.setValue(mini[9])
114        self.nypey_max.setValue(max[9])
115
116        self.nx_min.setMinimum(mini[0])
117        self.nx_max.setMaximum(max[0])
118        self.ny_min.setMinimum(mini[1])
119        self.ny_max.setMaximum(max[1])
120        self.nz_min.setMinimum(mini[2])
121        self.nz_max.setMaximum(max[2])
122        self.npex_min.setMinimum(mini[3])
123        self.npex_max.setMaximum(max[3])
124        self.npey_min.setMinimum(mini[4])
125        self.npey_max.setMaximum(max[4])
126        self.npxnpy_min.setMinimum(mini[5])
127        self.npxnpy_max.setMaximum(max[5])
128        self.np_min.setMinimum(mini[6])
129        self.np_max.setMaximum(max[6])
130
131        self.ngpts_min.setMinimum(mini[7])
132        self.ngpts_max.setMaximum(max[7])
133        self.ngpts_min.setMaximum(max[7])
134        self.ngpts_max.setMinimum(mini[7])
135
136        self.nxpex_min.setMinimum(mini[8])
137        self.nxpex_max.setMaximum(max[8])
138        self.nxpex_min.setMaximum(max[8])
139        self.nxpex_max.setMinimum(mini[8])
140
141        self.nypey_min.setMinimum(mini[9])
142        self.nypey_max.setMaximum(max[9])
143        self.nypey_min.setMaximum(max[9])
144        self.nypey_max.setMinimum(mini[9])
145
146
147
148
149
150        con.commit()
151        c.close()
152        con.close()
153
154
155
156
157    def check(self):
158        pathx = configwr.read_config()
159        pathx = pathx[19]
160
161        dtb = str('.palm_gf_data.db')
162        #con = sqlite3.connect("/localdata/.palm_gf_data.db")
163        con = sqlite3.connect(pathx + '/.palm_gf_data.db')
164        c = con.cursor()
165        c.execute("SELECT * FROM " + 'grid_limits')
166        mini = c.fetchone()
167        max = c.fetchone()
168        if self.nx_min.value() < mini[0]:
169
170            self.nx_min.setValue(mini[0])
171
172
173    def process1(self):
174        self.calc_label.setText('loading...')
175        QtGui.QApplication.processEvents()
176
177
178
179    def load_result(self):
180        #print("LOADED!!!")
181        import decimal
182
183        pathx = configwr.read_config()
184        pathx = pathx[19]
185
186        self.setEnabled(False)
187        QtGui.QApplication.processEvents()
188        self.load_trigger()
189        self.process1()
190        database = str('.palm_gf_data.db')
191        conn = sqlite3.connect(pathx + '/.palm_gf_data.db')
192        c = conn.cursor()
193        c.execute("SELECT * FROM " + 'grid_current')
194        results = c.fetchall()
195
196        self.tableWidget.setRowCount(len(results))
197
198        i = 0
199        j = 0
200        k = 0
201
202        while i < len(results):
203            line = results[i]
204            while j < 10:
205                var = line[j]
206
207                if j == 7:
208                    self.tableWidget.setItem(i, j, self.MyTableWidgetItem(str("%.1e" % var), j + i))
209                    #print("%.2e" % int(var), "%.4e" % int(1782))
210
211                else:
212                    self.tableWidget.setItem(i, j, self.MyTableWidgetItem(str(var), j+i))
213                #item = self.MyTableWidgetItem(str(var), k)
214                #self.tableWidget.setItem(i, j, item)
215
216                j += 1
217                #if j == 3:
218                    #print(k)
219                #k += 1
220            #k -= 7
221            k += 1
222            j = 0
223            i += 1
224
225        c.close()
226        conn.close()
227        self.calc_label.setText('loading completed')
228        self.label_11.setText(str(len(results)) + ' results ')
229        self.setEnabled(True)
230        QtGui.QApplication.processEvents()
231
232    def filter_results(self):
233
234        pathx = configwr.read_config()
235        pathx = pathx[19]
236
237        self.setEnabled(False)
238        self.calc_label.setText('calculating...')
239        QtGui.QApplication.processEvents()
240        database = str('.palm_gf_data.db')
241        conn = sqlite3.connect(pathx + '/.palm_gf_data.db')
242        c = conn.cursor()
243        c.execute("SELECT * FROM " + "grid_current")
244        results = c.fetchall()
245        #print(results)
246
247        self.tableWidget.setRowCount(len(results))
248
249        i = 0
250        j = 0
251        row_cnt = -1
252        while i < len(results):
253            line = results[i]
254
255            if line[0] <= self.nx_max.value():
256
257                if line[0] >= self.nx_min.value():
258
259                    if line[1] <= self.ny_max.value():
260
261                        if line[1] >= self.ny_min.value():
262
263                            if line[2] <= self.nz_max.value():
264
265                                if line[2] >= self.nz_min.value():
266
267                                    if line[3] <= self.npex_max.value():
268
269                                        if line[3] >= self.npex_min.value():
270
271                                            if line[4] <= self.npey_max.value():
272
273                                                if line[4] >= self.npey_min.value():
274
275                                                    if line[5] <= self.npxnpy_max.value():
276
277                                                        if line[5] >= self.npxnpy_min.value():
278
279                                                            if line[6] <= self.np_max.value():
280
281                                                                if line[6] >= self.np_min.value():
282
283                                                                    if line[7] <= self.ngpts_max.value():
284
285                                                                        if line[7] >= self.ngpts_min.value():
286
287                                                                            if line[8] <= self.nxpex_max.value():
288
289                                                                                if line[8] >= self.nxpex_min.value():
290
291                                                                                    if line[9] <= self.nypey_max.value():
292
293                                                                                        if line[9] >= self.nypey_min.value():
294
295                                                                                            row_cnt += 1
296                                                                                            while j < 10:
297                                                                                                var = line[j]
298
299                                                                                                if j == 7:
300                                                                                                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(str("%.1e" % var), i))
301
302                                                                                                else:
303                                                                                                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(str(var), i))
304
305                                                                                                j += 1
306
307            j = 0
308            i += 1
309
310        c.close()
311        conn.close()
312        self.tableWidget.setRowCount(row_cnt + 1)
313        self.setEnabled(True)
314        self.calc_label.setText('calculation completed')
315        self.label_11.setText(str(row_cnt + 1) + ' results')
316        QtGui.QApplication.processEvents()
317
318
319
320    def sort_table(self, column):
321
322        fnx_mn = self.nx_min.value()
323        fnx_mx = self.nx_max.value()
324        fny_mn = self.ny_min.value()
325        fny_mx = self.ny_max.value()
326        fnz_mn = self.nz_min.value()
327        fnz_mx = self.nz_max.value()
328        fnpex_mn = self.npex_min.value()
329        fnpex_mx = self.npex_max.value()
330        fnpey_mn = self.npex_min.value()
331        fnpey_mx = self.npey_max.value()
332        fnpxnpy_mn = self.npxnpy_min.value()
333        fnpxnpy_mx = self.npxnpy_max.value()
334        fnp_mn = self.np_min.value()
335        fnp_mx = self.np_max.value()
336        fngpts_mn = self.ngpts_min.value()
337        fngpts_mx = self.ngpts_max.value()
338        nxpex_mn = self.nxpex_min.value()
339        nxpex_mx = self.nxpex_max.value()
340        nypey_mn = self.nypey_min.value()
341        nypey_mx = self.nypey_max.value()
342
343
344        if column == str("nx"):
345            sorted_col = "nx"
346
347            if self.nx_table.isChecked() is True:
348                order = " DESC"
349
350            else:
351
352                order = " ASC"
353
354            self.ny_table.setChecked(False)
355            self.nz_table.setChecked(False)
356            self.npex_table.setChecked(False)
357            self.npey_table.setChecked(False)
358            self.npexnpey_table.setChecked(False)
359            self.np_table.setChecked(False)
360            self.ngpts_table.setChecked(False)
361            self.nxpex_table.setChecked(False)
362            self.nypey_table.setChecked(False)
363
364        if column == str("ny"):
365            sorted_col = "ny"
366
367            if self.ny_table.isChecked() is True:
368                order = " DESC"
369
370            else:
371
372                order = " ASC"
373
374            self.nx_table.setChecked(False)
375            self.nz_table.setChecked(False)
376            self.npex_table.setChecked(False)
377            self.npey_table.setChecked(False)
378            self.npexnpey_table.setChecked(False)
379            self.np_table.setChecked(False)
380            self.ngpts_table.setChecked(False)
381            self.nxpex_table.setChecked(False)
382            self.nypey_table.setChecked(False)
383
384        if column == str("nz"):
385            sorted_col = "nz"
386
387            if self.nz_table.isChecked() is True:
388                order = " DESC"
389
390            else:
391
392                order = " ASC"
393
394            self.nx_table.setChecked(False)
395            self.ny_table.setChecked(False)
396            self.npex_table.setChecked(False)
397            self.npey_table.setChecked(False)
398            self.npexnpey_table.setChecked(False)
399            self.np_table.setChecked(False)
400            self.ngpts_table.setChecked(False)
401            self.nxpex_table.setChecked(False)
402            self.nypey_table.setChecked(False)
403
404        if column == str("npex"):
405            sorted_col = "npex"
406
407            if self.npex_table.isChecked() is True:
408                order = " DESC"
409
410            else:
411
412                order = " ASC"
413
414            self.ny_table.setChecked(False)
415            self.nz_table.setChecked(False)
416            self.nx_table.setChecked(False)
417            self.npey_table.setChecked(False)
418            self.npexnpey_table.setChecked(False)
419            self.np_table.setChecked(False)
420            self.ngpts_table.setChecked(False)
421            self.nxpex_table.setChecked(False)
422            self.nypey_table.setChecked(False)
423
424        if column == str("npey"):
425            sorted_col = "npey"
426
427            if self.npey_table.isChecked() is True:
428                order = " DESC"
429
430            else:
431
432                order = " ASC"
433
434
435            self.ny_table.setChecked(False)
436            self.nz_table.setChecked(False)
437            self.npex_table.setChecked(False)
438            self.nx_table.setChecked(False)
439            self.npexnpey_table.setChecked(False)
440            self.np_table.setChecked(False)
441            self.ngpts_table.setChecked(False)
442            self.nxpex_table.setChecked(False)
443            self.nypey_table.setChecked(False)
444
445        if column == str("npexnpey"):
446            sorted_col = "npxnpy"
447
448            if self.npexnpey_table.isChecked() is True:
449                order = " DESC"
450
451            else:
452
453                order = " ASC"
454
455            self.ny_table.setChecked(False)
456            self.nz_table.setChecked(False)
457            self.npex_table.setChecked(False)
458            self.npey_table.setChecked(False)
459            self.nx_table.setChecked(False)
460            self.np_table.setChecked(False)
461            self.ngpts_table.setChecked(False)
462            self.nxpex_table.setChecked(False)
463            self.nypey_table.setChecked(False)
464
465        if column == str("np"):
466            sorted_col = "np"
467
468            if self.np_table.isChecked() is True:
469                order = " DESC"
470
471            else:
472
473                order = " ASC"
474
475            self.ny_table.setChecked(False)
476            self.nz_table.setChecked(False)
477            self.npex_table.setChecked(False)
478            self.npey_table.setChecked(False)
479            self.npexnpey_table.setChecked(False)
480            self.nx_table.setChecked(False)
481            self.ngpts_table.setChecked(False)
482            self.nxpex_table.setChecked(False)
483            self.nypey_table.setChecked(False)
484
485        if column == str("ngpts"):
486            sorted_col = "ngpts"
487
488            if self.ngpts_table.isChecked() is True:
489                order = " DESC"
490
491            else:
492
493                order = " ASC"
494
495            self.ny_table.setChecked(False)
496            self.nz_table.setChecked(False)
497            self.npex_table.setChecked(False)
498            self.npey_table.setChecked(False)
499            self.npexnpey_table.setChecked(False)
500            self.np_table.setChecked(False)
501            self.nx_table.setChecked(False)
502            self.nxpex_table.setChecked(False)
503            self.nypey_table.setChecked(False)
504
505        if column == str("nxnpex"):
506            sorted_col = "nxnpex"
507
508            if self.nxpex_table.isChecked() is True:
509                order = " DESC"
510
511            else:
512
513                order = " ASC"
514
515            self.ny_table.setChecked(False)
516            self.nz_table.setChecked(False)
517            self.npex_table.setChecked(False)
518            self.npey_table.setChecked(False)
519            self.npexnpey_table.setChecked(False)
520            self.np_table.setChecked(False)
521            self.nx_table.setChecked(False)
522            self.ngpts_table.setChecked(False)
523            self.nypey_table.setChecked(False)
524
525        if column == str("nynpey"):
526            sorted_col = "nynpey"
527
528            if self.nypey_table.isChecked() is True:
529                order = " DESC"
530
531            else:
532
533                order = " ASC"
534
535            self.ny_table.setChecked(False)
536            self.nz_table.setChecked(False)
537            self.npex_table.setChecked(False)
538            self.npey_table.setChecked(False)
539            self.npexnpey_table.setChecked(False)
540            self.np_table.setChecked(False)
541            self.nx_table.setChecked(False)
542            self.ngpts_table.setChecked(False)
543            self.nxpex_table.setChecked(False)
544
545        else:
546            pass
547
548        pathx = configwr.read_config()
549        pathx = pathx[19]
550
551
552        conn = sqlite3.connect(pathx + "/.palm_gf_data.db")
553        c = conn.cursor()
554        c.execute("SELECT * FROM grid_current  WHERE nx <= " + str(fnx_mx) + " AND nx >= "  + str(fnx_mn) + " AND ny <= " + str(fny_mx) + " AND ny >= " + str(fny_mn) + " AND nz <= " + str(fnz_mx) +
555                  " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
556        str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
557                  "npxnpy <= " + str(fnpxnpy_mx) + " AND npxnpy >= " + str(fnpxnpy_mn) + " AND np <= " + str(fnp_mx) + " AND np >= " + str(fnp_mn) + " AND ngpts <= " + str(fngpts_mx) + " AND ngpts >= " + str(fngpts_mn) +
558        " AND nxnpex <= " + str(nxpex_mx) + " AND nxnpex >= " + str(nxpex_mn) + " AND nynpey <= " + str(nypey_mx) + " AND nynpey >= " + str(nypey_mn) +
559        " ORDER BY " + str(sorted_col) + str(order))
560
561
562
563        sorted = c.fetchall()
564
565
566        c.close()
567        conn.close()
568        self.tableWidget.setRowCount(len(sorted))
569
570        for row_indx in range(0,len(sorted)):
571
572            for col_indx in range(0,10):
573                row = sorted[row_indx]
574                value = row[col_indx]
575                if col_indx == 7:
576                    self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str("%.1e" % value)))
577                else:
578                    self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str(value)))
579
580
581
582    def sort_order(self):
583
584        sorted = 0
585        fnx_mn = self.nx_min.value()
586        fnx_mx = self.nx_max.value()
587        fny_mn = self.ny_min.value()
588        fny_mx = self.ny_max.value()
589        fnz_mn = self.nz_min.value()
590        fnz_mx = self.nz_max.value()
591        fnpex_mn = self.npex_min.value()
592        fnpex_mx = self.npex_max.value()
593        fnpey_mn = self.npex_min.value()
594        fnpey_mx = self.npey_max.value()
595        fnpxnpy_mn = self.npxnpy_min.value()
596        fnpxnpy_mx = self.npxnpy_max.value()
597        fnp_mn = self.np_min.value()
598        fnp_mx = self.np_max.value()
599        fngpts_mn = self.ngpts_min.value()
600        fngpts_mx = self.ngpts_max.value()
601
602        if str(self.Sortvariable_box.currentIndex()) == str(1):
603
604
605            sorted_col = "nx"
606        if str(self.Sortvariable_box.currentIndex()) == str(2):
607
608            sorted_col = "ny"
609        if str(self.Sortvariable_box.currentIndex()) == str(3):
610
611            sorted_col = "nz"
612        if str(self.Sortvariable_box.currentIndex()) == str(4):
613
614            sorted_col = "npex"
615        if str(self.Sortvariable_box.currentIndex()) == str(5):
616
617            sorted_col = "npey"
618        if str(self.Sortvariable_box.currentIndex()) == str(6):
619
620            sorted_col = "npxnpy"
621        if str(self.Sortvariable_box.currentIndex()) == str(7):
622
623            sorted_col = "np"
624        if str(self.Sortvariable_box.currentIndex()) == str(8):
625
626            sorted_col = "ngpts"
627
628        #print(self.Sortvariable_box.currentIndex())
629
630        if str(self.Sortorder_box.currentIndex()) == str(1):
631
632            order = " ASC"
633
634        if str(self.Sortorder_box.currentIndex()) == str(2):
635
636            order = " DESC"
637
638
639
640        conn = sqlite3.connect("/localdata/.palm_gf_data.db")
641        c = conn.cursor()
642        c.execute("SELECT * FROM grid_current  WHERE nx <= " + str(fnx_mx) + " AND nx >= "  + str(fnx_mn) + " AND ny <= " + str(fny_mx) + " AND ny >= " + str(fny_mn) + " AND nz <= " + str(fnz_mx) +
643                  " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
644        str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
645                  "npxnpy <= " + str(fnpxnpy_mx) + " AND npxnpy >= " + str(fnpxnpy_mn) + " AND np <= " + str(fnp_mx) + " AND np >= " + str(fnp_mn) + " AND ngpts <= " + str(fngpts_mx) + " AND ngpts >= " + str(fngpts_mn) +
646        " ORDER BY " + str(sorted_col) + str(order))
647
648
649
650        sorted = c.fetchall()
651
652        c.close()
653        conn.close()
654        self.tableWidget.setRowCount(len(sorted))
655
656        for row_indx in range(0,len(sorted)):
657
658            for col_indx in range(0,8):
659                row = sorted[row_indx]
660                value = row[col_indx]
661                self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str(var)))
662
663
664
665
666
667
668        #print(len(sorted))
669        #print(sorted)
670
671    def get_path(wildcard):
672        import wx
673        app = wx.App(None)
674        style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
675        dialog = wx.FileDialog(None, 'Open', wildcard='*.db')
676        if dialog.ShowModal() == wx.ID_OK:
677            path = dialog.GetPath()
678
679            print(path, file)
680        else:
681            path = None
682        dialog.Destroy()
683        return
684
685
686
687
688
689
690
691
692
693
694
695if __name__ == "__main__":
696    app = QtGui.QApplication(sys.argv)
697    window = MyApp3()
698    window.setWindowTitle('Gridfilter')
699    window.show()
700    sys.exit(app.exec_())
701
Note: See TracBrowser for help on using the repository browser.