-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
288 lines (247 loc) · 9.33 KB
/
main.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
# -----------------------------------------------------------
# VATSIM Live
#
# Script generate live geojson files to display traffic.
#
#
# TODO: match FIR boundary to polygon
#
# (C) 2015 Niels
# -----------------------------------------------------------
import urllib.request
import json
import csv
import random
import geojson
from geojson import Point, Feature, FeatureCollection
from datetime import datetime, timezone
import time
from dateutil import parser
import folium
def get_data_url():
with urllib.request.urlopen('https://status.vatsim.net/status.json') as url:
content = json.loads(url.read().decode())
data_url = random.choice(content['data']['v3'])
return data_url
def get_vatsim_data():
global global_timestamp, request_cnt, update_cnt, iter_start_time
while True:
with urllib.request.urlopen(get_data_url()) as url:
data = json.loads(url.read().decode())
data_time = parser.isoparse(data['general']['update_timestamp'])
request_cnt += 1
if global_timestamp == data_time:
print('equal timestamp, waiting 5s...\n')
log([
global_timestamp.strftime('%H:%M:%S'),
datetime.utcnow().strftime('%H:%M:%S'),
'skipped duplicate data'
])
time.sleep(5)
iter_start_time = datetime.now()
else:
global_timestamp = data_time
update_cnt += 1
return data
def create_feature(flight, slots=None):
global flight_count, booked_cid_cnt, cid_cs_mismatch, not_booked_cross, not_booked_event
flight_count += 1
flight_data = flight.copy()
for item in ['latitude', 'longitude', 'flight_plan']:
flight_data.pop(item)
if flight['flight_plan']:
flight_plan = {}
for item in flight['flight_plan'].items():
item = list(item)
item[0] = 'fp_{}'.format(item[0])
flight_plan[item[0]] = item[1]
flight_data.update(flight_plan)
# slot analysis (event mode setting)
if slots:
# search for match on CID
search_1 = list(filter(lambda pos: pos['CID'] == str(flight['cid']), slots))
if search_1:
flight_data.update({
'cid_bkd': '1'
})
booked_cid_cnt += 1
search_2 = list(filter(lambda pos: pos['C/S'] == str(flight['callsign']), slots))
if search_2:
flight_data.update({
'cs_match': '1'
})
else:
flight_data.update({
'cs_match': '0'
})
cid_cs_mismatch += 1
else:
# CID does not hold a booking
flight_data.update({
'cid_bkd': '0',
'cs_match': '0'
})
if flight['flight_plan']['departure'] and flight['flight_plan']['arrival']:
if flight['flight_plan']['departure'][0] in ['G', 'E', 'L']:
if flight['flight_plan']['arrival'][0] in ['K', 'C', 'T', 'M']:
not_booked_cross += 1
flight_data.update({
'cross_a_no_booking': '1'
})
if flight['flight_plan']['departure'] in ['ENGM', 'LOWW', 'EDDM', 'EHAM', 'EGKK', 'EIDW', 'LFPG', 'LTFM', 'LPPT', 'GCTS']:
if flight['flight_plan']['arrival'] in ['KATL', 'CYWG', 'CYVR', 'KIAH', 'CYUL', 'KBOS', 'KEWR', 'KPHL', 'KTPA', 'TFFR', 'TFFF', 'TBPB']:
not_booked_event += 1
flight_data.update({
'event_apts_no_booking': '1'
})
point_object = Point((flight['longitude'], flight['latitude']))
flight_feature = Feature(geometry=point_object, properties=flight_data)
return flight_feature
def load_slots():
slot_list = []
with open('data/bookings_ctpw22.csv', 'r') as slots:
for line in csv.DictReader(slots, delimiter=';'):
slot_list.append(line)
return slot_list
def log(data):
filename = 'logs/log {}.csv'.format(str(datetime.utcnow().strftime('%d%m%Y')))
with open(filename, 'a', newline='') as log_file:
writer = csv.writer(log_file)
writer.writerow(data)
def load_map_data():
cycle = '2202'
fn = 'AIRAC/{}/Boundaries.geojson'.format(cycle)
with open(fn, 'r') as f:
boundaries = json.load(f)
print(type(boundaries))
fn = 'AIRAC/{}/Countries.json'.format(cycle)
with open(fn, 'r') as f:
countries = json.load(f)
print(type(countries))
fn = 'AIRAC/{}/FIRs.json'.format(cycle)
with open(fn, 'r') as f:
firs = json.load(f)
print(type(firs))
fn = 'AIRAC/{}/UIRs.json'.format(cycle)
with open(fn, 'r') as f:
uirs = json.load(f)
print(type(uirs))
data_dict = {
'boundaries': boundaries,
'countries': countries,
'firs': firs,
'uirs': uirs
}
print(data_dict.keys())
return data_dict
if __name__ == '__main__':
# Settings
event = False
test = False
# Global params
global_timestamp = datetime.utcnow()
request_cnt = 0
update_cnt = 0
flight_count_persistent = 0
# load airac data
airac_data = load_map_data()
# Start loop
while True:
iter_start_time = datetime.now()
# counts
flight_count = 0
booked_cid_cnt = 0
cid_cs_mismatch = 0
not_booked_cross = 0
not_booked_event = 0
# start script
feature_list = []
if event:
slots = load_slots()
vatsim_data = get_vatsim_data()
vatsim_pilot_data = vatsim_data['pilots']
vatsim_controller_data = vatsim_data['controllers']
# pilot data
# create stats point:
vatsim_pilot_data.append(
{'cid': 1, 'name': 'Data', 'callsign': 'DATA_INFO', 'server': 'UK', 'pilot_rating': '', 'latitude': 58.340,
'longitude': -19.402, 'altitude': flight_count_persistent, 'groundspeed': '', 'transponder': '', 'heading': '', 'qnh_i_hg': '',
'qnh_mb': '', 'flight_plan': [], 'logon_time': '',
'last_updated': str(global_timestamp.strftime('%H:%M:%S'))}
)
for flight in vatsim_pilot_data:
if event:
feature_list.append(
create_feature(flight, slots)
)
else:
feature_list.append(
create_feature(flight)
)
collection = FeatureCollection(feature_list)
with open('live_output/live_flights.geojson', 'w') as out_file:
dump = geojson.dump(collection, out_file, indent=4)
# Temporary turned off for event
#
#
# # controller data
# for station in vatsim_controller_data:
#
# # filter radar controllers
# if station['facility'] > 5:
# # print(station)
# print('\n{} {} {}'.format(station['callsign'], station['frequency'], station['facility']))
#
# # separate station code from callsign
# callsign = station['callsign'].split('_')[0]
# print(callsign)
#
# # look for entry in firs data
# entry_match = None
# for entry in airac_data['firs']:
# if callsign == entry['ICAO']:
# print('found')
# print(entry)
# entry_match = entry
# elif entry['callsign_prefix']:
# if callsign == entry['callsign_prefix']:
# print('found alias')
# print(entry)
# entry_match = entry
#
# if not entry_match:
# print('NOTHING FOUND')
# information
print('{} - Flights connected: {}'.format(
global_timestamp.strftime('%H:%M:%S'),
flight_count,
))
if event:
print(
'Booked: {}, CID/CS mismatch: {}, Not Booked: Atlantic Crossing: {}, Between Event Airports: {}'.format(
booked_cid_cnt,
cid_cs_mismatch,
not_booked_cross,
not_booked_event
)
)
print('fetch/request ratio: {:.1%}'.format(request_cnt/update_cnt))
delay = (datetime.now(timezone.utc)-global_timestamp).seconds
print('delay {} secs'.format(delay))
print('\r')
log([
global_timestamp.strftime('%H:%M:%S'),
datetime.utcnow().strftime('%H:%M:%S'),
delay,
flight_count,
booked_cid_cnt,
cid_cs_mismatch,
not_booked_cross,
not_booked_event
])
# timer
flight_count_persistent = flight_count
if test:
time.sleep(2)
else:
time.sleep(15-(datetime.now()-iter_start_time).seconds)