Skip to content

Commit

Permalink
Refactor: Loop data I/O
Browse files Browse the repository at this point in the history
  • Loading branch information
kcwongaz committed Aug 30, 2022
1 parent a7a3de0 commit d0073b5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
68 changes: 68 additions & 0 deletions air_traffic/io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import pandas as pd
import numpy as np
import csv
import os

import air_traffic.loop as lp


def read_trajectories_range(datadir, start, end, verbose=False,
fname_only=False):
Expand Down Expand Up @@ -48,3 +52,67 @@ def fetch_fnames(datadir, date):
for file in files:
fname = os.path.join(subdir, file)
yield fname


def loop_write(dataset, savename):

with open(savename, mode="w") as f:
writer = csv.writer(f, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)

for dfs in dataset:
for df in dfs:

df = df.loc[df["distance"] <= 200]
dist = df["distance"].to_numpy()

min_loc = lp.locate_loops(dist)

_, min_time = lp.find_minima_spacetime(dist[:,1], dist[:,0],
min_loc)

# No loop then skip
if len(min_time) == 0:
continue

_, exit_time = lp.find_exitpoint(dist[:,1], dist[:,0], min_loc)

# Determine entry direction thus the holding area
if df["longitude"].iloc[0] < 114:
area = "CANTO" # loops in West
elif df["latitude"].iloc[0] > 21:
area = "ABBEY" # loops in Northeast
else:
area = "BETTY" # loops in Southeast

row = [area]
row.extend(min_time)
row.append(exit_time)
writer.writerow(row)


def loop_read(fname, keep_area=True):

data = []
with open(fname) as f:
reader = csv.reader(f, delimiter=",")
for row in reader:

# Throw away the loop area label
if not keep_area:
row = row[1:]
data.append(row)

return data


def loop_read_area(fname):

data_all = loop_read(fname)
data = {"ABBEY": [], "BETTY": [], "CANTO": []}

for entry in data_all:
area = entry[0]
data[area].append(entry[1:])

return data
2 changes: 1 addition & 1 deletion air_traffic/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ 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 redirected.
got rescheduled.
"""

s = []
Expand Down

0 comments on commit d0073b5

Please sign in to comment.