-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetar_extractor.py
432 lines (392 loc) · 16.7 KB
/
metar_extractor.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# IMPORT LIBS
import re
import pytaf
from datetime import datetime
import pandas as pd
from metar import Metar
from fractions import Fraction
from collections import OrderedDict
class metar_extractor(object)
def __init__(self, taf):
if isinstance(taf, TAF):
self._taf = taf
else:
raise DecodeError("Argument is not a METAR parser object")
def speci_group_function():
"""
Function IDs SPECI and ROUTINUE METARs
Outputs:n SPECI or ROUTINUE METARs
"""
speci_object=re.compile(r'SPECI')
speci_array=[]
contents00 =[]
for values in content01:
speci_match =speci_object.search(values)
if speci_match:
speci_array.append('SPECI ')
values=re.sub('SPECI', '',values)
contents00.append(values)
else:
speci_array.append('ROUTINE')
contents00.append(values)
return(content01,speci_array)
#print(speci_group_function())
# print('===========>')
# print('=========================>METAR COMPLETED speci_group_function')
def time_group_function(content02):
"""
Function built to extract time from pytaf using any METARs
Extracts - 'origin_minutes',' origin_hours', 'orgin_date'
"""
time_array = []
for values in content02:
#print(values)
YYYY = values[0:4]
MM = values[5:7]
DD = values[8:10]
hh = values[11:13]
mm = values[14:16]
#print(YYYY,MM,DD,hh,mm)
#print(datetime(int(YYYY), int(MM), int(DD),int(hh),int(mm)).strftime('%Y-%m-%d %H:%M'))
time_array.append(datetime(int(YYYY), int(MM), int(DD),int(hh),int(mm)).strftime('%Y-%m-%d %H:%M'))
return(time_array)
def visibility_group_function():
"""
Function built for extracting taf._weather_groups[X]["visibility"]
Attribute "winds" has 2 outputs
'visibility': None
or
'visibility':{'more': P, 'range': '3', 'unit': 'SM'}
'visibility':{'range': '10 000', 'more': True, 'unit': 'M'}
"""
visibility_array = []
for values in content01:
taf = pytaf.TAF(values)
weather_groups = taf._weather_groups
visibilitytf = weather_groups[0]["visibility"]
if not visibilitytf:
res2 = 10
visibility_array.append(res2)
else:
if weather_groups[0]["visibility"]["unit"] == 'M':
if weather_groups[0]["visibility"]["range"] == '10 000':
res2 = 10
visibility_array.append(res2)
else:
if int(weather_groups[0]["visibility"]["range"])<900:
res2 = 0.125
elif 900<=int(weather_groups[0]["visibility"]["range"])<1200:
res2 = 0.5
elif 1200<=int(weather_groups[0]["visibility"]["range"])<1600:
res2 = 0.75
elif 1600<=int(weather_groups[0]["visibility"]["range"])<3200:
res2 = 1
elif 3200<=int(weather_groups[0]["visibility"]["range"])<4800:
res2 = 2
elif 4800<=int(weather_groups[0]["visibility"]["range"])<6000:
res2 = 3
elif 6000<=int(weather_groups[0]["visibility"]["range"])<8000:
res2 = 4
elif 8000<=int(weather_groups[0]["visibility"]["range"])<9000:
res2 = 5
elif 9000>=int(weather_groups[0]["visibility"]["range"]):
res2 = 10
visibility_array.append(res2)
else:
res2 = weather_groups[0]["visibility"]["range"]
if (' ' in res2) == True:
res2=res2.split()
res3=Fraction(res2[1])
res2=int(res2[0])+float(res3)
else:
if ('/' in res2) == True:
res2=Fraction(res2)
res2=float(res2)
else:
res2=float(res2)
visibility_array.append(res2)
visibilitytf = []
return(visibility_array)
def time_range_function():
time_array=time_group_function(content02)
time_array = time_array[0:len(time_array)-1]
res =[]
ds = []
index_starttime = [i for i in range(len(time_array)-1)]
index_starttime = [i+1 for i in index_starttime]
index_endtime = [i for i in range(len(time_array)-1)]
for values01,values02,values03 in zip(time_array,index_starttime,index_endtime):
startime=datetime.strptime(time_array[values02],'%Y-%m-%d %H:%M')
endtime=datetime.strptime(time_array[values03],'%Y-%m-%d %H:%M')
res= startime - endtime
res=(':'.join(str(res).split(':')[:2]))
ds.append(res)
return(ds)
def clouds_group_function():
"""
Function built for extracting taf._weather_groups[X]["clouds"]
Attribute "Clouds" has 2 outputs
'clouds': None
or
'clouds': [{'layer': 'XXX', 'ceiling': 'XXX', 'type': XXX}]
"""
layer_val = []
ceiling_val =[]
clouds_array = [layer_val, ceiling_val]
for values in content01:
#print(values)
res1 = []
res2 = []
taf = pytaf.TAF(values)
weather_groups = taf._weather_groups
cloudstf = weather_groups[0]["clouds"]
vvtf = weather_groups[0]["vertical_visibility"]
results3 =[]
results4 =[]
if not cloudstf and not vvtf:
res1 = ["-9999"]
res2 = ["-9999"]
layer_val.append(res1)
ceiling_val.append(res2)
elif vvtf:
res1 = ["VV"]
res2 = [weather_groups[0]["vertical_visibility"]]
layer_val.append(res1)
ceiling_val.append(res2)
elif cloudstf and not vvtf:
if weather_groups[0]["clouds"][0]["layer"] == 'CLR' or weather_groups[0]["clouds"][0]["layer"] == 'CAVOK' or weather_groups[0]["clouds"][0]["layer"] == 'NSC' or weather_groups[0]["clouds"][0]["layer"] == 'SKC'or weather_groups[0]["clouds"][0]["layer"] == 'NCD':
res1 = ["CLR"]
res2 = ["9999"]
layer_val.append(res1)
ceiling_val.append(res2)
else:
for k in range(0,len(weather_groups[0]["clouds"])):
#print(weather_groups[0]["clouds"])
res1 = weather_groups[0]["clouds"][k]["layer"]
res2 = weather_groups[0]["clouds"][k]["ceiling"]
results3.append(res1)
results4.append(res2)
layer_val.append(results3)
ceiling_val.append(results4)
cloudstf = []
return(clouds_array)
def present_wx_group_function():
"""
Function built for extracting taf._weather_groups[X]["weather"]
Attribute "Weather" has 3 outputs
'weather': None
or
'weather': [{'intensity': XXX, 'modifier': XXX, 'phenomenon': XXX}]
"""
present_wx=[]
for values in content01:
res1 = []
res2 = []
#print(values)
taf = pytaf.TAF(values)
weather_groups = taf._weather_groups
weathertf = weather_groups[0]["weather"]
res1 = []
res2 = []
res3 = []
res4 = []
results1=[]
if not weathertf:
res1 = "None"
present_wx.append(res1)
elif weathertf:
if len(weather_groups[0]["weather"]) == 1:
res1 = weather_groups[0]["weather"][0]["intensity"]
res2 = weather_groups[0]["weather"][0]["modifier"]
res3 = weather_groups[0]["weather"][0]["phenomenon"]
res4=res1+res2+res3
res4="".join(res4)
res4 = "".join(OrderedDict.fromkeys(res4))
present_wx.append(res4)
else:
for k in range(0,len(weather_groups[0]["weather"])):
res1 = weather_groups[0]["weather"][k]["intensity"]
res2 = weather_groups[0]["weather"][k]["modifier"]
res3 = weather_groups[0]["weather"][k]["phenomenon"]
res4=res1+res2+res3
res4="".join(res4)
results1.append(res4)
results1=" ".join(results1)
print(results1)
results1 = "".join(OrderedDict.fromkeys(results1))
present_wx.append(results1)
weathertf = []
return(present_wx)
def present_wx_group_function_2():
dumby01=[]
dumby02=[]
present_wx = present_wx_group_function()
for value in present_wx:
value=value.replace('BR', '').replace('HZ', '').replace('FU', '').replace('DU', '').replace('BLSN', '').replace('BLDU', '').replace('FG', '').replace('FZFG', '').replace('MIFG', '').replace('PRFG','')
dumby01.append(value)
for value in dumby01:
if value=='':
dumby02.append('-9999')
else:
dumby02.append(value)
return(dumby02)
def visibility_obscuration_function():
dumby01 =[]
present_wx = present_wx_group_function()
for value in present_wx:
obscuration_object=re.compile(r'(BLSN\sFZFG)|(BLSN\sFG)|(BR)|(HZ)|(FU)|(DU)|(BLSN)|(BLDU)|(FG)|(FZFG)|(MIFG)|(PRFG)')
obscuration_match = obscuration_object.search(value)
if obscuration_match:
obscuration_string = obscuration_match.group()
dumby01.append(obscuration_string)
else:
obscuration_string = '-9999'
dumby01.append(obscuration_string)
return(dumby01)
def lowest_cloud_group_function():
clouds=clouds_group_function()
cloudx_layers = clouds[0]
cloudx_heights =clouds[1]
cloud_layer=[]
cloud_height=[]
for values00,values01 in zip(cloudx_layers,cloudx_heights):
results1 = []
results2 = []
res1 = []
res2 = []
for values02,values03 in zip(values00,values01):
if values02=='BKN' or values02=='OVC' or values02=='VV':
res1 = values02
res2 = int(values03) * 100
results1.append(res1)
results2.append(res2)
elif values02=='CLR' or values02=='FEW' or values02=='SCT':
res1 = '9999'
res2 = 9999
results1.append(res1)
results2.append(res2)
elif values02=='-9999':
res1 = '-9999'
res2 = -9999
results1.append(res1)
results2.append(res2)
cloud_layer.append(results1)
cloud_height.append(results2)
lowest_cloud_height=[]
lowest_cloud_layer=[]
lowest=[]
for zabby1,zabby2 in zip(cloud_height,cloud_layer):
res3 = min(zabby1)
index_cloud = zabby1.index(min(zabby1))
res4 = zabby2[index_cloud]
lowest_cloud_height.append(res3)
lowest_cloud_layer.append(res4)
lowest = [lowest_cloud_height, lowest_cloud_layer]
return(lowest)
def wind_group_function():
"""
Function built for extracting taf._weather_groups[X]["winds"]
Attribute "Clouds" has 2 outputs
'wind': None
or
wind': {'direction': '210', 'speed': '06', 'gust': None, 'unit': 'KT'}
"""
direction_val =[]
speed_val =[]
gust_val =[]
unit_val = []
wind_array = [direction_val,speed_val,gust_val,unit_val]
for values in content01:
#print(values)
taf = pytaf.TAF(values)
weather_groups = taf._weather_groups
windtf = weather_groups[0]["wind"]
#print(windtf)
if not windtf:
res1 = -9999
res2 = -9999
res3 = -9999
res4 = -9999
direction_val.append(res1)
speed_val.append(res2)
gust_val.append(res3)
unit_val.append(res4)
else:
if weather_groups[0]["wind"]["direction"] == 'VRB':
res1 = 777
res2 = int(weather_groups[0]["wind"]["speed"])
res4 = weather_groups[0]["wind"]["unit"]
direction_val.append(res1)
speed_val.append(res2)
unit_val.append(res4)
else:
res1 = int(weather_groups[0]["wind"]["direction"])
res2 = int(weather_groups[0]["wind"]["speed"])
res4 = weather_groups[0]["wind"]["unit"]
direction_val.append(res1)
speed_val.append(res2)
unit_val.append(res4)
if not weather_groups[0]["wind"]["gust"]:
res3 = -9999
gust_val.append(res3)
else:
res3 = weather_groups[0]["wind"]["gust"]
gust_val.append(res3)
windtf = []
return(wind_array)
def flight_cat_function():
"""
Function built for to Output Flight Cat]
Output : VFR, MVFR, IFR, MVFR, VFR
"""
cloud_array = lowest_cloud_group_function()
cloud_array = cloud_array[0]
visibility_array = visibility_group_function()
flt_cat = []
for val01,val02 in zip(visibility_array,cloud_array):
res=[]
if (val01<0.5 and val02<200) or (val01<0.5 and not val02<200) or (val02<200 and not val02<1/2):
res='VLIFR'
elif (0.5<=val01<1 and 200<=val02<500) or (0.5<=val01<1 and not 200<=val02<500) or (200<=val02<500 and not 0.5<=val01<1):
res='LIFR'
elif (1<=val01<3 and 500<=val02<1000) or (1<=val01<3 and not 500<=val02<1000) or (500<=val02<1000 and not 1<=val01<3):
res='IFR'
elif (3<=val01<=5 and 1000<=val02<=3000) or (3<=val01<=5 and not 1000<=val02<=3000) or (1000<=val02<=3000 and not 3<=val01<=5):
res='MVFR'
elif val01>5 and val02>3000:
res='VFR'
flt_cat.append(res)
return(flt_cat)
def icao_get_function():
icao = []
for values in content01:
taf = pytaf.TAF(values)
icao.append(taf._taf_header["icao_code"])
return(icao)
def build_table():
wind_array = wind_group_function()
visibility_array = visibility_group_function()
time_array = time_group_function(content02)
delta = time_range_function()
speci=speci_group_function()
flt_cat=flight_cat_function()
icao = icao_get_function()
present_wx_array=present_wx_group_function_2()
vis_obscur_wx_array=visibility_obscuration_function()
dmicao = pd.DataFrame({'ICAO' : icao})
lowest_clouds = lowest_cloud_group_function()
dmtime = pd.DataFrame({'time': time_array})
dmvis = pd.DataFrame({'vis': visibility_array})
dmdt = pd.DataFrame({'dt': delta})
dms = pd.DataFrame({'Message Type': speci[1]})
dmwx = pd.DataFrame({'Present Wx':present_wx_array})
dmch = pd.DataFrame({'Cld Hgt': lowest_clouds[0]})
dmcl = pd.DataFrame({'Cld Type': lowest_clouds[1]})
dmwd = pd.DataFrame({'ddd': wind_array[0]})
dmws = pd.DataFrame({'ff': wind_array[1]})
dmwg = pd.DataFrame({'gg': wind_array[2]})
dmfc = pd.DataFrame({'Flt Cat' : flt_cat})
dmov = pd.DataFrame({'Vis Obc':vis_obscur_wx_array})
result = pd.concat([dmicao,dms,dmtime,dmdt,dmwd,dmws,dmwg,dmfc,dmvis,dmch,dmcl,dmwx,dmov], axis=1, sort=False)
return(result)
return(build_table())