def calculateaveflow(self): PyQt4.QtGui.QApplication.setOverrideCursor(PyQt4.QtCore.Qt.WaitCursor) date_from = self.FromDateTime.dateTime().toPyDateTime() date_to = self.ToDateTime.dateTime().toPyDateTime() #Identify distinct set of obsid and instrumentid with Accvol-data and within the user-defined date_time-interval: sql= """select distinct obsid, instrumentid from(select * from w_flow where flowtype = "Accvol" and date_time >="%s" and date_time <="%s" and obsid IN %s)"""%(date_from,date_to,(str(self.observations)).encode('utf-8').replace('[','(').replace(']',')')) #utils.pop_up_info(sql)#debug uniqueset = utils.sql_load_fr_db(sql)[1] # The unique set of obsid and instrumentid is kept in uniqueset negativeflow = False for pyobsid, pyinstrumentid in uniqueset: sql= """select date_time, reading from w_flow where flowtype = 'Accvol' and obsid='%s' and instrumentid='%s' and date_time >='%s' and date_time <='%s' order by date_time"""%(pyobsid,pyinstrumentid,date_from,date_to) recs = utils.sql_load_fr_db(sql)[1] """Transform data to a numpy.recarray""" My_format = [('date_time', datetime.datetime), ('values', float)] #Define format with help from function datetime table = np.array(recs, dtype=My_format) #NDARRAY table2=table.view(np.recarray) # RECARRAY Makes the two columns into callable objects, i.e. write table2.values j=0 for row in table2:#This is where Aveflow is calculated for each obs and also written to db if j>0:#first row is "start-value" for Accvol and there is no Aveflow to be calculated Volume = (table2.values[j] - table2.values[j-1])*1000#convert to L since Accvol is supposed to be in m3 """ Get help from function datestr2num to get date and time into float""" DeltaTime = 24*3600*(datestr2num(table2.date_time[j]) - datestr2num(table2.date_time[j-1]))#convert to seconds since numtime is days Aveflow = Volume/DeltaTime#L/s if Aveflow<0: negativeflow = True sql = """insert or ignore into w_flow(obsid,instrumentid,flowtype,date_time,reading,unit) values('%s','%s','Aveflow','%s','%s','l/s')"""%(pyobsid,pyinstrumentid,table2.date_time[j],Aveflow) utils.sql_alter_db(sql) j = j + 1 if negativeflow: self.iface.messageBar().pushMessage("Info","Please notice that negative flow was encountered.", 0, duration=5) PyQt4.QtGui.QApplication.restoreOverrideCursor() self.close()
def vacuum_db(self): force_another_db = False if self.db: use_current_db = utils.Askuser("YesNo","""Vill du packa %s?"""%self.db,'Which database?') if use_current_db.result == 1: dbpath = self.db force_another_db = True elif use_current_db.result == 0: force_another_db = True elif use_current_db.result == '': return if not self.db or force_another_db: dbpath = QFileDialog.getOpenFileName(None, 'Ange db som ska packas','',"Spatialite (*.sqlite)")[0] QApplication.setOverrideCursor(Qt.WaitCursor) utils.sql_alter_db(dbpath,'vacuum') QApplication.restoreOverrideCursor()
def ChangedLocale(self): # TODO: remove in version 1.4 sql = u"select description from about_db where description like 'locale:%'" connection_ok, result = utils.sql_load_fr_db(sql) if not self.locale_combobox.currentText(): return if connection_ok: print(str(result)) if len(result) > 1: utils.MessagebarAndLog.info(bar_msg=u'More than one row with locale found in db. No update can be done.') return if len(result) == 1: sql = u"update or ignore about_db set description='locale:%s'"%self.locale_combobox.currentText() sql += u" where description like 'locale:%'" utils.sql_alter_db(sql) elif len(result) == 0: sql = u"insert or ignore into about_db (description) values ('locale:%s')"%self.locale_combobox.currentText() utils.sql_alter_db(sql)
def vacuum_db(self): force_another_db = False if self.db: use_current_db = utils.Askuser("YesNo", """Vill du packa %s?""" % self.db, 'Which database?') if use_current_db.result == 1: dbpath = self.db force_another_db = True elif use_current_db.result == 0: force_another_db = True elif use_current_db.result == '': return if not self.db or force_another_db: dbpath = QFileDialog.getOpenFileName(None, 'Ange db som ska packas', '', "Spatialite (*.sqlite)")[0] QApplication.setOverrideCursor(Qt.WaitCursor) utils.sql_alter_db(dbpath, 'vacuum') QApplication.restoreOverrideCursor()
def drop_db_views(self): sql1="delete from views_geometry_columns where view_name = 'strat_obs_p_for_qgsi2threejs'" sql2="drop view if exists strat_obs_p_for_qgsi2threejs" utils.sql_alter_db(sql1) utils.sql_alter_db(sql2) sql1="delete from views_geometry_columns where view_name = ?" sql2="drop view if exists " for key in self.strat_layers_dict: utils.sql_alter_db_by_param_subst(sql1,(key,)) utils.sql_alter_db(sql2 + key)