Index: palm/trunk/SCRIPTS/palm_gf
===================================================================
--- palm/trunk/SCRIPTS/palm_gf	(revision 2117)
+++ palm/trunk/SCRIPTS/palm_gf	(revision 2308)
@@ -42,4 +42,5 @@
 import sys
 import os
+import time
 import palm_gf_files.palm_gf_exec as execute_script
 import palm_gf_files.palm_gf_conf as config_wr
@@ -47,5 +48,7 @@
 import subprocess as sub
 
-from PyQt4 import QtCore, QtGui, uic
+
+from PyQt4 import QtCore, QtGui, uic, Qt
+from PyQt4.QtCore import QCoreApplication
 from palm_gf_files.palm_gf_tools import MyApp3
 
@@ -67,5 +70,5 @@
 
 class MyApp1(QtGui.QMainWindow, Ui_MainWindow):
-    def __init__(self, parent=None):
+    def __init__(self):
 
 
@@ -81,6 +84,15 @@
         self.move(frameGm.topLeft())
 
+
+        #centerPoint = str(centerPoint)
+        #xcenter = centerPoint.split('(')[1].split(',')[0]
+        #ycenter = centerPoint.split('(')[1].split(',')[1].split(')')[0]
+        #print xcenter, ycenter
+        #centerPoint = QtCore.QPoint(int(xcenter) - 418, int(ycenter))
+        #frameGm.moveCenter(centerPoint)
+        #self.move(frameGm.topLeft())
+
         self.setupUi(self)
-        self.startbutton.clicked.connect(self.run_program)  # starts grid_calculation
+        self.startbutton.clicked.connect(self.input_check)  # starts grid_calculation
         self.psolver_box.currentIndexChanged.connect(self.vis)  # triggers visibility of fft_method option
         self.save_button.clicked.connect(self.save_results)  # saves .db file under given name
@@ -89,29 +101,61 @@
         self.result_headline.setVisible(False)
         self.result_label.setVisible(False)
-        self.line_res_up.setVisible(False)
+        #self.line_res_up.setVisible(False)
         self.line_res_down.setVisible(False)
         self.view_result_button.setVisible(False)
-        self.tableWidget.setVisible(False)
+        #self.tableWidget.setVisible(False)
         self.load_text_label.setVisible(False)
         self.nor_spinbox.setVisible(False)
         self.filename_line.setVisible(False)
         self.save_button.setVisible(False)
+        self.testbar.setVisible(False)
+        self.d_box.setVisible(False)
+        self.tpn_box.setVisible(False)
+        self.tolerance_bar.setVisible(False)
+        self.tolerance_value.setVisible(False)
         self.view_result_button.clicked.connect(self.load_gui)
-
-        #self.startbutton.clicked.connect(self.progress)
-        #self.progressBar.setEnabled(False)
-        #self.progressBar.setVisible(False)
-
-    #def progress(self):
-    #    import time
-    #    self.progressBar.setVisible(True)
-    #    self.progressBar.setDisabled(False)
-    #    self.progressBar.setValue(10)
-    #    time.sleep(1)
-    #    self.progressBar.setValue(50)
-    #    time.sleep(1)
-    #    self.progressBar.setValue(100)
-    #    time.sleep(1)
-    #    self.progressBar.setEnabled(False)
+        self.threadclass = Threadclass()
+        self.actionSetting.triggered.connect(self.settings)
+        self.question_box.clicked.connect(self.settings)
+        self.warning_label.setVisible(False)
+
+
+
+
+    def closeEvent(self, QCloseEvent):
+
+        config_wr.closing_cleanup()
+        QCloseEvent.accept()
+
+
+    def input_check(self):
+
+        if int(self.nx_min_box.value()) <= int(self.nx_max_box.value()) and int(self.ny_min_box.value()) <= int(self.nx_max_box.value()) and int(self.nz_min_box.value()) <= int(self.nz_max_box.value()):
+            self.warning_label.setVisible(False)
+            self.run_program()
+
+        else:
+
+            self.warning_label.setStyleSheet('color: red')
+            self.warning_label.setVisible(True)
+
+
+
+
+
+
+
+    def settings(self):
+
+        self.w = MyPopup()
+        #self.w.setGeometry(Qt.QRect(100, 100, 400, 200))
+        frameGm = self.frameGeometry()
+        screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
+        centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
+        frameGm.moveCenter(centerPoint)
+        self.w.move(frameGm.topLeft())
+        self.w.show()
+
+
 
     def vis(self):
