-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate_LARA-config.py
324 lines (246 loc) · 11.8 KB
/
Create_LARA-config.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from inspect import indentsize
from textwrap import indent
import json
import regex as re
from dms2dec.dms_convert import dms2dec
from extra_functions.basic_functions import cleanup
import collections
import warnings
warnings.filterwarnings('ignore', category= SyntaxWarning)
with open(f"area-attributes.json", "r") as file:
attributes_dict = json.load(file)
fileDef = "C:/Users/matis/OneDrive/Documenten/Euroscope/Plugins/Topsky/TopSkyAreas.txt"
#fileDef = "C:/Users/matis/Downloads/Trash/TopSkyAreas_PT.txt"
#fileDef = "C:\\Users\\matis\\OneDrive\\Documenten\\Euroscope\\EDGG-Full-Package_20220226132637-220201-0005\\EDGG\\Plugins\\Topsky_Default\\TopSkyAreas.txt"
vacc = "BELUX"
vaccHTTP = "www.beluxvacc/extra_areas"
area_filter = {
"RemoveByName" : ["EBGB_ATZ", "KOK_Gate"],
"RemoveByCategory" : ["HOLD", "ATZ", "NTZ", "S", "BUFFERH", "BUFFER", "HOGATE"]
}
def Reason_discard(area):
msg = ""
if not "coordinates" in area:
msg = f"{area["name"]} has no coordinates"
elif area["name"] in area_filter["RemoveByName"]:
msg = f"{area["name"]} is in area_filter (name)"
elif area["category"] in area_filter["RemoveByCategory"]:
msg = f"{area["name"]} is in area_filter (category: '{area["category"]}')"
else:
msg = f"Issue with {area["name"]} could not be determined\n{area}"
return f"WARN: {msg} -> area discarted"
#Filters and converts list of possible areas into a definitive dictionary of actual areas.
def filterTextBlockForAreas(area_txtblocks, area_filter):
all_areas = []
for block in area_txtblocks:
possible_area = IdentifyTS_AreaBlock_AndToStandardFormat(block)
if (
"coordinates" in possible_area
and possible_area["name"] not in area_filter["RemoveByName"]
and possible_area["category"] not in area_filter["RemoveByCategory"]
):
all_areas.append(possible_area)
else:
print(Reason_discard(possible_area))
return all_areas
#Converts TS coordinates into VG coordinates
def Convert_TScoordinates_toVGformat(topskycoordinates):
vatgl_coordinates = []
val = ""
for coordinate in topskycoordinates.split("\n"):
if re.match(r"^N0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)W([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", coordinate):
val = re.sub(r"^N0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)W([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", "\g<1>\g<2>\g<3>;-\g<5>\g<6>\g<7>\g<8>", coordinate )
#Degree decimal minutes
elif re.match(r"^N0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)E([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", coordinate):
val = re.sub(r"^N0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)E([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", "\g<1>\g<2>\g<3>;\g<5>\g<6>\g<7>\g<8>", coordinate )
elif re.match(r"^S0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)W([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", coordinate):
val = re.sub(r"^S0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)W([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", "-\g<1>\g<2>\g<3>;-\g<5>\g<6>\g<7>\g<8>", coordinate )
elif re.match(r"^S0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)E([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?",coordinate):
val = re.sub(r"^S0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)E([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?", "-\g<1>\g<2>\g<3>;\g<5>\g<6>\g<7>\g<8>", coordinate )
else:
print("No coordinate match found for", coordinate, "Check line 40")
coors = val.replace(" ", "").split(';')
vatgl_coordinates.append(coors)
return vatgl_coordinates
def Convert_TScoordiantes_ToCircle(topskycoordinates):
circle_split = topskycoordinates.split(":")
coorLine = " ".join(circle_split[1:3])
coors = Convert_TScoordinates_toVGformat(coorLine)[0]
circle_VG = {
"centre" : coors,
"radius" : circle_split[3]
}
#area["label"] = "" #will be popped line 155
return circle_VG
def Rewrite_TScoordinates(topskycoordinates):
if topskycoordinates.startswith("CIRCLE:"):
circle_split = topskycoordinates.split(":")
coors = circle_split[1:3]
circle_coors = {
"centre" : coors,
"radius" : float(circle_split[3])
}
return circle_coors
joined_coors = topskycoordinates.split("\n")
split_coors = []
for coor in joined_coors:
tmp = coor.split(" ")
split_coors.append(tmp)
return split_coors
#Creates dictionary of areas
def IdentifyTS_AreaBlock_AndToStandardFormat(block):
data = {}
for line in block.split("\n"):
if line.startswith("CATEGORY"):
data["category"] = line.split(":")[1]
elif line.startswith("AREA"):
data["category"] = line.split(":")[1]
if line.startswith("LABEL"):
data["label"] = line.split(":")[1:3]
# lat =line.split(":")[1]
# lon = line.split(":")[2]
# data["label"] = f"{lat} {lon}"
if line.startswith("AREA"):
values = line.split(":")
data["name"] = values[2]
if line.startswith("LIMITS"):
values = line.split(":")
data["lower"] = int(values[1])
data["upper"] = int(values[2])
if line.startswith("ACTIVE"):
activations = line.replace("ACTIVE:", "")
if "active" in data:
data["active"] += "\n" + activations
else:
data["active"] = activations
##################################
if line.startswith("CIRCLE"):
data["coordinates"] = line
# circle_split = data["coordinates"].split(":")
# coorLine = " ".join(circle_split[1:3])
# coors = Convert_TScoordinates_toVGformat(coorLine)[0]
# circle_VG = {
# "centre" : coors,
# "radius" : circle_split[3]
# }
# data["coordinates"] = circle_VG
if re.match(r"^(N|S)0?([0-9]{2}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?(:|\s)(E|W)([0-9]{3}).([0-9]{2}).([0-9]{1,2})(.[0-9]{3,})?",line):
line = line.replace(":"," ") #Coordinates can be separated by : or \s but let's standardise with \s
if "coordinates" in data:
data["coordinates"] += "\n" + line
else:
data["coordinates"] = line
if "active" in data:
data["active"] = data["active"].split("\n")
return data
#Creates activation dictionary
def TSAreaActivationToStandardFormat(rules):
notams = []
eaup = []
schedules = []
controllers = []
for rule in rules:
if rule == "1":
schedules.append("0101:1231::0000:2359:") #always active
elif rule.startswith("NOTAM"):
r = rule.replace("NOTAM:","")[5:] #Hide fir and :
notams.append(r)
elif rule.startswith("AUP"):
r = rule.replace("AUP:","")
eaup.append(r)
elif rule.startswith("ID"):
r = rule.replace("ID:","")
controllers.append(r)
else:
schedules.append(rule)
activation = {
"NOTAM" : notams,
"EAUP" : eaup,
"ControllerID" : controllers,
"Schedules" : schedules
}
return activation
#Finds or creates a TS-label
def FindLabel(area):
#Poly areas with a label
if "label" in area:
return area["label"], area
#Circles (centre)
elif area["coordinates"].startswith("CIRCLE:"):
area["label"] = "" #will be popped line 155
return area["coordinates_TS"]["centre"], area
#Poly areas without a label
else:
print("WARN: No label-position found for area:", area["name"], " -> First coordiante will be label position.")
area["label"] = "" #will be popped line 155
return area["coordinates_TS"][0], area
def addAttributes(area,attributes_dict):
if area["name"] in attributes_dict:
for item in attributes_dict[area["name"]]:
area[item] = attributes_dict[area["name"]][item]
return area
#LARA-config workflow
def CreateLARAConfiguration(all_areas, vacc, vaccHTTP,attributes_dict):
Lara_config = {}
areas = []
for area in all_areas:
area["coordinates_TS"] = Rewrite_TScoordinates(area["coordinates"])
if "active" in area:
area["activation"] = TSAreaActivationToStandardFormat(area["active"])
area.pop("active")
else:
area["activation"] = {}
area["label_TS"], area = FindLabel(area)
#All these have been repurposed elsewhere with a different key
area.pop("coordinates")
area.pop("label")
area = addAttributes(area,attributes_dict) #Allows you to save entry conditions and remarks.
areas.append(area)
vacc_config = {
"Areas" : areas,
"vACC_HTTP_Activation" : vaccHTTP
}
Lara_config = {
vacc : vacc_config
}
return Lara_config
#Post - To find possible malconfigurations
def Find_anomalies(lara_config, vacc):
areas = lara_config[vacc]["Areas"]
names = []
anomaly_schedule = []
anomaly_coordinates = []
anomaly_activation = []
for area in areas:
names.append(area["name"])
if len(area["activation"]) > 0: #If activation is defined
schedules = area["activation"]["Schedules"]
for s in schedules:
if not re.search(r"(\d{4,6}:\d{4,6}:\d{0,8}:\d{0,4}:\d{0,4}(?::[0-9]{1,6}:[0-9]{1,6})?(?::[\S]+)?)", s):
anomaly_schedule.append(s)
else:
anomaly_activation.append(area["name"])
# if len(area["activation"]["NOTAM"]) == 0 and len(area["activation"]["EAUP"]) == 0 and len(area["activation"]["ControllerID"]) == 0 and len(area["activation"]["Schedules"]) == 0:
# anomaly_activation.append(area["name"])
if len(area["coordinates_TS"]) < 3 and type(area["coordinates_TS"]) == list:
anomaly_coordinates.append(area["name"])
non_unique_names = [item for item, count in collections.Counter(names).items() if count > 1]
anomalies = {
"Bad_Schedules" : anomaly_schedule, #Likely areas activated by runways etc. Please activate these using vaccHTTP and remove their schedule in the json.
"Duplicates_AreaNames" : non_unique_names, #This area is defined multiple times.
"<3_coordinates" : anomaly_coordinates, #This area does not have coordinates defined. At this time, it might also be a circle.
"no_activation" : anomaly_activation #This area is not activated automatically. Please activate using vaccHTTP. Or if unused, remove
}
print("Anomalies file create. Please check it Output/anomalies.json and the documentation!")
return anomalies
#Initializer code
with open(fileDef, "r") as file:
area_txtblocks = cleanup(file)
all_areas = filterTextBlockForAreas(area_txtblocks, area_filter)
lara_config = CreateLARAConfiguration(all_areas, vacc, vaccHTTP,attributes_dict)
#print(lara_config)
anomalies = Find_anomalies(lara_config, vacc)
with open(f"./output/{vacc}.json", "w") as file:
file.write(json.dumps(lara_config, indent=4))
with open("./output/anomalies.json", "w") as file:
file.write(json.dumps(anomalies, indent=4))