-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI_to_Topsky.py
149 lines (107 loc) · 4.97 KB
/
API_to_Topsky.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
import requests
import json
import pandas as pd
from datetime import datetime
from datetime import timedelta
import sys
def CreateTimeBlocks(temp):
RSA = temp["RSA"].iloc[0]
parsed_df = pd.DataFrame(columns=["RSA","WEF","UNT","MNM FL","MAX FL"])
times = []
starts = list(temp["UNT"])
ends = list(temp["WEF"])
times.extend(starts)
times.extend(ends)
times = set(times)
times = list(times)
times.sort()
time = 0
Exit = len(times)
#For each time block
while time < Exit-1:
#Get current time block
block_start = times[time]
block_end = times[time+1]
#Initialize block FL
low = sys.maxsize
high = 0
#Check all entries of the same RSA
for index, row in temp.iterrows():
area_start = row["WEF"]
area_end = row["UNT"]
if str(int(row["MNM FL"])).lstrip('0') == "": # "0" ==> ""
area_low = 0
else:
area_low = int(str(int(row["MNM FL"])).lstrip('0')) #"095" ==> 95
if str(int(row["MAX FL"])).lstrip('0') == "":
area_high = 0
else:
area_high = int(str(int(row["MAX FL"])).lstrip('0'))
#If entry is within our time frame amend FL-block
if area_start <= block_start and area_end >= block_end:
#if block_start <= area_start and area_end <= block_end:
if area_low < low:
low = area_low
if area_high > high:
high = area_high
row = {"RSA" : RSA, "WEF" : block_start, "UNT" :block_end, "MNM FL":low, "MAX FL": high }
#print(row)
#If the block-FL not altered and remained max,0 then the area never became active. There's a break between activation times therefore we do not append
if high != 0 and low != sys.maxsize:
#Add data for this time block
#parsed_df = parsed_df.append(row,ignore_index=True)
parsed_df.loc[len(parsed_df)] = [RSA,block_start,block_end,low,high]
time += 1 #Go to the next time block
return parsed_df #All the data of each time block for one RSA
def writeAreas(area):
data_one_RSA = ""
#For all the data of each time block of one RSA
#RSA,NOTAM,REMARK,MNM FL,MAX FL,WEF,UNT,FUA/EU RS,FIR,UIR
for index, row in area.iterrows():
try: #To get data
AreaName = row["RSA"]
SchedStartDate = row["WEF"].strftime('%m%d')
SchedEndDate = row["UNT"].strftime('%m%d')
StartTime = row["WEF"].strftime('%H%M')
EndTime = row["UNT"].strftime('%H%M')
SchedWeekdays = datetime.today().weekday() + 1
Lower = int(row["MNM FL"])*100
Upper = int(row["MAX FL"])*100
row = f"{AreaName}:{SchedStartDate}:{SchedEndDate}:{SchedWeekdays}:{StartTime}:{EndTime}:{Lower}:{Upper}:AUP/UUP\n"
#print(row)
#Save data
data_one_RSA += row
except Exception as e:
print("Couldn't read data on row ", index)
print(e.args)
return data_one_RSA
def Parsedates(area):
date = datetime.today().strftime('%Y-%m-%d')
area["start_time"] = datetime.strptime('%s %s' % (date,area["start_time"]), '%Y-%m-%d %H:%M:%S')
area["end_time"] = datetime.strptime('%s %s' % (date,area["end_time"]), '%Y-%m-%d %H:%M:%S')
midnight = datetime.strptime('%s %s' % (date,"00:00"), '%Y-%m-%d %H:%M')
if area["start_time"] >= area["end_time"] and area["end_time"] >= midnight: #Later than midnight is next day, but only if start time later than end (Otherwise later than midnight is same day)(Same time means active all day)
area["end_time"] += timedelta(days=1)
return area
topsky_data = ""
temp = pd.DataFrame(columns=["RSA","WEF","UNT","MNM FL","MAX FL"])
for neighbourg in ["EB","EH","LF","ED"]:
data_raw = requests.get("https://eaup.ambitio.cyou/%s" % (neighbourg)).text
data = json.loads(data_raw)
name = data["areas"][0]["name"]
for area in data["areas"]:
area = Parsedates(area)
#If area is the same, add it
if area["name"] == name:
temp.loc[len(temp)] = [area["name"],area["start_time"],area["end_time"],area["minimum_fl"],area["maximum_fl"]]
#If area is different, parse the previous entries first, then initialize a a clean df with current entry
else:
areas = CreateTimeBlocks(temp)
topsky_data += writeAreas(areas)
temp = pd.DataFrame(columns=["RSA","WEF","UNT","MNM FL","MAX FL"])
temp.loc[0] = [area["name"],area["start_time"],area["end_time"],area["minimum_fl"],area["maximum_fl"]]
name = area["name"]
with open("./topsky_data.txt","w") as file:
file.write(topsky_data)
print(topsky_data)
print("Data valid until", data["notice_info"]["valid_til"])