@@ -126,43 +170,25 @@
 
     def load_gui(self):
-        print("not yet possible")
         self.child_win = MyApp3()
         self.child_win.show()
-        #self.load_results_2()
-        #MyApp2(self).show()
-        # import filter_script
-        # upt = filter_script.MyApp2()
-        # upt.cur_grid()
-
-    def load_results(self):
-        self.setGeometry(330, 250, 584, 622)
-        self.tableWidget.setVisible(True)
-        conn = sqlite3.connect(".palm_gf_data.db")
-        c = conn.cursor()
-        nor = int(self.nor_spinbox.value())
-        self.tableWidget.setRowCount(nor)
-        c.execute("SELECT * FROM grid_current")
-        results = c.fetchmany(nor)
-        i = 0
-        j = 0
-
-        while i < nor:
-            line = results[i]
-            while j < 6:
-                var = line[j]
-
-                self.tableWidget.setItem(i, j, QtGui.QTableWidgetItem(str(var)))
-
-                j += 1
-            j = 0
-            i += 1
-
-        c.close()
-        conn.close()
+
+    def run_transfer(self):
+        self.threadclass = Threadclass()
+        self.connect(self.threadclass, QtCore.SIGNAL('finish'), self.behind_run)
+        #self.connect(self.threadclass, QtCore.SIGNAL('progress'), self.up2date)
+        self.connect(self.threadclass, QtCore.SIGNAL('startqmsg'), self.takeoff)
+        self.connect(self.threadclass, QtCore.SIGNAL('check'), self.checkme)
+        self.threadclass.runx()
 
     def run_program(self):
-        self.setEnabled(False)
+
+
+        self.result_label.setText('Starting calculation...')
+        #self.QtCore.QCoreApplication.processEvents()
+        QtGui.QApplication.processEvents()
+        #self.setEnabled(False)
         # self.Ui_MainWindow.setDisabled(True)
-        self.qmsg()
+        #self.qmsg_wait(0)
+        #print("bool state ", self.npex_npey_box.checkState())                                 # XXX
         np_min = str(self.np_min_box.value())
         np_max = str(self.np_max_box.value())
@@ -175,5 +201,9 @@
         tpn = str(self.tpn_box.value())
         dnpexnpey = str(self.d_box.value())
+        dnpexnpey_tolerance = str(self.tolerance_value.value())
         dnxny = str(self.dnxny_box.value())
+        myobject = MyPopup()
+        ld_thrs = myobject.load_thres.value()
+        rslt_thrs = myobject.result_thres.value()
 
         if str(self.psolver_box.currentIndex()) == str(1):
@@ -199,120 +229,67 @@
         else:
             spctre = False
-        print(poisfft, switch, temperton, mlt_grid, spctre)
-        config_wr.write_config(np_min, np_max, nx_min, nx_max, ny_min, ny_max, nz_min, nz_max, tpn, dnpexnpey, dnxny,
-                               str(poisfft), str(switch), str(temperton), str(mlt_grid), str(spctre))
-        execute_script.grid_executer()
-        #execute_script.grid_maxmin()
+
+        if int(self.strict_box.checkState()) != 2:
+            tpn = 0
+
+        if int(self.npex_npey_box.checkState()) != 2:
+            dnpexnpey = 0
+            dnpexnpey_tolerance = 0
+
+        #print(poisfft, switch, temperton, mlt_grid, spctre)
+        config_wr.write_config(np_min, np_max, nx_min, nx_max, ny_min, ny_max, nz_min, nz_max, tpn, dnpexnpey,dnpexnpey_tolerance, dnxny,
+                               str(poisfft), str(switch), str(temperton), str(mlt_grid), str(spctre), rslt_thrs, ld_thrs)
+        #time.sleep(1)
+        # execute_script.grid_executer() temp deactive
+        self.testbar.setVisible(True)
+        QtCore.QCoreApplication.processEvents()
+        self.run_transfer()
+
+
+    def behind_run(self):
+        trigger_bool = False
+        #print("behind_run executed")
         checkfile = open(".palm_gf_tmp", "r")
