[GRASS-SVN] r65314 - grass-addons/grass7/gui/wxpython/wx.mwprecip

svn_grass at osgeo.org svn_grass at osgeo.org
Sun May 24 14:42:56 PDT 2015


Author: krejcmat
Date: 2015-05-24 14:42:56 -0700 (Sun, 24 May 2015)
New Revision: 65314

Modified:
   grass-addons/grass7/gui/wxpython/wx.mwprecip/g.gui.mwprecip.py
   grass-addons/grass7/gui/wxpython/wx.mwprecip/mw3.py
   grass-addons/grass7/gui/wxpython/wx.mwprecip/mw_util.py
Log:
modify path mgr, quantile, GUI

Modified: grass-addons/grass7/gui/wxpython/wx.mwprecip/g.gui.mwprecip.py
===================================================================
--- grass-addons/grass7/gui/wxpython/wx.mwprecip/g.gui.mwprecip.py	2015-05-24 12:32:46 UTC (rev 65313)
+++ grass-addons/grass7/gui/wxpython/wx.mwprecip/g.gui.mwprecip.py	2015-05-24 21:42:56 UTC (rev 65314)
@@ -6,14 +6,17 @@
 sys.path.insert(1, os.path.join(os.path.dirname(sys.path[0]), 'etc', 'g.gui.mwprecip'))
 from mw_util import *
 from mw3 import *
+import tempfile
 
 from core.gcmd import GMessage, GError
 from gui_core import gselect
+import wx.lib.scrolledpanel as scrolled
 
 
-class DBconn(wx.Panel):
+class DBconn(wx.ScrolledWindow):
     def __init__(self, parent, settings={}):
-        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
+        wx.ScrolledWindow.__init__(self, parent,  wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL)
+        self.SetScrollRate( 5, 5 )
         self.settings = settings
         self.database = BaseInput(self, label='Name of database')
         self.schema = BaseInput(self, label='Name of schema')
@@ -81,9 +84,10 @@
         return self.settings
 
 
-class PointInterpolationPanel(wx.Panel):
+class PointInterpolationPanel(wx.ScrolledWindow):
     def __init__(self, parent, settings=None):
-        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
+        wx.ScrolledWindow.__init__(self, parent,  wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL)
+        self.SetScrollRate( 5, 5 )
         self.settings = settings
         self.interpolState = wx.CheckBox(self, label='interpolate points along links')
         self.interpolState.Bind(wx.EVT_CHECKBOX, self.onCheckInterpol)
@@ -126,30 +130,49 @@
         return self.settings
 
 
-class BaselinePanel(wx.Panel):
+class BaselinePanel(wx.ScrolledWindow):
     def __init__(self, parent, settings={}):
-        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
+        wx.ScrolledWindow.__init__(self, parent,  wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL)
+        self.SetScrollRate( 5, 5 )
         self.settings = settings
 
-        self.dryWin = wx.RadioButton(self, label='Compute from dry windows', style=wx.RB_GROUP)
+        self.noDryWin = wx.RadioButton(self, label='Compute without dry window', style=wx.RB_GROUP)
+        self.dryWin = wx.RadioButton(self, label='Compute from dry windows')
         self.fromFile = wx.RadioButton(self, label='Direct text input')
+
         self.baselTypeTxt = wx.StaticText(self, label='Select statistic method')
         self.baselType = wx.ComboBox(self, id=wx.ID_ANY, value="quantile", choices=['avg', 'mode', 'quantile'])
+        self.baselType.Bind(wx.EVT_ACTIVATE,self.onChangeStatistic)
+        self.baselType.Bind(wx.EVT_COMBOBOX,self.onChangeStatistic)
         self.round = BaseInput(self, 'Round data to "n" of decimal places')  # TODO MODE disable
         self.quantile = BaseInput(self, 'Set quantile in %')  # TODO quantile disable
         self.aw = BaseInput(self, 'Antena wetting value')
-        self.dryInterval = TextInput(self, 'Set interval of dry period')
+        self.dryInterval = TextInput(self, 'Set interval(s) of dry period')
         self.fromFileVal = TextInput(self, 'Set baseline values in csv format')
-        self.okBtt = wx.Button(self, wx.ID_OK, label='ok and close')
-        self.onChangeMethod(None)
+        #self.okBtt = wx.Button(self, wx.ID_OK, label='ok and close')
+        self.onChangeMethod()
+        self.onChangeStatistic()
+
         self.fromFile.Bind(wx.EVT_RADIOBUTTON, self.onChangeMethod)
         self.dryWin.Bind(wx.EVT_RADIOBUTTON, self.onChangeMethod)
-        self.okBtt.Bind(wx.EVT_BUTTON, self.saveSettings)
+        self.noDryWin.Bind(wx.EVT_RADIOBUTTON, self.onChangeMethod)
+        #self.okBtt.Bind(wx.EVT_BUTTON, self.saveSettings)
         if len(settings) > 0:
             self.loadSettings(None)
 
         self._layout()
+    def onChangeStatistic(self,evt=None):
+        if self.baselType.GetValue() == 'avg':
+            self.round.Disable()
+            self.quantile.Disable()
 
+        if self.baselType.GetValue() == 'mode':
+            self.round.Enable()
+            self.quantile.Disable()
+        if self.baselType.GetValue() == 'quantile':
+            self.round.Disable()
+            self.quantile.Enable()
+
     def loadSettings(self, sett=None):
         if sett is not None:
             self.settings = sett
@@ -157,6 +180,8 @@
             self.fromFile.SetValue(self.settings['fromFile'])
         if 'dryWin' in self.settings:
             self.dryWin.SetValue(self.settings['dryWin'])
+        if 'noDryWin' in self.settings:
+            self.noDryWin.SetValue(self.settings['noDryWin'])
         if 'quantile' in self.settings:
             self.quantile.SetValue(self.settings['quantile'])
         if 'baselType' in self.settings:
