File size: 39,617 Bytes
b659d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95f321
 
 
b659d55
e95f321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b659d55
e95f321
b659d55
e95f321
b659d55
 
e95f321
 
 
b659d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95f321
 
 
 
 
 
 
 
 
 
 
 
b659d55
 
 
e95f321
 
 
 
 
 
b659d55
 
 
 
 
 
 
 
 
e95f321
b659d55
e95f321
 
 
b659d55
e95f321
 
b659d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95f321
b659d55
 
e95f321
 
 
 
b659d55
 
 
 
 
e95f321
b659d55
 
 
e95f321
b659d55
 
 
 
 
e95f321
 
 
 
 
b659d55
 
 
 
 
 
 
 
 
 
 
 
 
e95f321
 
b659d55
e95f321
 
b659d55
e95f321
 
 
 
 
 
 
 
b659d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95f321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b659d55
 
 
 
e95f321
 
b659d55
 
 
e95f321
 
b659d55
e95f321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b659d55
 
 
 
 
e95f321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b659d55
 
 
 
 
 
 
e95f321
 
 
 
 
 
b659d55
e95f321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# 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
    """
    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")
        
        # Add datetime column for easier processing
        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')
        
        # Sort by acquisition time (most recent first)
        usa_firms = usa_firms.sort_values('datetime', ascending=False)
        
        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
    
    Args:
        inciweb_df: DataFrame with InciWeb incident data (must have latitude/longitude)
        firms_df: DataFrame with FIRMS hotspot data
        max_distance_km: Maximum distance in km to consider a match
    
    Returns:
        Enhanced inciweb_df with FIRMS data and activity status
    """
    if firms_df.empty or inciweb_df.empty:
        return inciweb_df
    
    print(f"Matching {len(firms_df)} FIRMS hotspots to {len(inciweb_df)} InciWeb incidents...")
    
    # Initialize new columns
    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():
        incident_coords = (incident['latitude'], incident['longitude'])
        
        # Find FIRMS hotspots within the specified distance
        hotspot_distances = []
        matched_hotspots = []
        
        for _, hotspot in firms_df.iterrows():
            hotspot_coords = (hotspot['latitude'], hotspot['longitude'])
            
            try:
                distance = geodesic(incident_coords, hotspot_coords).kilometers
                if distance <= max_distance_km:
                    hotspot_distances.append(distance)
                    matched_hotspots.append(hotspot)
            except Exception as e:
                continue  # Skip invalid coordinates
        
        if matched_hotspots:
            matched_df = pd.DataFrame(matched_hotspots)
            
            # Calculate aggregated metrics
            num_hotspots = len(matched_hotspots)
            total_frp = matched_df['frp'].sum() if 'frp' in matched_df.columns else 0
            avg_confidence = matched_df['confidence'].mean() if 'confidence' in matched_df.columns else 0
            latest_hotspot = matched_df['datetime'].max() if 'datetime' in matched_df.columns else None
            
            # 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 hotspot coordinates for visualization
            hotspot_coords = [(hs['latitude'], hs['longitude'], hs.get('frp', 1)) 
                            for hs in matched_hotspots]
            inciweb_df.at[idx, 'hotspot_coords'] = hotspot_coords
            
            print(f"  {incident['name']}: {num_hotspots} hotspots, {total_frp:.1f} FRP, {activity_level} activity")
    
    # Mark incidents without recent hotspots as potentially inactive
    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

