Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -165,10 +165,10 @@ def get_snow_quality_description(density):
|
|
| 165 |
else:
|
| 166 |
return "Dense/settled snow"
|
| 167 |
|
| 168 |
-
def
|
| 169 |
-
"""Get
|
| 170 |
try:
|
| 171 |
-
# Get snow forecast images
|
| 172 |
base_urls = [
|
| 173 |
"https://graphical.weather.gov/images/conus/SnowAmt1_conus.gif",
|
| 174 |
"https://graphical.weather.gov/images/conus/SnowAmt2_conus.gif",
|
|
@@ -222,16 +222,27 @@ def update_weather(lat, lon):
|
|
| 222 |
lat = float(lat)
|
| 223 |
lon = float(lon)
|
| 224 |
if not (-90 <= lat <= 90 and -180 <= lon <= 180):
|
| 225 |
-
return "Invalid coordinates. Latitude must be between -90 and 90, longitude between -180 and 180.", None,
|
| 226 |
|
| 227 |
-
# Get forecast
|
| 228 |
forecast = get_noaa_forecast(lat, lon)
|
| 229 |
-
radar_images = get_radar_images()
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
|
| 234 |
-
# Create updated map
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
m = folium.Map(location=[lat, lon], zoom_start=9)
|
| 236 |
|
| 237 |
# Add all Montana peaks
|
|
@@ -247,18 +258,10 @@ def update_weather(lat, lon):
|
|
| 247 |
folium.Marker([lat, lon], popup=f"Selected Location<br>Lat: {lat:.4f}, Lon: {lon:.4f}").add_to(m)
|
| 248 |
|
| 249 |
m.add_child(folium.ClickForLatLng()) # Enable click events
|
| 250 |
-
|
| 251 |
-
return (
|
| 252 |
-
forecast,
|
| 253 |
-
radar_images["current"],
|
| 254 |
-
radar_images["forecast_6hr"],
|
| 255 |
-
radar_images["forecast_12hr"],
|
| 256 |
-
m._repr_html_()
|
| 257 |
-
)
|
| 258 |
-
except ValueError as e:
|
| 259 |
-
return f"Error: Invalid coordinate format - {str(e)}", None, None, None, create_map()
|
| 260 |
except Exception as e:
|
| 261 |
-
|
|
|
|
| 262 |
|
| 263 |
def make_peak_click_handler(peak_name):
|
| 264 |
"""Creates a click handler for a specific peak."""
|
|
@@ -295,36 +298,20 @@ with gr.Blocks(title="Montana Mountain Weather") as demo:
|
|
| 295 |
submit_btn = gr.Button("Get Weather", variant="primary")
|
| 296 |
|
| 297 |
with gr.Column(scale=2):
|
| 298 |
-
map_display = gr.HTML(
|
| 299 |
-
|
| 300 |
-
with gr.Row():
|
| 301 |
-
forecast_output = gr.Textbox(
|
| 302 |
-
label="Forecast",
|
| 303 |
-
lines=12,
|
| 304 |
-
placeholder="Select a location or mountain peak to see the forecast..."
|
| 305 |
-
)
|
| 306 |
|
| 307 |
with gr.Row():
|
| 308 |
with gr.Column():
|
| 309 |
-
|
| 310 |
-
label="
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
type="pil"
|
| 314 |
-
)
|
| 315 |
-
with gr.Column():
|
| 316 |
-
forecast_6hr = gr.Image(
|
| 317 |
-
label="6-Hour Precipitation Forecast",
|
| 318 |
-
show_label=True,
|
| 319 |
-
container=True,
|
| 320 |
-
type="pil"
|
| 321 |
)
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
label="12-Hour Precipitation Forecast",
|
| 325 |
show_label=True,
|
| 326 |
container=True,
|
| 327 |
-
type="
|
| 328 |
)
|
| 329 |
|
| 330 |
# Handle submit button click
|
|
@@ -333,9 +320,7 @@ with gr.Blocks(title="Montana Mountain Weather") as demo:
|
|
| 333 |
inputs=[lat_input, lon_input],
|
| 334 |
outputs=[
|
| 335 |
forecast_output,
|
| 336 |
-
|
| 337 |
-
forecast_6hr,
|
| 338 |
-
forecast_12hr,
|
| 339 |
map_display
|
| 340 |
]
|
| 341 |
)
|
|
@@ -351,9 +336,7 @@ with gr.Blocks(title="Montana Mountain Weather") as demo:
|
|
| 351 |
inputs=[lat_input, lon_input],
|
| 352 |
outputs=[
|
| 353 |
forecast_output,
|
| 354 |
-
|
| 355 |
-
forecast_6hr,
|
| 356 |
-
forecast_12hr,
|
| 357 |
map_display
|
| 358 |
]
|
| 359 |
)
|
|
@@ -369,6 +352,13 @@ with gr.Blocks(title="Montana Mountain Weather") as demo:
|
|
| 369 |
- Sacajawea Peak: 45°53′45″N 110°58′7″W
|
| 370 |
- Pioneer Mountain: 45°13′55″N 111°27′2″W
|
| 371 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
**Snow Quality Guide:**
|
| 373 |
- Ultra-light powder: ≤ 50 kg/m³
|
| 374 |
- Light powder: 51-70 kg/m³
|
|
|
|
| 165 |
else:
|
| 166 |
return "Dense/settled snow"
|
| 167 |
|
| 168 |
+
def get_radar_and_snow_forecasts():
|
| 169 |
+
"""Get current radar and animated snow forecast."""
|
| 170 |
try:
|
| 171 |
+
# Get animated snow forecast images
|
| 172 |
base_urls = [
|
| 173 |
"https://graphical.weather.gov/images/conus/SnowAmt1_conus.gif",
|
| 174 |
"https://graphical.weather.gov/images/conus/SnowAmt2_conus.gif",
|
|
|
|
| 222 |
lat = float(lat)
|
| 223 |
lon = float(lon)
|
| 224 |
if not (-90 <= lat <= 90 and -180 <= lon <= 180):
|
| 225 |
+
return "Invalid coordinates. Latitude must be between -90 and 90, longitude between -180 and 180.", None, get_map(lat, lon)
|
| 226 |
|
| 227 |
+
# Get forecast
|
| 228 |
forecast = get_noaa_forecast(lat, lon)
|
|
|
|
| 229 |
|
| 230 |
+
# Get animated snow forecast
|
| 231 |
+
animated_forecast = get_radar_and_snow_forecasts()
|
| 232 |
|
| 233 |
+
# Create updated map
|
| 234 |
+
map_html = get_map(lat, lon)
|
| 235 |
+
|
| 236 |
+
return forecast, animated_forecast, map_html
|
| 237 |
+
except ValueError as e:
|
| 238 |
+
return f"Error: Invalid coordinate format - {str(e)}", None, get_map(lat, lon)
|
| 239 |
+
except Exception as e:
|
| 240 |
+
return f"Error: {str(e)}", None, get_map(lat, lon)
|
| 241 |
+
|
| 242 |
+
def get_map(lat, lon):
|
| 243 |
+
"""Create a map centered on the given coordinates with markers."""
|
| 244 |
+
try:
|
| 245 |
+
# Create map with appropriate zoom level
|
| 246 |
m = folium.Map(location=[lat, lon], zoom_start=9)
|
| 247 |
|
| 248 |
# Add all Montana peaks
|
|
|
|
| 258 |
folium.Marker([lat, lon], popup=f"Selected Location<br>Lat: {lat:.4f}, Lon: {lon:.4f}").add_to(m)
|
| 259 |
|
| 260 |
m.add_child(folium.ClickForLatLng()) # Enable click events
|
| 261 |
+
return m._repr_html_()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
except Exception as e:
|
| 263 |
+
print(f"Error creating map: {str(e)}")
|
| 264 |
+
return None
|
| 265 |
|
| 266 |
def make_peak_click_handler(peak_name):
|
| 267 |
"""Creates a click handler for a specific peak."""
|
|
|
|
| 298 |
submit_btn = gr.Button("Get Weather", variant="primary")
|
| 299 |
|
| 300 |
with gr.Column(scale=2):
|
| 301 |
+
map_display = gr.HTML(get_map(45.5, -111.0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
with gr.Row():
|
| 304 |
with gr.Column():
|
| 305 |
+
forecast_output = gr.Textbox(
|
| 306 |
+
label="Forecast",
|
| 307 |
+
lines=12,
|
| 308 |
+
placeholder="Select a location or mountain peak to see the forecast..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
)
|
| 310 |
+
animated_forecast = gr.Image(
|
| 311 |
+
label="12-Hour Snow Forecast Animation",
|
|
|
|
| 312 |
show_label=True,
|
| 313 |
container=True,
|
| 314 |
+
type="filepath"
|
| 315 |
)
|
| 316 |
|
| 317 |
# Handle submit button click
|
|
|
|
| 320 |
inputs=[lat_input, lon_input],
|
| 321 |
outputs=[
|
| 322 |
forecast_output,
|
| 323 |
+
animated_forecast,
|
|
|
|
|
|
|
| 324 |
map_display
|
| 325 |
]
|
| 326 |
)
|
|
|
|
| 336 |
inputs=[lat_input, lon_input],
|
| 337 |
outputs=[
|
| 338 |
forecast_output,
|
| 339 |
+
animated_forecast,
|
|
|
|
|
|
|
| 340 |
map_display
|
| 341 |
]
|
| 342 |
)
|
|
|
|
| 352 |
- Sacajawea Peak: 45°53′45″N 110°58′7″W
|
| 353 |
- Pioneer Mountain: 45°13′55″N 111°27′2″W
|
| 354 |
|
| 355 |
+
**Snow Forecast Legend:**
|
| 356 |
+
The animated snow forecast shows predicted snow accumulation over the next 12 hours:
|
| 357 |
+
- Frame 1: 0-3 hours
|
| 358 |
+
- Frame 2: 3-6 hours
|
| 359 |
+
- Frame 3: 6-9 hours
|
| 360 |
+
- Frame 4: 9-12 hours
|
| 361 |
+
|
| 362 |
**Snow Quality Guide:**
|
| 363 |
- Ultra-light powder: ≤ 50 kg/m³
|
| 364 |
- Light powder: 51-70 kg/m³
|