Spaces:
Sleeping
Sleeping
File size: 25,803 Bytes
f1d1dd9 ef1ec06 f1d1dd9 ef1ec06 f1d1dd9 ef1ec06 f1aa366 ef1ec06 f1aa366 ef1ec06 f1aa366 ef1ec06 f1aa366 ef1ec06 f1aa366 ef1ec06 f1aa366 ef1ec06 f1d1dd9 ef1ec06 f1d1dd9 ef1ec06 f1aa366 ef1ec06 f1aa366 ef1ec06 f1d1dd9 0048666 f1d1dd9 ef1ec06 f1d1dd9 ef1ec06 f1d1dd9 f1aa366 f1d1dd9 f1aa366 f1d1dd9 ef1ec06 f1d1dd9 0048666 f1d1dd9 ef1ec06 f1d1dd9 0048666 f1d1dd9 f1aa366 06d238a f1aa366 06d238a f1aa366 06d238a f1aa366 06d238a f1aa366 f1d1dd9 ef1ec06 f1d1dd9 f1aa366 0048666 f1aa366 0048666 f1aa366 0048666 f1d1dd9 ef1ec06 f1d1dd9 0048666 f1d1dd9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
import gradio as gr
import folium
import requests
import json
from datetime import datetime
import time
import urllib.parse
class RainViewerMap:
def __init__(self):
self.api_url = "https://api.rainviewer.com/public/weather-maps.json"
self.tile_host = "https://tilecache.rainviewer.com"
self.nhc_api_base = "https://mapservices.weather.noaa.gov/tropical/rest/services/tropical/NHC_tropical_weather/MapServer"
self.storm_cache = {}
def get_radar_data(self):
"""Fetch available radar timestamps from RainViewer API"""
try:
response = requests.get(self.api_url)
data = response.json()
return data
except Exception as e:
print(f"Error fetching radar data: {e}")
return None
def get_hurricane_data(self):
"""Fetch active hurricane/tropical storm data from NOAA NHC"""
active_storms = []
error_messages = []
# Try multiple approaches to get hurricane data
try:
# Approach 1: Try the simplified hurricane data endpoint
simple_url = "https://www.nhc.noaa.gov/CurrentStorms.json"
try:
response = requests.get(simple_url, timeout=10)
if response.status_code == 200:
storms_data = response.json()
for storm in storms_data.get('activeStorms', []):
active_storms.append({
'source': 'NHC CurrentStorms',
'data': storm,
'type': 'current_storms'
})
except Exception as e:
error_messages.append(f"CurrentStorms.json failed: {e}")
# Approach 2: Try NOAA GIS layers for specific storm data
if not active_storms:
layers_url = f"{self.nhc_api_base}/layers?f=json"
try:
layers_response = requests.get(layers_url, timeout=15)
if layers_response.status_code == 200:
layers_data = layers_response.json()
# Look for Atlantic storms (AT1-AT5) forecast points
for layer in layers_data.get('layers', []):
layer_name = layer.get('name', '').lower()
layer_id = layer.get('id')
if ('at1' in layer_name or 'at2' in layer_name or 'at3' in layer_name) and 'forecast points' in layer_name:
query_url = f"{self.nhc_api_base}/{layer_id}/query"
params = {
'where': '1=1',
'outFields': '*',
'f': 'geojson',
'returnGeometry': 'true'
}
try:
query_response = requests.get(query_url, params=params, timeout=15)
if query_response.status_code == 200:
geojson_data = query_response.json()
if geojson_data.get('features'):
active_storms.append({
'source': 'NOAA GIS',
'layer_name': layer.get('name', ''),
'layer_id': layer_id,
'geojson': geojson_data,
'type': 'gis_layer'
})
except Exception as e:
error_messages.append(f"Layer {layer_id} query failed: {e}")
except Exception as e:
error_messages.append(f"Layers API failed: {e}")
# Approach 3: Add sample data if no real data available (for testing)
if not active_storms:
sample_storm = {
'source': 'Sample Data',
'type': 'sample',
'geojson': {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [-80.0, 26.0]
},
'properties': {
'STORMNAME': 'Sample Hurricane',
'STORMTYPE': 'Hurricane',
'MAXWIND': '75',
'MSLP': '980',
'FHOUR': 'Current'
}
}
]
}
}
active_storms.append(sample_storm)
error_messages.append("No real storm data available, showing sample data")
# Store error messages for debugging
self.storm_cache['errors'] = error_messages
self.storm_cache['last_update'] = datetime.now().isoformat()
return active_storms
except Exception as e:
error_messages.append(f"General error fetching hurricane data: {e}")
self.storm_cache['errors'] = error_messages
print(f"Hurricane data fetch error: {e}")
return []
def get_storm_track_data(self, layer_id):
"""Fetch track data for a specific storm layer"""
try:
# Look for corresponding track layer (usually layer_id - 1 or nearby)
track_layer_id = layer_id - 1 if layer_id > 0 else layer_id + 1
query_url = f"{self.nhc_api_base}/{track_layer_id}/query"
params = {
'where': '1=1',
'outFields': '*',
'f': 'geojson'
}
response = requests.get(query_url, params=params)
if response.status_code == 200:
return response.json()
return None
except Exception as e:
print(f"Error fetching track data: {e}")
return None
def create_map(self, lat=40.7128, lon=-74.0060, zoom=8, show_radar=True, time_index=0, show_hurricanes=True):
"""Create a Folium map with optional radar overlay and hurricane tracking"""
# Create base map
m = folium.Map(
location=[lat, lon],
zoom_start=zoom,
tiles='OpenStreetMap'
)
if show_radar:
radar_data = self.get_radar_data()
if radar_data and 'radar' in radar_data:
# Get available timestamps
past_data = radar_data['radar']['past']
nowcast_data = radar_data['radar']['nowcast']
all_times = past_data + nowcast_data
if all_times and time_index < len(all_times):
# Get the radar tile path for selected time
selected_time = all_times[time_index]
tile_path = selected_time['path']
timestamp = selected_time['time']
# Create tile URL template
tile_url = f"{self.tile_host}{tile_path}/512/{{z}}/{{x}}/{{y}}/2/1_1.png"
# Add radar overlay
folium.raster_layers.TileLayer(
tiles=tile_url,
attr='RainViewer',
name='Radar',
overlay=True,
control=True,
opacity=0.6
).add_to(m)
# Add timestamp info
dt = datetime.fromtimestamp(timestamp)
folium.Marker(
[lat + 0.1, lon + 0.1],
popup=f"Radar Time: {dt.strftime('%Y-%m-%d %H:%M:%S')}",
icon=folium.Icon(color='blue', icon='info-sign')
).add_to(m)
# Add hurricane tracking
if show_hurricanes:
self.add_hurricane_layers(m)
# Add layer control
folium.LayerControl().add_to(m)
return m._repr_html_()
def add_hurricane_layers(self, folium_map):
"""Add hurricane tracking layers to the map"""
hurricane_data = self.get_hurricane_data()
for storm_info in hurricane_data:
storm_type = storm_info.get('type', 'unknown')
if storm_type == 'current_storms':
# Handle CurrentStorms.json format
storm_data = storm_info['data']
self.add_current_storms_markers(folium_map, storm_data)
elif storm_type == 'gis_layer':
# Handle GIS layer format
geojson_data = storm_info['geojson']
layer_name = storm_info.get('layer_name', 'Unknown Layer')
for feature in geojson_data.get('features', []):
if feature['geometry']['type'] == 'Point':
coords = feature['geometry']['coordinates']
props = feature.get('properties', {})
# Extract storm information
storm_name = props.get('STORMNAME', props.get('STORMNUM', 'Unknown Storm'))
storm_type_detail = props.get('STORMTYPE', 'Unknown')
max_wind = props.get('MAXWIND', props.get('INTENSITY', 'N/A'))
pressure = props.get('MSLP', 'N/A')
forecast_hour = props.get('FHOUR', props.get('TAU', 'Current'))
# Determine marker color based on storm intensity
color = self.get_storm_color(max_wind)
# Create popup with storm details
popup_text = f"""
<b>{storm_name}</b><br>
Type: {storm_type_detail}<br>
Max Wind: {max_wind} kt<br>
Pressure: {pressure} mb<br>
Forecast Hour: {forecast_hour}<br>
Source: {layer_name}
"""
# Add marker
folium.CircleMarker(
location=[coords[1], coords[0]],
radius=8,
popup=popup_text,
color=color,
fill=True,
fillColor=color,
fillOpacity=0.7,
weight=2
).add_to(folium_map)
elif storm_type == 'sample':
# Handle sample data
geojson_data = storm_info['geojson']
for feature in geojson_data.get('features', []):
if feature['geometry']['type'] == 'Point':
coords = feature['geometry']['coordinates']
props = feature.get('properties', {})
storm_name = props.get('STORMNAME', 'Sample Storm')
storm_type_detail = props.get('STORMTYPE', 'Sample')
max_wind = props.get('MAXWIND', '75')
pressure = props.get('MSLP', '980')
color = self.get_storm_color(max_wind)
popup_text = f"""
<b>{storm_name} (Sample)</b><br>
Type: {storm_type_detail}<br>
Max Wind: {max_wind} kt<br>
Pressure: {pressure} mb<br>
Note: This is sample data for demonstration
"""
folium.CircleMarker(
location=[coords[1], coords[0]],
radius=10,
popup=popup_text,
color=color,
fill=True,
fillColor=color,
fillOpacity=0.7,
weight=3
).add_to(folium_map)
def add_current_storms_markers(self, folium_map, storm_data):
"""Add markers for storms from CurrentStorms.json format"""
# This method handles the specific format from NHC CurrentStorms.json
# Implementation depends on the actual JSON structure
pass
def add_storm_tracks(self, folium_map, track_data):
"""Add storm track lines to the map"""
for feature in track_data.get('features', []):
if feature['geometry']['type'] == 'LineString':
coords = feature['geometry']['coordinates']
# Convert coordinates from [lon, lat] to [lat, lon] for Folium
track_coords = [[coord[1], coord[0]] for coord in coords]
props = feature.get('properties', {})
storm_name = props.get('STORMNAME', 'Storm Track')
folium.PolyLine(
locations=track_coords,
color='red',
weight=3,
opacity=0.8,
popup=f"Track: {storm_name}"
).add_to(folium_map)
def get_storm_color(self, max_wind):
"""Get color based on storm intensity (Saffir-Simpson scale)"""
try:
wind_speed = float(max_wind) if max_wind != 'N/A' else 0
if wind_speed >= 137: # Category 5
return 'purple'
elif wind_speed >= 113: # Category 4
return 'red'
elif wind_speed >= 96: # Category 3
return 'orange'
elif wind_speed >= 83: # Category 2
return 'yellow'
elif wind_speed >= 64: # Category 1
return 'green'
elif wind_speed >= 34: # Tropical Storm
return 'blue'
else: # Tropical Depression
return 'lightblue'
except:
return 'gray'
def get_available_times(self):
"""Get list of available radar times for dropdown"""
radar_data = self.get_radar_data()
if radar_data and 'radar' in radar_data:
past_data = radar_data['radar']['past']
nowcast_data = radar_data['radar']['nowcast']
all_times = past_data + nowcast_data
time_options = []
for i, time_data in enumerate(all_times):
timestamp = time_data['time']
dt = datetime.fromtimestamp(timestamp)
time_options.append(f"{i}: {dt.strftime('%Y-%m-%d %H:%M:%S')}")
return time_options
return ["No data available"]
def create_gradio_app():
rain_viewer = RainViewerMap()
# Get available times for dropdown with error handling
try:
time_options = rain_viewer.get_available_times()
if not time_options:
time_options = ["No data available"]
except Exception as e:
print(f"Error getting initial time options: {e}")
time_options = ["Loading..."]
with gr.Blocks(title="RainViewer Radar Map") as demo:
gr.Markdown("# Weather Radar & Hurricane Tracker")
gr.Markdown("Interactive weather map with radar data from RainViewer and hurricane tracking from NOAA NHC")
with gr.Row():
with gr.Column(scale=1):
lat_input = gr.Number(
label="Latitude",
value=40.7128,
info="Map center latitude"
)
lon_input = gr.Number(
label="Longitude",
value=-74.0060,
info="Map center longitude"
)
zoom_input = gr.Slider(
minimum=1,
maximum=15,
value=8,
step=1,
label="Zoom Level"
)
show_radar = gr.Checkbox(
label="Show Radar",
value=True
)
show_hurricanes = gr.Checkbox(
label="Show Hurricanes",
value=True
)
time_dropdown = gr.Dropdown(
choices=time_options,
label="Radar Time",
value=time_options[-1] if time_options else None,
info="Select radar timestamp"
)
gr.Markdown("### Hurricane Information")
storm_list = gr.HTML(
value="Loading storm data...",
label="Active Storms"
)
update_btn = gr.Button("Update Map", variant="primary")
refresh_times_btn = gr.Button("Refresh Times")
refresh_storms_btn = gr.Button("Refresh Storms")
with gr.Column(scale=2):
map_html = gr.HTML(
value=rain_viewer.create_map(),
label="Radar Map"
)
def update_map(lat, lon, zoom, show_radar_flag, show_hurricanes_flag, selected_time):
time_index = 0
if selected_time and ":" in selected_time and selected_time != "No data available" and selected_time != "Loading...":
try:
time_index = int(selected_time.split(":")[0])
# Validate time_index against available data
available_times = rain_viewer.get_available_times()
if time_index >= len(available_times):
time_index = len(available_times) - 1 if available_times else 0
except (ValueError, IndexError):
time_index = 0 # Default to first time if parsing fails
return rain_viewer.create_map(lat, lon, zoom, show_radar_flag, time_index, show_hurricanes_flag)
def refresh_times():
new_times = rain_viewer.get_available_times()
# Return both updated choices and a safe default value
safe_value = new_times[0] if new_times else None # Use first item instead of last
return gr.Dropdown(choices=new_times, value=safe_value)
def get_storm_list_html():
"""Generate HTML for storm list display"""
hurricane_data = rain_viewer.get_hurricane_data()
if not hurricane_data:
return "<p>No active storms currently detected.</p>"
html_content = "<div style='font-family: Arial, sans-serif;'>"
html_content += "<h4>Active Storms:</h4>"
for i, storm_info in enumerate(hurricane_data):
storm_type = storm_info.get('type', 'unknown')
source = storm_info.get('source', 'Unknown')
html_content += f"<div style='margin: 10px 0; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>"
html_content += f"<strong>Storm #{i+1}</strong> - Source: {source}<br>"
if storm_type == 'nhc_storm':
storm_id = storm_info.get('storm_id', 'Unknown')
forecast_points = storm_info.get('forecast_points', {})
features = forecast_points.get('features', [])
if features:
# Get current position (tau=0) and latest forecast
current_pos = None
latest_forecast = None
for feature in features:
props = feature.get('properties', {})
tau = props.get('tau', 999)
if tau == 0:
current_pos = props
if not latest_forecast or tau > latest_forecast.get('tau', -1):
latest_forecast = props
display_props = current_pos if current_pos else latest_forecast
if display_props:
storm_name = display_props.get('stormname', storm_id)
storm_type_detail = display_props.get('stormtype', 'Unknown')
max_wind = display_props.get('maxwind', 'N/A')
pressure = display_props.get('mslp', 'N/A')
advisory_num = display_props.get('advisnum', 'N/A')
advisory_date = display_props.get('advdate', 'N/A')
ss_num = display_props.get('ssnum', 0)
lat = display_props.get('lat', 'N/A')
lon = display_props.get('lon', 'N/A')
html_content += f"<strong>Name:</strong> {storm_name}<br>"
html_content += f"<strong>Type:</strong> {storm_type_detail}<br>"
html_content += f"<strong>Max Wind:</strong> {max_wind} kt<br>"
html_content += f"<strong>Pressure:</strong> {pressure} mb<br>"
html_content += f"<strong>Category:</strong> {ss_num if ss_num > 0 else 'N/A'}<br>"
html_content += f"<strong>Position:</strong> {lat}°N, {abs(float(lon)) if lon != 'N/A' else 'N/A'}°W<br>"
html_content += f"<strong>Advisory:</strong> #{advisory_num}<br>"
html_content += f"<strong>Updated:</strong> {advisory_date}<br>"
html_content += f"<strong>Data Points:</strong> {len(features)}<br>"
elif storm_type == 'sample':
html_content += "<em>Sample data for demonstration</em><br>"
html_content += "Name: Sample Hurricane<br>"
html_content += "Max Wind: 75 kt<br>"
html_content += "Pressure: 980 mb<br>"
html_content += "</div>"
# Add error information if available
errors = rain_viewer.storm_cache.get('errors', [])
if errors:
html_content += "<div style='margin-top: 15px; padding: 10px; background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 5px;'>"
html_content += "<strong>Debug Info:</strong><br>"
for error in errors[-3:]: # Show last 3 errors
html_content += f"• {error}<br>"
html_content += "</div>"
html_content += "</div>"
return html_content
update_btn.click(
fn=update_map,
inputs=[lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown],
outputs=map_html
)
refresh_times_btn.click(
fn=refresh_times,
outputs=time_dropdown
)
refresh_storms_btn.click(
fn=get_storm_list_html,
outputs=storm_list
)
# Initialize components on load
def initialize_app():
storm_html = get_storm_list_html()
# Also refresh time options on load to ensure they're current
current_times = rain_viewer.get_available_times()
safe_time_value = current_times[0] if current_times else None
return storm_html, gr.Dropdown(choices=current_times, value=safe_time_value)
demo.load(
fn=initialize_app,
outputs=[storm_list, time_dropdown]
)
# Auto-update only on manual controls (not time dropdown to prevent conflicts)
for input_component in [lat_input, lon_input, zoom_input, show_radar, show_hurricanes]:
input_component.change(
fn=update_map,
inputs=[lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown],
outputs=map_html
)
# Only update map when time dropdown is explicitly changed by user
time_dropdown.select(
fn=update_map,
inputs=[lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown],
outputs=map_html
)
return demo
if __name__ == "__main__":
app = create_gradio_app()
app.launch(share=True) |