-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetar_parse.py
118 lines (103 loc) · 3.14 KB
/
metar_parse.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
"""A script for processing METAR data.
This uses the metar library available at:
https://github.com/python-metar/python-metar
"""
from datetime import datetime
from metar import Metar
import pytz
class metobs:
"""A class to store METAR observations.
temp = temperature in C
dewp = dewpoint in C
w_s = wind speed in kts
w_d = wind direction in deg
w_g = wind gusts in kts
cb = boolean specifying CBs near airport
vis = visibility in km
pres = pressure in hPa
cld = cloud base in ft
"""
def __init__(self, temp, dewp, w_s, w_d, w_g, cb, vis, pres, cld):
"""Setup the class."""
self.temp = temp
self.dewp = dewp
self.w_s = w_s
self.w_d = w_d
self.w_g = w_g
self.cb = cb
self.vis = vis
self.pres = pres
self.cld = cld
def get_metars(inf, verbose):
"""A function to parse metars from a file and convert into a dict of metobs objects.
Input:
- inf: The input file (as a string filename)
Output:
- a dict of metobs read from the file
"""
fid = open(inf, 'r')
met_dict = {}
for line in fid:
data = line.rstrip('\n').split(',')
metdate = datetime.strptime(data[1], '%Y-%m-%d %H:%M')
metdate = metdate.replace(tzinfo=pytz.UTC)
mettxt = data[2]
obs = Metar.Metar(mettxt, strict=False)
obs.time = metdate
try:
temp = obs.temp.value()
except Exception as e:
if verbose:
print("ERROR: Bad temperature data", e)
temp = 15
try:
dewp = obs.dewpt.value()
except Exception as e:
if verbose:
print("ERROR: Bad dewpoint data", e)
dewp = 10
try:
w_s = obs.wind_speed.value()
except Exception as e:
if verbose:
print("ERROR: Bad wind speed data", e)
w_s = 0
try:
w_g = obs.wind_gust.value()
except Exception as e:
if verbose:
print("ERROR: Bad wind gust data", e)
w_g = 0
try:
w_d = obs.wind_dir.value()
except Exception as e:
if verbose:
print("ERROR: Bad wind direction data", e)
w_d = 0
try:
press = obs.press.value()
except Exception as e:
if verbose:
print("ERROR: Bad pressure data", e)
press = 1013.25
try:
vis = obs.vis.value()
except Exception as e:
if verbose:
print("ERROR: Bad visibility data", e)
vis = 10000
cb = False
try:
for wx in obs.sky:
if (wx[2] == "CB"):
cb = True
except Exception as e:
if verbose:
print("ERROR: Bad CB data", e)
cld = 10000
if (len(obs.sky) > 0):
if obs.sky[0][1] is not None:
cld = obs.sky[0][1].value()
cur_obs = metobs(temp, dewp, w_s, w_d, w_g, cb, vis, press, cld)
met_dict[metdate] = cur_obs
return met_dict