def get_airports_from_icao_code(departure_airport_icao_code, destination_airport_icao_code): with open_database_session() as session: departure_airport = Airport.airport_from_icao_code( session, departure_airport_icao_code) destination_airport = Airport.airport_from_icao_code( session, destination_airport_icao_code) return departure_airport, destination_airport
def _check_intersections_related_to_airports(self, departure_airport, destination_airport): intersections = [] sections = self._sections_from_airports(departure_airport, destination_airport) longitude_based = Airport.should_be_longitude_based( departure_airport, destination_airport) follow_ascending_order = Airport.follow_ascending_order( departure_airport, destination_airport) bbox = bounding_box_related_to_airports(departure_airport, destination_airport) sorting_key = (lambda x: reference_point(x, longitude_based)) sorting_reverse = (not follow_ascending_order) cells = self._stsc.cells_within_bounding_box( bbox, sorting_key=sorting_key, sorting_reverse=sorting_reverse) if not cells or not sections: return intersections iter_sections, iter_cells = iter(sections), iter(cells) section, cell = next(iter_sections), next(iter_cells) def move_section_iterator(section, cell): return ((follow_ascending_order and section.section_point < reference_point(cell, longitude_based)) or (not follow_ascending_order and section.section_point > reference_point(cell, longitude_based))) while True: try: distance = (ObstacleDetector.distance_between_section_and_cell( section, cell)) if distance < cell.radius: intersection = (ObstacleDetector. _intersection_between_section_and_cell( section, cell)) if intersection.flight_ids: intersections.append(intersection) section = next(iter_sections) else: if move_section_iterator(section, cell): # section is placed before cell section = next(iter_sections) else: cell = next(iter_cells) except StopIteration: break merged_intersections = self.merge_intersections_with_the_same_convection_cell( intersections) logger.debug( 'Found following intersections {0} from departure {1} to destination {2}' .format(merged_intersections, departure_airport, destination_airport)) return merged_intersections
def _check_multiple_intersections(departure_airport, destination_airport, convection_cells, algorithm_name=None, **kwargs): intersections = [] algorithm = ALGORITHM_MAP[algorithm_name] sections = algorithm.sections_from_airports( departure_airport, destination_airport, **kwargs, ) longitude_based = Airport.should_be_longitude_based( departure_airport, destination_airport) follow_ascending_order = Airport.follow_ascending_order( departure_airport, destination_airport) convection_cells.sort(key=(lambda x: reference_point(x, longitude_based)), reverse=(not follow_ascending_order)) if not sections or not convection_cells: return intersections iter_sections, iter_cells = iter(sections), iter(convection_cells) section, cell = next(iter_sections), next(iter_cells) def should_move_section_iterator(section, cell): return ( (follow_ascending_order and section.section_point < reference_point(cell, longitude_based)) or (not follow_ascending_order and section.section_point > reference_point(cell, longitude_based))) while True: try: distance = distance_between_section_and_cell(section, cell) if distance < cell.radius: impact = measure_impact_convection_cell_on_section( section, cell) if impact: intersection = Intersection(cell, departure_airport, destination_airport, impact) intersections.append(intersection) cell = next(iter_cells) else: if should_move_section_iterator(section, cell): # section is placed before cell, move cell section = next(iter_sections) else: cell = next(iter_cells) except StopIteration: break return intersections
def _gen_departure_destination_airports(airport_tracking_list): if airport_tracking_list is None: yield from _gen_default_airport_tracking_list() else: for departure_airport_code, destination_airport_code in airport_tracking_list: departure_airport = Airport.airport_from_icao_code( session, departure_airport_code) destination_airport = Airport.airport_from_icao_code( session, destination_airport_code) yield departure_airport, destination_airport
def check_obstacles_related_to_flight_location(self, prev_flight_location, curr_flight_location): '''Check for obstacles in current flight location''' logger.debug( 'Check for weather obstacle in current flight location {0}'.format( curr_flight_location)) flight = prev_flight_location.flight flight_plan = flight.flight_plan airports = ObstacleDetector.DepartureAndDestinationAirports( flight_plan.departure_airport, flight_plan.destination_airport) normalized_flight_locations_ = normalized_flight_locations( [prev_flight_location, curr_flight_location]) if not normalized_flight_locations_: return [] normalized_flight_location = normalized_flight_locations_[0] curr_flight_ids = self.flight_ids_in_the_same_airway_of_normalized_flight_location( normalized_flight_location, airports) longitude_based = Airport.should_be_longitude_based(*airports) follow_ascending_order = Airport.follow_ascending_order(*airports) intersections = self.check_intersections_related_to_airports(*airports) intersection_index = 0 def move_intersection_iterator(flight_location, intersection): return ((follow_ascending_order and reference_point( flight_location, longitude_based) > reference_point( intersection.convection_cell, longitude_based)) or (not follow_ascending_order and reference_point( flight_location, longitude_based) < reference_point( intersection.convection_cell, longitude_based))) while intersection_index < len(intersections): intersection = intersections[intersection_index] if not move_intersection_iterator(normalized_flight_location, intersection): break intersection_index += 1 obstacles = [] for index in range(intersection_index, len(intersections)): cell, next_flight_ids = intersections[index] how_likely = ObstacleDetector.likelihood_of_encounter_obstacle( curr_flight_ids, next_flight_ids) obstacle = Obstacle(curr_flight_location, cell, how_likely) obstacles.append(obstacle) logger.debug( 'Found the following obstacles {0} for flight location {1}'.format( obstacles, curr_flight_location)) return obstacles
def check_for_intersections(departure_airport, destination_airport, convection_cell): intersections = [] sections = Section.sections_from_airports(departure_airport, destination_airport) clusters_freq = [len(set(s.labels)) for s in sections] print('clusters found') print(clusters_freq) longitude_based = Airport.should_be_longitude_based( departure_airport, destination_airport) follow_ascending_order = Airport.follow_ascending_order( departure_airport, destination_airport) cells = [convection_cell] # TODO: maybe use more than one cell iter_sections, iter_cells = iter(sections), iter(cells) section, cell = next(iter_sections), next(iter_cells) def move_section_iterator(section, cell): return ( (follow_ascending_order and section.section_point < reference_point(cell, longitude_based)) or (not follow_ascending_order and section.section_point > reference_point(cell, longitude_based))) while True: try: distance = (ObstacleDetector.distance_between_section_and_cell( section, cell)) if distance < cell.radius: intersection = (intersection_between_section_and_cell( section, cell)) if intersection.flight_ids: intersections.append(intersection) section = next(iter_sections) else: if move_section_iterator(section, cell): # section is placed before cell section = next(iter_sections) else: cell = next(iter_cells) except StopIteration: break return intersections
def get_airport_from_airport_data(airport_data): return Airport(icao_code=airport_data['icao'], name=airport_data['name'], latitude=round(airport_data['latitude'], 3), longitude=round(airport_data['longitude'], 3), altitude=convert_feet_to_meters(airport_data['altitude']), iata_code=airport_data['iata'] if not pd.isnull(airport_data['iata']) else None, country=airport_data['country'])
def convection_cells_stats(convection_cell_ids): global session with open_database_session() as session: BSB = Airport.airport_from_icao_code(session, 'SBBR') GRU = Airport.airport_from_icao_code(session, 'SBGR') print('create intersections table from', BSB, 'to', GRU) # create_intersections_table(session, convection_cell_ids, BSB, GRU) print('#' * 20) print() print('create intersections table from', GRU, 'to', BSB) create_intersections_table(session, convection_cell_ids, GRU, BSB) print('#' * 20) print() print('generate flights vs convection cells charts') plot_flights_vs_convection_cells(session, convection_cell_ids)
def _normalize_from_section_points(flight_locations, section_points): normalized_flight_locations = [] fl = flight_locations[0] flight = fl.flight flight_plan = flight.flight_plan longitude_based = Airport.should_be_longitude_based( flight_plan.departure_airport, flight_plan.destination_airport) follow_ascending_order = Airport.follow_ascending_order( flight_plan.departure_airport, flight_plan.destination_airport) section_points_iterator = iter(section_points) mid_point = next(section_points_iterator) flight_locations.sort( key=(lambda fl: fl.longitude if longitude_based else fl.latitude), reverse=(not follow_ascending_order), ) for prev_location, curr_location in zip(flight_locations, flight_locations[1:]): try: while _check_mid_point_before_flight_location( mid_point, prev_location, longitude_based, follow_ascending_order): mid_point = next(section_points_iterator) except StopIteration: logger.error( 'Invalid set of flight locations {0!r} and {1!r} of flight {2!r}' .format(prev_location, curr_location, flight)) break # leave the outer for loop if _check_mid_point_within_flight_locations(mid_point, prev_location, curr_location, longitude_based): normalized_flight_location = _normalize_flight_location( mid_point, prev_location, curr_location, longitude_based) normalized_flight_locations.append(normalized_flight_location) logger.debug( 'Reduce {0} flight locations to {1} normalized flight locations'. format(len(flight_locations), len(normalized_flight_locations))) return normalized_flight_locations
def _get_flight_addresses_from_airports(departure_airport, destination_airport): '''Return flight ICAO24 addresses from departure airport to destination airport''' global session callsigns = Airport.callsigns_from_airports( session, departure_airport, destination_airport) bbox = bounding_box_related_to_airports( departure_airport, destination_airport) addresses = _get_addresses_from_callsigns_inside_bounding_box(callsigns, bbox) logger.debug('Flight addresses found from {0!r} to {1!r}: {2}'.format( departure_airport, destination_airport, addresses)) return addresses
def normalize_from_airports(session, departure_airport, destination_airport): '''Return SORTED normalized flight locations from departure airport to destination airport''' normalized_flight_locations = [ normalized_flight_location for flight_plan in (FlightPlan.flight_plans_from_airports( session, departure_airport, destination_airport)) for normalized_flight_location in ( normalize_from_flight_plan(flight_plan)) ] # sort flight locations longitude_based = Airport.should_be_longitude_based( departure_airport, destination_airport) follow_ascending_order = Airport.follow_ascending_order( departure_airport, destination_airport) normalized_flight_locations.sort(key=(lambda fl: fl.longitude if longitude_based else fl.latitude), reverse=(not follow_ascending_order)) return normalized_flight_locations
def track_en_route_flights_by_airports( departure_airport_code, destination_airport_code, round_trip_mode=False): '''Keep track of current flights information from departure airport to destination airport.''' global session logger.info('Track flight addresses from {0} to {1} in {2} mode'.format( departure_airport_code, destination_airport_code, 'round trip' if round_trip_mode else 'one way')) address_to_flight = {} count_iterations = 0 with open_database_session() as session: departure_airport = Airport.airport_from_icao_code(session, departure_airport_code) destination_airport = Airport.airport_from_icao_code(session, destination_airport_code) while count_iterations < ITERATIONS_LIMIT_TO_SEARCH_FLIGHTS: time.sleep(SLEEP_TIME_TO_GET_FLIGHT_IN_SECS) if _should_update_flight_addresses(count_iterations): addresses = _update_flight_addresses( departure_airport, destination_airport, round_trip_mode) _update_flights(address_to_flight, addresses) count_iterations += 1
def normalize_from_flight_locations( flight_locations, partition_interval=FLIGHT_PATH_PARTITION_INTERVAL_IN_DEGREES): '''Normalize flight locatins FROM SPECIFIC FLIGHT''' if not flight_locations: return flight_locations fl = flight_locations[0] flight_plan = fl.flight.flight_plan section_points = Airport._section_points_from_airports( flight_plan.departure_airport, flight_plan.destination_airport, partition_interval) return _normalize_from_section_points(flight_locations, section_points)
def sections_from_airports(departure_airport, destination_airport, **kwargs): '''Return sections from flight locations''' min_entries_per_section = kwargs.get('min_entries_per_section', NUMBER_ENTRIES_PER_SECTION) key = ( departure_airport.icao_code, destination_airport.icao_code, min_entries_per_section, ) if key not in Section.cache: with open_database_session() as session: flight_locations = normalizer.normalize_from_airports( session, departure_airport, destination_airport) # section should be longitude based longitude_based = Airport.should_be_longitude_based( departure_airport, destination_airport) sections = [] if not flight_locations: return sections prev_flight_location = flight_locations[0] section_flight_locations = [prev_flight_location] for curr_flight_location in flight_locations[1:]: if Section._flight_locations_are_part_of_the_same_section( prev_flight_location, curr_flight_location, longitude_based): section_flight_locations.append(curr_flight_location) else: if len(section_flight_locations ) >= min_entries_per_section: section = Section.from_flight_locations( section_flight_locations) sections.append(section) section_flight_locations = [curr_flight_location] prev_flight_location = curr_flight_location Section.cache[key] = sections # return cached results return Section.cache[key]
def _section_of_normalized_flight_location(self, normalized_flight_location, departure_airport, destination_airport): sections = self._sections_from_airports(departure_airport, destination_airport) longitude_based = Airport.should_be_longitude_based( departure_airport, destination_airport) def is_normalized_flight_location_inside_section( normalize_flight_locations, section): return (float(section.section_point) == float( reference_point(normalized_flight_location, longitude_based))) for section in sections: if is_normalized_flight_location_inside_section( normalize_flight_locations, section): return section return None