souvik0306 commited on
Commit
ff5e53a
·
1 Parent(s): 8ac0324

modular weather script

Browse files
Files changed (3) hide show
  1. flight_distance.py +3 -2
  2. main.py +1 -8
  3. weather.py +50 -32
flight_distance.py CHANGED
@@ -101,9 +101,10 @@ def calculate_distances(airport_identifiers):
101
  # aircraft_specs = get_aircraft_details(aircraft_type)
102
  # print(aircraft_specs)
103
 
104
- # airport_list = ['SIN','CDG']
105
  # print(get_airport_lat_long(airport_list))
106
- # print(calculate_distances(airport_list))
 
107
 
108
  # fuel_burn_rate = aircraft_specs['Fuel_Consumption_kg/hr']
109
  # cruising_speed = aircraft_specs['Max_Fuel_Capacity_kg']
 
101
  # aircraft_specs = get_aircraft_details(aircraft_type)
102
  # print(aircraft_specs)
103
 
104
+ # airport_list = ['SIN','CDG', 'BOM']
105
  # print(get_airport_lat_long(airport_list))
106
+ # trip_distance = calculate_distances(airport_list)
107
+ # print(trip_distance)
108
 
109
  # fuel_burn_rate = aircraft_specs['Fuel_Consumption_kg/hr']
110
  # cruising_speed = aircraft_specs['Max_Fuel_Capacity_kg']
main.py CHANGED
@@ -3,7 +3,7 @@ from flight_distance import *
3
  from optimizer import *
4
  from weather import *
5
 
6
- airport_identifiers = ['BOM', 'CCU', 'DEL'] # Replace with actual identifiers
7
 
8
  #Get Airport Coordinates
9
  lat_long_dict = get_airport_lat_long(airport_identifiers)
@@ -19,11 +19,6 @@ route_factors = extract_route_factors(raw_weather)
19
  print("On Route weather: \n", raw_weather)
20
 
21
  # # Ensure the graph is bidirectional (undirected)
22
- # for (a, b), dist in list(trip_distance.items()):
23
- # trip_distance[(b, a)] = dist
24
-
25
- # # Find the optimal route with the new cost metric
26
- # Ensure the graph is bidirectional (undirected)
27
  for (a, b), dist in list(trip_distance.items()):
28
  trip_distance[(b, a)] = dist
29
 
