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

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

new version of palm_gf (bugfixes, changes and new options)

File size: 18.8 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
63        self.instant()
64
65        self.save_to_file_button.clicked.connect(self.get_path)
66
67    def instant(self):
68        checkfile = open(".palm_gf_tmp", "r")
69        # self.result_label.setText(str(checkfile.readline()))
70        res_text = str(checkfile.readline())  # XXX
71        result_nr = res_text.split(' ')[2]
72        checkfile.close()
73        if int(result_nr) < 100000:
74            self.load_result()
75
76
77
78    def load_trigger(self):
79        pathx = configwr.read_config()
80        pathx = pathx[19]
81
82
83        dtb = str('.palm_gf_data.db')
84        #con = sqlite3.connect("/localdata/.palm_gf_data.db")
85        con = sqlite3.connect(pathx + '.palm_gf_data.db')
86        c = con.cursor()
87        c.execute("SELECT * FROM " + 'grid_limits')
88        mini = c.fetchone()
89        max = c.fetchone()
90        self.nx_min.setValue(mini[0])
91        self.nx_max.setValue(max[0])
92        self.ny_min.setValue(mini[1])
93        self.ny_max.setValue(max[1])
94        self.nz_min.setValue(mini[2])
95        self.nz_max.setValue(max[2])
96        self.npex_min.setValue(mini[3])
97        self.npex_max.setValue(max[3])
98        self.npey_min.setValue(mini[4])
99        self.npey_max.setValue(max[4])
100        self.npxnpy_min.setValue(mini[5])
101        self.npxnpy_max.setValue(max[5])
102        self.np_min.setValue(mini[6])
103        self.np_max.setValue(max[6])
104        self.ngpts_min.setValue(mini[7])
105        self.ngpts_max.setValue(max[7])
106
107        self.nx_min.setMinimum(mini[0])
108        self.nx_max.setMaximum(max[0])
109        self.ny_min.setMinimum(mini[1])
110        self.ny_max.setMaximum(max[1])
111        self.nz_min.setMinimum(mini[2])
112        self.nz_max.setMaximum(max[2])
113        self.npex_min.setMinimum(mini[3])
114        self.npex_max.setMaximum(max[3])
115        self.npey_min.setMinimum(mini[4])
116        self.npey_max.setMaximum(max[4])
117        self.npxnpy_min.setMinimum(mini[5])
118        self.npxnpy_max.setMaximum(max[5])
119        self.np_min.setMinimum(mini[6])
120        self.np_max.setMaximum(max[6])
121        self.ngpts_min.setMinimum(mini[7])
122        self.ngpts_max.setMaximum(max[7])
123        self.ngpts_min.setMaximum(max[7])
124        self.ngpts_max.setMinimum(mini[7])
125
126
127
128        con.commit()
129        c.close()
130        con.close()
131
132
133
134
135    def check(self):
136        dtb = str('.palm_gf_data.db')
137        con = sqlite3.connect("/localdata/.palm_gf_data.db")
138        c = con.cursor()
139        c.execute("SELECT * FROM " + 'grid_limits')
140        mini = c.fetchone()
141        max = c.fetchone()
142        if self.nx_min.value() < mini[0]:
143
144            self.nx_min.setValue(mini[0])
145
146
147    def process1(self):
148        self.calc_label.setText('loading...')
149        QtGui.QApplication.processEvents()
150
151
152
153    def load_result(self):
154        #print("LOADED!!!")
155        import decimal
156        self.setEnabled(False)
157        QtGui.QApplication.processEvents()
158        self.load_trigger()
159        self.process1()
160        database = str('.palm_gf_data.db')
161        conn = sqlite3.connect("/localdata/.palm_gf_data.db")
162        c = conn.cursor()
163        c.execute("SELECT * FROM " + 'grid_current')
164        results = c.fetchall()
165
166        self.tableWidget.setRowCount(len(results))
167
168        i = 0
169        j = 0
170        k = 0
171
172        while i < len(results):
173            line = results[i]
174            while j < 8:
175                var = line[j]
176
177                if j == 7:
178                    self.tableWidget.setItem(i, j, self.MyTableWidgetItem(str("%.1e" % var), j + i))
179                    #print("%.2e" % int(var), "%.4e" % int(1782))
180
181                else:
182                    self.tableWidget.setItem(i, j, self.MyTableWidgetItem(str(var), j+i))
183                #item = self.MyTableWidgetItem(str(var), k)
184                #self.tableWidget.setItem(i, j, item)
185
186                j += 1
187                #if j == 3:
188                    #print(k)
189                #k += 1
190            #k -= 7
191            k += 1
192            j = 0
193            i += 1
194
195        c.close()
196        conn.close()
197        self.calc_label.setText('loading completed')
198        self.label_11.setText(str(len(results)) + ' results ')
199        self.setEnabled(True)
200        QtGui.QApplication.processEvents()
201
202    def filter_results(self):
203
204        self.setEnabled(False)
205        self.calc_label.setText('calculating...')
206        QtGui.QApplication.processEvents()
207        database = str('.palm_gf_data.db')
208        conn = sqlite3.connect("/localdata/.palm_gf_data.db")
209        c = conn.cursor()
210        c.execute("SELECT * FROM " + "grid_current")
211        results = c.fetchall()
212        #print(results)
213
214        self.tableWidget.setRowCount(len(results))
215
216        i = 0
217        j = 0
218        row_cnt = -1
219        while i < len(results):
220            line = results[i]
221
222            if line[0] <= self.nx_max.value():
223
224                if line[0] >= self.nx_min.value():
225
226                    if line[1] <= self.ny_max.value():
227
228                        if line[1] >= self.ny_min.value():
229
230                            if line[2] <= self.nz_max.value():
231
232                                if line[2] >= self.nz_min.value():
233
234                                    if line[3] <= self.npex_max.value():
235
236                                        if line[3] >= self.npex_min.value():
237
238                                            if line[4] <= self.npey_max.value():
239
240                                                if line[4] >= self.npey_min.value():
241
242                                                    if line[5] <= self.npxnpy_max.value():
243
244                                                        if line[5] >= self.npxnpy_min.value():
245
246                                                            if line[6] <= self.np_max.value():
247
248                                                                if line[6] >= self.np_min.value():
249
250                                                                    if line[7] <= self.ngpts_max.value():
251
252                                                                        if line[7] >= self.ngpts_min.value():
253
254                                                                            row_cnt += 1
255                                                                            while j < 8:
256                                                                                var = line[j]
257
258                                                                                if j == 7:
259                                                                                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(str("%.1e" % var), i))
260
261                                                                                else:
262                                                                                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(str(var), i))
263
264                                                                                j += 1
265
266            j = 0
267            i += 1
268
269        c.close()
270        conn.close()
271        self.tableWidget.setRowCount(row_cnt + 1)
272        self.setEnabled(True)
273        self.calc_label.setText('calculation completed')
274        self.label_11.setText(str(row_cnt + 1) + ' results')
275        QtGui.QApplication.processEvents()
276
277
278
279    def sort_table(self, column):
280
281        fnx_mn = self.nx_min.value()
282        fnx_mx = self.nx_max.value()
283        fny_mn = self.ny_min.value()
284        fny_mx = self.ny_max.value()
285        fnz_mn = self.nz_min.value()
286        fnz_mx = self.nz_max.value()
287        fnpex_mn = self.npex_min.value()
288        fnpex_mx = self.npex_max.value()
289        fnpey_mn = self.npex_min.value()
290        fnpey_mx = self.npey_max.value()
291        fnpxnpy_mn = self.npxnpy_min.value()
292        fnpxnpy_mx = self.npxnpy_max.value()
293        fnp_mn = self.np_min.value()
294        fnp_mx = self.np_max.value()
295        fngpts_mn = self.ngpts_min.value()
296        fngpts_mx = self.ngpts_max.value()
297
298        if column == str("nx"):
299            sorted_col = "nx"
300
301            if self.nx_table.isChecked() is True:
302                order = " DESC"
303
304            else:
305
306                order = " ASC"
307
308            self.ny_table.setChecked(False)
309            self.nz_table.setChecked(False)
310            self.npex_table.setChecked(False)
311            self.npey_table.setChecked(False)
312            self.npexnpey_table.setChecked(False)
313            self.np_table.setChecked(False)
314            self.ngpts_table.setChecked(False)
315
316        if column == str("ny"):
317            sorted_col = "ny"
318
319            if self.ny_table.isChecked() is True:
320                order = " DESC"
321
322            else:
323
324                order = " ASC"
325
326            self.nx_table.setChecked(False)
327            self.nz_table.setChecked(False)
328            self.npex_table.setChecked(False)
329            self.npey_table.setChecked(False)
330            self.npexnpey_table.setChecked(False)
331            self.np_table.setChecked(False)
332            self.ngpts_table.setChecked(False)
333
334        if column == str("nz"):
335            sorted_col = "nz"
336
337            if self.nz_table.isChecked() is True:
338                order = " DESC"
339
340            else:
341
342                order = " ASC"
343
344            self.nx_table.setChecked(False)
345            self.ny_table.setChecked(False)
346            self.npex_table.setChecked(False)
347            self.npey_table.setChecked(False)
348            self.npexnpey_table.setChecked(False)
349            self.np_table.setChecked(False)
350            self.ngpts_table.setChecked(False)
351
352        if column == str("npex"):
353            sorted_col = "npex"
354
355            if self.npex_table.isChecked() is True:
356                order = " DESC"
357
358            else:
359
360                order = " ASC"
361
362            self.ny_table.setChecked(False)
363            self.nz_table.setChecked(False)
364            self.nx_table.setChecked(False)
365            self.npey_table.setChecked(False)
366            self.npexnpey_table.setChecked(False)
367            self.np_table.setChecked(False)
368            self.ngpts_table.setChecked(False)
369
370        if column == str("npey"):
371            sorted_col = "npey"
372
373            if self.npey_table.isChecked() is True:
374                order = " DESC"
375
376            else:
377
378                order = " ASC"
379
380
381            self.ny_table.setChecked(False)
382            self.nz_table.setChecked(False)
383            self.npex_table.setChecked(False)
384            self.nx_table.setChecked(False)
385            self.npexnpey_table.setChecked(False)
386            self.np_table.setChecked(False)
387            self.ngpts_table.setChecked(False)
388
389        if column == str("npexnpey"):
390            sorted_col = "npxnpy"
391
392            if self.npexnpey_table.isChecked() is True:
393                order = " DESC"
394
395            else:
396
397                order = " ASC"
398
399            self.ny_table.setChecked(False)
400            self.nz_table.setChecked(False)
401            self.npex_table.setChecked(False)
402            self.npey_table.setChecked(False)
403            self.nx_table.setChecked(False)
404            self.np_table.setChecked(False)
405            self.ngpts_table.setChecked(False)
406
407        if column == str("np"):
408            sorted_col = "np"
409
410            if self.np_table.isChecked() is True:
411                order = " DESC"
412
413            else:
414
415                order = " ASC"
416
417            self.ny_table.setChecked(False)
418            self.nz_table.setChecked(False)
419            self.npex_table.setChecked(False)
420            self.npey_table.setChecked(False)
421            self.npexnpey_table.setChecked(False)
422            self.nx_table.setChecked(False)
423            self.ngpts_table.setChecked(False)
424
425        if column == str("ngpts"):
426            sorted_col = "ngpts"
427
428            if self.ngpts_table.isChecked() is True:
429                order = " DESC"
430
431            else:
432
433                order = " ASC"
434
435            self.ny_table.setChecked(False)
436            self.nz_table.setChecked(False)
437            self.npex_table.setChecked(False)
438            self.npey_table.setChecked(False)
439            self.npexnpey_table.setChecked(False)
440            self.np_table.setChecked(False)
441            self.nx_table.setChecked(False)
442
443
444
445        conn = sqlite3.connect("/localdata/.palm_gf_data.db")
446        c = conn.cursor()
447        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) +
448                  " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
449        str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
450                  "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) +
451        " ORDER BY " + str(sorted_col) + str(order))
452
453
454
455        sorted = c.fetchall()
456
457        c.close()
458        conn.close()
459        self.tableWidget.setRowCount(len(sorted))
460
461        for row_indx in range(0,len(sorted)):
462
463            for col_indx in range(0,8):
464                row = sorted[row_indx]
465                value = row[col_indx]
466                if col_indx == 7:
467                    self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str("%.1e" % value)))
468                else:
469                    self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str(value)))
470
471
472
473    def sort_order(self):
474
475        sorted = 0
476        fnx_mn = self.nx_min.value()
477        fnx_mx = self.nx_max.value()
478        fny_mn = self.ny_min.value()
479        fny_mx = self.ny_max.value()
480        fnz_mn = self.nz_min.value()
481        fnz_mx = self.nz_max.value()
482        fnpex_mn = self.npex_min.value()
483        fnpex_mx = self.npex_max.value()
484        fnpey_mn = self.npex_min.value()
485        fnpey_mx = self.npey_max.value()
486        fnpxnpy_mn = self.npxnpy_min.value()
487        fnpxnpy_mx = self.npxnpy_max.value()
488        fnp_mn = self.np_min.value()
489        fnp_mx = self.np_max.value()
490        fngpts_mn = self.ngpts_min.value()
491        fngpts_mx = self.ngpts_max.value()
492
493        if str(self.Sortvariable_box.currentIndex()) == str(1):
494
495
496            sorted_col = "nx"
497        if str(self.Sortvariable_box.currentIndex()) == str(2):
498
499            sorted_col = "ny"
500        if str(self.Sortvariable_box.currentIndex()) == str(3):
501
502            sorted_col = "nz"
503        if str(self.Sortvariable_box.currentIndex()) == str(4):
504
505            sorted_col = "npex"
506        if str(self.Sortvariable_box.currentIndex()) == str(5):
507
508            sorted_col = "npey"
509        if str(self.Sortvariable_box.currentIndex()) == str(6):
510
511            sorted_col = "npxnpy"
512        if str(self.Sortvariable_box.currentIndex()) == str(7):
513
514            sorted_col = "np"
515        if str(self.Sortvariable_box.currentIndex()) == str(8):
516
517            sorted_col = "ngpts"
518
519        #print(self.Sortvariable_box.currentIndex())
520
521        if str(self.Sortorder_box.currentIndex()) == str(1):
522
523            order = " ASC"
524
525        if str(self.Sortorder_box.currentIndex()) == str(2):
526
527            order = " DESC"
528
529
530
531        conn = sqlite3.connect("/localdata/.palm_gf_data.db")
532        c = conn.cursor()
533        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) +
534                  " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
535        str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
536                  "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) +
537        " ORDER BY " + str(sorted_col) + str(order))
538
539
540
541        sorted = c.fetchall()
542
543        c.close()
544        conn.close()
545        self.tableWidget.setRowCount(len(sorted))
546
547        for row_indx in range(0,len(sorted)):
548
549            for col_indx in range(0,8):
550                row = sorted[row_indx]
551                value = row[col_indx]
552                self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str(var)))
553
554
555
556
557
558
559        #print(len(sorted))
560        #print(sorted)
561
562    def get_path(wildcard):
563        import wx
564        app = wx.App(None)
565        style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
566        dialog = wx.FileDialog(None, 'Open', wildcard='*.db')
567        if dialog.ShowModal() == wx.ID_OK:
568            path = dialog.GetPath()
569
570            print(path, file)
571        else:
572            path = None
573        dialog.Destroy()
574        return
575
576
577
578
579
580
581
582
583
584
585
586if __name__ == "__main__":
587    app = QtGui.QApplication(sys.argv)
588    window = MyApp3()
589    window.setWindowTitle('Gridfilter')
590    window.show()
591    sys.exit(app.exec_())
592
Note: See TracBrowser for help on using the repository browser.