-        self.result_label.setText(str(checkfile.readline()))
+        #self.result_label.setText(str(checkfile.readline()))
+        res_text = str(checkfile.readline())          # XXX
+        self.result_label.setText(res_text)
+        result_nr = res_text.split(' ')[2]
         if int(checkfile.readline()) == 1:
             self.view_result_button.setVisible(True)
+            trigger_bool = True
         self.result_label.setVisible(True)
         self.result_headline.setVisible(True)
         self.line_res_down.setVisible(True)
-        self.line_res_up.setVisible(True)
+        #self.line_res_up.setVisible(True)
         self.load_text_label.setVisible(False)
         self.nor_spinbox.setVisible(False)
         checkfile.close()
-        self.setEnabled(True)
+        #self.setEnabled(True)
         self.filename_line.setVisible(True)
         self.save_button.setVisible(True)
-
-    def load_results_2(self):
-
-        database = str(".palm_gf_data.db")
-        print(database)
-        self.tableWidget.setVisible(True)
-        conn = sqlite3.connect(database)  # ".palm_gf_data.db"
-        c = conn.cursor()
-        c.execute("SELECT * FROM " + 'grid_current')  # "grid_current" database[:-3]
-        # c.execute("SELECT * FROM " + database)
-        results = c.fetchall()
-
-        self.tableWidget.setRowCount(len(results))
-
-        i = 0
-        j = 0
-
-        while i < len(results):
-            line = results[i]
-            while j < 6:
-                var = line[j]
-
-                self.tableWidget.setItem(i, j, QtGui.QTableWidgetItem(str(var)))
-
-                if i == 0 and j == 0:
-                    np_min = line[0]
-                    np_max = line[0]
-                    npex_min = line[1]
-                    npex_max = line[1]
-                    npey_min = line[2]
-                    npey_max = line[2]
-                    nx_min = line[3]
-                    nx_max = line[3]
-                    ny_min = line[4]
-                    ny_max = line[4]
-                    nz_min = line[5]
-                    nz_max = line[5]
-                else:
-                    if line[0] > np_max:
-                        np_max = line[0]
-                    if line[0] < np_min:
-                        np_min = line[0]
-                    if line[1] > npex_max:
-                        npex_max = line[1]
-                    if line[1] < npex_min:
-                        npex_min = line[1]
-                    if line[2] > npey_max:
-                        npey_max = line[2]
-                    if line[2] < npey_min:
-                        npey_min = line[2]
-                    if line[3] > nx_max:
-                        nx_max = line[3]
-                    if line[3] < nx_min:
-                        nx_min = line[3]
-                    if line[4] > ny_max:
-                        ny_max = line[4]
-                    if line[4] < ny_min:
-                        ny_min = line[4]
-                    if line[5] > nz_max:
-                        nz_max = line[5]
-                    if line[5] < nz_min:
-                        nz_min = line[5]
-
-                j += 1
-            j = 0
-            i += 1
-
-        c.close()
-        conn.close()
-
-        self.np_max.setValue(np_max)
-
-        self.np_min.setValue(np_min)
-        self.npex_max.setValue(npex_max)
-        self.npex_min.setValue(npex_min)
-        self.npey_max.setValue(npey_max)
-        self.npey_min.setValue(npey_min)
-        self.nx_max.setValue(nx_max)
-        self.nx_min.setValue(nx_min)
-        self.ny_max.setValue(ny_max)
-        self.ny_min.setValue(ny_min)
-        self.nz_max.setValue(nz_max)
-        self.nz_min.setValue(nz_min)
-        self.np_max.setMaximum(np_max)
-        self.np_min.setMinimum(np_min)
-        self.npex_max.setMaximum(npex_max)
-        self.npex_min.setMinimum(npex_min)
-        self.npey_max.setMaximum(npey_max)
-        self.npey_min.setMinimum(npey_min)
-        self.nx_max.setMaximum(nx_max)
-        self.nx_min.setMinimum(nx_min)
-        self.ny_max.setMaximum(ny_max)
-        self.ny_min.setMinimum(ny_min)
-        self.nz_max.setMaximum(nz_max)
-        self.nz_min.setMinimum(nz_min)
-
-        self.nr_output_label.setText(str(len(results)) + ' results')
+        parameters = config_wr.read_config()
+
+
+        if trigger_bool is True and int(result_nr) < int(parameters[18]):
+            self.result_label.setText(res_text + 'Loading Ui...')
+            self.load_gui()                                              # XXXX
+            self.result_label.setText(res_text)
+            #self.close()
+            #frameGm = self.frameGeometry()
+            #screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
+            #centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
+            #frameGm = self.frameGeometry()
+            #screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
+            #centerPoint = str(centerPoint)
+            #xcenter = centerPoint.split('(')[1].split(',')[0]
+            #ycenter = centerPoint.split('(')[1].split(',')[1].split(')')[0]
+            ##print xcenter, ycenter
+            #centerPoint = QtCore.QPoint(int(xcenter) - 292, int(ycenter))
+            #frameGm.moveCenter(centerPoint)
+            #self.move(frameGm.topLeft())
+
+
+
+
 
 
@@ -325,5 +302,6 @@
         if dialog.ShowModal() == wx.ID_OK:
             path = dialog.GetPath()