@@ -34,5 +29,3 @@ optimal_route, optimal_distance = find_optimal_route(airport_identifiers, trip_d
34
  print("Optimal Route:", " -> ".join(optimal_route) + f" -> {optimal_route[0]}")
35
  print("Total Adjusted Distance/Cost:", optimal_distance)
36
 
37
- # print("Optimal Route:", " -> ".join(optimal_route) + f" -> {optimal_route[0]}")
38
- # print("Total Adjusted Distance/Cost:", optimal_distance)
 
3
  from optimizer import *
4
  from weather import *
5
 
6
+ airport_identifiers = ['BLR', 'CCU', 'DEL'] # Replace with actual identifiers
7
 
8
  #Get Airport Coordinates
9
  lat_long_dict = get_airport_lat_long(airport_identifiers)
 
19
  print("On Route weather: \n", raw_weather)
20
 
21
  # # Ensure the graph is bidirectional (undirected)
 
 
 
 
 
22
  for (a, b), dist in list(trip_distance.items()):
23
  trip_distance[(b, a)] = dist
24
 
 
29
  print("Optimal Route:", " -> ".join(optimal_route) + f" -> {optimal_route[0]}")
30
  print("Total Adjusted Distance/Cost:", optimal_distance)
31
 
 
 
weather.py CHANGED
@@ -25,51 +25,69 @@ def fetch_weather(lat, lon):
25
  response = requests.get(url)
26
  return response.json()
27
 
28
- # Fetch weather along all possible routes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def fetch_weather_for_all_routes(airport_identifiers, airports):
30
  route_factors = {}
 
31
 
32
- # Generate all possible routes (permutations)
33
  for route in itertools.permutations(airport_identifiers, len(airport_identifiers)):
34
- route_key = " -> ".join(route) # Key for route factors
35
  route_factors[route_key] = []
36
 
37
  for i in range(len(route) - 1):
38
  start_airport = route[i]
39
  end_airport = route[i + 1]
40
- start_coords = (airports[start_airport][0], airports[start_airport][1])
41
- end_coords = (airports[end_airport][0], airports[end_airport][1])
42
-
43
- # Get 4 intermediate points along the route
44
- points = get_intermediate_points(start_coords, end_coords)
45
-
46
- # Include start and end airport coordinates
47
- points.insert(0, start_coords)
48
- points.append(end_coords)
49
-
50
- # Fetch weather for each point
51
- weather_descriptions = []
52
- temperatures = []
53
-
54
- for point in points:
55
- weather = fetch_weather(point[0], point[1])
56
- weather_descriptions.append(weather['weather'][0]['description'])
57
- temperatures.append(weather['main']['temp'])
58
-
59
- # Aggregate weather for the route segment
60
- avg_temperature = sum(temperatures) / len(temperatures)
61
- most_common_weather = max(set(weather_descriptions), key=weather_descriptions.count)
62
-
63
- # Store the result in the route_factors dictionary for each route segment
64
- segment_key = f"{start_airport} -> {end_airport}"
65
  route_factors[route_key].append({
66
- "segment": segment_key,
67
- "weather": most_common_weather,
68
- "temperature": round(avg_temperature, 2)
69
  })
 
70
  return route_factors
71
 
72
- # # Example airport coordinates
73
  # airports = {
74
  # 'SIN': (1.3644, 103.9915), # Singapore Changi Airport
75
  # 'LAX': (33.9416, -118.4085), # Los Angeles International Airport
 
25
  response = requests.get(url)
26
  return response.json()
27
 
28
+ # Fetch weather data for a specific segment (using caching to avoid redundant requests)
29
+ def get_segment_weather(start_coords, end_coords):
30
+ points = get_intermediate_points(start_coords, end_coords)
31
+ points.insert(0, start_coords)
32
+ points.append(end_coords)
33
+
34
+ weather_descriptions = []
35
+ temperatures = []
36
+
37
+ for point in points:
38
+ weather = fetch_weather(point[0], point[1])
39
+ weather_descriptions.append(weather['weather'][0]['description'])
40
+ temperatures.append(weather['main']['temp'])
41
+
42
+ avg_temperature = sum(temperatures) / len(temperatures)
43
+ most_common_weather = max(set(weather_descriptions), key=weather_descriptions.count)
44
+
45
+ return {
46
+ "weather": most_common_weather,
47
+ "temperature": round(avg_temperature, 2)
48
+ }
49
+
50
+ # Fetch and cache weather data for each segment in the routes
51
+ def fetch_segment_weather_data(airport_identifiers, airports):
52
+ segment_weather_cache = {}
53
+
54
+ for route in itertools.permutations(airport_identifiers, len(airport_identifiers)):
55
+ for i in range(len(route) - 1):
56
+ start_airport = route[i]
57
+ end_airport = route[i + 1]
58
+ segment_key = tuple(sorted([start_airport, end_airport]))
59
+
60
+ if segment_key not in segment_weather_cache:
61
+ start_coords = (airports[start_airport][0], airports[start_airport][1])
62
+ end_coords = (airports[end_airport][0], airports[end_airport][1])
63
+ segment_weather_cache[segment_key] = get_segment_weather(start_coords, end_coords)
64
+
65
+ return segment_weather_cache
66
+
67
+ # Aggregate weather data for all routes using the cached segment data
68
  def fetch_weather_for_all_routes(airport_identifiers, airports):
69
  route_factors = {}
70
+ segment_weather_cache = fetch_segment_weather_data(airport_identifiers, airports)
71
 
 
72
  for route in itertools.permutations(airport_identifiers, len(airport_identifiers)):
73
+ route_key = " -> ".join(route)
74
  route_factors[route_key] = []
75
 
76
  for i in range(len(route) - 1):
77
  start_airport = route[i]
78
  end_airport = route[i + 1]
79
+ segment_key = tuple(sorted([start_airport, end_airport]))
80
+ segment_weather = segment_weather_cache[segment_key]
81
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  route_factors[route_key].append({
83
+ "segment": f"{start_airport} -> {end_airport}",
84
+ "weather": segment_weather["weather"],
85
+ "temperature": segment_weather["temperature"]
86
  })
87
+
88
  return route_factors
89
 
90
+ # # Example usage
91
  # airports = {
92
  # 'SIN': (1.3644, 103.9915), # Singapore Changi Airport
93
  # 'LAX': (33.9416, -118.4085), # Los Angeles International Airport