7 from pprint import pprint
19 c.execute("SELECT s.id sid,p.id pid FROM sensors s,st_parameters p where s.st_id=p.st_id and p.id>=0")
22 print "No connection to DB"
26 dt = datetime.datetime.now()
27 d_truncated = datetime.date(dt.year, dt.month, dt.day)
32 return dt + datetime.timedelta(days=1)
36 return dt - datetime.timedelta(days=1)
39 dt = datetime.date(2000,01,01)
42 def GetData(sid,pid,fromDate=Yesterday(),toDate=Today()):
45 c.execute("SELECT id,timestamp,value FROM sensor_values WHERE sensor_id=%s and parameter_id=%s and timestamp>=%s AND timestamp<%s",[sid,pid,fromDate.strftime('%Y-%m-%d %H:%M:%S'),toDate.strftime('%Y-%m-%d %H:%M:%S')])
48 print "No connection to DB"
51 def FixRecord(id,value):
54 command="UPDATE sensor_values SET value={} WHERE id='{}'".format(value,id)
57 print "No connection to DB"
60 def ProcessTable(sid,pid):
63 data=GetData(sid,pid,Prehistoric(),Today())
67 data=GetData(sid,pid,Today(),Tomorrow())
79 sValue=np.array(sValue)
81 sValueFilt=scipy.signal.medfilt(sValue,5)
83 sValueDiff=abs(sValue-sValueFilt)
85 avg=np.mean(sValueDiff)
87 for i in range(0,len(sTime)-1):
88 if sValueDiff[i]>avg*filterThreshold:
89 print "fixing %s : %5.2f %5.2f %5.2f" % (sTime[i],sValue[i],sValueFilt[i],sValueDiff[i])
90 FixRecord(sID[i],sValueFilt[i])
94 if len(sys.argv)==2 and sys.argv[1]=='current':
99 if len(sys.argv)==2 and sys.argv[1]=='all':
106 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
107 cfg.readfp(open('/etc/weathermon.conf'))
108 dbhost = cfg.get("mysql","host")
109 dbuser = cfg.get("mysql","user")
110 dbpasswd = cfg.get("mysql","passwd")
111 dbdb = cfg.get("mysql","db")
113 filterWindow = int(cfg.get("filter","window"))
114 filterThreshold = float(cfg.get("filter","threshold"))
118 print "Error reading configuration file"
123 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbdb,use_unicode=True)
124 database.set_character_set('utf8')
125 c = database.cursor()
126 c.execute('SET NAMES utf8;')
132 print "Error connecting database"
137 for sid,pid in tables:
139 print "Processing sensor %d, parameter %d " % (sid,pid)
141 ProcessTable(sid,pid)