Spaces:
Build error
Build error
Commit ·
b7e906d
1
Parent(s): ff5e53a
flight_distance updated
Browse files- flight_distance.py +130 -23
flight_distance.py
CHANGED
|
@@ -96,34 +96,141 @@ def calculate_distances(airport_identifiers):
|
|
| 96 |
|
| 97 |
return distances
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# aircraft_type = input("Enter the aircraft type: ")
|
| 101 |
# aircraft_specs = get_aircraft_details(aircraft_type)
|
| 102 |
# print(aircraft_specs)
|
| 103 |
|
| 104 |
-
# airport_list = ['
|
| 105 |
# print(get_airport_lat_long(airport_list))
|
|
|
|
| 106 |
# trip_distance = calculate_distances(airport_list)
|
| 107 |
# print(trip_distance)
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
-
# reserve_fuel_percentage = 0.05 # 5% reserve
|
| 113 |
-
|
| 114 |
-
# #Reserver Fuel calculation
|
| 115 |
-
# reserve_fuel = reserve_fuel_percentage*max_fuel_capacity
|
| 116 |
-
|
| 117 |
-
# #Flight Time
|
| 118 |
-
# flight_time = trip_distance/cruising_speed
|
| 119 |
-
|
| 120 |
-
# #Fuel needed for Trip
|
| 121 |
-
# fuel_required = flight_time*fuel_burn_rate
|
| 122 |
-
|
| 123 |
-
# total_fuel_with_reserve = fuel_required+reserve_fuel
|
| 124 |
-
|
| 125 |
-
# if total_fuel_with_reserve>max_fuel_capacity:
|
| 126 |
-
# print("Cant Fly without refuel")
|
| 127 |
-
# else:
|
| 128 |
-
# print(f"Total fuel required for the trip (including reserve): {round(total_fuel_with_reserve,2)} kg")
|
| 129 |
-
|
|
|
|
| 96 |
|
| 97 |
return distances
|
| 98 |
|
| 99 |
+
def calculate_distances(airport_identifiers):
|
| 100 |
+
"""
|
| 101 |
+
Calculate the distance between multiple airports.
|
| 102 |
+
|
| 103 |
+
:param airport_identifiers: List of airport names or IATA codes
|
| 104 |
+
:return: Dictionary with airport pairs as keys and their distances in kilometers as values
|
| 105 |
+
"""
|
| 106 |
+
lat_long_dict = get_airport_lat_long(airport_identifiers)
|
| 107 |
+
if isinstance(lat_long_dict, str): # Check if there was an error fetching airport data
|
| 108 |
+
return lat_long_dict
|
| 109 |
+
|
| 110 |
+
# Calculate the distance for each combination of airports
|
| 111 |
+
distances = {}
|
| 112 |
+
identifiers = list(lat_long_dict.keys())
|
| 113 |
+
for i in range(len(identifiers)):
|
| 114 |
+
for j in range(i + 1, len(identifiers)):
|
| 115 |
+
airport1 = identifiers[i]
|
| 116 |
+
airport2 = identifiers[j]
|
| 117 |
+
lat1, lon1 = lat_long_dict[airport1]
|
| 118 |
+
lat2, lon2 = lat_long_dict[airport2]
|
| 119 |
+
distance = haversine_distance(lat1, lon1, lat2, lon2)
|
| 120 |
+
distances[(airport1, airport2)] = distance
|
| 121 |
+
|
| 122 |
+
return distances
|
| 123 |
+
|
| 124 |
+
def calculate_fuel_and_time(distance, cruising_speed, fuel_burn_rate, reserve_fuel_percentage, max_fuel_capacity):
|
| 125 |
+
"""
|
| 126 |
+
Calculate the total fuel required for a given distance including reserve fuel and flight time.
|
| 127 |
+
|
| 128 |
+
:param distance: Distance of the trip in kilometers
|
| 129 |
+
:param cruising_speed: Cruising speed of the aircraft in km/h
|
| 130 |
+
:param fuel_burn_rate: Fuel consumption rate in kg/hr
|
| 131 |
+
:param reserve_fuel_percentage: Percentage of fuel to keep as reserve
|
| 132 |
+
:param max_fuel_capacity: Maximum fuel capacity of the aircraft in kg
|
| 133 |
+
:return: Total fuel required including reserve, and estimated flight time
|
| 134 |
+
"""
|
| 135 |
+
# Phase speeds and times
|
| 136 |
+
climb_speed = 280 # km/h
|
| 137 |
+
climb_time = 15 / 60 # 15 minutes in hours
|
| 138 |
+
descent_speed = 250 # km/h
|
| 139 |
+
descent_time = 10 / 60 # 10 minutes in hours
|
| 140 |
+
|
| 141 |
+
# Calculate distances for each phase
|
| 142 |
+
climb_distance = climb_speed * climb_time
|
| 143 |
+
descent_distance = descent_speed * descent_time
|
| 144 |
+
cruise_distance = distance - (climb_distance + descent_distance)
|
| 145 |
+
|
| 146 |
+
# Adjust if cruise distance is negative (short flights)
|
| 147 |
+
if cruise_distance < 0:
|
| 148 |
+
climb_time = climb_distance / climb_speed
|
| 149 |
+
descent_time = descent_distance / descent_speed
|
| 150 |
+
cruise_distance = 0
|
| 151 |
+
|
| 152 |
+
cruise_time = cruise_distance / cruising_speed
|
| 153 |
+
|
| 154 |
+
# Total flight time
|
| 155 |
+
total_flight_time = climb_time + cruise_time + descent_time
|
| 156 |
+
|
| 157 |
+
# Fuel calculations
|
| 158 |
+
fuel_required = total_flight_time * fuel_burn_rate
|
| 159 |
+
reserve_fuel = reserve_fuel_percentage * max_fuel_capacity
|
| 160 |
+
total_fuel_with_reserve = fuel_required + reserve_fuel
|
| 161 |
+
|
| 162 |
+
return total_fuel_with_reserve, total_flight_time
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def check_route_feasibility(aircraft_type, trip_distances, aircraft_specs):
|
| 166 |
+
"""
|
| 167 |
+
Check if the aircraft can fly each route without needing refuel and return expected flight times.
|
| 168 |
+
|
| 169 |
+
:param aircraft_type: The type of the aircraft (e.g., "Airbus A320")
|
| 170 |
+
:param trip_distances: Dictionary with airport pairs and distances
|
| 171 |
+
:param aircraft_specs: Dictionary containing aircraft details
|
| 172 |
+
:return: Dictionary with feasibility and flight time for each route
|
| 173 |
+
"""
|
| 174 |
+
# Extract aircraft specifications
|
| 175 |
+
fuel_burn_rate = aircraft_specs['Fuel_Consumption_kg/hr'] # kg per hour
|
| 176 |
+
cruising_speed = aircraft_specs['Speed_kmh'] # km/h
|
| 177 |
+
max_fuel_capacity = aircraft_specs['Max_Fuel_Capacity_kg'] # kg
|
| 178 |
+
reserve_fuel_percentage = 0.05 # 5% reserve
|
| 179 |
+
|
| 180 |
+
results = {}
|
| 181 |
+
|
| 182 |
+
# Check feasibility for each route
|
| 183 |
+
for (airport1, airport2), distance in trip_distances.items():
|
| 184 |
+
# Calculate the total fuel required and flight time for the trip including reserve
|
| 185 |
+
total_fuel_with_reserve, flight_time = calculate_fuel_and_time(
|
| 186 |
+
distance, cruising_speed, fuel_burn_rate, reserve_fuel_percentage, max_fuel_capacity
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if total_fuel_with_reserve > max_fuel_capacity:
|
| 190 |
+
results[(airport1, airport2)] = {
|
| 191 |
+
"Can Fly": False,
|
| 192 |
+
"Reason": f"Cannot fly without refuel. Required: {round(total_fuel_with_reserve, 2)} kg, Capacity: {max_fuel_capacity} kg",
|
| 193 |
+
"Expected Flight Time (hrs)": round(flight_time, 2)
|
| 194 |
+
}
|
| 195 |
+
else:
|
| 196 |
+
results[(airport1, airport2)] = {
|
| 197 |
+
"Can Fly": True,
|
| 198 |
+
"Expected Flight Time (hrs)": round(flight_time, 2),
|
| 199 |
+
"Total Fuel Required (kg)": round(total_fuel_with_reserve, 2)
|
| 200 |
+
}
|
| 201 |
+
return results
|
| 202 |
+
|
| 203 |
+
def can_fly_route(aircraft_type, airport_identifiers):
|
| 204 |
+
"""
|
| 205 |
+
Determine if the aircraft can fly the route without needing refuel, considering fuel capacity and reserve.
|
| 206 |
+
|
| 207 |
+
:param aircraft_type: The type of the aircraft (e.g., "Airbus A320")
|
| 208 |
+
:param airport_identifiers: List of airport names or IATA codes
|
| 209 |
+
:return: String message indicating if the aircraft can complete the trip or not
|
| 210 |
+
"""
|
| 211 |
+
# Fetch aircraft details
|
| 212 |
+
aircraft_specs = get_aircraft_details(aircraft_type)
|
| 213 |
+
if isinstance(aircraft_specs, str):
|
| 214 |
+
return aircraft_specs # Return the error message if aircraft details are not found
|
| 215 |
+
|
| 216 |
+
# Calculate distances between airports
|
| 217 |
+
trip_distances = calculate_distances(airport_identifiers)
|
| 218 |
+
if isinstance(trip_distances, str):
|
| 219 |
+
return trip_distances # Return the error message if distances could not be calculated
|
| 220 |
+
|
| 221 |
+
# Check if the aircraft can fly each route without refuel
|
| 222 |
+
return check_route_feasibility(aircraft_type, trip_distances, aircraft_specs)
|
| 223 |
+
|
| 224 |
# aircraft_type = input("Enter the aircraft type: ")
|
| 225 |
# aircraft_specs = get_aircraft_details(aircraft_type)
|
| 226 |
# print(aircraft_specs)
|
| 227 |
|
| 228 |
+
# airport_list = ['CCU', 'CDG', 'SIN']
|
| 229 |
# print(get_airport_lat_long(airport_list))
|
| 230 |
+
|
| 231 |
# trip_distance = calculate_distances(airport_list)
|
| 232 |
# print(trip_distance)
|
| 233 |
|
| 234 |
+
# # Check if the aircraft can fly the route without refuel
|
| 235 |
+
# result = can_fly_route(aircraft_type, airport_list)
|
| 236 |
+
# print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|