# Install required packages if missing import subprocess import sys def install_package(package): try: __import__(package) except ImportError: print(f"Installing {package}...") subprocess.check_call([sys.executable, "-m", "pip", "install", package]) # Install required packages required_packages = [ 'gradio', 'pandas', 'requests', 'beautifulsoup4', 'plotly', 'folium', 'numpy', 'geopy' ] for package in required_packages: install_package(package) # Now import everything import gradio as gr import pandas as pd import requests from bs4 import BeautifulSoup import plotly.express as px import plotly.graph_objects as go import folium from folium.plugins import MarkerCluster, HeatMap import re import numpy as np from urllib.parse import urljoin import time import json import os from geopy.distance import geodesic from datetime import datetime, timedelta import warnings warnings.filterwarnings('ignore') # Function to convert degrees, minutes, seconds to decimal degrees def dms_to_decimal(degrees, minutes, seconds, direction): decimal = float(degrees) + float(minutes)/60 + float(seconds)/3600 if direction in ['S', 'W', '-']: decimal = -decimal return decimal # Function to parse DMS coordinates from text def parse_dms_coordinates(text): if not text: return None, None # Clean up the text text = text.replace('**', '').replace('\n', ' ').strip() # Look for DMS format lat_pattern = r'(\d+)°\s*(\d+)\'\s*(\d+\.?\d*)\'?\'\s*(?:Latitude|[NS])' lon_pattern = r'(-?\d+)°\s*(\d+)\'\s*(\d+\.?\d*)\'?\'\s*(?:Longitude|[EW])' lat_match = re.search(lat_pattern, text) lon_match = re.search(lon_pattern, text) latitude = None longitude = None if lat_match: lat_deg, lat_min, lat_sec = lat_match.groups() # Determine direction (N positive, S negative) lat_dir = 'N' if 'S' in text: lat_dir = 'S' latitude = dms_to_decimal(lat_deg, lat_min, lat_sec, lat_dir) if lon_match: lon_deg, lon_min, lon_sec = lon_match.groups() # Determine direction (E positive, W negative) lon_dir = 'E' if 'W' in text or '-' in lon_deg: lon_dir = 'W' longitude = dms_to_decimal(lon_deg.replace('-', ''), lon_min, lon_sec, lon_dir) return latitude, longitude # Function to fetch NASA FIRMS data def fetch_firms_data(): """ Fetch NASA FIRMS VIIRS active fire data for the last 24 hours Filters for USA only and returns relevant fire hotspot data with cleaned numeric fields """ firms_url = "https://firms.modaps.eosdis.nasa.gov/data/active_fire/viirs/csv/J1_VIIRS_C2_Global_24h.csv" try: print("Fetching NASA FIRMS data...") response = requests.get(firms_url, timeout=60) response.raise_for_status() # Read CSV data from io import StringIO firms_df = pd.read_csv(StringIO(response.text)) print(f"Retrieved {len(firms_df)} global fire hotspots") # Filter for USA coordinates (approximate bounding box) # Continental US, Alaska, Hawaii usa_firms = firms_df[ ( # Continental US ((firms_df['latitude'] >= 24.5) & (firms_df['latitude'] <= 49.0) & (firms_df['longitude'] >= -125.0) & (firms_df['longitude'] <= -66.0)) | # Alaska ((firms_df['latitude'] >= 54.0) & (firms_df['latitude'] <= 72.0) & (firms_df['longitude'] >= -180.0) & (firms_df['longitude'] <= -130.0)) | # Hawaii ((firms_df['latitude'] >= 18.0) & (firms_df['latitude'] <= 23.0) & (firms_df['longitude'] >= -162.0) & (firms_df['longitude'] <= -154.0)) ) ].copy() print(f"Filtered to {len(usa_firms)} USA fire hotspots") # Clean numeric columns to handle "nominal" values if 'frp' in usa_firms.columns: # Clean FRP column usa_firms['frp'] = usa_firms['frp'].astype(str).str.replace('nominal', '', regex=False) usa_firms['frp'] = usa_firms['frp'].str.replace(r'[^\d\.]', '', regex=True) # Keep only digits and decimals usa_firms['frp'] = usa_firms['frp'].replace('', '0') # Replace empty strings with 0 usa_firms['frp'] = pd.to_numeric(usa_firms['frp'], errors='coerce').fillna(0) print(f"Cleaned FRP column, mean FRP: {usa_firms['frp'].mean():.2f}") if 'confidence' in usa_firms.columns: # Clean confidence column usa_firms['confidence'] = usa_firms['confidence'].astype(str).str.replace('nominal', '', regex=False) usa_firms['confidence'] = usa_firms['confidence'].str.replace(r'[^\d\.]', '', regex=True) usa_firms['confidence'] = usa_firms['confidence'].replace('', '50') # Default confidence usa_firms['confidence'] = pd.to_numeric(usa_firms['confidence'], errors='coerce').fillna(50) print(f"Cleaned confidence column, mean confidence: {usa_firms['confidence'].mean():.2f}") # Add datetime column for easier processing if 'acq_date' in usa_firms.columns and 'acq_time' in usa_firms.columns: try: usa_firms['datetime'] = pd.to_datetime( usa_firms['acq_date'] + ' ' + usa_firms['acq_time'].astype(str).str.zfill(4), format='%Y-%m-%d %H%M', errors='coerce' ) # Sort by acquisition time (most recent first) usa_firms = usa_firms.sort_values('datetime', ascending=False) print(f"Added datetime column, latest detection: {usa_firms['datetime'].max()}") except Exception as e: print(f"Warning: Could not create datetime column: {e}") return usa_firms except Exception as e: print(f"Error fetching FIRMS data: {e}") return pd.DataFrame() # Function to match FIRMS hotspots with InciWeb incidents def match_firms_to_inciweb(inciweb_df, firms_df, max_distance_km=50): """ Match FIRMS hotspots to InciWeb incidents based on geographic proximity Enhanced with better error handling and data cleaning """ if firms_df.empty or inciweb_df.empty: print("Warning: Empty dataframes passed to matching function") return inciweb_df try: print(f"Matching {len(firms_df)} FIRMS hotspots to {len(inciweb_df)} InciWeb incidents...") # Initialize new columns safely inciweb_df = inciweb_df.copy() inciweb_df['firms_hotspots'] = 0 inciweb_df['total_frp'] = 0.0 # Fire Radiative Power inciweb_df['avg_confidence'] = 0.0 inciweb_df['latest_hotspot'] = None inciweb_df['is_active'] = False inciweb_df['hotspot_coords'] = None inciweb_df['activity_level'] = 'Unknown' # Only process incidents that have coordinates incidents_with_coords = inciweb_df[ (inciweb_df['latitude'].notna()) & (inciweb_df['longitude'].notna()) ].copy() print(f"Processing {len(incidents_with_coords)} incidents with coordinates...") for idx, incident in incidents_with_coords.iterrows(): try: incident_coords = (float(incident['latitude']), float(incident['longitude'])) # Find FIRMS hotspots within the specified distance matched_hotspots = [] for _, hotspot in firms_df.iterrows(): try: hotspot_lat = float(hotspot['latitude']) hotspot_lon = float(hotspot['longitude']) hotspot_coords = (hotspot_lat, hotspot_lon) distance = geodesic(incident_coords, hotspot_coords).kilometers if distance <= max_distance_km: # Create a clean hotspot record with safe conversions clean_hotspot = { 'latitude': hotspot_lat, 'longitude': hotspot_lon, 'frp': float(hotspot.get('frp', 0)) if pd.notna(hotspot.get('frp')) else 0.0, 'confidence': float(hotspot.get('confidence', 50)) if pd.notna(hotspot.get('confidence')) else 50.0, 'datetime': hotspot.get('datetime', None), 'distance': distance } matched_hotspots.append(clean_hotspot) except (ValueError, TypeError, KeyError) as e: # Skip invalid hotspot data continue if matched_hotspots: # Calculate aggregated metrics safely num_hotspots = len(matched_hotspots) total_frp = sum(hs['frp'] for hs in matched_hotspots) avg_confidence = sum(hs['confidence'] for hs in matched_hotspots) / num_hotspots if num_hotspots > 0 else 0.0 # Get latest hotspot time latest_hotspot = None hotspot_times = [hs['datetime'] for hs in matched_hotspots if hs['datetime'] is not None] if hotspot_times: latest_hotspot = max(hotspot_times) # Determine activity level based on hotspot count and FRP if num_hotspots >= 20 and total_frp >= 100: activity_level = 'Very High' elif num_hotspots >= 10 and total_frp >= 50: activity_level = 'High' elif num_hotspots >= 5 and total_frp >= 20: activity_level = 'Medium' elif num_hotspots >= 1: activity_level = 'Low' else: activity_level = 'Minimal' # Update the incident data inciweb_df.at[idx, 'firms_hotspots'] = num_hotspots inciweb_df.at[idx, 'total_frp'] = total_frp inciweb_df.at[idx, 'avg_confidence'] = avg_confidence inciweb_df.at[idx, 'latest_hotspot'] = latest_hotspot inciweb_df.at[idx, 'is_active'] = True inciweb_df.at[idx, 'activity_level'] = activity_level # Store simplified hotspot coordinates for visualization hotspot_coords_str = str([(hs['latitude'], hs['longitude'], hs['frp']) for hs in matched_hotspots[:10]]) # Limit to 10 for performance inciweb_df.at[idx, 'hotspot_coords'] = hotspot_coords_str print(f" {incident['name']}: {num_hotspots} hotspots, {total_frp:.1f} FRP, {activity_level} activity") except Exception as e: print(f" Error processing incident {incident.get('name', 'Unknown')}: {e}") continue # Calculate final statistics active_count = (inciweb_df['is_active'] == True).sum() total_with_coords = len(incidents_with_coords) print(f"Found {active_count} active incidents out of {total_with_coords} with coordinates") return inciweb_df except Exception as e: print(f"Error in match_firms_to_inciweb: {e}") # Return original dataframe with safety columns if matching completely fails inciweb_df = inciweb_df.copy() for col in ['firms_hotspots', 'total_frp', 'avg_confidence', 'latest_hotspot', 'is_active', 'hotspot_coords', 'activity_level']: if col not in inciweb_df.columns: if col in ['firms_hotspots']: inciweb_df[col] = 0 elif col in ['total_frp', 'avg_confidence']: inciweb_df[col] = 0.0 elif col in ['is_active']: inciweb_df[col] = False elif col in ['activity_level']: inciweb_df[col] = 'Unknown' else: inciweb_df[col] = None return inciweb_df # Function to scrape InciWeb data from the accessible view page def fetch_inciweb_data(): base_url = "https://inciweb.wildfire.gov" accessible_url = urljoin(base_url, "/accessible-view") try: print(f"Fetching data from: {accessible_url}") response = requests.get(accessible_url, timeout=30) response.raise_for_status() except requests.exceptions.RequestException as e: print(f"Error fetching data from InciWeb: {e}") return pd.DataFrame() soup = BeautifulSoup(response.content, "html.parser") incidents = [] # Find all incident links and process them incident_links = soup.find_all("a", href=re.compile(r"/incident-information/")) for link in incident_links: try: incident = {} # Extract incident name and ID from link incident["name"] = link.text.strip() incident["link"] = urljoin(base_url, link.get("href")) incident["id"] = link.get("href").split("/")[-1] # Navigate through the structure to get incident details row = link.parent if row and row.name == "td": row_cells = row.parent.find_all("td") # Parse the row cells to extract information if len(row_cells) >= 5: incident_type_cell = row_cells[1] if len(row_cells) > 1 else None if incident_type_cell: incident["type"] = incident_type_cell.text.strip() location_cell = row_cells[2] if len(row_cells) > 2 else None if location_cell: incident["location"] = location_cell.text.strip() state_match = re.search(r'([A-Z]{2})', incident["location"]) if state_match: incident["state"] = state_match.group(1) else: state_parts = incident["location"].split(',') if state_parts: incident["state"] = state_parts[0].strip() else: incident["state"] = None size_cell = row_cells[3] if len(row_cells) > 3 else None if size_cell: size_text = size_cell.text.strip() # Clean up size text and handle various formats size_text = size_text.replace('nominal', '').strip() # Remove 'nominal' text # Extract numeric value from size text if size_text and size_text != '': size_match = re.search(r'(\d+(?:,\d+)*)', size_text) if size_match: try: incident["size"] = int(size_match.group(1).replace(',', '')) except ValueError: incident["size"] = None else: incident["size"] = None else: incident["size"] = None updated_cell = row_cells[4] if len(row_cells) > 4 else None if updated_cell: incident["updated"] = updated_cell.text.strip() incidents.append(incident) except Exception as e: print(f"Error processing incident: {e}") continue df = pd.DataFrame(incidents) # Ensure all expected columns exist with safe defaults expected_columns = { "size": None, "type": "Unknown", "location": "Unknown", "state": None, "updated": "Unknown" } for col, default_val in expected_columns.items(): if col not in df.columns: df[col] = default_val # Safe numeric conversion with better error handling if 'size' in df.columns: # Clean the size column first df['size'] = df['size'].astype(str).str.replace('nominal', '', regex=False) df['size'] = df['size'].str.replace(r'[^\d,]', '', regex=True) # Keep only digits and commas df['size'] = df['size'].replace('', None) # Replace empty strings with None # Convert to numeric safely df["size"] = pd.to_numeric(df["size"].str.replace(',', '') if df["size"].dtype == 'object' else df["size"], errors="coerce") print(f"Fetched {len(df)} incidents") return df # Enhanced coordinate extraction with multiple methods def get_incident_coordinates_basic(incident_url): """Enhanced coordinate extraction with proper DMS parsing""" try: print(f" Fetching coordinates from: {incident_url}") response = requests.get(incident_url, timeout=20) response.raise_for_status() soup = BeautifulSoup(response.content, "html.parser") # Method 1: Look for coordinate table rows with proper DMS parsing for row in soup.find_all('tr'): th = row.find('th') if th and 'Coordinates' in th.get_text(strip=True): coord_cell = row.find('td') if coord_cell: coord_content = coord_cell.get_text(strip=True) print(f" Found coordinate cell content: {coord_content}") # Parse latitude values (look for degrees, minutes, seconds) lat_deg_match = re.search(r'(\d+)\s*°.*?Latitude', coord_content) lat_min_match = re.search(r'(\d+)\s*\'.*?Latitude', coord_content) lat_sec_match = re.search(r'(\d+\.?\d*)\s*\'\'.*?Latitude', coord_content) # Parse longitude values (look for them after Latitude keyword) longitude_part = coord_content[coord_content.find('Latitude'):] if 'Latitude' in coord_content else coord_content lon_deg_match = re.search(r'[-]?\s*(\d+)\s*°', longitude_part) lon_min_match = re.search(r'(\d+)\s*\'', longitude_part) # Look for longitude seconds in div elements or text lon_sec_div = coord_cell.find('div', class_=lambda c: c and 'margin-right' in c) if lon_sec_div: lon_sec_value = lon_sec_div.get_text(strip=True) lon_sec_match = re.search(r'(\d+\.?\d*)', lon_sec_value) print(f" Found longitude seconds in div: {lon_sec_value}") else: lon_sec_match = re.search(r'(\d+\.?\d*)\s*\'\'', longitude_part) print(f" Parsed components - lat_deg: {lat_deg_match.group(1) if lat_deg_match else None}, " f"lat_min: {lat_min_match.group(1) if lat_min_match else None}, " f"lat_sec: {lat_sec_match.group(1) if lat_sec_match else None}") print(f" lon_deg: {lon_deg_match.group(1) if lon_deg_match else None}, " f"lon_min: {lon_min_match.group(1) if lon_min_match else None}, " f"lon_sec: {lon_sec_match.group(1) if lon_sec_match else None}") # If all values are found, convert to decimal coordinates if lat_deg_match and lat_min_match and lat_sec_match and lon_deg_match and lon_min_match and lon_sec_match: lat_deg = float(lat_deg_match.group(1)) lat_min = float(lat_min_match.group(1)) lat_sec = float(lat_sec_match.group(1)) lon_deg = float(lon_deg_match.group(1)) lon_min = float(lon_min_match.group(1)) lon_sec = float(lon_sec_match.group(1)) # Convert DMS to decimal degrees latitude = lat_deg + lat_min/60 + lat_sec/3600 longitude = -(lon_deg + lon_min/60 + lon_sec/3600) # Western hemisphere is negative print(f" Converted DMS to decimal: {latitude}, {longitude}") return latitude, longitude # Method 2: Look for meta tags with coordinates meta_tags = soup.find_all("meta") for meta in meta_tags: if meta.get("name") == "geo.position": coords = meta.get("content", "").split(";") if len(coords) >= 2: try: lat, lon = float(coords[0].strip()), float(coords[1].strip()) print(f" Found coordinates via meta tags: {lat}, {lon}") return lat, lon except ValueError: pass # Method 3: Look for script tags with map data script_tags = soup.find_all("script") for script in script_tags: if not script.string: continue script_text = script.string # Look for map initialization patterns if "L.map" in script_text or "leaflet" in script_text.lower(): setview_match = re.search(r'setView\s*\(\s*\[\s*(-?\d+\.?\d*)\s*,\s*(-?\d+\.?\d*)\s*\]', script_text, re.IGNORECASE) if setview_match: lat, lon = float(setview_match.group(1)), float(setview_match.group(2)) print(f" Found coordinates via map script: {lat}, {lon}") return lat, lon # Look for direct coordinate assignments lat_match = re.search(r'(?:lat|latitude)\s*[=:]\s*(-?\d+\.?\d*)', script_text, re.IGNORECASE) lon_match = re.search(r'(?:lon|lng|longitude)\s*[=:]\s*(-?\d+\.?\d*)', script_text, re.IGNORECASE) if lat_match and lon_match: lat, lon = float(lat_match.group(1)), float(lon_match.group(1)) print(f" Found coordinates via script variables: {lat}, {lon}") return lat, lon # Method 4: Use predetermined coordinates for known incidents (fallback) known_coords = get_known_incident_coordinates(incident_url) if known_coords: print(f" Using known coordinates: {known_coords}") return known_coords print(f" No coordinates found for {incident_url}") return None, None except Exception as e: print(f" Error extracting coordinates from {incident_url}: {e}") return None, None def get_known_incident_coordinates(incident_url): """Fallback coordinates for some known incident locations""" # Extract incident name/ID from URL incident_id = incident_url.split('/')[-1] if incident_url else "" # Some predetermined coordinates for major fire-prone areas known_locations = { # These are approximate coordinates for demonstration 'horse-fire': (42.0, -104.0), # Wyoming 'aggie-creek-fire': (64.0, -153.0), # Alaska 'big-creek-fire': (47.0, -114.0), # Montana 'conner-fire': (39.5, -116.0), # Nevada 'trout-fire': (35.0, -106.0), # New Mexico 'basin-fire': (34.0, -112.0), # Arizona 'rowena-fire': (45.0, -121.0), # Oregon 'post-fire': (44.0, -115.0), # Idaho } for key, coords in known_locations.items(): if key in incident_id.lower(): return coords return None # Function to get coordinates for a subset of incidents (for demo efficiency) def add_coordinates_to_incidents(df, max_incidents=30): """Add coordinates to incidents with improved success rate""" df = df.copy() df['latitude'] = None df['longitude'] = None # Prioritize recent wildfires, then other incidents recent_wildfires = df[ (df['type'].str.contains('Wildfire', na=False)) & (df['updated'].str.contains('ago|seconds|minutes|hours', na=False)) ].head(max_incidents // 2) other_incidents = df[ ~df.index.isin(recent_wildfires.index) ].head(max_incidents // 2) sample_df = pd.concat([recent_wildfires, other_incidents]).head(max_incidents) print(f"Getting coordinates for {len(sample_df)} incidents (prioritizing recent wildfires)...") success_count = 0 for idx, row in sample_df.iterrows(): if pd.notna(row.get("link")): try: lat, lon = get_incident_coordinates_basic(row["link"]) if lat is not None and lon is not None: # Validate coordinates are reasonable for USA if 18.0 <= lat <= 72.0 and -180.0 <= lon <= -65.0: # USA bounds including Alaska/Hawaii df.at[idx, 'latitude'] = lat df.at[idx, 'longitude'] = lon success_count += 1 print(f" ✅ {row['name']}: {lat:.4f}, {lon:.4f}") else: print(f" ❌ {row['name']}: Invalid coordinates {lat}, {lon}") else: print(f" ⚠️ {row['name']}: No coordinates found") # Small delay to avoid overwhelming the server time.sleep(0.3) except Exception as e: print(f" ❌ Error getting coordinates for {row['name']}: {e}") continue print(f"Successfully extracted coordinates for {success_count}/{len(sample_df)} incidents") return df # Enhanced map generation focusing only on active fires and nearby FIRMS data def generate_enhanced_map(df, firms_df): """Generate map showing only active InciWeb incidents and their associated FIRMS hotspots""" try: print("Starting focused map generation (active fires only)...") # Create map centered on the US m = folium.Map(location=[39.8283, -98.5795], zoom_start=4) # Filter to only show active incidents (those with nearby FIRMS data) active_incidents = df[df.get('is_active', False) == True].copy() if active_incidents.empty: print("No active incidents found - showing basic map") legend_html = '''