-
Notifications
You must be signed in to change notification settings - Fork 19
/
metar-test.py
58 lines (58 loc) · 1.65 KB
/
metar-test.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
# Free for personal use. Prohibited from commercial use without consent.
import urllib,re
def get_metar(airport):
try:
stream = urllib.urlopen('http://www.aviationweather.gov/metar/data?ids='+airport+'&format=raw&hours=0&taf=off&layout=off&date=0');
for line in stream:
if '<!-- Data starts here -->' in line:
return re.sub('<[^<]+?>', '', stream.readline())
return 'INVALID'
except Exception, e:
print str(e);
return 'INVALID'
finally:
stream.close();
def get_vis(metar):
components = metar.split(' ');
for component in components:
if 'SM' in component:
return re.sub('SM', '', component)
return 'INVALID'
def get_vis_category(vis):
if '/' in vis:
return 'LIFR'
vis_int = int(vis)
if vis < 3:
return 'IFR'
if vis <= 5:
return 'MVFR'
return 'VFR'
def get_ceiling(metar):
components = metar.split(' ' );
minimum_ceiling = 10000
for component in components:
if 'BKN' in component or 'OVC' in component:
ceiling = int(filter(str.isdigit,component)) * 100
if(ceiling < minimum_ceiling):
minimum_ceiling = ceiling
return minimum_ceiling
def get_ceiling_category(ceiling):
if ceiling < 500:
return 'LIFR'
if ceiling < 1000:
return 'IFR'
if ceiling < 3000:
return 'MVFR'
return 'VFR'
def get_category(metar):
vis = get_vis_category(get_vis(metar))
ceiling = get_ceiling_category(get_ceiling(metar))
if(vis == 'INVALID' or ceiling == 'INVALID')
return 'INVALID'
if(vis == 'LIFR' or ceiling == 'LIFR')
return 'LIFR'
if(vis == 'IFR' or ceiling == 'IFR')
return 'IFR'
if(vis == 'MVFR' or ceiling == 'MVFR')
return 'MVFR'
return 'VFR'