Skip to content

Commit

Permalink
Fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kcwongaz committed Aug 30, 2022
1 parent b31ce56 commit 8a952b5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 82 deletions.
7 changes: 4 additions & 3 deletions air_traffic/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def read_trajectories_range(datadir, start, end, verbose=False,
continue

if fname_only:
yield fetch_fnames(datadir, date)
yield fetch_fnames(datadir, dstr)
else:
yield read_trajectories(datadir, dstr)

Expand All @@ -41,7 +41,7 @@ def read_trajectories(datadir, date):
for subdir, _, files in os.walk(path):
for file in files:
fname = os.path.join(subdir, file)
yield pd.read_csv(fname, header=0, index_col=0)
yield pd.read_csv(fname, header=0)


def fetch_fnames(datadir, date):
Expand Down Expand Up @@ -71,7 +71,7 @@ def loop_write(dataset, savename):
_, min_time = lp.find_minima_spacetime(dist[:,1], dist[:,0],
min_loc)

# No loop then skip
# Skip if no loop is found
if len(min_time) == 0:
continue

Expand Down Expand Up @@ -113,6 +113,7 @@ def loop_read_area(fname):

for entry in data_all:
area = entry[0]

data[area].append(entry[1:])

return data
142 changes: 66 additions & 76 deletions air_traffic/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ def locate_loops(flight):
Locate the loop location for a single flight.
"""

# locations = [(70, 80), (90, 102), (103, 125)]

# It is not necessary to completely distinguish the three loops area
# It is not necessary to completely distinguish the three loops areas
locations = [(70, 80), (90, 130)]

dist = flight[:, 1]
Expand All @@ -148,22 +146,76 @@ def locate_loops(flight):
return min_loc


def label_flight_dir(flight_dict):
def reschedule_flight(flight_min, flight_exit, target, tol=60):
"""
Generate a dict labeling where the loops locate in each flight.
This is done looking at which direction does the flight come in.
Intake a sorted list of flight loop minima and the corresponding exit points,
then compute the time saved after the target flight (indexed by exit order)
got rescheduled.
"""

min_loc = {}
for key in flight_dict:
dist = flight_dict[key][:, 1]
ind = np.where(dist <= 200) # look only at the later part of the flight
s = []
while target is not None:
exit_this = flight_exit[target]
t_saved = np.zeros(len(flight_min))

lat = flight_dict[key][ind, 2]
lon = flight_dict[key][ind, 3]
for i in range(target + 1, len(flight_min)):
matched = flight_min[i][np.abs(flight_min[i] - exit_this) < tol]

if len(matched) > 0:
t_saved[i] = flight_exit[i] - matched[0]
if len(matched) > 1:
print("Warning: More than one match found!")

# If there are no promotable flight, end the search.
# If there are more than one promotable flight, choose the one gives
# the largest save (greedy search)
if np.sum(t_saved) == 0:
target = None
s.append(0)
else:
# Greedy search
target = np.argmax(t_saved)

# Priority preserving search
# target = np.nonzero(t_saved)[0][0]

s.append(t_saved[target])

s = np.array(s)
return s


def detect_selection_problem(flight_min, flight_exit, tol=60):

count = 0

for target in range(len(flight_min)):
exit_this = flight_exit[target]
t_saved = np.zeros(len(flight_min))

for i in range(target + 1, len(flight_min)):
matched = flight_min[i][np.abs(flight_min[i] - exit_this) < tol]

if len(matched) > 0:
t_saved[i] = flight_exit[i] - matched[0]
if len(matched) > 1:
print("Warning: More than one match found!")

# Compute how many candidate promotion is there
num_selection = len(np.nonzero(t_saved)[0])
if num_selection > 1:
count += 1

return count

return min_loc

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# --------------------------------------------------------------------------- #
# The functions below were used in my original code for rescheduling analysis,
# but they were not so useful in the current verion;
# they should probably be removed.
# --------------------------------------------------------------------------- #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #

def sort_flight_keys(flight_dict, min_loc):

Expand All @@ -187,7 +239,7 @@ def sort_flight_keys(flight_dict, min_loc):
flight_exit.append(exit_time)
keys.append(key)

# Sort the list of flight by exist time
# Sort the list of flight by exit time
ind_sort = np.argsort(flight_exit)

flight_exit = np.array(flight_exit)
Expand Down Expand Up @@ -220,65 +272,3 @@ def sort_flight_minima(flight_dict, min_loc):
flight_dist.append(min_dist)

return flight_min, flight_exit, flight_dist


def reschedule_flight(flight_min, flight_exit, target, tol=60):
"""
Intake a sorted list of flight loop minima and the corresponding exit points,
then compute the time saved after the target flight (indexed by exit order)
got rescheduled.
"""

s = []
while target is not None:
exit_this = flight_exit[target]
t_saved = np.zeros(len(flight_min))

for i in range(target + 1, len(flight_min)):
matched = flight_min[i][np.abs(flight_min[i] - exit_this) < tol]

if len(matched) > 0:
t_saved[i] = flight_exit[i] - matched[0]
if len(matched) > 1:
print("Warning: More than one match found!")

# If there are no promotable flight, end the search.
# If there are more than one promotable flight, choose the one gives
# the largest save (greedy search)
if np.sum(t_saved) == 0:
target = None
s.append(0)
else:
# Greedy search
target = np.argmax(t_saved)

# Priority preserving search
# target = np.nonzero(t_saved)[0][0]
s.append(t_saved[target])

s = np.array(s)
return s


def detect_selection_problem(flight_min, flight_exit, tol=60):

count = 0

for target in range(len(flight_min)):
exit_this = flight_exit[target]
t_saved = np.zeros(len(flight_min))

for i in range(target + 1, len(flight_min)):
matched = flight_min[i][np.abs(flight_min[i] - exit_this) < tol]

if len(matched) > 0:
t_saved[i] = flight_exit[i] - matched[0]
if len(matched) > 1:
print("Warning: More than one match found!")

# Compute how many candidate promotion is there
num_selection = len(np.nonzero(t_saved)[0])
if num_selection > 1:
count += 1

return count
6 changes: 3 additions & 3 deletions air_traffic/trajectory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from geopy import distance
import geopy.distance as gd


def find_first_landed(df):
Expand Down Expand Up @@ -43,7 +43,7 @@ def distance(row1, row2):
point1 = (row1["latitude"], row1["longitude"])
point2 = (row2["latitude"], row2["longitude"])

d = distance.distance(point1, point2).km
d = gd.distance(point1, point2).km
return d


Expand All @@ -52,5 +52,5 @@ def distance_hkia(row):
hkia = (22.308046, 113.918480)
point = (row["latitude"], row["longitude"])

d = distance.distance(point, hkia).km
d = gd.distance(point, hkia).km
return d

0 comments on commit 8a952b5

Please sign in to comment.