-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollectData.py
166 lines (128 loc) · 4.45 KB
/
collectData.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
from opensky_api import OpenSkyApi
import time
import os
import numpy as np
import argparse
import datetime
parser = argparse.ArgumentParser()
parser.add_argument("--username", help="OpenSky Username")
parser.add_argument("--password", help="OpenSky Password")
parser.add_argument('--bbox', nargs='+', type=float)
args = parser.parse_args()
BBOX = tuple(args.bbox)
USERNAME = args.username
PASSWORD = args.password
cols = ['alert', 'altitude', 'callsign', 'geoaltitude', 'groundspeed', 'hour',
'icao24', 'last_position', 'latitude', 'longitude', 'onground', 'spi',
'squawk', 'timestamp', 'track', 'vertical_rate']
alert = []
# baro altitude
altitude = []
callsign = []
geoaltitude = []
groundspeed = []
hour = []
icao24 = []
last_position = []
latitude = []
longitude = []
onground = []
spi = []
squawk = []
timestamp = []
track = []
vertical_rate = []
# hour represents the hour since the python script began, not the
# hour of the day
record_hour = 0
# counting the number of states collecting
counter = 0
while True:
start = time.time()
api = OpenSkyApi(username=USERNAME,password=PASSWORD)
try:
states = api.get_states(time_secs=time.time(),bbox=BBOX)
except:
##OpenSky data is updated every 5 seconds so sleep for 5
time.sleep(5)
continue
# collecting lat/lon data
try:
for s in states.states:
latitude.append(s.latitude)
longitude.append(s.longitude)
geoaltitude.append(s.geo_altitude)
altitude.append(s.baro_altitude)
track.append(s.heading)
icao24.append(s.icao24)
groundspeed.append(s.velocity)
vertical_rate.append(s.vertical_rate)
callsign.append(s.callsign)
last_position.append(datetime.datetime.fromtimestamp(s.time_position))
timestamp.append(datetime.datetime.fromtimestamp(s.last_contact))
date_info = time.gmtime(s.last_contact)
hour.append(datetime.datetime(date_info.tm_year,date_info.tm_mon,date_info.tm_mday,date_info.tm_hour))
spi.append(s.spi)
squawk.append(s.squawk)
alert.append(False)
onground.append(s.on_ground)
# this will occur if there is no data in states
except:
timeinfo = time.gmtime()
day = timeinfo.tm_mday
month = timeinfo.tm_mon
year = timeinfo.tm_year
hr = timeinfo.tm_hour
minute = timeinfo.tm_min
sec = timeinfo.tm_sec
print("Error collecting data for {}/{}/{}/{}:{}".format(year,month,day,hr,minute,sec))
end = time.time()
if not abs(end-start) > 5:
time.sleep(5-abs(end-start))
continue
end = time.time()
# We only want to collect data at most every 5 seconds
if not abs(end-start) > 5:
time.sleep(5-abs(end-start))
counter += 1
## ~ 1 hour worth of data: 3600 seconds / 5 seconds data collection
## This could be updated based on the start time.time() and the current time.time()
## to make this exactly 1 hour instead of ~ 1 hour
if counter == 720:
timeinfo = time.gmtime()
day = timeinfo.tm_mday
month = timeinfo.tm_mon
year = timeinfo.tm_year
hr = timeinfo.tm_hour
minute = timeinfo.tm_min
sec = timeinfo.tm_sec
# convert data to a matrix of (N, 2)
opensky_data = np.vstack([alert, altitude, callsign, geoaltitude, groundspeed,
hour, icao24, last_position, latitude, longitude,
onground, spi, squawk, timestamp, track, vertical_rate]).T
alert = []
# baro altitude
altitude = []
callsign = []
geoaltitude = []
groundspeed = []
hour = []
icao24 = []
last_position = []
latitude = []
longitude = []
onground = []
spi = []
squawk = []
timestamp = []
track = []
vertical_rate = []
# save data
os.makedirs("data/{}/{}/{}/".format(year,month,day),exist_ok=True)
if not os.path.isfile('data/{}/{}/{}/{}.npy'.format(year,month,day,hr)):
np.savez_compressed('data/{}/{}/{}/{}.npz'.format(year,month,day,hr),data=opensky_data)
else:
np.savez_compressed('data/{}/{}/{}/{}_{}.npz'.format(year,month,day,hr,minute),data=opensky_data)
record_hour += 1
counter = 0
print("Hours Processed: {}".format(record_hour))