# 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()
                        size_match = re.search(r'(\d+(?:,\d+)*)', size_text)
                        if size_match:
                            incident["size"] = int(size_match.group(1).replace(',', ''))
                        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
    for col in ["size", "type", "location", "state", "updated"]:
        if col not in df.columns:
            df[col] = None
    
    df["size"] = pd.to_numeric(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 fallback methods"""
    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 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 2: Look for coordinate table rows
        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_text = coord_cell.get_text(strip=True)
                    
                    # Try to extract decimal coordinates
                    lat_match = re.search(r'(-?\d+\.?\d+)', coord_text)
                    if lat_match:
                        # Look for longitude after latitude
                        lon_match = re.search(r'(-?\d+\.?\d+)', coord_text[lat_match.end():])
                        if lon_match:
                            try:
                                lat = float(lat_match.group(1))
                                lon = float(lon_match.group(1))
                                print(f"  Found coordinates via table: {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 with FIRMS data
def generate_enhanced_map(df, firms_df):
    """Generate map with both InciWeb incidents and FIRMS hotspots"""
    # Create map centered on the US
    m = folium.Map(location=[39.8283, -98.5795], zoom_start=4)
    
    # Add FIRMS heat map layer for all USA hotspots (even if no InciWeb coordinates)
    if not firms_df.empty:
        print(f"Adding {len(firms_df)} FIRMS hotspots to map...")
        heat_data = [[row['latitude'], row['longitude'], min(row.get('frp', 1), 100)] 
                    for _, row in firms_df.iterrows()]
        
        if heat_data:
            HeatMap(
                heat_data,
                name="Fire Intensity Heatmap (NASA FIRMS)",
                radius=15,
                blur=10,
                max_zoom=1,
                gradient={0.2: 'blue', 0.4: 'lime', 0.6: 'orange', 1: 'red'}
            ).add_to(m)
            
            # Add some sample FIRMS points as markers
            sample_firms = firms_df.head(100)  # Show top 100 hotspots as individual markers
            for _, hotspot in sample_firms.iterrows():
                folium.CircleMarker(
                    location=[hotspot['latitude'], hotspot['longitude']],
                    radius=2 + min(hotspot.get('frp', 1) / 10, 8),
                    popup=f"πŸ”₯ FIRMS Hotspot<br>FRP: {hotspot.get('frp', 'N/A')} MW<br>Confidence: {hotspot.get('confidence', 'N/A')}%<br>Time: {hotspot.get('acq_time', 'N/A')}",
                    color='red',
                    fillColor='orange',
                    fillOpacity=0.7,
                    weight=1
                ).add_to(m)
    
    # Add incident markers if we have coordinates
    incidents_with_coords = df[(df['latitude'].notna()) & (df['longitude'].notna())]
    
    if not incidents_with_coords.empty:
        print(f"Adding {len(incidents_with_coords)} InciWeb incidents with coordinates to map...")
        
        # Add incident markers
        incident_cluster = MarkerCluster(name="InciWeb Incidents").add_to(m)
        
        # Track statistics
        active_incidents = 0
        inactive_incidents = 0
        
        for _, row in incidents_with_coords.iterrows():
            lat, lon = row['latitude'], row['longitude']
            
            # Determine marker color based on activity and type
            if row.get('is_active', False):
                active_incidents += 1
                activity_level = row.get('activity_level', 'Unknown')
                if activity_level == 'Very High':
                    color = 'red'
                    icon = 'fire'
                elif activity_level == 'High':
                    color = 'orange'
                    icon = 'fire'
                elif activity_level == 'Medium':
                    color = 'yellow'
                    icon = 'fire'
                else:
                    color = 'lightred'
                    icon = 'fire'
            else:
                inactive_incidents += 1
                color = 'gray'
                icon = 'pause'
            
            # Create detailed popup
            popup_content = f"""
            <div style="width: 300px;">
                <h4>{row.get('name', 'Unknown')}</h4>
                <b>Type:</b> {row.get('type', 'N/A')}<br>
                <b>Location:</b> {row.get('location', 'N/A')}<br>
                <b>Size:</b> {row.get('size', 'N/A')} acres<br>
                <b>Last Updated:</b> {row.get('updated', 'N/A')}<br>
                
                <hr style="margin: 10px 0;">
                <h5>πŸ”₯ Fire Activity (NASA FIRMS)</h5>
                <b>Status:</b> {'πŸ”΄ ACTIVE' if row.get('is_active', False) else '⚫ Inactive'}<br>
                <b>Activity Level:</b> {row.get('activity_level', 'Unknown')}<br>
                <b>Hotspots (24h):</b> {row.get('firms_hotspots', 0)}<br>
                <b>Total Fire Power:</b> {row.get('total_frp', 0):.1f} MW<br>
                <b>Avg Confidence:</b> {row.get('avg_confidence', 0):.1f}%<br>
                
                <a href="{row.get('link', '#')}" target="_blank">πŸ“‹ More Details</a>
            </div>
            """
            
            folium.Marker(
                location=[lat, lon],
                popup=folium.Popup(popup_content, max_width=350),
                icon=folium.Icon(color=color, icon=icon, prefix='fa')
            ).add_to(incident_cluster)
            
            # Add hotspot visualization for active incidents
            if row.get('is_active', False) and row.get('hotspot_coords'):
                hotspot_coords = row.get('hotspot_coords', [])
                if hotspot_coords:
                    # Add individual hotspot markers (smaller, less intrusive)
                    for coord in hotspot_coords[:20]:  # Limit to 20 hotspots per incident
                        folium.CircleMarker(
                            location=[coord[0], coord[1]],
                            radius=3 + min(coord[2] / 20, 10),  # Size based on FRP
                            popup=f"πŸ”₯ Hotspot<br>FRP: {coord[2]:.1f} MW",
                            color='red',
                            fillColor='orange',
                            fillOpacity=0.7
                        ).add_to(m)
    else:
        print("No InciWeb incidents have coordinates, showing FIRMS data only")
        active_incidents = 0
        inactive_incidents = len(df)
    
    # Add custom legend
    total_hotspots = len(firms_df) if not firms_df.empty else 0
    total_incidents = len(df)
    
    legend_html = f'''
    <div style="position: fixed; 
                bottom: 50px; left: 50px; width: 250px; height: 320px; 
                border:2px solid grey; z-index:9999; font-size:12px;
                background-color:white; padding: 10px;
                border-radius: 5px; font-family: Arial;">
        <div style="font-weight: bold; margin-bottom: 8px; font-size: 14px;">πŸ”₯ Wildfire Activity Status</div>
        
        <div style="margin-bottom: 8px;"><b>InciWeb Incidents:</b></div>
        <div style="display: flex; align-items: center; margin-bottom: 3px;">
            <div style="background-color: red; width: 12px; height: 12px; margin-right: 5px; border-radius: 50%;"></div>
            <div>Very High Activity</div>
        </div>
        <div style="display: flex; align-items: center; margin-bottom: 3px;">
            <div style="background-color: orange; width: 12px; height: 12px; margin-right: 5px; border-radius: 50%;"></div>
            <div>High Activity</div>
        </div>
        <div style="display: flex; align-items: center; margin-bottom: 3px;">
            <div style="background-color: yellow; width: 12px; height: 12px; margin-right: 5px; border-radius: 50%;"></div>
            <div>Medium Activity</div>
        </div>
        <div style="display: flex; align-items: center; margin-bottom: 3px;">
            <div style="background-color: lightcoral; width: 12px; height: 12px; margin-right: 5px; border-radius: 50%;"></div>
            <div>Low Activity</div>
        </div>
        <div style="display: flex; align-items: center; margin-bottom: 8px;">
            <div style="background-color: gray; width: 12px; height: 12px; margin-right: 5px; border-radius: 50%;"></div>
            <div>Inactive/No Data</div>
        </div>
        
        <div style="margin-bottom: 5px;"><b>NASA FIRMS Data:</b></div>
        <div style="display: flex; align-items: center; margin-bottom: 3px;">
            <div style="background-color: orange; width: 12px; height: 12px; margin-right: 5px; border-radius: 50%;"></div>
            <div>Fire Hotspots (24h)</div>
        </div>
        <div style="margin-bottom: 8px; font-style: italic;">Heat map shows fire intensity</div>
        
        <div style="font-size: 11px; margin-top: 10px; padding-top: 5px; border-top: 1px solid #ccc;">
            <b>Statistics:</b><br>
            πŸ”΄ Active InciWeb: {active_incidents}<br>
            ⚫ Inactive InciWeb: {inactive_incidents}<br>
            πŸ“ Total InciWeb: {total_incidents}<br>
            🌑️ Total FIRMS Hotspots: {total_hotspots}<br>
            πŸ“Š Incidents with Coords: {len(incidents_with_coords)}
        </div>
    </div>
    '''
    
    # Add layer control
    folium.LayerControl().add_to(m)
    
    # Get map HTML and add legend
    map_html = m._repr_html_()
    map_with_legend = map_html.replace('</body>', legend_html + '</body>')
    
    return map_with_legend

# Enhanced visualization functions
def generate_enhanced_visualizations(df, firms_df):
    """Generate enhanced visualizations with FIRMS data integration"""
    figures = []
    
    if df.empty:
        return [px.bar(title="No data available")]
    
    # 1. Activity Status Overview
    if 'is_active' in df.columns:
        activity_summary = df['is_active'].value_counts().reset_index()
        activity_summary.columns = ['is_active', 'count']
        activity_summary['status'] = activity_summary['is_active'].map({True: 'Active (FIRMS detected)', False: 'Inactive/Unknown'})
        
        fig1 = px.pie(
            activity_summary,
            values='count',
            names='status',
            title="πŸ”₯ Wildfire Activity Status (Based on NASA FIRMS Data)",
            color_discrete_map={'Active (FIRMS detected)': 'red', 'Inactive/Unknown': 'gray'}
        )
        fig1.update_traces(textinfo='label+percent+value')
    else:
        fig1 = px.bar(title="Activity status data not available")
    figures.append(fig1)
    
    # 2. Activity Level Distribution
    if 'activity_level' in df.columns and df['activity_level'].notna().any():
        activity_levels = df[df['activity_level'] != 'Unknown']['activity_level'].value_counts().reset_index()
        activity_levels.columns = ['activity_level', 'count']
        
        # Define order and colors
        level_order = ['Very High', 'High', 'Medium', 'Low', 'Minimal']
        color_map = {'Very High': 'darkred', 'High': 'red', 'Medium': 'orange', 'Low': 'yellow', 'Minimal': 'lightblue'}
        
        fig2 = px.bar(
            activity_levels,
            x='activity_level',
            y='count',
            title="πŸ“Š Fire Activity Levels (NASA FIRMS Intensity)",
            labels={'activity_level': 'Activity Level', 'count': 'Number of Incidents'},
            color='activity_level',
            color_discrete_map=color_map,
            category_orders={'activity_level': level_order}
        )
    else:
        fig2 = px.bar(title="Activity level data not available")
    figures.append(fig2)
    
    # 3. Fire Radiative Power vs Incident Size
    if 'total_frp' in df.columns and 'size' in df.columns:
        active_df = df[(df['is_active'] == True) & (df['total_frp'] > 0) & (df['size'].notna())].copy()
        
        if not active_df.empty:
            fig3 = px.scatter(
                active_df,
                x='size',
                y='total_frp',
                size='firms_hotspots',
                color='activity_level',
                hover_data=['name', 'state', 'firms_hotspots'],
                title="πŸ”₯ Fire Intensity vs Incident Size (Active Fires Only)",
                labels={'size': 'Incident Size (acres)', 'total_frp': 'Total Fire Radiative Power (MW)'},
                color_discrete_map={'Very High': 'darkred', 'High': 'red', 'Medium': 'orange', 'Low': 'yellow'}
            )
            fig3.update_layout(xaxis_type="log", yaxis_type="log")
        else:
            fig3 = px.bar(title="No active fires with size and intensity data")
    else:
        fig3 = px.bar(title="Fire intensity vs size data not available")
    figures.append(fig3)
    
    # 4. Hotspot Detection Over Time (if FIRMS data available)
    if not firms_df.empty and 'datetime' in firms_df.columns:
        # Group by hour to show detection pattern
        firms_df['hour'] = firms_df['datetime'].dt.floor('H')
        hourly_detections = firms_df.groupby('hour').size().reset_index(name='detections')
        
        fig4 = px.line(
            hourly_detections,
            x='hour',
            y='detections',
            title="πŸ• Fire Hotspot Detections Over Time (Last 24 Hours)",
            labels={'hour': 'Time', 'detections': 'Number of Hotspots Detected'}
        )
        fig4.update_traces(line_color='red')
    else:
        fig4 = px.bar(title="FIRMS temporal data not available")
    figures.append(fig4)
    
    # 5. State-wise Active vs Inactive Breakdown
    if 'state' in df.columns and 'is_active' in df.columns:
        state_activity = df.groupby(['state', 'is_active']).size().reset_index(name='count')
        state_activity['status'] = state_activity['is_active'].map({True: 'Active', False: 'Inactive'})
        
        # Get top 10 states by total incidents
        top_states = df['state'].value_counts().head(10).index.tolist()
        state_activity_filtered = state_activity[state_activity['state'].isin(top_states)]
        
        if not state_activity_filtered.empty:
            fig5 = px.bar(
                state_activity_filtered,
                x='state',
                y='count',
                color='status',
                title="πŸ—ΊοΈ Active vs Inactive Incidents by State (Top 10)",
                labels={'state': 'State', 'count': 'Number of Incidents'},
                color_discrete_map={'Active': 'red', 'Inactive': 'gray'}
            )
        else:
            fig5 = px.bar(title="State activity data not available")
    else:
        fig5 = px.bar(title="State activity data not available")
    figures.append(fig5)
    
    return figures

# Main application function
def create_enhanced_wildfire_app():
    """Create the enhanced Gradio application"""
    
    with gr.Blocks(title="Enhanced InciWeb + NASA FIRMS Wildfire Tracker", theme=gr.themes.Soft()) as app:
        gr.Markdown("""
        # πŸ”₯ Enhanced Wildfire Tracker
        ## InciWeb Incidents + NASA FIRMS Real-Time Fire Detection
        
        This application combines wildfire incident reports from InciWeb with real-time satellite fire detection data from NASA FIRMS to provide:
        - **Active fire status** based on satellite hotspot detection
        - **Fire intensity metrics** using Fire Radiative Power (FRP)
        - **Real-time hotspot mapping** from the last 24 hours
        - **Enhanced situational awareness** for wildfire management
        """)
        
        with gr.Row():
            fetch_btn = gr.Button("πŸš€ Fetch Latest Data (InciWeb + NASA FIRMS)", variant="primary", size="lg")
            status_text = gr.Textbox(label="Status", interactive=False, value="Ready to fetch data...")
        
        with gr.Tabs():
            with gr.TabItem("πŸ—ΊοΈ Enhanced Map"):
                map_display = gr.HTML(label="Interactive Map with Fire Activity")
                
            with gr.TabItem("πŸ“Š Enhanced Analytics"):
                with gr.Row():
                    plot_selector = gr.Dropdown(
                        choices=[
                            "Activity Status Overview",
                            "Fire Activity Levels", 
                            "Intensity vs Size Analysis",
                            "Hotspot Detection Timeline",
                            "State Activity Breakdown"
                        ],
                        label="Select Visualization",
                        value="Activity Status Overview"
                    )
                plot_display = gr.Plot(label="Enhanced Analytics")
                
            with gr.TabItem("πŸ“‹ Data Tables"):
                with gr.Tabs():
                    with gr.TabItem("InciWeb Incidents"):
                        inciweb_table = gr.Dataframe(label="InciWeb Incidents with FIRMS Integration")
                    with gr.TabItem("NASA FIRMS Hotspots"):
                        firms_table = gr.Dataframe(label="NASA FIRMS Fire Hotspots (USA, 24h)")
                        
            with gr.TabItem("πŸ“ Export Data"):
                gr.Markdown("### Download Enhanced Dataset")
                with gr.Row():
                    download_csv = gr.File(label="Download Enhanced CSV")
                    download_geojson = gr.File(label="Download GeoJSON")
        
        # Store data in state
        app_state = gr.State({})
        
        def fetch_and_process_data():
            """Main data processing function"""
            try:
                yield "πŸ“‘ Fetching InciWeb incident data...", None, None, None, None, None, None
                
                # Fetch InciWeb data
                inciweb_df = fetch_inciweb_data()
                if inciweb_df.empty:
                    yield "❌ Failed to fetch InciWeb data", None, None, None, None, None, None
                    return
                
                yield f"βœ… Found {len(inciweb_df)} InciWeb incidents. Getting coordinates...", None, None, None, None, None, None
                
                # Get coordinates for sample incidents
                inciweb_df = add_coordinates_to_incidents(inciweb_df, max_incidents=15)
                
                yield "πŸ›°οΈ Fetching NASA FIRMS fire detection data...", None, None, None, None, None, None
                
                # Fetch FIRMS data
                firms_df = fetch_firms_data()
                if firms_df.empty:
                    yield "⚠️ FIRMS data unavailable, proceeding with InciWeb only", None, None, inciweb_df, firms_df, None, None
                    return
                
                yield f"βœ… Found {len(firms_df)} USA fire hotspots. Matching with incidents...", None, None, None, None, None, None
                
                # Match FIRMS data to InciWeb incidents
                enhanced_df = match_firms_to_inciweb(inciweb_df, firms_df)
                
                yield "πŸ—ΊοΈ Generating enhanced map...", None, None, None, None, None, None
                
                # Generate map and visualizations
                map_html = generate_enhanced_map(enhanced_df, firms_df)
                plots = generate_enhanced_visualizations(enhanced_df, firms_df)
                
                # Prepare export data - create temporary files
                import tempfile
                
                # Create CSV file
                csv_file = tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False)
                enhanced_df.to_csv(csv_file.name, index=False)
                csv_file.close()
                
                active_count = (enhanced_df.get('is_active', pd.Series([False])) == True).sum()
                total_hotspots = len(firms_df)
                
                final_status = f"βœ… Complete! Found {active_count} active fires with {total_hotspots} total hotspots"
                
                yield (final_status, map_html, plots[0], enhanced_df, firms_df, csv_file.name, 
                       {"inciweb_df": enhanced_df, "firms_df": firms_df, "plots": plots})
                
            except Exception as e:
                yield f"❌ Error: {str(e)}", None, None, None, None, None, None
        
        def update_plot(plot_name, state_data):
            """Update plot based on selection"""
            if not state_data or "plots" not in state_data:
                return px.bar(title="No data available")
            
            plot_options = [
                "Activity Status Overview",
                "Fire Activity Levels", 
                "Intensity vs Size Analysis",
                "Hotspot Detection Timeline",
                "State Activity Breakdown"
            ]
            
            try:
                plot_index = plot_options.index(plot_name)
                return state_data["plots"][plot_index]
            except (ValueError, IndexError):
                return state_data["plots"][0] if state_data["plots"] else px.bar(title="Plot not available")
        
        # Wire up the interface
        fetch_btn.click(
            fetch_and_process_data,
            outputs=[status_text, map_display, plot_display, inciweb_table, firms_table, download_csv, app_state]
        )
        
        plot_selector.change(
            update_plot,
            inputs=[plot_selector, app_state],
            outputs=[plot_display]
        )
    
    return app

# Create and launch the application
if __name__ == "__main__":
    app = create_enhanced_wildfire_app()
    app.launch(share=True, debug=True)