forked from saltastro/timDIMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_current_weather.py
96 lines (82 loc) · 3.4 KB
/
read_current_weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 27 13:25:00 2013
@author: jpk
This script returns the current weather information which was written by the
WeatherPlot script. The average values for the current weather conditions are
returned, as well as warning flags set by the WeatherPlot script. Checks were
build in to ensure the data received are valid.
"""
import MySQLdb as ml
import numpy as np
import datetime
def read_current_weather():
# open a mysql connection
try:
conn = ml.connect(host = "db2.suth.saao.ac.za",user = "weatherreader",passwd = "weatherreader",db = "suthweather")
cursor = conn.cursor()
# for any connection issue, return a FALSE validity flag
except ml.Error, e:
valid = False
return valid, [0]
# query the current weather table
cursor.execute("select * from current_weather")
temp = cursor.fetchall()
# close the mysql connection
cursor.close()
conn.close()
# make a recarray from the current weather
try:
cw = np.array(list(temp),dtype=[('datetime','S20'),
('avg_t_min_tdew', 'f4'),('avg_hum', 'f4'),
('avg_cloud', 'f4'),('avg_wind', 'f4'),
('avg_temp', 'f4'),('t_min_tdew_warn', 'i4'),
('hum_warn', 'i4'),('rain_warn', 'i4'),
('cloud_warn', 'i4'),('wind_warn', 'i4'),
('temp_warn', 'i4')])[0]
# if the retuned tuple is empty, no current weather data is available
except IndexError:
valid = False
return valid, [0]
# check the time stamp validity. All the times are in SAST
cw_time = datetime.datetime.strptime(cw['datetime'], '%Y-%m-%d %H:%M:%S')
timediff = datetime.datetime.now() - cw_time # make sure system time is in SAST
# if the current weather info is older than 10min return a FALSE validity
# flag
if timediff.seconds > 600:
valid = False
return valid, [0]
else:
valid = True
# if all the checks were passed, return the validity flag and
# the rec array with the current weather info
return valid, cw
if __name__ == "__main__":
valid, cw = read_current_weather()
if valid:
print ""
print "------------ Current Weather Info ------------"
print "TimeStamp (SAST) : ", cw['datetime']
print "Avg. T - T(dew) : ", cw['avg_t_min_tdew']
print "Avg. Humidity : ", cw['avg_hum']
print "Avg. Rel. Sky Temp : ", cw['avg_cloud']
print "Avg. Wind Speed : ", cw['avg_wind']
print "Avg. Temp : ", cw['avg_temp']
print "------------ Warnings ------------------------"
print " T - T(dew) Warn. : ", cw['t_min_tdew_warn']
print " Hum. Warn. : ", cw['hum_warn']
print " Sky Con. Warn. : ", cw['rain_warn']
print " Cloud Warn. : ", cw['cloud_warn']
print " Wind Speed Warn. : ", cw['wind_warn']
print " Temp Warn. : ", cw['temp_warn']
print ""
print ""
print " Warn = 0 --> All clear"
print " Warn = 1 --> Warning, Close to Closing Limits"
print " Warn = 2 --> Close Telescope"
print ""
print ""
else:
print ""
print "No current weather information is available, close telescope"
print ""