@@ -176,6 +201,7 @@
             self.settings = sett
         self.settings['fromFile'] = self.fromFile.GetValue()
         self.settings['dryWin'] = self.dryWin.GetValue()
+        self.settings['noDryWin'] = self.noDryWin.GetValue()
 
         self.settings['baselType'] = self.baselType.GetValue()
         self.settings['round'] = self.round.GetValue()
@@ -186,14 +212,15 @@
         return self.settings
 
     def onChangeMethod(self, evt=None):
-        if self.dryWin.GetValue() is False:
+        if self.fromFile.GetValue() is True:
             self.baselType.Disable()
             self.round.Disable()
             self.aw.Disable()
             self.dryInterval.Disable()
             self.quantile.Disable()
             self.fromFileVal.Enable()
-        else:
+
+        elif self.dryWin.GetValue() is True:
             self.baselType.Enable()
             self.round.Enable()
             self.aw.Enable()
@@ -201,8 +228,18 @@
             self.dryInterval.Enable()
             self.fromFileVal.Disable()
 
+        elif self.noDryWin.GetValue() is True:
+            self.baselType.Enable()
+            self.round.Enable()
+            self.aw.Enable()
+            self.quantile.Enable()
+            self.dryInterval.Disable()
+            self.fromFileVal.Disable()
+        self.onChangeStatistic()
+
     def _layout(self):
         sizer = wx.BoxSizer(wx.VERTICAL)
+        sizer.Add(self.noDryWin, flag=wx.EXPAND)
         sizer.Add(self.dryWin, flag=wx.EXPAND)
         sizer.Add(self.fromFile, flag=wx.EXPAND)
         sizer.Add(self.baselTypeTxt, flag=wx.EXPAND)
@@ -215,8 +252,8 @@
         sizer.Add(self.fromFileVal, flag=wx.EXPAND)
         sizer.AddSpacer(10, 0, wx.EXPAND)
         # sizer.Add(self.SLpanel, flag=wx.EXPAND)
-        sizer.Add(self.okBtt, flag=wx.EXPAND)
-        self.SetSizerAndFit(sizer)
+        #sizer.Add(self.okBtt, flag=wx.EXPAND)
+        self.SetSizer(sizer)
 
 
 '''
@@ -227,9 +264,10 @@
 '''
 
 
-class DataMgrMW(wx.Panel):
+class DataMgrMW(wx.ScrolledWindow):
     def __init__(self, parent, settings={}):
-        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
+        wx.ScrolledWindow.__init__(self, parent,  wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL)
+        self.SetScrollRate( 5, 5 )
         self.settings = settings
 
         self.stBoxTWIN = wx.StaticBox(self, wx.ID_ANY, 'Time windows MW')
@@ -486,30 +524,35 @@
         self.SetSizerAndFit(mainSizer)
 
 
-class MyFrame(wx.Frame):
+class MWMainFrame(wx.Frame):
     def __init__(self, parent, id, title):