-            print(path)
+
+            print(path, file)
         else:
             path = None
@@ -357,4 +335,432 @@
         msgbox.exec_()
 
+    def tester(self):
+        #print("tester was executed from run method")
+        pass
+
+    def qmsg_wait(self, progress):
+        global msgbox
+        msgbox = QtGui.QMessageBox()
+        layout = msgbox.layout()
+        layout.itemAtPosition(layout.rowCount() - 1, 0).widget().hide()
+        global progressx
+        progressx = QtGui.QProgressBar()
+        layout.addWidget(progressx, layout.rowCount(), 0, 1, layout.columnCount())
+
+        msgbox.setWindowTitle("Progress")
+        msgbox.Information
+        msgbox.setText("Calculating, please be patient.")
+
+        progressx.setValue(int(progress))
+        #msgbox.setModal(False)
+        msgbox.exec_()
+
+    def up2date(self, progress):
+
+        progressx.setValue(int(progress))
+        if progress == 100:
+            msgbox.done(1)
+
+    def takeoff(self):
+        #self.qmsg_wait(0)
+        pass
+
+    def checkme(self, check):
+        #print(check)
+        self.testbar.setValue(int(check))
+
+        if check == 100:
+            self.testbar.setVisible(False)
+        QtGui.QApplication.processEvents()
+
+
+
+
+
+class Threadclass(QtCore.QThread):
+    def __init__(self, parent = None):
+        super(Threadclass, self).__init__(parent)
+
+    def runx(self):
+        #print("runx exec.")
+        self.execute()
+
+
+
+    def execute(self):
+        self.emit(QtCore.SIGNAL('startqmsg'), 0)
+
+        parameters = config_wr.read_config()
+        min_procs = int(parameters[0])
+        max_procs = int(parameters[1])
+        tpn = int(parameters[2])
+        dnpexnpey = float(parameters[3])
+        dnpexnpey_tol = int(parameters[4])
+        dnxny = float(parameters[5])
+        nx_min = int(parameters[6])
+        nx_max = int(parameters[7])
+        ny_min = int(parameters[8])
+        ny_max = int(parameters[9])
+        nz_min = int(parameters[10])
+        nz_max = int(parameters[11])
+        poisfft = parameters[12]
+        switch = parameters[13]
+        tempterton = parameters[14]
+        mlt_grid = parameters[15]
+        spectr = parameters[16]
+        result_thrs = parameters[17]
+
+
+        path = "/localdata/.palm_gf_data.db"
+        pathx = parameters[19] + '/.palm_gf_data.db'
+
+
+        conn = sqlite3.connect(pathx)
+        #conn = sqlite3.connect(".palm_gf_data.db")
+        c = conn.cursor()
+        c.execute("DROP TABLE IF EXISTS grid_current")
+        c.execute("DROP TABLE IF EXISTS grid_limits")
+        c.execute(
+            "CREATE TABLE IF NOT EXISTS grid_current(nx INT, ny INT, nz INT, npex INT, npey INT, npxnpy FLOAT, np INT, ngpts INT)")
+        c.execute(
+            "CREATE TABLE IF NOT EXISTS grid_limits(nx INT, ny INT, nz INT, npex INT, npey INT, npxnpy FLOAT, np INT, ngpts INT)")
+        conn.commit()
+        main_bool = True
+
+
+
+
+        if poisfft == str(True):
+            poisfft = True
+        else:
+            poisfft = False
+
+        if switch == str(True):
+            switch = True
+        else:
+            switch = False
+
+        if tempterton == str(True):
+            tempterton = True
+        else:
+            tempterton = False
+
+        if mlt_grid == str(True):
+            mlt_grid = True
+        else:
+            mlt_grid = False
+
+        if spectr == str(True):
+            spectr = True
+        else:
+            spectr = False
+
+        #print(spectr, type(spectr))
+        #print(poisfft, switch, tempterton, mlt_grid, spectr)
+
+        np_used = min_procs
+        counter = 0
+
+        nx = nx_min
+        ny = ny_min
+        nz = nz_min
+
+        from math import floor
+
+        def factors(n):
+            result = []
+            for i in range(2, n + 1):  # test all integers between 2 and n
+                s = 0
+                while n / i == floor(n / float(i)):  # is n/i an integer?
+                    n = n / float(i)
+                    s += 1
+                if s > 0:
+                    for k in range(s):
+                        result.append(i)  # i is a pf s times
+                        if n == 1:
+                            return result
+
+        while np_used <= max_procs:
+            a = 1
+
+            while a <= np_used:
+                prcs_var = np_used % a
+                if prcs_var != 0:
+                    a += 1
+                elif prcs_var == 0:
+                    npex = a
+                    npey = int(np_used / npex)
+
+                    if tpn != 0:                                   # XXX
+
+                        if np_used % tpn != 0:
+                            a += 1
+                            continue
+
+                    if dnpexnpey != 0 and npex / npey != dnpexnpey:
+                        a += 1
+                        continue
+
+                    if dnpexnpey != 0:
+
+
+                        if float(npex) / float(npey) < (dnpexnpey - dnpexnpey*dnpexnpey_tol/100):
+                            a += 1
+                            continue
+
+                        if float(npex) / float(npey) > (dnpexnpey + dnpexnpey*dnpexnpey_tol/100):
+                            a += 1
+                            continue
+
+                    while nx <= nx_max:
+                        if (nx + 1) % npex != 0:
+                            nx += 1
+                            continue
+
+                        if mlt_grid is True and (nx + 1) % 2 != 0:
+                            nx += 1
+                            continue
+
+                        if switch is True and (nx + 1) % npey != 0:
+                            nx += 1
+                            continue
+                        if npex > nx:
+                            nx += 1
+                            continue
+
+                        while ny <= ny_max:
+
+                            if dnxny != 0 and float(nx) / float(ny) != float(dnxny):
+                                ny += 1
+                                continue
+                            if (ny + 1) % npey != 0:
+                                ny += 1
+                                continue
+
+                            #if mlt_grid is True and ny % 2 != 0:       mlt and mlt_noOpt have same cond.
+                            #    ny += 1
+                            #    continue
+
+                            if (ny + 1) % npex != 0 and switch is True:
+                                ny += 1
+                                continue
+                            if npey > ny:
+                                ny += 1
+                                continue
+
+                            while nz <= nz_max:
+
+                                if mlt_grid is True and nz % 2 != 0:
+                                    nz += 1
+                                    continue
+
+                                if poisfft is True and nz % npex != 0:
+                                    nz += 1
+                                    continue
+
+                                if spectr is True and nz % npey != 0:
+                                    nz += 1
+                                    continue
+
+                                if tempterton is True and nx > 1 and ny > 1:  # and nz < 1:
+
+                                    nx_list = factors(nx + 1)
+
+                                    i = 0
+                                    nx_var = nx_list[i]
+
+
+                                    while i < len(nx_list):
+                                        if nx_var != 2 or nx_var != 3 or nx_var != 5:
+                                            i += 1
+                                            continue
+
+                                        i += 1
+                                    ny_list = factors(ny + 1)
+                                    i = 0
+                                    ny_var = ny_list[i]
+                                    while i < len(ny_list):
+                                        if ny_var != 2 or ny_var != 3 or ny_var != 5:
+                                            i += 1
+                                            continue
+                                        i += 1
+
+                                counter += 1
+                                if counter > int(result_thrs):
+                                    break
+
+                                npxnpy = format(float(npex) / float(npey), '.2f')
+                                c.execute(
+                                    """INSERT OR REPLACE INTO grid_current(nx, ny, nz, npex, npey, npxnpy, np, ngpts) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
+                                    (nx, ny, nz, npex, npey, npxnpy, (npex * npey), (nx * ny * nz)))
+
+                                nz += 1
+                            nz = nz_min
+                            ny += 1
+                        ny = ny_min
+                        nx += 1
+                    nx = nx_min
+                    a += 1
+                    #  a += 1
+            np_used += 1
+            progr_act = 100*(float(np_used-1)/float(max_procs))
+            self.emit(QtCore.SIGNAL('check'), progr_act)
+            #print(np_used, max_procs, progr_act)
+            self.emit(QtCore.SIGNAL('progress'), progr_act)
+
+            conn.commit()
+
+        conn.commit()
+        #c.close()
+        #conn.close()
+        #
+        ## ********************************
+        #
+        #conn = sqlite3.connect(parameters[19] + '/.palm_gf_data.db')
+        #print parameters[19] + '/.palm_gf_data.db'
+        #c = conn.cursor()
+        try:
+            c.execute("SELECT nx FROM grid_current ORDER BY nx DESC LIMIT 1")
+            mx_nx = c.fetchone()[0]
+            # print(mx_nx)
+            c.execute("SELECT nx FROM grid_current ORDER BY nx  LIMIT 1")
+            mn_nx = c.fetchone()[0]
+            # print(mn_nx)
+            c.execute("SELECT ny FROM grid_current ORDER BY ny DESC LIMIT 1")
+            mx_ny = c.fetchone()[0]
+            # print(mx_ny)
+            c.execute("SELECT ny FROM grid_current ORDER BY ny  LIMIT 1")
+            mn_ny = c.fetchone()[0]
+            # print(mn_ny)
+            c.execute("SELECT nz FROM grid_current ORDER BY nz DESC LIMIT 1")
+            mx_nz = c.fetchone()[0]
+            # print(mx_nz)
+            c.execute("SELECT nz FROM grid_current ORDER BY nz  LIMIT 1")
+            mn_nz = c.fetchone()[0]
+            # print(mn_nz)
+            c.execute("SELECT npex FROM grid_current ORDER BY npex DESC LIMIT 1")
+            mx_npex = c.fetchone()[0]
+            # print(mx_npex)
+            c.execute("SELECT npex FROM grid_current ORDER BY npex  LIMIT 1")
+            mn_npex = c.fetchone()[0]
+            # print(mn_npex)
+            c.execute("SELECT npey FROM grid_current ORDER BY npey DESC LIMIT 1")
+            mx_npey = c.fetchone()[0]
+            # print(mx_npey)
+            c.execute("SELECT npey FROM grid_current ORDER BY npey  LIMIT 1")
+            mn_npey = c.fetchone()[0]
+            # print(mn_npey)
+            c.execute("SELECT npxnpy FROM grid_current ORDER BY npxnpy DESC LIMIT 1")
+            mx_npxnpy = c.fetchone()[0]
+            # print(mx_npxnpy)
+            c.execute("SELECT npxnpy FROM grid_current ORDER BY npxnpy  LIMIT 1")
+            mn_npxnpy = c.fetchone()[0]
+            # print(mn_npxnpy)
+            c.execute("SELECT np FROM grid_current ORDER BY np DESC LIMIT 1")
+            mx_np = c.fetchone()[0]
+            # print(mx_np)
+            c.execute("SELECT np FROM grid_current ORDER BY np LIMIT 1")
+            mn_np = c.fetchone()[0]
+            # print(mn_np)
+            c.execute("SELECT ngpts FROM grid_current ORDER BY ngpts DESC LIMIT 1")
+            mx_ngpts = c.fetchone()[0]
+            # print(mx_ngpts)
+            c.execute("SELECT ngpts FROM grid_current ORDER BY ngpts LIMIT 1")
+            mn_ngpts = c.fetchone()[0]
+            # print(mn_ngpts)
+
+            conn.commit()
+            c.execute(
+                """INSERT OR REPLACE INTO grid_limits(nx, ny, nz, npex, npey, npxnpy, np, ngpts) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
+                (mn_nx, mn_ny, mn_nz, mn_npex, mn_npey, mn_npxnpy, mn_np, mn_ngpts))
+
+            c.execute(
+                """INSERT OR REPLACE INTO grid_limits(nx, ny, nz, npex, npey, npxnpy, np, ngpts) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
+                (mx_nx, mx_ny, mx_nz, mx_npex, mx_npey, mx_npxnpy, mx_np, mx_ngpts))
+            conn.commit()
+
+            c.close()
+            conn.close()
+        except TypeError:
+
+            checkfile = open(".palm_gf_tmp", "w")
+            if counter != 0:
+                checkfile.write("Gridfinder found " + str(counter) + " results.\n1")
+            else:
+                checkfile.write("Check input, no Results found.\n0")
+            checkfile.close()
+
+        checkfile = open(".palm_gf_tmp", "w")
+        if counter != 0:
+            checkfile.write("Gridfinder found " + str(counter) + " results.\n1")
+        else:
+            checkfile.write("Check input, no Results found.\n0")
+        checkfile.close()
+        self.emit(QtCore.SIGNAL('finish'), 1)
+        #print("finished with main execute")
+
+
+
+
+
+qtpopupFile = palm_bin + '/palm_gf_files/palm_gf_settings.ui'
+Ui_Pop, QtBaseClass = uic.loadUiType(qtpopupFile)
+
+
+class MyPopup(QtGui.QWidget, Ui_Pop):
+
+    def __init__(self):
+        Qt.QWidget.__init__(self)
+        Ui_Pop.__init__(self)
+
+        self.setupUi(self)
+        self.show
+        self.savepath.clicked.connect(self.path_to_save)
+        self.begin_check()
+        self.buttonBox.accepted.connect(self.end_check)
+        self.buttonBox.rejected.connect(self.close)
+
+
+    def begin_check(self):
+        file_check = palm_dir + '/trunk/SCRIPTS/.palm_gf_config'
+        if os.path.isfile(file_check) is True:
+            parameters = config_wr.read_config_settings()
+            self.linepath.setText(parameters[0])
+            self.result_thres.setValue(int(parameters[1]))
+            self.load_thres.setValue(int(parameters[2]))
+
+    def end_check(self):
+        config_wr.write_config_settings(self.linepath.text(), self.result_thres.value(), self.load_thres.value())
+
+        self.close()
+
+
+
+
+
+    def path_to_save(self, wildcard= None):
+        import wx
+        app = wx.App(None)
+        style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
+        dialog = wx.DirDialog(None, 'Open')
+        if dialog.ShowModal() == wx.ID_OK:
+            path = str(dialog.GetPath())
+
+            print(path)
+            self.linepath.setText(path)
+
+
+
+        else:
+            path = None
+        dialog.Destroy()
+        return
+
+
+
+
+
+
+
 
 if __name__ == "__main__":
