-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmet_weather.py
executable file
·244 lines (171 loc) · 7.8 KB
/
met_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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# get weather from Met Office
# by Sean Danischevsky
# install: I used:
# /usr/local/bin/python3 -m pip install geojson suncalc python-dateutil
# /usr/local/bin/python3 -m pip install timezonefinder[numba] --user # also installs optional dependencies for increased performance
#later in venv I used venv:
# cd weatherWHAT
# python -m venv venv
# source venv/bin/activate
# pip install geojson suncalc python-dateutil timezonefinder geopy
# support: https://groups.google.com/g/metoffice-datapoint
import api
import ss_download
# from https://raw.githubusercontent.com/MetOffice/weather_datahub_utilities/main/site_specific_download/ss_download.py
# i replaced print(req.text)
# with return (req.json())
import datetime
from dateutil import tz
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
def get_now(lon, lat):
now= datetime.datetime.now().astimezone(datetime.timezone.utc)
local_timezone_name= get_local_timezone_name(lon, lat)
local_now= convert_utc_to_local(now, local_timezone_name)
return now, local_timezone_name, local_now
def get_local_timezone_name(lon, lat):
#given a longitude and latitude, return the local time name, e.g. 'Europe/Berlin'
from timezonefinder import TimezoneFinder
tf = TimezoneFinder()
return tf.timezone_at(lng= lon, lat= lat) # 'Europe/Berlin'
def convert_from_iso(date_string):
# input time (date_string): 2022-08-01T17:00Z
# outputs timezone-aware datetime object, in UTC
import datetime
return datetime.datetime.fromisoformat(date_string[:-1]).astimezone(datetime.timezone.utc)
def convert_utc_to_local(datetime_object, local_timezone_name):
# given a datetime object as UTC
# and a local timezone name, e.g. 'Europe/Berlin'
# return local time
from dateutil import tz
to_zone = tz.gettz(local_timezone_name)
#print (to_zone) # tzfile('/usr/share/zoneinfo/Europe/Berlin')
return datetime_object.astimezone(to_zone)
def get_next_sunrise_or_sunset_msg(now, lon, lat, local_timezone_name):
import suncalc
import datetime
try:
suncalc_times= suncalc.get_times(now, lon, lat)
sunrise= suncalc_times['sunrise']
sunrise_utc= datetime.datetime.fromtimestamp(sunrise.replace(tzinfo=datetime.timezone.utc).timestamp(), tz=datetime.timezone.utc)
sunset= suncalc_times['sunset']
sunset_utc= datetime.datetime.fromtimestamp(sunset.replace(tzinfo=datetime.timezone.utc).timestamp(), tz=datetime.timezone.utc)
if (sunrise_utc < now < sunset_utc):
#it's day time
next_sunrise_or_sunset_msg= "sunset\n{}".format(convert_utc_to_local(sunset_utc, local_timezone_name).strftime("%H:%M"))
else:
# night time
next_sunrise_or_sunset_msg= "sunrise\n{}".format(convert_utc_to_local(sunrise_utc, local_timezone_name).strftime("%H:%M"))
except AttributeError:
#We're at the North pole and there's no sunset
next_sunrise_or_sunset_msg= ""
return next_sunrise_or_sunset_msg
# met office api significant weather codes
significantWeatherCode= {0: "Clear night",
1: "Sunny day",
2: "Partly cloudy (night)",
3: "Partly cloudy (day)",
4: "Not used",
5: "Mist",
6: "Fog",
7: "Cloudy",
8: "Overcast",
9: "Light rain shower (night)",
10: "Light rain shower (day)",
11: "Drizzle",
12: "Light rain",
13: "Heavy rain shower (night)",
14: "Heavy rain shower (day)",
15: "Heavy rain",
16: "Sleet shower (night)",
17: "Sleet shower (day)",
18: "Sleet",
19: "Hail shower (night)",
20: "Hail shower (day)",
21: "Hail",
22: "Light snow shower (night)",
23: "Light snow shower (day)",
24: "Light snow",
25: "Heavy snow shower (night)",
26: "Heavy snow shower (day)",
27: "Heavy snow",
28: "Thunder shower (night)",
29: "Thunder shower (day)",
30: "Thunder"}
def get_current_timestamp_index(forecast, given_time):
# parse met office JSON file to get nearest timestamp to given_time
features = forecast['features']
timeSeries= features[0]['properties']['timeSeries']
# return min(timeSeries, key=lambda x:abs(convert_from_iso(x['time'])- given_time))
idx= min(range(len(timeSeries)), key= lambda x: abs(convert_from_iso(timeSeries[x]['time'])- given_time))
idx= min(range(len(timeSeries)), key= lambda t: abs(
datetime.datetime.fromisoformat(timeSeries[t]['time'][:-1]).replace(tzinfo= datetime.timezone.utc)
- given_time))
return idx
def get_high_low_msg(timeSeries, now, local_timezone_name):
#parse met office JSON file to get highest temperature in next 24 hours
high= max(timeSeries, key= lambda time: time['screenTemperature'])
low= min(timeSeries, key= lambda time: time['screenTemperature'])
# choose hi/lo: earliest low, unless it's less than an hour away.
low_time_utc= datetime.datetime.fromtimestamp(convert_from_iso(low['time']).replace(tzinfo= datetime.timezone.utc).timestamp(), tz= datetime.timezone.utc)
high_time_utc= datetime.datetime.fromtimestamp(convert_from_iso(high['time']).replace(tzinfo= datetime.timezone.utc).timestamp(), tz= datetime.timezone.utc)
if (low_time_utc < high_time_utc) and (convert_from_iso(low['time']) > now):
# low is next
high_low_msg= "low {}°\n{}".format(str(round(low['screenTemperature'])), convert_utc_to_local(convert_from_iso(low['time']), local_timezone_name).strftime("%H:%M"))
else:
# high is next
high_low_msg= "high {}°\n{}".format(str(round(high['screenTemperature'])), convert_utc_to_local(convert_from_iso(high['time']), local_timezone_name).strftime("%H:%M"))
return high_low_msg
def make_default_icon_dirs():
import os
for forecast_icon in significantWeatherCode.values():
#print (icon)
#print (os.path.join(, icon))
basedir= os.path.join(os.path.dirname(__file__), 'icons','default', forecast_icon)
#print (basedir)
os.mkdir(basedir)
if __name__=="__main__":
#make_default_icon_dirs()
#exit()
now, local_timezone_name, local_now= get_now(api.lon, api.lat)
print ("Time now:", now)
print (f"Now as {local_timezone_name}: {local_now}")
# sunrise/sunset time
print (get_next_sunrise_or_sunset_msg(now, api.lon, api.lat, local_timezone_name))
# hourly forecast
forecast = ss_download.retrieve_forecast(ss_download.base_url, "hourly", {"apikey": api.key}, api.lat, api.lon, "FALSE", "TRUE")
#print (forecast)
#daily= get_daily_forecast(lon, lat)
#print (daily)
features= forecast['features']
timeSeries= features[0]['properties']['timeSeries']
idx= get_current_timestamp_index(forecast, now)
# current temperature
screenTemperature= timeSeries[idx]['screenTemperature']
temperature_msg= str(round(screenTemperature))+ "°"
print ("temperature:")
print(temperature_msg)
# significant weather code, e.g. 'Light rain shower (night)'
print(significantWeatherCode[timeSeries[idx]['significantWeatherCode']])
# hi / low temperature
print (get_high_low_msg(timeSeries[idx:][:24], now, local_timezone_name))
# summary_message,
# replace with forecast symbols for next days
# of there's an alert, replace with alert:
# https://www.metoffice.gov.uk/weather/guides/rss
# times (hours for rain, UV, etc)
hours=[convert_utc_to_local(convert_from_iso(t['time']), local_timezone_name).strftime("%H") for t in timeSeries[idx:][:24]]
print (hours)
# UV index
uvIndex= [t['uvIndex'] for t in timeSeries[idx:][:24]]
print ("uvIndex:")
print(uvIndex)
# Rain forecast
precipitationRate= [t['precipitationRate'] for t in timeSeries[idx:][:24]]
print ("precipitationRate:")
print(precipitationRate)
probOfPrecipitation= [t['probOfPrecipitation']/100.0 for t in timeSeries[idx:][:24]]
print ("probOfPrecipitation:")
print(probOfPrecipitation)