This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
forked from sbp/phenny
-
Notifications
You must be signed in to change notification settings - Fork 19
/
metar.py
306 lines (264 loc) · 8.73 KB
/
metar.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# metar.py
# Copyright (c) 2013, mutantmonkey <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
import datetime
INTENSITY = {
"-": "light",
"+": "heavy",
"VC": "in the vicinity:",
}
DESCRIPTOR = {
"MI": "shallow",
"PR": "partial",
"BC": "patches",
"DR": "low drifting",
"BL": "blowing",
"SH": "showers",
"TS": "thunderstorm",
"FZ": "freezing",
}
PRECIPITATION = {
"DZ": "drizzle",
"RA": "rain",
"SN": "snow",
"SG": "snow grains",
"IC": "ice crystals",
"PL": "ice pellets",
"GR": "hail",
"GS": "small hail",
"UP": "unknown precipitation",
}
OBSCURATION = {
"BR": "mist",
"FG": "fog",
"VA": "volcanic ash",
"DU": "widespread dust",
"SA": "sand",
"HZ": "haze",
"PY": "spray",
}
CLOUD_COVER = {
"SKC": "clear",
"CLR": "clear",
"NSC": "clear",
"FEW": "a few clouds",
"SCT": "scattered clouds",
"BKN": "broken clouds",
"OVC": "overcast",
"VV": "indefinite ceiling",
}
OTHER = {
"PO": "whirls",
"SQ": "squals",
"FC": "tornado",
"SS": "sandstorm",
"DS": "duststorm",
}
import re
class Weather(object):
cover = None
height = None
wind_speed = None
wind_direction = None
intensity = None
descriptor = None
precipitation = None
obscuration = None
other = None
conditions = None
def describe_wind(self):
if self.wind_speed is not None:
if self.wind_speed < 1:
return "calm"
elif self.wind_speed < 4:
return "light air"
elif self.wind_speed < 7:
return "light breeze"
elif self.wind_speed < 11:
return "gentle breeze"
elif self.wind_speed < 16:
return "moderate breeze"
elif self.wind_speed < 22:
return "fresh breeze"
elif self.wind_speed < 28:
return "strong breeze"
elif self.wind_speed < 34:
return "near gale"
elif self.wind_speed < 41:
return "gale"
elif self.wind_speed < 56:
return "storm"
elif self.wind_speed < 64:
return "violent storm"
else:
return "hurricane"
else:
return 'unknown'
def windsock(self):
if self.wind_direction is not None:
if (self.wind_speed <= 22.5) or (self.wind_speed > 337.5):
return '\u2191'
elif (self.wind_speed > 22.5) and (self.wind_speed <= 67.5):
return '\u2197'
elif (self.wind_speed > 67.5) and (self.wind_speed <= 112.5):
return '\u2192'
elif (self.wind_speed > 112.5) and (self.wind_speed <= 157.5):
return '\u2198'
elif (self.wind_speed > 157.5) and (self.wind_speed <= 202.5):
return '\u2193'
elif (self.wind_speed > 202.5) and (self.wind_speed <= 247.5):
return '\u2199'
elif (self.wind_speed > 247.5) and (self.wind_speed <= 292.5):
return '\u2190'
elif (self.wind_speed > 292.5) and (self.wind_speed <= 337.5):
return '\u2196'
else:
return '?'
def __repr__(self):
chunks = []
if self.cover:
chunks.append(self.cover)
chunks.append('{0}°C'.format(self.temperature))
if self.pressure:
chunks.append('{0} hPa'.format(self.pressure))
if self.conditions:
chunks.append(self.conditions)
wind = self.wind_speed if self.wind_speed is not None else '?'
chunks.append('{note} {speed} m/s ({windsock})'.format(
note=self.describe_wind(),
speed=wind,
windsock=self.windsock()))
ret = ', '.join(chunks) + ' - {station} {time}'
return ret.format(station=self.station,
time=self.time.strftime("%H:%MZ"))
def build_regex(key, classifier):
ret = "|".join([re.escape(x) for x in classifier.keys()])
return r"(?P<{key}>{regex})".format(key=re.escape(key), regex=ret)
def weather_regex():
ret = r'\s'
ret += build_regex('intensity', INTENSITY) + r'?'
ret += build_regex('descriptor', DESCRIPTOR) + r'?'
ret += build_regex('precipitation', PRECIPITATION) + r'?'
ret += build_regex('obscuration', OBSCURATION) + r'?'
ret += build_regex('other', OTHER) + r'?'
ret += r'\s'
return re.compile(ret)
def parse_temp(t):
if t[0] == 'M':
return -int(t[1:])
return int(t)
def parse(data):
w = Weather()
data = data.splitlines()
metar = data[1].split()
w.metar = data[1]
w.station = metar[0]
metar = metar[1:]
# time
time_re = re.compile(r"\d{2}(?P<hour>\d{2})(?P<min>\d{2})Z")
m = time_re.search(w.metar)
if m:
w.time = datetime.time(hour=int(m.group('hour')),
minute=int(m.group('min')))
# mode
#if metar[0] == "AUTO":
# metar = metar[1:]
# wind speed
wind_re = re.compile(r"(?P<direction>\d{3})(?P<speed>\d+)(G(?P<gust>\d+))?(?P<unit>KT|MPS)")
m = wind_re.search(w.metar)
if m:
w.wind_direction = int(m.group('direction'))
if m.group('unit') == "KT":
# convert knots to m/s
w.wind_speed = round(int(m.group('speed')) * 1852 / 3600)
if m.group('gust'):
w.wind_gust = round(int(m.group('speed')) * 1852 / 3600)
else:
w.wind_gust = None
else:
w.wind_speed = int(m.group('speed'))
if m.group('gust'):
w.wind_gust = int(m.group('gust'))
else:
w.wind_gust = None
metar = metar[1:]
# visibility
# 0800N?
visibility_re = re.compile(r"(?P<vis>(?P<dist>\d+)SM|(?P<disti>\d{4})\s|CAVOK)")
m = visibility_re.search(w.metar)
if m:
if m.group('dist'):
w.visibility = m.group('dist')
elif m.group('disti'):
w.visibility = m.group('disti')
elif m.group('vis') == 'CAVOK':
w.cover = "clear"
w.visibility = m.group('vis')
else:
w.visibility = None
# runway visibility range
# conditions
matches = weather_regex().finditer(w.metar)
for m in matches:
if not m:
continue
weather = []
if m.group('intensity'):
w.intensity = INTENSITY[m.group('intensity')]
weather.append(w.intensity)
if m.group('descriptor'):
w.descriptor = DESCRIPTOR[m.group('descriptor')]
weather.append(w.descriptor)
if m.group('precipitation'):
w.precipitation = PRECIPITATION[m.group('precipitation')]
weather.append(w.precipitation)
if m.group('obscuration'):
w.obscuration = OBSCURATION[m.group('obscuration')]
weather.append(w.obscuration)
if m.group('other'):
w.other = OTHER[m.group('other')]
weather.append(w.other)
if len(weather) > 0:
w.conditions = " ".join(weather)
# cloud cover
cover_re = re.compile(build_regex('cover', CLOUD_COVER) +\
r"(?P<height>\d*)")
matches = cover_re.finditer(w.metar)
for m in matches:
w.cover = CLOUD_COVER[m.group('cover')]
w.height = m.group('height')
# temperature
temp_re = re.compile(r"(?P<temp>[M\d]+)\/(?P<dewpoint>[M\d]+)")
m = temp_re.search(w.metar)
if m:
w.temperature = parse_temp(m.group('temp'))
w.dewpoint = parse_temp(m.group('dewpoint'))
else:
w.temperature = None
# pressure
pressure_re = re.compile(r"([QA])(\d+)")
m = pressure_re.search(w.metar)
if m and m.group(1) == 'A':
# convert inHg to hPa
w.pressure = round(float(m.group(2)) * 0.3386389)
elif m:
w.pressure = int(m.group(2))
else:
w.pressure = None
return w
if __name__ == "__main__":
import glob
for station in glob.glob('test/metar/*.TXT'):
with open(station) as f:
print(parse(f.read()))