Airport.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. import configparser
  3. import glob
  4. import os
  5. import sys
  6. from formats.SctEseFormat import SctEseFormat
  7. class Airport:
  8. def parseAirport(self, airport):
  9. if None == airport.get('runways'):
  10. return False
  11. self.runways = airport['runways'].split(':')
  12. return True
  13. def findGngData(data, path):
  14. if None == data.get('gngwildcard'):
  15. return None, None
  16. # find the newest ESE file
  17. files = glob.glob(os.path.join(path, data['gngwildcard'] + '.ese'))
  18. latestEse = max(files, key=os.path.getctime)
  19. # search for the corresponding SCT file
  20. latestSct = os.path.splitext(latestEse)[0] + '.sct'
  21. # check if the files exist
  22. if False == os.path.isfile(latestEse) or False == os.path.isfile(latestSct):
  23. return None, None
  24. return latestSct, latestEse
  25. def parsePlanning(self, planning):
  26. for key in planning:
  27. # check if the runways match
  28. rwy = key.upper()
  29. if rwy not in self.runways:
  30. sys.stderr.write('Unable to find ' + rwy + ' in defined runways')
  31. return False
  32. # check if arrivals are defined
  33. arrivals = planning[key].split(':')
  34. if 0 == len(arrivals):
  35. sys.stderr.write('No arrival routes for ' + rwy)
  36. return False
  37. # assign the arrival routes to the runways
  38. self.arrivalRoutes[rwy] = arrivals
  39. return True
  40. def __init__(self, filepath : str, icao : str):
  41. self.runways = []
  42. self.arrivalRoutes = {}
  43. config = configparser.ConfigParser()
  44. config.read(filepath)
  45. dataConfig = None
  46. airportConfig = None
  47. planningConfig = None
  48. # search the required sections
  49. for key in config:
  50. if 'DATA' == key:
  51. dataConfig = config['DATA']
  52. elif 'AIRPORT' == key:
  53. airportConfig = config['AIRPORT']
  54. elif 'PLANNING' == key:
  55. planningConfig = config['PLANNING']
  56. # find the GNG-file data
  57. sctFile, eseFile = Airport.findGngData(dataConfig, os.path.dirname(filepath))
  58. if None == sctFile or None == eseFile:
  59. sys.stderr.write('No GNG-files found')
  60. sys.exit(-1)
  61. # parse the airport information
  62. if None == airportConfig or False == self.parseAirport(airportConfig):
  63. sys.stderr.write('No or no valid airport configuration found')
  64. sys.exit(-1)
  65. # parse the planning information
  66. if None == planningConfig or False == self.parsePlanning(planningConfig):
  67. sys.stderr.write('No or no valid planning configuration found')
  68. sys.exit(-1)
  69. # get all required arrival routes and avoid duplicates
  70. requiredArrivalRoutes = []
  71. for rwy in self.arrivalRoutes:
  72. for arrival in self.arrivalRoutes[rwy]:
  73. if arrival not in requiredArrivalRoutes:
  74. requiredArrivalRoutes.append(arrival)
  75. # parse the GNG data
  76. print('Used GNG-Data: ' + eseFile)
  77. self.gngData = SctEseFormat(sctFile, eseFile, icao, requiredArrivalRoutes)