-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweather_sample.py
More file actions
161 lines (142 loc) · 3.64 KB
/
weather_sample.py
File metadata and controls
161 lines (142 loc) · 3.64 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/local/bin python
import time, socket
class ARCSATWeather:
def __init__(self):
None
def getValues(self):
data=[]
column=[]
temp=None
wind=None
windgusts=None
humidity=None
dewpt=None
sigma=None
encl=None
encl35m=None
""" Send cmd to UDP(host, port). Wait for and print the result. """
host='wxhost.apo.nmsu.edu'
port=6251
cmd='all\n'
#try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(2)
s.connect((host, port))
s.send(cmd)
reply = s.recv(1024)
s.close()
print reply
r=reply.split(' ')
#print r
for item in r:
if '=' in item:
i=item.split('=')
#print i
column.append(i[0])
data.append(i[1])
#except:
# print 'socket failed'
# return
try:
pos= column.index('airTemp')
temp=data[pos]
except:
temp = -100
try:
pos=column.index('winds')
wind=data[pos]
except:
wind=-1
try:
pos=column.index('gusts')
windgusts=data[pos]
except:
windgusts=-1
try:
pos=column.index('humidity')
humidity=data[pos]
except:
humidity=-100
try:
pos=column.index('encl35m')
encl=data[pos]
except:
encl=-1
try:
pos=column.index('encl25m')
encl25m=data[pos]
except:
encl25m=-1
try:
pos=column.index('irscsd')
sigma=data[pos]
except:
sigma=-1
print [temp, sigma,wind,windgusts,humidity, encl,encl25m]
return [temp, sigma,wind,windgusts,humidity, encl,encl25m]
def cloudCondition(self, c):
c=float(c)
if c==-1:
return 0
if c <15:
return 1
if c>=15 and c<30:
return 2
if c>=30:
return 3
def windCondition(self, w,g):
if w<10 and g<10:
return 1
if w>=10 and w<35 and g<35 or g>=10:
return 2
if w>=35 or g>=35:
return 3
if w==-1:
return 0
else:
return 0
def enclosure(self,e):
if e=='open':
return 1
if e=='close':
return 0
if e==-1:
return -1
def bypass(self):
arrCat=[]
arr=[]
f_in=open('config.dat','r')
for line in f_in:
if ';' in line:
None
else:
l=line.rstrip('\n').split('\t')
arr.append(l)
f_in.close()
return arr[0][1],arr[1][1]
def writeFile(self):
try:
val=self.getValues()
#print val
cloud=self.cloudCondition(val[1])
wind=self.windCondition(float(val[2]), float(val[3]))
byp = self.bypass()
if byp[0] == 'no':
if byp[1] == '35m':
enc=self.enclosure(val[5])
else:
enc=val[6]
else:
enc=1
#print cloud, wind, enc
print out
f_out=open('weather.log','w')
f_out.write(out)
f_out.close()
a=ARCSATWeather()
while True:
try:
a.writeFile()
time.sleep(30)
except:
print time.strftime("%Y-%m-%d %H:%M:%S failed")