-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather_check.py
190 lines (148 loc) · 5.94 KB
/
weather_check.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
"""
Simple module to pull out the liklihood of rain within the next 48 hours
or less.
Requires:
- API Secret key
- latitude, Longitude
- Hours to look ahead when determining forecast
Returns:
- 0 for no need to salt
- 1 for need to salt
- negative numbers for errors
Documentation on the Darksky API is available here:
https://darksky.net/dev/
"""
# pylint: disable=C0103
import argparse
import logging
import requests
def retrieve_forecast(secret_key,
latitude,
longitude,
hours_from_now):
"""
Pull a forecast for a specific location from Darksky.
"""
if (secret_key is None or latitude is None or
longitude is None or hours_from_now is None):
logging.error('You haven\'t provided one or more required arguments')
exit(-1)
url_base = 'https://api.darksky.net/forecast'
excludes = 'flags,currently,minutely,daily,alerts'
units = 'si'
payload = {
'exclude': excludes,
'units': units
}
r = requests.get('{base}/{secret}/{latitude},{longitude}'.format(
base=url_base,
secret=secret_key,
latitude=latitude,
longitude=longitude), params=payload)
logging.debug('Compiled URL: %s', r.url)
logging.debug('Response Code: %d', r.status_code)
if r.status_code == 200:
need_salt = parse_request(r.json(), hours_from_now)
else:
logging.error('Something broke whilst attempting to retrieve a '
'forecast')
exit(-2)
return need_salt
def parse_request(response_data=None, hours_from_now=15):
"""
We just want to know if there is a liklihood of rain between 1700h and
0800h with a temperature below 0 degrees so we know we should spread
some salt...
"""
logging.debug('response_data parsing:')
if hours_from_now > 48:
# We only have 48 hours of forecast data, limit to that
hours_from_now = 48
if response_data is None:
raise IOError('Unable to retrieve forecast')
hourly_data = response_data['hourly']['data']
logging.debug(hourly_data)
lay_salt = True
too_much_precip = False
for item in range(0, hours_from_now - 1): # Decrement for 0 based list
precip_prob = hourly_data[item]['precipProbability']
precip_intensity = hourly_data[item]['precipIntensity']
temperature = hourly_data[item]['temperature']
dewpoint = hourly_data[item]['dewPoint']
logging.debug('\n')
logging.debug('Chance of Rain (out of 1): %s,', precip_prob)
logging.debug('Intensity of Rain (out of 1): %s,', precip_intensity)
logging.debug('Temp: %s,', temperature)
logging.debug('Dew Point: %s,', dewpoint)
if (temperature <= 0 and # Chance of frost
(temperature <= dewpoint or
(precip_prob > 0 and precip_prob <= 0.5 and
precip_intensity <= 0.3))):
# The above checks that its below freezing, and that either we are
# likely to see a dew/frost, or that its likely to rain lightly...
# Its worth laying some salt!
lay_salt = True
logging.debug('Salt should be laid')
else:
lay_salt = False
logging.debug('Salt should not be laid')
# return lay_salt
if precip_prob > 0.5 and precip_intensity > 0.3:
# Its likely to rain enough to wash away any salt we lay down
too_much_precip = True
return lay_salt and not too_much_precip
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Find out if we need to layout salt for ice the next'
' morning')
parser.add_argument(dest='hours_from_now',
type=int,
default=15,
help='How many hours from now do we need to check?')
parser.add_argument(dest='latitude',
type=float,
help='Latitude in decimal form, i.e 51.123456')
parser.add_argument(dest='longitude',
type=float,
help='Longitude in decimal form, i.e. -1.123456')
parser.add_argument('--log',
type=str,
required=False,
help='Set log level to:'
' DEBUG, INFO, WARNING, ERROR, CRITICAL')
parser.add_argument('--quiet',
dest='quiet',
action='store_true',
required=False,
help='Do not output result ot STDOUT')
parser.set_defaults(quiet=False)
parser.add_argument('--secret',
type=str,
required=True,
help='API Secret to authenticate against darksky.net,'
' retrieve yours from https://darksky.net/dev/account')
arguments = vars(parser.parse_args())
logFormatStr = '%(levelname)s:%(asctime)s %(message)s'
if arguments['log']:
logging.basicConfig(format=logFormatStr, level=arguments['log'].upper())
else:
logging.basicConfig(format=logFormatStr)
logging.info('Retrieving local weather forecast')
salt_needed = retrieve_forecast(secret_key=arguments['secret'],
latitude=arguments['latitude'],
longitude=arguments['longitude'],
hours_from_now=arguments['hours_from_now'])
message = ''
if salt_needed:
# salt needed
message = 'You need to lay out some salt tonight!'
salt_needed = 1
else:
# salt not needed
message = 'You don\'t need to worry about salt tonight.'
salt_needed = 0
logging.info(message)
if not arguments['quiet']:
print(message)
exit(salt_needed)