agrd commited on
Commit
e8fa881
·
verified ·
1 Parent(s): d531f57

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +174 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import math
3
+
4
+ # --- Data and Functions from your takeoff_model.py ---
5
+ # I've copied all the calculation logic directly into this file
6
+ # to make it a self-contained application.
7
+
8
+ # Data Digitized from the PA-28-181 Chart
9
+ TAKEOFF_DATA = {
10
+ 0: {8.283945422852682: 892.4445513013161, 29.982568972771546: 1283.66893069664},
11
+ 1000: {-3.8119853338943273: 888.693875097673, 30.775981246618983: 1507.1707639598485},
12
+ 2000: {-12.181282683176057: 892.3002945242533, 30.905812345975832: 1674.6528821301918},
13
+ 3000: {-22.421109575043573: 882.9236040151468, 31.045260563803566: 1854.5410831279678},
14
+ 4000: {-29.855142153032396: 893.021578409569, 30.908216625593553: 2077.7544028370503},
15
+ 5000: {-37.61856103864879: 878.2112159644166, 30.775981246618983: 2307.1707639598485},
16
+ }
17
+ REFERENCE_WEIGHT_LBS = 2550.0
18
+ MIN_CHART_WEIGHT_LBS = 2050.0
19
+
20
+ def _interpolate_1d(x, x_points, y_points):
21
+ if x <= x_points[0]: return y_points[0]
22
+ if x >= x_points[-1]: return y_points[-1]
23
+ for i in range(len(x_points) - 1):
24
+ if x_points[i] <= x <= x_points[i+1]:
25
+ x1, x2 = x_points[i], x_points[i+1]
26
+ y1, y2 = y_points[i], y_points[i+1]
27
+ return y1 + (y2 - y1) * ((x - x1) / (x2 - x1))
28
+ return y_points[0]
29
+
30
+ def _get_distance_at_temp_and_alt(temp, alt, data):
31
+ alt_points = sorted(data.keys())
32
+ alt_low, alt_high = -1, -1
33
+ if alt <= alt_points[0]: alt_low = alt_high = alt_points[0]
34
+ elif alt >= alt_points[-1]: alt_low = alt_high = alt_points[-1]
35
+ else:
36
+ for i in range(len(alt_points) - 1):
37
+ if alt_points[i] <= alt <= alt_points[i+1]:
38
+ alt_low, alt_high = alt_points[i], alt_points[i+1]
39
+ break
40
+ temp_points_low = sorted(data[alt_low].keys())
41
+ dist_points_low = [data[alt_low][t] for t in temp_points_low]
42
+ dist_at_alt_low = _interpolate_1d(temp, temp_points_low, dist_points_low)
43
+ if alt_low == alt_high: return dist_at_alt_low
44
+ temp_points_high = sorted(data[alt_high].keys())
45
+ dist_points_high = [data[alt_high][t] for t in temp_points_high]
46
+ dist_at_alt_high = _interpolate_1d(temp, temp_points_high, dist_points_high)
47
+ return _interpolate_1d(alt, [alt_low, alt_high], [dist_at_alt_low, dist_at_alt_high])
48
+
49
+ def _calculate_weight_correction(base_distance, weight_lbs):
50
+ if weight_lbs >= REFERENCE_WEIGHT_LBS:
51
+ return 0.0
52
+ x_points = [1999.6411139821994, 2544.621494879893]
53
+ y_points = [840.3100775193798, 1410.8527131782948]
54
+ distance_at_actual_weight = _interpolate_1d(weight_lbs, x_points, y_points)
55
+ distance_at_reference_weight = _interpolate_1d(REFERENCE_WEIGHT_LBS, x_points, y_points)
56
+ weight_correction_delta = distance_at_actual_weight - distance_at_reference_weight
57
+ return weight_correction_delta
58
+
59
+ def _calculate_headwind_correction(wind_knots):
60
+ # This function calculates the wind correction delta for HEADWIND.
61
+ x_points = [0.16104294478526526, 15.04722311914756]
62
+ y_points = [1139.2960929932183, 847.9173393606702]
63
+ distance_at_actual_wind = _interpolate_1d(wind_knots, x_points, y_points)
64
+ distance_at_zero_wind = _interpolate_1d(0, x_points, y_points)
65
+ wind_correction_delta = distance_at_actual_wind - distance_at_zero_wind
66
+ return wind_correction_delta
67
+
68
+ def _calculate_tailwind_correction(wind_knots):
69
+ # This function calculates the wind correction delta for TAILWIND.
70
+ x_points = [0.16104294478526526, 5.268404907975452]
71
+ y_points = [1139.2960929932183, 1414.1427187600893]
72
+ distance_at_actual_wind = _interpolate_1d(wind_knots, x_points, y_points)
73
+ distance_at_zero_wind = _interpolate_1d(0, x_points, y_points)
74
+ wind_correction_delta = distance_at_actual_wind - distance_at_zero_wind
75
+ return wind_correction_delta
76
+
77
+ def calculate_density_altitude(pressure_altitude_ft, outside_air_temp_c):
78
+ """
79
+ Calculates density altitude based on pressure altitude and outside air temperature.
80
+ """
81
+ # Step 1: Calculate the ISA standard temperature at the given pressure altitude.
82
+ isa_temp_c = 15 - (2 * (pressure_altitude_ft / 1000))
83
+
84
+ # Step 2: Calculate the density altitude using the standard formula.
85
+ density_altitude_ft = pressure_altitude_ft + (120 * (outside_air_temp_c - isa_temp_c))
86
+
87
+ return density_altitude_ft
88
+
89
+ def calculate_takeoff_roll(indicated_altitude_ft, qnh_hpa, temperature_c, weight_kg, wind_type, wind_speed, safety_factor):
90
+ # This function now returns all intermediate steps for display in the GUI.
91
+
92
+ # Step 0: Convert aircraft mass from kg to lbs for internal calculations
93
+ weight_lbs = weight_kg * 2.20462
94
+
95
+ # Step 1: Calculate Pressure Altitude from Indicated Altitude and QNH
96
+ pressure_altitude = indicated_altitude_ft + ((1013.2 - qnh_hpa) * 27)
97
+
98
+ # Step 2: Calculate Density Altitude
99
+ density_altitude = calculate_density_altitude(pressure_altitude, temperature_c)
100
+
101
+ base_distance = _get_distance_at_temp_and_alt(temperature_c, pressure_altitude, TAKEOFF_DATA)
102
+ weight_delta = _calculate_weight_correction(base_distance, weight_lbs)
103
+ distance_after_weight = base_distance + weight_delta
104
+
105
+ wind_delta = 0.0
106
+ if wind_type == "Headwind":
107
+ wind_delta = _calculate_headwind_correction(wind_speed)
108
+ elif wind_type == "Tailwind":
109
+ wind_delta = _calculate_tailwind_correction(wind_speed)
110
+
111
+ distance_after_wind = distance_after_weight + wind_delta
112
+
113
+ # Apply the safety factor
114
+ final_distance_ft = distance_after_wind * safety_factor
115
+ final_distance_m = final_distance_ft * 0.3048 # Conversion factor for feet to meters
116
+
117
+ return (
118
+ f"{pressure_altitude:.0f} ft",
119
+ f"{density_altitude:.0f} ft",
120
+ f"{base_distance:.1f} ft",
121
+ f"{weight_delta:.1f} ft",
122
+ f"{distance_after_weight:.1f} ft",
123
+ f"{wind_delta:.1f} ft",
124
+ f"{final_distance_ft:.0f} ft\n{final_distance_m:.0f} m"
125
+ )
126
+
127
+ # --- Custom CSS for Styling the Columns ---
128
+ # This CSS will style the textboxes within the columns that have the specific classes.
129
+ custom_css = """
130
+ .orange-column .gradio-textbox { background-color: #FFF5E1 !important; border-color: #F39C12 !important; }
131
+ .orange-column .gradio-textbox > label > span { color: #E67E22 !important; }
132
+ .green-column .gradio-textbox { background-color: #E8F8F5 !important; border-color: #2ECC71 !important; }
133
+ .green-column .gradio-textbox > label > span { color: #1E8449 !important; }
134
+ .green-column textarea { font-size: 1.2em !important; font-weight: bold !important; text-align: center !important; }
135
+ """
136
+
137
+ # --- Gradio Interface Definition ---
138
+ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
139
+ gr.Markdown(
140
+ """
141
+ # PA-28-181 Takeoff Ground Roll Calculator
142
+ Enter the conditions below to calculate the takeoff distance.
143
+ """
144
+ )
145
+
146
+ with gr.Row():
147
+ # Column 1: Inputs
148
+ with gr.Column(scale=2):
149
+ altitude = gr.Slider(minimum=0, maximum=8000, value=2000, step=50, label="Indicated Altitude (ft)")
150
+ qnh = gr.Slider(minimum=950, maximum=1050, value=1013.2, step=0.1, label="QNH (hPa)")
151
+ temp = gr.Slider(minimum=-40, maximum=40, value=21, step=1, label="Outside Air Temp (°C)")
152
+ weight = gr.Slider(minimum=930, maximum=1160, value=1090, step=5, label="Aircraft Mass (kg)")
153
+ wind_type = gr.Radio(["Headwind", "Tailwind"], label="Wind Type", value="Headwind")
154
+ wind_speed = gr.Slider(minimum=0, maximum=20, value=8, step=1, label="Wind Speed (knots)")
155
+ safety_factor = gr.Slider(minimum=1.0, maximum=2.0, value=1.0, step=0.05, label="Safety Factor")
156
+
157
+ # Column 2: Intermediate Computations (Orange)
158
+ with gr.Column(scale=2, elem_classes="orange-column"):
159
+ pressure_alt_output = gr.Textbox(label="1. Calculated Pressure Altitude", interactive=False)
160
+ density_alt_output = gr.Textbox(label="2. Calculated Density Altitude", interactive=False)
161
+ base_dist_output = gr.Textbox(label="3. Base Distance (from Alt/Temp)", interactive=False)
162
+ weight_delta_output = gr.Textbox(label="4. Weight Correction Delta", interactive=False)
163
+ dist_after_weight_output = gr.Textbox(label="5. Distance After Weight Adj.", interactive=False)
164
+ wind_delta_output = gr.Textbox(label="6. Wind Correction Delta", interactive=False)
165
+
166
+ # Column 3: Final Output (Green)
167
+ with gr.Column(scale=1, elem_classes="green-column"):
168
+ output_distance = gr.Textbox(label="Final Ground Roll", interactive=False, lines=2)
169
+
170
+ inputs = [altitude, qnh, temp, weight, wind_type, wind_speed, safety_factor]
171
+ outputs = [pressure_alt_output, density_alt_output, base_dist_output, weight_delta_output, dist_after_weight_output, wind_delta_output, output_distance]
172
+
173
+ btn = gr.Button("Calculate", variant="primary")
174
+ btn.click(fn=calculate_takeoff_roll, inputs=inputs, outputs=outputs)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio