SctEseFormat.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python
  2. import sys
  3. from aman.types.ArrivalRoute import ArrivalRoute
  4. from aman.types.Runway import Runway
  5. from aman.types.Waypoint import Waypoint
  6. class SctEseFormat:
  7. def readFile(filename : str):
  8. fileBlocks = {}
  9. block = None
  10. # read the file line by line and create segments based on []-entries
  11. with open(filename) as file:
  12. for line in file:
  13. line = line.strip()
  14. # found a new segment
  15. if line.startswith('['):
  16. block = line[1:-1]
  17. fileBlocks.setdefault(block, [])
  18. # append the last backend
  19. elif None != block and 0 != len(line):
  20. fileBlocks[block].append(line)
  21. return fileBlocks
  22. def parseWaypoint(waypoint : str, nameIdx : int, latitudeIdx : int, longitudeIdx : int):
  23. split = list(filter(None, waypoint.split(' ')))
  24. if len(split) <= longitudeIdx:
  25. sys.stderr.write('Invalid waypoint format: ' + waypoint)
  26. sys.exit(-1)
  27. return Waypoint(name = split[nameIdx], latitude = split[latitudeIdx], longitude = split[longitudeIdx])
  28. def parseRunway(runway : str):
  29. split = list(filter(None, runway.split(' ')))
  30. if 9 != len(split) or '' == split[8]:
  31. return None, None, None
  32. waypoint0 = Waypoint(name = split[0], latitude = split[4], longitude = split[5])
  33. waypoint1 = Waypoint(name = split[1], latitude = split[6], longitude = split[7])
  34. return split[8], Runway(waypoint0, waypoint1), Runway(waypoint1, waypoint0)
  35. def extractSctInformation(self, sctFilepath : str):
  36. config = SctEseFormat.readFile(sctFilepath)
  37. foundAirports = False
  38. foundRunways = False
  39. foundVOR = False
  40. foundNDB = False
  41. foundFix = False
  42. for key in config:
  43. if 'VOR' == key:
  44. foundVOR = True
  45. elif 'NDB' == key:
  46. foundNDB = True
  47. elif 'FIXES' == key:
  48. foundFix = True
  49. elif 'AIRPORT' == key:
  50. foundAirports = True
  51. elif 'RUNWAY' == key:
  52. foundRunways = True
  53. if False == foundVOR:
  54. sys.stderr.write('Unable to find VOR-entries in the sector file')
  55. sys.exit(-1)
  56. if False == foundNDB:
  57. sys.stderr.write('Unable to find NDB-entries in the sector file')
  58. sys.exit(-1)
  59. if False == foundFix:
  60. sys.stderr.write('Unable to find FIX-entries in the sector file')
  61. sys.exit(-1)
  62. if False == foundAirports:
  63. sys.stderr.write('Unable to find AIRPORT-entries in the sector file')
  64. sys.exit(-1)
  65. if False == foundRunways:
  66. sys.stderr.write('Unable to find RUNWAY-entries in the sector file')
  67. sys.exit(-1)
  68. # extract all waypoints
  69. for waypoint in config['VOR']:
  70. waypoint = SctEseFormat.parseWaypoint(waypoint, 0, 2, 3)
  71. self.Waypoints.setdefault(waypoint.Name, []).append(waypoint)
  72. for waypoint in config['NDB']:
  73. waypoint = SctEseFormat.parseWaypoint(waypoint, 0, 1, 2)
  74. self.Waypoints.setdefault(waypoint.Name, []).append(waypoint)
  75. for waypoint in config['FIXES']:
  76. waypoint = SctEseFormat.parseWaypoint(waypoint, 0, 1, 2)
  77. self.Waypoints.setdefault(waypoint.Name, []).append(waypoint)
  78. # extract the airports
  79. for airport in config['AIRPORT']:
  80. airport = SctEseFormat.parseWaypoint(airport, 0, 2, 3)
  81. self.Airports.setdefault(airport.Name, []).append(airport)
  82. # extract the runways
  83. for runway in config['RUNWAY']:
  84. airport, runway0, runway1 = SctEseFormat.parseRunway(runway)
  85. if None != airport:
  86. if not airport in self.Runways:
  87. self.Runways.setdefault(airport, [])
  88. self.Runways[airport].append(runway0)
  89. self.Runways[airport].append(runway1)
  90. def parseArrivalRoute(self, route : str, airport : Waypoint):
  91. # split the route and validate that it is a STAR for the airport
  92. split = route.split(':')
  93. if 5 != len(split) or 'STAR' != split[0] or split[1] != airport.Name:
  94. return None
  95. # find all waypoints
  96. waypoints = []
  97. route = list(filter(None, split[4].split(' ')))
  98. for waypoint in route:
  99. # find the waypoint in the route
  100. coordinates = self.Waypoints[waypoint]
  101. # no waypoint with this name defined
  102. if None == coordinates:
  103. sys.stderr.write('Unable to find waypoint ' + waypoint)
  104. sys.exit(-1)
  105. # multiple waypoints, but use Haversine distance to distinct between candidates
  106. elif 1 != len(coordinates):
  107. minDistance = sys.float_info.max
  108. nearest = None
  109. # we assume that waypoints with the same name are not close each other
  110. for coordinate in coordinates:
  111. distance = coordinate.haversine(airport)
  112. # found a closer waypoint
  113. if minDistance > distance:
  114. minDistance = distance
  115. nearest = coordinate
  116. if None == nearest:
  117. sys.stderr.write('Unable to find a close waypoint for ' + waypoint)
  118. sys.exit(-1)
  119. waypoints.append(nearest)
  120. # extend the list of waypoints
  121. else:
  122. waypoints.append(coordinates[0])
  123. # create the arrival route
  124. return ArrivalRoute(split[3], split[2], waypoints)
  125. def extractArrivalRoutes(self, eseFilepath : str, airport : str, allowedRoutes : list):
  126. config = SctEseFormat.readFile(eseFilepath)
  127. foundSidsStars = False
  128. # search the airport in the extracted list
  129. if not airport in self.Airports:
  130. sys.stderr.write('Unable to find the requested airport')
  131. sys.exit(-1)
  132. airport = self.Airports[airport][0]
  133. for key in config:
  134. if 'SIDSSTARS' == key:
  135. foundSidsStars = True
  136. if False == foundSidsStars:
  137. sys.stderr.write('Unable to find SIDSSTARS-entries in the sector file')
  138. sys.exit(-1)
  139. # parse all arrival routes
  140. for line in config['SIDSSTARS']:
  141. route = self.parseArrivalRoute(line, airport)
  142. if None != route and route.Name in allowedRoutes:
  143. self.ArrivalRoutes.setdefault(route.Runway, []).append(route)
  144. def __init__(self, sctFilepath : str, eseFilepath : str, airport : str, allowedRoutes : list):
  145. self.ArrivalRoutes = {}
  146. self.Waypoints = {}
  147. self.Airports = {}
  148. self.Runways = {}
  149. self.extractSctInformation(sctFilepath)
  150. self.extractArrivalRoutes(eseFilepath, airport, allowedRoutes)