forked from johnsbuck/FAA-Rules-Phase-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
executable file
·229 lines (182 loc) · 7.08 KB
/
module.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
from __future__ import division
# from pretty import pprint
# import matplotlib.pyplot as plt
from datetime import timedelta, datetime
datetimeformat = "%H:%M:%S.%f" # 2015-12-09 01:18:41.891210
def fiveNumberSummary(lst):
'''Construct 5 number summary:
first, min, max, average, last
Parameters:
List of Integers
Returns:
List of 5 Integers -
first: First value
min: Smallest value
max: Largest value
avg: The average of all values
last: Last value
'''
assert len(lst) > 0, "list must contain values"
return {
"first": lst[0],
"last": lst[-1],
"min": min(lst),
"max": max(lst),
"avg": sum(lst) / len(lst)
}
def phaseClassification(correlatedData, time):
'''Classify each period's phase of flight, and return this list.
Parameters:
correlatedData - Data containing details on
Timestamps, Altitude, and Speed.
time - Timestamp of current time being seeked.
Return:
str - Phase of Flight described as "Taxi", "Cruise", "Ascend", "Descend",
or "Unknown".
'''
# Set create periods
period = restructureDataToPeriods(correlatedData, time)
# Start process
phases = "Unknown"
if len(period) == 0:
return phases
(timestamps, altitudes, speeds) = zip(*period)
# plt.plot(altitudes, 'ro')
# plt.show()
altitudeSummary = fiveNumberSummary(altitudes)
speedSummary = fiveNumberSummary(speeds)
# Check for weird things like if the max is in the middle, and aircraft goes up and back down
# print altitudeSummary
# If there is little altitude difference
if abs(altitudeSummary["first"] - altitudeSummary["last"]) < 10:
#If low speed and low altitude
if speedSummary["avg"] <= 100 and altitudeSummary["max"] <= 5:
phases = "Taxi"
elif abs(altitudeSummary["max"] - altitudeSummary["min"]) < 10:
phases = "Cruise"
else:
phases = "Unknown"
else: # Otherwise, large altitude difference
# Note, this may not be a reliable indicator of whether aircraft is ascending or descending
if altitudeSummary["first"] < altitudeSummary["last"]:
phases = "Ascend"
elif altitudeSummary["first"] > altitudeSummary["last"]:
phases = "Descend"
else:
phases = "Unknown"
# print altitudeSummary
# print altitudes
# print altitudeSummary, speedSummary
return phases
def ruleClassification(correlatedData, time):
'''Classifies the rule of flight
Parameters:
correlatedData - Data containing details on
Timestamps, Altitude, and Speed.
time - Timestamp of current time being seeked.
Returns:
str - Rule of Flight described as "IFR", "VFR", or "Unknown".
'''
# Set create periods
period = restructureDataToPeriods(correlatedData, time)
# Start process
rules = "Unknown"
if len(period) == 0:
return rules
elif "flightrules" in period[0]:
(timestamps, altitudes, speeds, flightrules) = zip(*period)
actualDataIndex = 0
for i in range(timestamps):
if timestamp >= time:
actualDataIndex = i
break
if flightrules[i].find('FR') > -1:
return flightrules[i]
elif i > 0 and i < (len(timestamps) - 1) \
and flightRules[i-1].find('FR') > -1 and flightRules[i+1].find('FR') > -1:
if abs(altitudes[i] - altitudes[i-1]) <= abs(altitudes[i] - altitudes[i+1]):
return flightrules[i-1]
else:
return flightrules[i+1]
else:
(timestamps, altitudes, speeds) = zip(*period)
altitudeSummary = fiveNumberSummary(altitudes)
speedSummary = fiveNumberSummary(speeds)
flightApprox = int(5 * round(float(altitudeSummary["avg"])/5)) % 10
if flightApprox == 5 or altitudeSummary["avg"] == 0:
rules = "VFR"
elif flightApprox == 0:
rules = "IFR"
else:
rules = "Unknown"
# print int(5 * round(float(altitudeSummary["avg"])/5))
# print altitudeSummary
# print altitudes
return rules
def restructureDataToPeriods(data, time):
'''Restructure the data so that it consists of data 30 seconds before the
time given, and 30 seconds after the time given.
Input python object structure:
[
{ "timestamp": "2015-12-09 02:42:45.107267", "alt": 87, "speed": 16 },
{ "timestamp": "2015-12-09 02:42:46.101267", "alt": 91, "speed": 21 },
]
Output python object structure:
[
1 :
("2015-12-09 02:42:45.107267", 87, 16), ("2015-12-09 02:42:46.101267", 91, 21), ...
2: ...
]
Parameters:
data - Data containing information on aircraft.
time - Timestamp of required information
Returns:
List of tuples
'''
time = time[:-3]
formatTime = datetime.strptime(time, datetimeformat)
restructured = []
# For data within time section
for i in data:
if i["ts"].find('.') <= -1:
i["ts"] += '.000000'
i["ts"] = i["ts"][:-3]
if (formatTime - timedelta(seconds=30)) <= datetime.strptime(i["ts"], datetimeformat) \
and (formatTime + timedelta(seconds=30)) >= datetime.strptime(i["ts"], datetimeformat):
if len(i) > 3:
restructured.append( (i["ts"], i["alt"], i["speed"], i["flightrules"]) )
else:
restructured.append( (i["ts"], i["alt"], i["speed"]) )
return restructured
def checkData(data):
''' Redundancy check integrity of data passed in through JSON
- Run through data entries
- Include "good" entries
* altitude and speed greater than or equal to 0
* difference in altitude less than ALT_THRESH
* difference in speed less than SPD_THRESH
- Skip continuous "bad" entries if exist
Parameters:
data - a list of data points from input
Returns:
cleaned - list of valid data points filtered from input
'''
# Set threshold to determine unusable entries
ALT_THRESH = 5 # altitude is in hundred feet
SPD_THRESH = 100 # speed is in thousand-feet/s
JSON_ALT_NAME = "alt"
JSON_SPD_NAME = "speed"
# print str(len(data)) + " entries"
cleaned = [data[0]]
for i in range(len(data)-1):
i = i + 1
alt_diff = abs(data[i-1][JSON_ALT_NAME] - data[i][JSON_ALT_NAME])
spd_diff = abs(data[i][JSON_SPD_NAME] - data[i-1][JSON_SPD_NAME])
last_alt = abs(data[i][JSON_ALT_NAME] - cleaned[-1][JSON_ALT_NAME])
last_spd = abs(data[i][JSON_SPD_NAME] - cleaned[-1][JSON_SPD_NAME])
if data[i][JSON_ALT_NAME] >= 0 and data[i][JSON_SPD_NAME] >= 0 and \
(i > 0 and alt_diff < ALT_THRESH and spd_diff < SPD_THRESH) and \
(last_alt < ALT_THRESH and last_spd < SPD_THRESH):
cleaned.append(data[i])
# print "cleaned:", len(cleaned), "entries"
return cleaned