-        wx.Frame.__init__(self, parent, id, title, size=(480, 640),style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
-        self.workPath = os.path.dirname(os.path.realpath(__file__))
+        wx.Frame.__init__(self, parent, id, title,style=wx.DEFAULT_FRAME_STYLE )
+        #self.workPath = tempfile.gettempdir()
+        self.workPath = os.path.join(pathToMapset(), "temp")
         self.initWorkingFoldrs()
         self.settings = {}
+        self.settings['workPath']=self.workPath
         self.settingsLst = []
         self.mainSizer = wx.BoxSizer(wx.VERTICAL)
         self.panelSizer = wx.BoxSizer(wx.VERTICAL)
         self.mainPanel = wx.Panel(self,id=wx.ID_ANY)
 
+
         menubar = wx.MenuBar()
         settMenu = wx.Menu()
-        fileMenu = wx.Menu()
         databaseItem = settMenu.Append(wx.ID_ANY, 'Database', 'Set database')
-        baselineItem = settMenu.Append(wx.ID_ANY, 'Baseline', 'Set baseline methods')
+        #baselineItem = settMenu.Append(wx.ID_ANY, 'Baseline', 'Set baseline methods')
         geometry = settMenu.Append(wx.ID_ANY, 'Geometry', 'Create vector geometry')
+        woringPath = settMenu.Append(wx.ID_ANY, 'Working Dir', 'Set working directory')
         quitItem = settMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
-        menubar.Append(settMenu, '&Options')
+        menubar.Append(settMenu, '&Menu')
 
         self.SetMenuBar(menubar)
         self.Bind(wx.EVT_MENU, self.onQuit, quitItem)
         self.Bind(wx.EVT_MENU, self.onSetDatabase, databaseItem)
-        self.Bind(wx.EVT_MENU, self.onSetBaseline, baselineItem)
+        self.Bind(wx.EVT_MENU, self.onSetWorkPath, woringPath)
+
+        #self.Bind(wx.EVT_MENU, self.onSetBaseline, baselineItem)
         self.Bind(wx.EVT_MENU, self.onSetGeometry, geometry)
 
         #def initNotebook(self):
@@ -519,11 +562,14 @@
         self.dataMgrMW.getEndBtt.Bind(wx.EVT_BUTTON, self.getMaxTime)
         self.dataMgrMW.getStartBtt.Bind(wx.EVT_BUTTON, self.getMinTime)
 
+        self.baselinePnl=BaselinePanel(self.ntb)
+
         #self.dataMgrRG = DataMgrMW(self.ntb )
-        self.pointInter = PointInterpolationPanel(self.ntb)
+        #self.pointInter = PointInterpolationPanel(self.ntb)
         self.ntb.AddPage(page=self.dataMgrMW, text='MW data')
+        self.ntb.AddPage(page=self.baselinePnl, text='Baseline')
         #self.ntb.AddPage(page=self.dataMgrRG, text='RG data')
-        self.ntb.AddPage(page=self.pointInter, text='Points Interpolation')
+        #self.ntb.AddPage(page=self.pointInter, text='Points Interpolation')
 
         self.grassLayers = GrassLayers(self.ntb, self.settings)
         self.ntb.AddPage(page=self.grassLayers, text='Colors')
@@ -572,7 +618,10 @@
             self.newScheme.Disable()
 
     def OnLoadSettings(self, evt=None):
+        print 'combobox'
         currSelId = self.profilSelection.GetSelection()
+        print(currSelId)
+        print  self.settingsLst[currSelId]
         self.settings = self.settingsLst[currSelId]
         try:
             self.dataMgrMW.loadSettings(self.settings)
@@ -594,6 +643,10 @@
             self.grassLayers.loadSettings(self.settings)
         except:
             pass
+
+
+
+
     def OnSaveSettings(self, evt=None, toFile=True):
         try:
             self.settings = self.dataMgrMW.saveSettings(sett=self.settings)
@@ -603,6 +656,7 @@
             # self.settings=self.dataMgrRG.saveSettings(sett=self.settings)
             # except:
         # pass
+
         try:
             self.settings = self.databasePnl.saveSettings(sett=self.settings)
         except:
@@ -615,16 +669,20 @@
             self.settings = self.grassLayers.saveSettings(sett=self.settings)
         except:
             pass
+
         self.settings['workSchema'] = self.profilSelection.GetValue()
+        #self.settings['workPath'] = self.workPath
+
         if self.schema.GetValue() is not None:
             self.settings['workSchema'] = self.schema.GetValue()
-
+        print self.settings
         if toFile:
             tmpPath = os.path.join(self.workPath, "save", self.settings['workSchema'])
             saveDict(tmpPath, self.settings)
             self.findProject()
 
     def initWorkingFoldrs(self):
+
         savePath = os.path.join(self.workPath, 'save')
         if not os.path.exists(savePath):
             os.makedirs(savePath)
@@ -692,34 +750,24 @@
         '''
         pass
 
-    def onSetBaseline(self, evt):
-        self.bsDialog = wx.Dialog(self, id=wx.ID_ANY,
-                                  title='Baseline settings',
-                                  style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
-                                  size=wx.DefaultSize,
-                                  pos=wx.DefaultPosition)
+    def onSetWorkPath(self,evt):
 
-        # self.bsDialog.SetSize((500, 500))
+        #f = tempfile.TemporaryFile()
+        dlg = wx.DirDialog(self,
+                           message="Select working directory",
+                           defaultPath=self.workPath,
+                           style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
 
-        if self.settings:
-            self.baselinePnl = BaselinePanel(self.bsDialog, self.settings)
-        else:
-            self.baselinePnl = BaselinePanel(self.bsDialog)
+        if dlg.ShowModal() == wx.ID_OK:
+            self.workPath = dlg.GetPath()
+            dlg.Destroy()
+            self.initWorkingFoldrs()
+            self.profilSelection.Clear()
+            self.settingsLst=[]
+            self.findProject()
 
-        self.baselinePnl.okBtt.Bind(wx.EVT_BUTTON, self._onSetBaselineDLG)
-        dbSizer = wx.BoxSizer(wx.VERTICAL)
-        dbSizer.Add(self.baselinePnl, flag=wx.EXPAND)
+        GMessage('Working path destination: %s' % self.workPath)
 
-        self.bsDialog.SetSizer(dbSizer)
-        self.bsDialog.SetBestFittingSize()
-        self.bsDialog.ShowModal()
-        self.bsDialog.Destroy()
-
-    def _onSetBaselineDLG(self, evt):
-        self.settings = self.baselinePnl.saveSettings()
-        print self.settings
-        self.bsDialog.Destroy()
-
     def onSetDatabase(self, evt):
         self.dbDialog = wx.Dialog(self, id=wx.ID_ANY,
                                   title='Database connection settings',
@@ -877,8 +925,8 @@
         interface.initVectorGrass()
 
         #if interpolate points along lines
-        if self.pointInter.interpolState.GetValue():
-            interface.initPInterpolation()
+        #if self.pointInter.interpolState.GetValue():
+        #    interface.initPInterpolation()
 
         interface.initTimeWinMW()
         interface.initBaseline()
@@ -917,8 +965,10 @@
 
     def initConnection(self, info=False):
         conninfo = None
+
         try:
             conninfo = {'name': self.settings['database']}
+
         except:
             GMessage('name of database is missing')
             return
@@ -934,7 +984,7 @@
             conninfo['port'] = self.settings['port']
         if 'passwd' in self.settings:
             conninfo['password'] = self.settings['passwd']
-
+        conninfo['workPath']=self.settings['workPath']
         if conninfo is None:
             self.connStatus = False
             GMessage('Database connection failed')
@@ -1000,21 +1050,29 @@
                 baselInit['type'] = 'values'
                 if 'fromFileVal' in self.settings:
                     baselInit['pathToFile'] = self.settings['fromFileVal']
+                    methodSel = True
                 else:
                     self.errMsg('Path to file with baseline values is not defined')
-                methodSel = True
 
+
         if 'dryWin' in self.settings:
             if self.settings['dryWin']:
                 baselInit['type'] = 'fromDryWin'
                 if 'dryInterval' in self.settings:
                     baselInit['pathToFile'] = self.settings['dryInterval']
+                    methodSel = True
                 else:
                     self.errMsg('Dry interval is not defined')
+
+
+        if 'noDryWin' in self.settings:
+            if self.settings['noDryWin']:
+                baselInit['type'] = 'noDryWin'
                 methodSel = True
 
         if not methodSel:
             self.errMsg('Baseline method is not selected')
+
         print baselInit
         self.baseline = Baseline(**baselInit)
 
@@ -1067,7 +1125,7 @@
 
     def initTemporalMgr(self):
         GrassTemporalMgr(self.dbConn, self.twin)
-        GMessage('Finish')
+        #GMessage('Finish')
 
     def errMsg(self, label):
         print label
@@ -1075,7 +1133,7 @@
 
 class MyApp(wx.App):
     def OnInit(self):
-        frame = MyFrame(None, -1, "MW manager")
+        frame = MWMainFrame(None, -1, "MW manager")
         frame.Show(True)
         self.SetTopWindow(frame)
         return True

Modified: grass-addons/grass7/gui/wxpython/wx.mwprecip/mw3.py
===================================================================
--- grass-addons/grass7/gui/wxpython/wx.mwprecip/mw3.py	2015-05-24 12:32:46 UTC (rev 65313)
+++ grass-addons/grass7/gui/wxpython/wx.mwprecip/mw3.py	2015-05-24 21:42:56 UTC (rev 65314)
@@ -14,6 +14,7 @@
 from core.gcmd import RunCommand
 from grass.pygrass.modules import Module
 
+import numpy as np
 
 try:
     from grass.script import core as grass
@@ -253,7 +254,7 @@
                 self.lon = float(f.next())
             f.close()
         except IOError as (errno, strerror):
-            print "I/O error({0}): {1}".format(errno, strerror)
+            grass.message( "I/O error({0}): {1}".format(errno, strerror))
 
         gaugeTMPfile = "gauge_tmp"
         removeLines(old_file=path,
@@ -270,7 +271,7 @@
                     tmp.append(stri)
                 f.close()
         except IOError as (errno, strerror):
-            print "I/O error({0}): {1}".format(errno, strerror)
+            grass.message( "I/O error({0}): {1}".format(errno, strerror))
 
         # write list of string to database
         try:
@@ -278,7 +279,7 @@
                 io.writelines(tmp)
                 io.close()
         except IOError as (errno, strerror):
-            print "I/O error({0}): {1}".format(errno, strerror)
+            grass.message( "I/O error({0}): {1}".format(errno, strerror))
 
         if not isTableExist(self.db.connection, self.schema, self.db.rgaugeTableName):
             # #create table for raingauge stations
@@ -310,7 +311,7 @@
 
 
 class Baseline():
-    def __init__(self, type, pathToFile, statFce='mode', quantile=97, roundMode=3, aw=0):
+    def __init__(self, type='noDryWin', pathToFile=None, statFce='mode', quantile=97, roundMode=3, aw=0):
         self.quantile = quantile
         self.roundMode = roundMode
         self.aw = aw
@@ -318,6 +319,7 @@
         self.type = type
         self.statFce = statFce
 
+        '''
         if statFce == 'mode':
             if self.roundMode is None:
                  grass.warning('Value "round" is  missing.')
@@ -330,10 +332,9 @@
         if self.type == 'values':
             if self.pathToFile is None:
                  grass.warning('Baseline values are not defined.')
-
         print self.pathToFile
+        '''
 
-
 class TimeWindows():
     def __init__(self, database, IDtype, sumStep, startTime=None,
                  endTime=None, linksIgnored=False, linksOnly=False, links=None, linksMap=None):
@@ -381,7 +382,7 @@
         self.crateTimeWin()
 
     def sumValues(self):
-        # #summing values per (->user)timestep interval
+        ##summing values per (->user)timestep interval
         self.viewDB = 'computed_precip_sum'
         sql = "CREATE %s %s.%s as  \
                SELECT %s ,round(avg(precip)::numeric,3) as %s, date_trunc('%s',time)as time  \
@@ -562,6 +563,8 @@
         database = self.database
         tMin = self.timeWin.timestamp_min
         tMax = self.timeWin.timestamp_max
+        startTime=self.timeWin.startTime
+        endTime=self.timeWin.endTime
 
         def computeBaselinFromMode(recordTable):
             sql = "SELECT linkid from %s group by 1" % recordTable
@@ -591,7 +594,10 @@
             # io1.write('mode|' + str(baseline.aw))
             # io1.close
 
+
         def computeBaselineFromTime():
+
+
             def chckTimeValidity(tIn):
                 # print tIn
                 tIn = str(tIn).replace("\n", "")
@@ -623,62 +629,71 @@
             tmp = []
             st = ''
             #print baseline.statFce
-            ######## AVG ##########
+            ######## AVG #########
             if baseline.statFce == 'avg':
-                try:
-                    #print baseline.pathToFile
-                    f = open(baseline.pathToFile, 'r')
-                except IOError as (errno, strerror):
-                    print baseline.pathToFile
-                    grass.warning('Path to file with dry-window definiton not exist; %s' % baseline.pathTofile)
 
-                for line in f:
-                    st += line.replace("\n", "")
-                    if 'i' in line.split("\n")[0]:  #get baseline form interval
-                        fromt = f.next()
-                        if not chckTimeValidity(fromt):
-                            return False
-                        st += fromt.replace("\n", "")
-                        tot = f.next()
-                        if not chckTimeValidity(tot):
-                            return False
-                        #validate input data
-                        if not isTimeValid(fromt) or not isTimeValid(tot):
-                            grass.warning("Input data are not valid. Parameter 'baselitime'")
-                            return False
 
-                        st += tot.replace("\n", "")
+                if baseline.type == 'noDryWin':
+                    if baseline.statFce == 'avg':
                         sql = "SELECT linkid, avg(a) FROM %s.record \
                                WHERE time >='%s' AND time<='%s' group by linkid order by 1" % (
-                            database.schema, fromt, tot)
+                            database.schema, startTime, endTime)
                         resu = database.connection.executeSql(sql, True, True)
                         tmp.append(resu)
+                else:
+                    try:
+                        #print baseline.pathToFile
+                        f = open(baseline.pathToFile, 'r')
+                    except IOError as (errno, strerror):
+                        #print baseline.pathToFile
+                        grass.warning('Path to file with dry-window definiton not exist; %s' % baseline.pathTofile)
+                    for line in f:
+                        st += line.replace("\n", "")
+                        if 'i' in line.split("\n")[0]:  #get baseline form interval
+                            fromt = f.next()
+                            if not chckTimeValidity(fromt):
+                                return False
+                            st += fromt.replace("\n", "")
+                            tot = f.next()
+                            if not chckTimeValidity(tot):
+                                return False
+                            #validate input data
+                            if not isTimeValid(fromt) or not isTimeValid(tot):
+                                grass.warning("Input data are not valid. Parameter 'baselitime'")
+                                return False
 
-                    else:  # baseline one moment
-                        time = line.split("\n")[0]
-                        #validate input data
-                        if not isTimeValid(time):
-                            grass.warning("Input data are not valid. Parameter 'baselitime'")
-                            return False
-                        try:
-                            time = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
-                        except ValueError:
-                            grass.message('Wrong datetime format')
-                            return False
-                        st += str(time).replace("\n", "")
-                        fromt = time + timedelta(seconds=-60)
-                        if not chckTimeValidity(fromt):
-                            return False
-                        tot = time + timedelta(seconds=+60)
-                        if not chckTimeValidity(tot):
-                            return False
-                        sql = "SELECT linkid, avg(a) FROM %s.record \
-                               WHERE time >='%s' AND time<='%s' group by linkid order by 1" % (
-                            database.schema, fromt, tot)
-                        resu = database.connection.executeSql(sql, True, True)
-                        tmp.append(resu)
-                        continue
+                            st += tot.replace("\n", "")
+                            sql = "SELECT linkid, avg(a) FROM %s.record \
+                                   WHERE time >='%s' AND time<='%s' group by linkid order by 1" % (
+                                database.schema, fromt, tot)
+                            resu = database.connection.executeSql(sql, True, True)
+                            tmp.append(resu)
 
+                        else:  # baseline one moment
+                            time = line.split("\n")[0]
+                            #validate input data
+                            if not isTimeValid(time):
+                                grass.warning("Input data are not valid. Parameter 'baselitime'")
+                                return False
+                            try:
+                                time = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
+                            except ValueError:
+                                grass.message('Wrong datetime format')
+                                return False
+                            st += str(time).replace("\n", "")
+                            fromt = time + timedelta(seconds=-60)
+                            if not chckTimeValidity(fromt):
+                                return False
+                            tot = time + timedelta(seconds=+60)
+                            if not chckTimeValidity(tot):
+                                return False
+                            sql = "SELECT linkid, avg(a) FROM %s.record \
+                                   WHERE time >='%s' AND time<='%s' group by linkid order by 1" % (
+                                database.schema, fromt, tot)
+                            resu = database.connection.executeSql(sql, True, True)
+                            tmp.append(resu)
+                            continue
+
                 mydict1 = {}
                 i = True
                 #print(tmp)
@@ -715,64 +730,70 @@
             ######## MODE or QUANTILE ##########
             elif baseline.statFce == 'mode' or baseline.statFce == 'quantile':
                 #print 'mode***'
-                try:
-                    print baseline.pathToFile
-                    f = open(baseline.pathToFile, 'r')
-                except IOError as (errno, strerror):
-                    grass.warning('Path to file with dry-window definiton not exist')
-                    return False
+
                 #parse input file
-                for line in f:
-                    st += line.replace("\n", "")
-                    if 'i' in line.split("\n")[0]:  #get baseline  intervals
-                        fromt = f.next()
-                        if not chckTimeValidity(fromt):
-                            return False
-                        st += fromt.replace("\n", "")
-                        tot = f.next()
-                        if not chckTimeValidity(tot):
-                            return False
-                        #validate input data
-                        if not isTimeValid(fromt) or not isTimeValid(tot):
-                            grass.warning("Input data are not valid. Parameter 'baselitime'")
-                            return False
-                        st += tot.replace("\n", "")
-                        sql = "SELECT linkid, a from  %s.record WHERE time >='%s' and time<='%s'" % (
-                            database.schema, fromt, tot)
-                        resu = database.connection.executeSql(sql, True, True)
-                        resu += resu
+                if baseline.type == 'noDryWin':
+                    sql = "SELECT linkid, a from  %s.record WHERE time >='%s' and time<='%s'" % (
+                    database.schema, startTime, endTime)
+                    resu = database.connection.executeSql(sql, True, True)
+                    database.connection.executeSql(sql, False, True)
+                else:
+                    try:
+                        #print baseline.pathToFile
+                        f = open(baseline.pathToFile, 'r')
+                    except IOError as (errno, strerror):
+                        grass.warning('Path to file with dry-window definiton not exist')
+                        return False
+                    for line in f:
+                        st += line.replace("\n", "")
+                        if 'i' in line.split("\n")[0]:  #get baseline  intervals
+                            fromt = f.next()
+                            if not chckTimeValidity(fromt):
+                                return False
+                            st += fromt.replace("\n", "")
+                            tot = f.next()
+                            if not chckTimeValidity(tot):
+                                return False
+                            #validate input data
+                            if not isTimeValid(fromt) or not isTimeValid(tot):
+                                grass.warning("Input data are not valid. Parameter 'baselitime'")
+                                return False
+                            st += tot.replace("\n", "")
+                            sql = "SELECT linkid, a from  %s.record WHERE time >='%s' and time<='%s'" % (
+                                database.schema, fromt, tot)
+                            resu = database.connection.executeSql(sql, True, True)
+                            resu += resu
 
-                    else:  #get baseline one moment
-                        time = line.split("\n")[0]
-                        if not isTimeValid(time):
-                            grass.warning("Input data are not valid. Parameter 'baselitime'")
-                            return False
-                        try:
-                            time = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
-                        except ValueError:
-                            grass.message('Wrong datetime format')
-                            return False
-                        st += str(time).replace("\n", "")
-                        fromt = time + timedelta(seconds=-60)
-                        if not chckTimeValidity(fromt):
-                            return False
-                        tot = time + timedelta(seconds=+60)
-                        if not chckTimeValidity(tot):
-                            return False
+                        else:  #get baseline one moment
+                            time = line.split("\n")[0]
+                            if not isTimeValid(time):
+                                grass.warning("Input data are not valid. Parameter 'baselitime'")
+                                return False
+                            try:
+                                time = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
+                            except ValueError:
+                                grass.message('Wrong datetime format')
+                                return False
+                            st += str(time).replace("\n", "")
+                            fromt = time + timedelta(seconds=-60)
+                            if not chckTimeValidity(fromt):
+                                return False
+                            tot = time + timedelta(seconds=+60)
+                            if not chckTimeValidity(tot):
+                                return False
 
-                        sql = "SELECT linkid,  a from %s.record WHERE time >='%s' and time<='%s'" % (
-                            database.schema, fromt, tot)
-                        resu = database.connection.executeSql(sql, True, True)
-                        resu += resu
-                        continue
-
+                            sql = "SELECT linkid,  a from %s.record WHERE time >='%s' and time<='%s'" % (
+                                database.schema, fromt, tot)
+                            resu = database.connection.executeSql(sql, True, True)
+                            resu += resu
+                            continue
                 tmp.append(resu)
-                table_mode_tmp = "mode_tmp"
-                sql = "CREATE TABLE %s.%s ( linkid integer,a real);" % (database.schema, table_mode_tmp)
+                table_tmp = baseline.statFce+ '_tmp'
+                sql = "CREATE TABLE %s.%s ( linkid integer,a real);" % (database.schema, table_tmp)
                 database.connection.executeSql(sql, False, True)
 
                 #write values to flat file
-                io = open(os.path.join(database.pathworkSchemaDir, "mode_tmp"), 'w+')
+                io = open(os.path.join(database.pathworkSchemaDir, table_tmp), 'w+')
                 c = 0
                 for it in tmp:
                     for i in it:
@@ -783,15 +804,15 @@
 
                 #update table
                 try:
-                    io1 = open(os.path.join(database.pathworkSchemaDir, "mode_tmp"), "r")
-                    database.connection.copyfrom(io1, "%s.%s" % (database.schema, table_mode_tmp))
+                    io1 = open(os.path.join(database.pathworkSchemaDir, table_tmp), "r")
+                    database.connection.copyfrom(io1, "%s.%s" % (database.schema, table_tmp))
                     io1.close()
-                    os.remove(os.path.join(database.pathworkSchemaDir, "mode_tmp"))
+                    os.remove(os.path.join(database.pathworkSchemaDir, table_tmp))
                 except IOError as (errno, strerror):
-                    grass.warning('Cannot open mode_tmp file')
+                    grass.warning('Cannot open <%s> file'% table_tmp)
                     return False
 
-                recname = database.schema + '.' + table_mode_tmp
+                recname = database.schema + '.' + table_tmp
 
                 if baseline.statFce == 'mode':
                     computeBaselinFromMode(recname)
@@ -799,16 +820,11 @@
                 if baseline.statFce == 'quantile':
                     computeBaselineFromQuentile(recname)
 
-                sql = "DROP TABLE %s.%s" % (database.schema, table_mode_tmp)
+                sql = "DROP TABLE %s.%s" % (database.schema, table_tmp)
                 database.connection.executeSql(sql, False, True)
 
-            #write  unique mark to file
-            io1 = open(os.path.join(database.pathworkSchemaDir, "compute_precip_info"), 'w+')
-            st = st + '|' + str(baseline.aw)
-            io1.write(st)
-            io1.close
             return True
-
+        '''
         def computeBaselineFromQuentile(recordTable):
             sql = "SELECT linkid from %s group by linkid" % recordTable
             linksid = database.connection.executeSql(sql, True, True)
@@ -826,19 +842,53 @@
                         ORDER BY quartile\
                         limit 1" % (baseline.quantile, recordTable, linkid)
 
+
                 resu = database.connection.executeSql(sql, True, True)
-                # print resu
+
                 resu = resu[0][0]
                 tmp.append(str(linkid) + ',' + str(resu) + '\n')
 
+
+
             io0 = open(os.path.join(database.pathworkSchemaDir, "baseline"), 'w+')
             io0.writelines(tmp)
             io0.close()
+        '''
+        def Quantile(data, q, precision=1.0):
+            """
+            Returns the q'th percentile of the distribution given in the argument
+            'data'. Uses the 'precision' parameter to control the noise level.
+            """
+            #data = np.random.normal(size=2000000)
+            q=float(q)/100
+            N, bins = np.histogram(data, bins=precision*np.sqrt(len(data)))
+            norm_cumul = 1.0*N.cumsum() / len(data)
+            ret=bins[norm_cumul > q][0]
+           # print "error in  %s quantile"%q, ((1.0*(data < ret).sum() / len(data)) -q)
 
-            io1 = open(os.path.join(database.pathworkSchemaDir, "compute_precip_info"), 'w+')
-            io1.write('quantile' + str(baseline.quantile) + '|' + str(baseline.aw))
-            io1.close
+            return ret
 
+        def computeBaselineFromQuentile(recordTable):
+            sql = "SELECT linkid from %s group by linkid" % recordTable
+            linksid = database.connection.executeSql(sql, True, True)
+            tmp = []
+            # for each link  compute baseline
+            for linkid in linksid:
+                linkid = linkid[0]
+                sql = "SELECT a from %s where linkid=%s"% (recordTable, linkid)
+                resu = database.connection.executeSql(sql, True, True)
+                data=np.array(resu)
+                #data=[item for sublist in data for item in sublist]#merge lists
+                #print data
+                #quantileRes=Quantile(data, baseline.quantile)
+
+                quantileRes=np.percentile(data,  (100-float(baseline.quantile))/100)
+                tmp.append(str(linkid) + ',' + str(quantileRes)+ '\n')
+            #print tmp
+            io0 = open(os.path.join(database.pathworkSchemaDir, "baseline"), 'w+')
+            io0.writelines(tmp)
+            io0.close()
+
         def readBaselineFromText(path):
             with open(path, mode='r') as infile:
                 reader = csv.reader(infile, delimiter=',')
@@ -849,15 +899,21 @@
             # print 'valuesDirectly'
             self.baselineDict = readBaselineFromText(self.baseline.pathTofile)
 
-        elif self.baseline.type == 'fromDryWin':
-            grass.message('Computing baselines "time interval" "%s"...' % self.baseline.statFce)
-            # print 'fromDryWin'
+        elif self.baseline.type == 'fromDryWin' :
+            grass.message('Computing baselines "dry window" "%s"...' % self.baseline.statFce)
             if computeBaselineFromTime():
                 self.baselineDict = readBaselineFromText(os.path.join(database.pathworkSchemaDir, 'baseline'))
                 return True
             else:
                 return False
 
+        elif  self.baseline.type == 'noDryWin':
+            grass.message('Computing baselines "no dry window" "%s"...' % self.baseline.statFce)
+            if computeBaselineFromTime():
+                self.baselineDict = readBaselineFromText(os.path.join(database.pathworkSchemaDir, 'baseline'))
+                return True
+            else:
+                return False
     def logMsg(self, msg):
         if self.status.get('msg') == 'Done':
             self.status['msg'] = ''
@@ -865,6 +921,15 @@
         grass.warning(msg)
 
     def computePrecip(self, getData=False, dataOnly=False):
+        def checkValidity(freq, polarization):
+            if freq < 10000000:
+                return False
+
+            if polarization is  'V' or polarization is 'H':
+                return True
+            return False
+
+
         Aw = float(self.baseline.aw)
         link_num = self.database.connection.count("link")
         compPrecTab = "%s.%s" % (self.database.schema, self.database.computedPrecip)
@@ -961,35 +1026,39 @@
         skippedList = []
         for record in resu:
             curLinkData = linksDict[record[0]]  # record[0] is linkid
-
             if curLinkData[1] is None:
                 if not record[0] in skippedList:
                     curLinkData[1] = 'V'  # TODO REMOVE THIS!!!!!!!!!!!!!!!!
-                    #self.logMsg('Polarization value is missing. Linkid<%s> wil be skipped' % record[0])
-                    #skippedList.append(record[0])
-                continue
-            # if missing baseline. Link will be skip
-            if record[0] in self.baselineDict and (curLinkData[2] / 1000000) > 10:  #TODO
-                # coef_a_k[alpha, k]
-                coef_a_k = self.computeAlphaK(curLinkData[2], curLinkData[1])
 
+            if record[0] in self.baselineDict:
+                '''
+                coef_a_k[alpha, k]
+                record[0] is linkid
+                record[1] is time
+                record[2] is tx-rx
+                curLinkData[0] is lenght
+                curLinkData[1] is polarization
+                curLinkData[2] is frequency HZ
+                '''
+                if checkValidity(curLinkData[2], curLinkData[1]):
+                    coef_a_k = self.computeAlphaK(curLinkData[2], curLinkData[1])
+                else:
+                    self.logMsg('Data of link <%s> are not valid'%record[0])
+                    continue
+
                 #read value from dictionary
                 baseline_decibel = (self.baselineDict[record[0]])
 
                 #final precipiatation is R1
-                Ar = record[2] - baseline_decibel - Aw
-                #TODO check this condition
-                '''computePrecip
-                R1 = (yr / coef_a_k[1]) ** (1 / coef_a_k[0])
-                ValueError: negative number cannot be raised to a fractional
-                power
-                '''
-                if Ar > 0:
-                    yr = Ar / curLinkData[0]
-                    R1 = (yr / coef_a_k[1]) ** (1 / coef_a_k[0])
-                    #print R1
+                Am = record[2] - baseline_decibel - Aw
+                yl = Am / curLinkData[0]
+                aa = yl / coef_a_k[1]
+                if aa <0:
+                    aa*=-1
+                    R1 = aa ** (1 / coef_a_k[0])
+                    R1*=-1
                 else:
-                    R1 = 0
+                    R1 = aa ** (1 / coef_a_k[0])
 
                 #string for output flatfile
                 out = str(record[0]) + "|" + str(record[1]) + "|" + str(R1) + "\n"
@@ -1024,7 +1093,7 @@
         """
         freq /= 1000000
 
-        if polarization == "h":
+        if polarization == "H":
             # Coefficients for kH    1
             aj_kh = (-5.33980, -0.35351, -0.23789, -0.94158)
             bj_kh = (-0.10008, 1.26970, 0.86036, 0.64552)
@@ -1199,7 +1268,7 @@
         for win in f.read().splitlines():
             layerNum += 1
             win = self.database.schema + '.' + win
-            print win
+            grass.message( win)
             RunCommand('v.db.connect',
                        driver='pg',
                        map=self.database.linkVecMapName,
@@ -1275,9 +1344,9 @@
         io1 = open(regFilePath, 'w+')
         io1.writelines(regTMP), io1.close
         io1.close()
-        print 'datasetName', self.datasetName
-        print regFilePath
-        #sys.exit()
+        grass.message( 'datasetName %s'% self.datasetName)
+        grass.message(regFilePath)
+
         RunCommand('t.register',
                    input=self.datasetName,
                    type='vector',
@@ -1289,7 +1358,7 @@
 class Database():
     def __init__(self, name=None, user=None, password=None,
                  host=None, port=None, nodeVecMapName='node', linkVecMapName='link',
-                 linkPointsVecName='linkPoints', workSchema=None, dataSchema=None):
+                 linkPointsVecName='linkPoints',workPath=None, workSchema=None, dataSchema=None):
         self.dbConnStr=name
         self.dbName = name
         self.user = user
@@ -1315,8 +1384,10 @@
         self.nodeVecMapName = nodeVecMapName
         self.linkVecMapName = linkVecMapName
         self.linkPointsVecName = linkPointsVecName
-        self.pathworkSchemaDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "tmp_%s" % self.schema)
 
+        self.pathworkSchemaDir = os.path.join(workPath, "tmp_%s" % self.schema)
+        #self.pathworkSchemaDir = os.path.join(tempfile.gettempdir(), "tmp_%s" % self.schema)
+
         self.pyConnection()
         #if self.host:
         self.grassConnectionRemote()

Modified: grass-addons/grass7/gui/wxpython/wx.mwprecip/mw_util.py
===================================================================
--- grass-addons/grass7/gui/wxpython/wx.mwprecip/mw_util.py	2015-05-24 12:32:46 UTC (rev 65313)
+++ grass-addons/grass7/gui/wxpython/wx.mwprecip/mw_util.py	2015-05-24 21:42:56 UTC (rev 65314)
@@ -11,8 +11,8 @@
 import wx.lib.filebrowsebutton as filebrowse
 import codecs
 from core.gcmd import GMessage, GError
+from grass.script import core as grass
 
-
 class SaveLoad(wx.Panel):
     def __init__(self, parent):
         wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
@@ -383,57 +383,6 @@
         self.pathInput.SetValue(value)
 
 
-
-class FileInput(wx.Panel):
-    def __init__(self, parent, label, dir=False, tmpPath=None):
-        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
-        self.dir = dir
-        self.tmpPath = tmpPath
-        statText = wx.StaticText(self, id=wx.ID_ANY, label=label)
-
-        self.pathInput = wx.TextCtrl(self, id=wx.ID_ANY)
-        self.browseBtt = wx.Button(self, id=wx.ID_ANY, label='Browse')
-
-        sizer = wx.BoxSizer(wx.VERTICAL)
-        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
-        sizer2.Add(self.pathInput, flag=wx.EXPAND, proportion=1)
-        sizer2.Add(self.browseBtt, flag=wx.EXPAND)
-
-        sizer.Add(statText, flag=wx.EXPAND)
-        sizer.Add(sizer2, flag=wx.EXPAND)
-
-        self.SetSizer(sizer)
-        self.browseBtt.Bind(wx.EVT_BUTTON, self.onBrowse)
-
-    def onBrowse(self, event):
-        if self.dir:
-            openFileDialog = wx.DirDialog(self, "Choose a directory:",
-                                          style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST | wx.DD_CHANGE_DIR)
-            if openFileDialog.ShowModal() == wx.ID_CANCEL:
-                return  # the user changed idea...
-            path = openFileDialog.GetPath()
-            self.pathInput.SetValue(path)
-
-
-        else:
-            openFileDialog = wx.FileDialog(self, "Choose a file:", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
-            if openFileDialog.ShowModal() == wx.ID_CANCEL:
-                return  # the user changed idea...
-            path = openFileDialog.GetPath()
-            self.pathInput.SetValue(path)
-
-
-    def GetPath(self):
-        path = self.pathInput.GetValue()
-        if len(path) != 0:
-            return path
-        else:
-            return None
-
-    def SetPath(self, value):
-        self.pathInput.SetValue(value)
-
-
 def YesNo(parent, question, caption='Yes or no?'):
     dlg = wx.MessageDialog(parent, question, caption, wx.YES_NO | wx.ICON_QUESTION)
     result = dlg.ShowModal() == wx.ID_YES
@@ -453,6 +402,7 @@
             if full:
                 tmp.append(os.path.join(fpath, path))
             else:
+
                 tmp.append(path)
     if len(tmp) > 0:
         return tmp
@@ -496,6 +446,9 @@
     output_stream = (saveFileDialog.GetPath())
     return output_stream
 
+def pathToMapset():
+    gisenvDict = grass.gisenv()
+    return os.path.join(gisenvDict['GISDBASE'], gisenvDict['LOCATION_NAME'], gisenvDict['MAPSET'])
 
 def saveDict(fn, dict_rap):
     f = open(fn, "wb")
@@ -508,18 +461,23 @@
 
 
 def readDict(fn):
-    f = open(fn, 'rb')
+    f = open(fn, 'r')
     dict_rap = {}
-    for key, val in csv.reader(f):
-        try:
-            dict_rap[key] = eval(val)
-        except:
-            val = '"' + val + '"'
-            dict_rap[key] = eval(val)
-    f.close()
-    return (dict_rap)
+    try:
+        for key, val in csv.reader(f):
+            try:
+                dict_rap[key] = eval(val)
+            except:
+                val = '"' + val + '"'
+                dict_rap[key] = eval(val)
+        f.close()
+        return (dict_rap)
+    except IOError as e:
+        print "I/O error({0}): {1}".format(e.errno, e.strerror)
 
 
+
+
 def randomWord(length):
     return ''.join(random.choice(string.lowercase) for i in range(length))
 
@@ -539,5 +497,4 @@
     seconds = (dt - dt.min).seconds
     # // is a floor division, not a comment on following line:
     rounding = (seconds + roundTo / 2) // roundTo * roundTo  # rounding floor
-    return dt + timedelta(0, rounding - seconds, -dt.microsecond)
-
+    return dt + timedelta(0, rounding - seconds, -dt.microsecond)
\ No newline at end of file



More information about the grass-commit mailing list