File size: 42,005 Bytes
aa06233
e5fde52
 
 
aa06233
e5fde52
9e38f1c
 
 
 
 
 
 
 
e5fde52
 
 
 
aa06233
9e38f1c
e5fde52
9e38f1c
 
 
 
 
3c385aa
9e38f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddc7f1f
9e38f1c
 
 
 
ddc7f1f
9e38f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea901d
 
ddc7f1f
3ea901d
9e38f1c
3ea901d
 
 
9e38f1c
 
 
 
 
3ea901d
9e38f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea901d
 
 
9e38f1c
 
 
 
 
 
 
 
 
 
3ea901d
 
9e38f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddc7f1f
9e38f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cb3aa0
9e38f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa06233
3c385aa
aa06233
9e38f1c
aa06233
9e38f1c
95ff668
aa06233
9e38f1c
aa06233
9e38f1c
 
e5fde52
0719bc6
 
 
 
 
 
 
 
 
 
e5fde52
9e38f1c
 
aa06233
 
9e38f1c
e5fde52
9e38f1c
e5fde52
aa06233
9e38f1c
e5fde52
aa06233
9e38f1c
 
e5fde52
9e38f1c
ec9c90f
aa06233
0168ca8
 
 
9e38f1c
 
451c840
9e38f1c
0168ca8
10577df
 
 
 
 
 
0168ca8
 
 
 
ec9c90f
0168ca8
 
 
ec9c90f
0168ca8
10577df
ec9c90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0168ca8
451c840
ec9c90f
 
9e38f1c
ec9c90f
 
 
 
 
0168ca8
 
ec9c90f
0168ca8
 
ec9c90f
 
0168ca8
ec9c90f
0168ca8
 
 
ec9c90f
0168ca8
ec9c90f
0168ca8
ec9c90f
0168ca8
ec9c90f
10577df
0168ca8
ec9c90f
0168ca8
ec9c90f
 
 
 
 
0168ca8
4f88b20
0168ca8
 
ec9c90f
0168ca8
 
 
ec9c90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0168ca8
ec9c90f
 
 
 
 
 
642289e
ec9c90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0168ca8
4f88b20
0168ca8
ec9c90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f88b20
ec9c90f
 
 
31f1bd1
ec9c90f
 
 
642289e
ec9c90f
642289e
 
 
 
ec9c90f
 
 
31f1bd1
ec9c90f
 
 
 
 
31f1bd1
 
ec9c90f
 
 
 
 
 
 
 
 
 
 
642289e
ec9c90f
 
642289e
ec9c90f
 
 
 
 
 
 
 
 
642289e
 
0168ca8
 
ec9c90f
0168ca8
4f88b20
0168ca8
 
aa06233
 
0168ca8
10577df
e5fde52
9e38f1c
 
e5fde52
9e38f1c
aa06233
9e38f1c
 
aa06233
9e38f1c
 
 
 
 
 
 
e5fde52
aa06233
 
 
 
e5fde52
aa06233
9e38f1c
 
 
 
 
 
 
 
aa06233
 
9e38f1c
aa06233
9e38f1c
 
 
 
aa06233
 
 
 
 
9e38f1c
 
aa06233
9e38f1c
 
 
 
 
 
aa06233
9e38f1c
 
 
 
 
e5fde52
9e38f1c
 
 
aa06233
 
9e38f1c
 
aa06233
9e38f1c
 
 
 
aa06233
 
9e38f1c
 
 
 
 
 
 
 
 
 
aa06233
 
9e38f1c
aa06233
 
 
9e38f1c
 
aa06233
 
9e38f1c
aa06233
9e38f1c
 
 
aa06233
e5fde52
aa06233
 
 
 
 
 
 
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
import gradio as gr
import json
import os
import logging
import numpy as np
from datetime import datetime
import tempfile
import sys
import subprocess
import shutil
from datetime import datetime, timedelta
import xarray as xr
from ecmwf.opendata import Client
import requests

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Global wave data storage
wave_particle_cache = {}

class WorkingGRIBWaveFetcher:
    """
    Working GRIB wave fetcher - exactly like NWPS_SWAN but output particle format
    """
    def __init__(self):
        logger.info("πŸš€ Initializing WorkingGRIBWaveFetcher with Arctic support")
        self.client = Client("ecmwf")
        self.output_dir = os.getenv('OUTPUT_DIR', '/tmp/wave_data')
        os.makedirs(self.output_dir, exist_ok=True)
        
        # Set ECCODES environment variables to handle polar stereographic issues
        self._setup_eccodes_environment()
    
    def _setup_eccodes_environment(self):
        """Setup ECCODES environment variables to handle projection issues"""
        try:
            # Set environment variables that might help with polar stereographic processing
            os.environ['ECCODES_GRIB_STRICT_PARSING'] = '0'  # Relaxed parsing
            os.environ['ECCODES_GRIB_IGNORE_GRID_DEFINITION'] = '1'  # Ignore grid definition errors
            logger.info("Set ECCODES environment variables for relaxed parsing")
        except Exception as e:
            logger.warning(f"Could not set ECCODES environment variables: {e}")
    
    def fetch_noaa_wave_grib(self, forecast_hour=0):
        """Fetch global wave data from NOAA WW3 model - exactly like NWPS_SWAN"""
        try:
            logger.info(f"Fetching NOAA WW3 global wave GRIB data for forecast hour {forecast_hour}...")
            
            # NOAA GFS/WW3 wave data URL pattern
            base_url = "https://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod"
            
            # Try current date and previous days (in case of delayed updates)
            now = datetime.utcnow()
            dates_to_try = [
                now.strftime("%Y%m%d"),
                (now - timedelta(days=1)).strftime("%Y%m%d"),
                (now - timedelta(days=2)).strftime("%Y%m%d")
            ]
            
            # Try different model runs (00, 06, 12, 18 UTC) to find available data
            current_hour = now.hour
            
            # Start with the most recent available run
            if current_hour >= 18:
                preferred_runs = ["18", "12", "06", "00"]
            elif current_hour >= 12:
                preferred_runs = ["12", "06", "00", "18"]
            elif current_hour >= 6:
                preferred_runs = ["06", "00", "18", "12"]
            else:
                preferred_runs = ["00", "18", "12", "06"]
            
            # Try different dates and model runs
            for date_str in dates_to_try:
                logger.info(f"Trying date: {date_str}")
                for hour in preferred_runs:
                    try:
                        # Format forecast hour with leading zeros (f000, f001, f002, etc.)
                        forecast_str = f"f{forecast_hour:03d}"
                        
                        # Download multiple regional files for global coverage
                        successful_downloads = []
                        
                        # Try different regional GRIB files available on NOAA including Arctic
                        regional_files = [
                            (f"gfswave.t{hour}z.atlocn.0p16.{forecast_str}.grib2", "Atlantic"),
                            (f"gfswave.t{hour}z.epacif.0p16.{forecast_str}.grib2", "East_Pacific"), 
                            (f"gfswave.t{hour}z.wcoast.0p16.{forecast_str}.grib2", "West_Coast"),
                            (f"gfswave.t{hour}z.arctic.9km.{forecast_str}.grib2", "Arctic"),
                            (f"gfswave.t{hour}z.global.0p16.{forecast_str}.grib2", "Global"),
                        ]
                        
                        # Try to download each regional file
                        for filename, region_name in regional_files:
                            try:
                                url = f"{base_url}/gfs.{date_str}/{hour}/wave/gridded/{filename}"
                                logger.info(f"Attempting to download {region_name} region: {filename}")
                                
                                temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.grib2')
                                
                                response = requests.get(url, timeout=300)
                                if response.status_code == 200:
                                    temp_file.write(response.content)
                                    temp_file.close()
                                    successful_downloads.append((temp_file.name, region_name, hour, forecast_hour))
                                    logger.info(f"{region_name} GRIB file downloaded: {temp_file.name}")
                                else:
                                    logger.debug(f"HTTP {response.status_code} for {region_name}")
                                    os.unlink(temp_file.name)
                                    continue
                            except Exception as file_error:
                                logger.debug(f"Error downloading {region_name}: {file_error}")
                                continue
                        
                        # If we got at least one regional file, return the list
                        if successful_downloads:
                            logger.info(f"Successfully downloaded {len(successful_downloads)} regional files")
                            return successful_downloads
                            
                    except Exception as run_error:
                        logger.warning(f"Error trying {hour}Z run on {date_str}: {run_error}")
                        continue
            
            logger.error("Failed to download NOAA data from any model run")
            return None
                
        except Exception as e:
            logger.error(f"Error in fetch_noaa_wave_grib: {e}")
            return None
    
    def process_grib_file(self, grib_file_path, region_name=None):
        """Process GRIB file and extract wave data - exactly like NWPS_SWAN but for particles"""
        try:
            logger.info(f"Processing GRIB file: {grib_file_path}")
            
            # Use xarray + cfgrib for all regions including Arctic
            logger.info(f"Using xarray + cfgrib for {region_name} data processing")
            
            # Use xarray + cfgrib - simple approach like in girbplayground
            try:
                # Open the GRIB file with xarray + cfgrib engine
                ds = xr.open_dataset(grib_file_path, engine='cfgrib')
                all_vars = ds.variables
                    
                logger.info(f"Available variables: {list(all_vars.keys())}")
                
            except Exception as e:
                error_msg = str(e)
                logger.error(f"Error opening GRIB file with xarray + cfgrib: {error_msg}")
                return None
            
            # Extract wave height data
            wave_height_var = None
            wave_heights = None
            for var_name in ['swh', 'HTSGW', 'htsgw']:
                if var_name in all_vars:
                    wave_height_var = var_name
                    wave_heights = all_vars[var_name].values
                    logger.info(f"Using wave height variable: {wave_height_var}")
                    break
            
            if wave_heights is None:
                # Try broader search
                for var_name in all_vars:
                    if any(keyword in var_name.lower() for keyword in ['wave', 'height', 'swh']):
                        wave_height_var = var_name
                        wave_heights = all_vars[var_name].values
                        logger.info(f"Found wave height variable: {wave_height_var}")
                        break
            
            if wave_heights is None:
                logger.error("No wave height variables found in GRIB file")
                for ds in datasets:
                    ds.close()
                return None
            
            # Extract wave direction data
            wave_directions = None
            wave_dir_var = None
            for var_name in ['dirpw', 'DIRPW', 'dp', 'wvdir', 'WVDIR', 'dir']:
                if var_name in all_vars:
                    wave_dir_var = var_name
                    wave_directions = all_vars[var_name].values
                    logger.info(f"Found wave direction variable: {wave_dir_var}")
                    break
            
            # Extract wave period data
            wave_periods = None
            wave_period_var = None
            for var_name in ['perpw', 'PERPW', 'tp', 'wvper', 'WVPER', 'per']:
                if var_name in all_vars:
                    wave_period_var = var_name
                    wave_periods = all_vars[var_name].values
                    logger.info(f"Found wave period variable: {wave_period_var}")
                    break
            
            # Get coordinates from the dataset
            lats = ds.latitude.values if 'latitude' in ds else ds.lat.values
            lons = ds.longitude.values if 'longitude' in ds else ds.lon.values
            
            # Log what we found
            if wave_directions is not None:
                logger.info(f"Wave directions shape: {wave_directions.shape}, range: {np.nanmin(wave_directions):.1f}-{np.nanmax(wave_directions):.1f} degrees")
            if wave_periods is not None:
                logger.info(f"Wave periods shape: {wave_periods.shape}, range: {np.nanmin(wave_periods):.1f}-{np.nanmax(wave_periods):.1f} seconds")
            
            # Extract particle data for visualization
            particle_points = self._extract_particle_points(lats, lons, wave_heights, wave_directions, wave_periods, region_name)
            
            # Close the dataset
            ds.close()
            
            return particle_points
            
        except Exception as e:
            logger.error(f"Error processing GRIB file: {e}")
            return None
    
    def _extract_particle_points(self, lats, lons, wave_heights, wave_directions=None, wave_periods=None, region_name="Global", max_particles=1500):
        """Extract particle points with velocity vectors for wave animation"""
        try:
            # Create meshgrid for coordinates
            lon_grid, lat_grid = np.meshgrid(lons, lats)
            
            # Flatten arrays
            flat_lats = lat_grid.flatten()
            flat_lons = lon_grid.flatten()
            flat_waves = wave_heights.flatten()
            
            flat_dirs = None
            flat_periods = None
            
            if wave_directions is not None:
                flat_dirs = wave_directions.flatten()
            if wave_periods is not None:
                flat_periods = wave_periods.flatten()
            
            # Remove NaN values and invalid data
            valid_mask = (~np.isnan(flat_waves)) & (flat_waves > 0) & (flat_waves < 30)
            if flat_dirs is not None:
                valid_mask = valid_mask & ~np.isnan(flat_dirs)
            
            valid_lats = flat_lats[valid_mask]
            valid_lons = flat_lons[valid_mask] 
            valid_waves = flat_waves[valid_mask]
            
            if flat_dirs is not None:
                valid_dirs = flat_dirs[valid_mask]
            else:
                # Generate synthetic wave directions based on location patterns
                valid_dirs = self._generate_synthetic_directions(valid_lats, valid_lons)
                
            if flat_periods is not None:
                valid_periods = flat_periods[valid_mask]
            else:
                # Generate synthetic periods based on wave height
                valid_periods = np.clip(4 + valid_waves * 2, 3, 15)
            
            if len(valid_waves) == 0:
                return []
            
            # Sample points for particle visualization
            sample_size = min(max_particles, len(valid_waves))
            if sample_size < len(valid_waves):
                sample_indices = np.random.choice(len(valid_waves), size=sample_size, replace=False)
            else:
                sample_indices = np.arange(len(valid_waves))
            
            particle_points = []
            for idx in sample_indices:
                lat = float(valid_lats[idx])
                lon = float(valid_lons[idx])
                height = float(valid_waves[idx])
                direction = float(valid_dirs[idx])
                period = float(valid_periods[idx])
                
                # Calculate velocity components for particle movement
                # Wave direction is "coming from" in meteorological convention
                # Convert to mathematical convention (direction of travel)
                travel_direction = (direction + 180) % 360
                dir_rad = np.radians(travel_direction)
                
                # Velocity magnitude based on wave height and period
                # Wave celerity approximation: c = g*T/(2*pi) for deep water
                wave_speed = 9.81 * period / (2 * np.pi)  # m/s
                # Scale for visualization (convert to degrees per animation frame)
                velocity_scale = 0.001  # Adjust this for particle speed
                
                u_velocity = wave_speed * np.cos(dir_rad) * velocity_scale
                v_velocity = wave_speed * np.sin(dir_rad) * velocity_scale
                
                particle_points.append({
                    'lat': lat,
                    'lon': lon,
                    'wave_height': height,
                    'wave_direction': direction,
                    'wave_period': period,
                    'u_velocity': u_velocity,  # eastward component (degrees/frame)
                    'v_velocity': v_velocity,  # northward component (degrees/frame)
                    'particle_size': max(1, min(8, height * 2)),  # Size based on wave height
                    'color_intensity': min(1.0, height / 8.0),  # Color intensity based on height
                    'region': region_name
                })
            
            logger.info(f"Generated {len(particle_points)} particle points for {region_name}")
            return particle_points
            
        except Exception as e:
            logger.error(f"Error extracting particle points: {e}")
            return []
    
    def _generate_synthetic_directions(self, lats, lons):
        """Generate realistic wave directions based on geographic patterns"""
        try:
            directions = np.zeros_like(lats)
            
            for i, (lat, lon) in enumerate(zip(lats, lons)):
                # Simplified wind/wave pattern generation
                if abs(lat) < 30:  # Trade wind regions
                    if lon < 0:  # Atlantic/Americas
                        directions[i] = np.random.normal(90, 30)  # Generally eastward
                    else:  # Pacific/Asia
                        directions[i] = np.random.normal(270, 30)  # Generally westward
                elif abs(lat) > 60:  # Polar regions
                    directions[i] = np.random.uniform(0, 360)  # More variable
                else:  # Mid-latitudes
                    if lat > 0:  # Northern hemisphere
                        directions[i] = np.random.normal(225, 45)  # SW generally
                    else:  # Southern hemisphere  
                        directions[i] = np.random.normal(315, 45)  # NW generally
                
                # Ensure direction is in [0, 360) range
                directions[i] = directions[i] % 360
            
            return directions
            
        except Exception as e:
            logger.error(f"Error generating synthetic directions: {e}")
            return np.random.uniform(0, 360, len(lats))
    
    
    def process_multiple_regional_files(self, regional_files):
        """Process multiple regional GRIB files and combine particle data"""
        try:
            logger.info(f"Processing {len(regional_files)} regional GRIB files for global particle coverage...")
            
            all_particles = []
            regions_processed = []
            
            for grib_file_path, region_name, model_run, forecast_hour in regional_files:
                try:
                    logger.info(f"Processing {region_name} region: {grib_file_path}")
                    
                    # Process this regional file
                    particles = self.process_grib_file(grib_file_path, region_name=region_name)
                    
                    if particles:
                        # Add region info to each particle
                        for particle in particles:
                            particle['region'] = region_name
                            particle['model_run'] = model_run
                        
                        all_particles.extend(particles)
                        regions_processed.append(region_name)
                        logger.info(f"Successfully processed {region_name}: {len(particles)} particles")
                    else:
                        logger.warning(f"Failed to process {region_name} region")
                        
                    # Clean up temp file
                    if os.path.exists(grib_file_path):
                        os.unlink(grib_file_path)
                        
                except Exception as e:
                    logger.error(f"Error processing {region_name} region: {e}")
                    # Clean up temp file on error
                    if os.path.exists(grib_file_path):
                        os.unlink(grib_file_path)
                    continue
            
            if not all_particles:
                logger.error("No valid particle data found in any regional file")
                return None
            
            logger.info(f"Combined particle data from {len(regions_processed)} regions: {regions_processed}")
            logger.info(f"Total particles: {len(all_particles)}")
            
            # Calculate global statistics
            wave_heights = [p['wave_height'] for p in all_particles if p.get('wave_height')]
            
            return {
                'timestamp': datetime.utcnow().isoformat(),
                'data_source': f'NOAA_MULTI_REGIONAL_GRIB ({"_".join(regions_processed)})',
                'total_particles': len(all_particles),
                'regions_processed': regions_processed,
                'wave_statistics': {
                    'max_wave_height': float(max(wave_heights)) if wave_heights else None,
                    'min_wave_height': float(min(wave_heights)) if wave_heights else None,
                    'mean_wave_height': float(np.mean(wave_heights)) if wave_heights else None,
                    'std_wave_height': float(np.std(wave_heights)) if wave_heights else None
                },
                'forecast_info': {
                    'forecast_hour': 0,
                    'model_run': regions_processed[0] if regions_processed else 'DEMO',
                    'forecast_valid_time': datetime.utcnow().isoformat(),
                    'is_current': True
                },
                'particles': all_particles
            }
            
        except Exception as e:
            logger.error(f"Error processing multiple regional files: {e}")
            # Clean up any remaining temp files
            for grib_file_path, region_name, _, _ in regional_files:
                if os.path.exists(grib_file_path):
                    os.unlink(grib_file_path)
            return None
    
    def fetch_global_wave_particles(self, forecast_hour=0):
        """Main method to fetch global wave data formatted for particle animation"""
        try:
            logger.info("Fetching wave data from NOAA WW3 model for particle animation...")
            
            # Try NOAA for wave data
            result = self.fetch_noaa_wave_grib(forecast_hour)
            if result and isinstance(result, list):
                # Multiple regional files downloaded
                regional_files = result
                
                # Process multiple regional files and combine
                particle_data = self.process_multiple_regional_files(regional_files)
                
                return particle_data
            else:
                logger.error("NOAA failed - generating fallback demo data for particle animation")
                return self._generate_demo_particle_data(forecast_hour)
            
        except Exception as e:
            logger.error(f"Error in fetch_global_wave_particles: {e}")
            return self._generate_demo_particle_data(forecast_hour)
    
    def _generate_demo_particle_data(self, forecast_hour=0):
        """Generate demo wave particle data for visualization when GRIB data is unavailable"""
        logger.info(f"Generating demo wave particle data for +{forecast_hour}h forecast...")
        
        # Create realistic demo particles
        particles = []
        
        # Atlantic Ocean patterns
        for lat in range(-40, 61, 8):
            for lon in range(-80, 21, 10):
                if lat > 60 or lat < -60:
                    continue
                    
                base_height = np.random.uniform(1.0, 3.5)
                if abs(lat) > 40:
                    base_height += np.random.uniform(0.5, 2.0)
                
                if abs(lat) < 30:
                    wave_dir = np.random.normal(90, 20)
                else:
                    wave_dir = np.random.normal(225, 45)
                
                wave_dir = wave_dir % 360
                period = np.clip(4 + base_height * 1.5, 4, 14)
                
                travel_direction = (wave_dir + 180) % 360
                dir_rad = np.radians(travel_direction)
                wave_speed = 9.81 * period / (2 * np.pi)
                velocity_scale = 0.001
                
                u_velocity = wave_speed * np.cos(dir_rad) * velocity_scale
                v_velocity = wave_speed * np.sin(dir_rad) * velocity_scale
                
                particles.append({
                    'lat': float(lat + np.random.uniform(-2, 2)),
                    'lon': float(lon + np.random.uniform(-3, 3)),
                    'wave_height': round(float(base_height), 2),
                    'wave_direction': round(float(wave_dir), 1),
                    'wave_period': round(float(period), 1),
                    'u_velocity': u_velocity,
                    'v_velocity': v_velocity,
                    'particle_size': max(1, min(6, base_height * 1.5)),
                    'color_intensity': min(1.0, base_height / 6.0),
                    'region': 'Atlantic_Demo'
                })
        
        # Pacific Ocean patterns  
        for lat in range(-50, 61, 8):
            for lon in range(120, 241, 12):
                if lat > 60 or lat < -60:
                    continue
                    
                base_height = np.random.uniform(1.2, 4.0)
                if abs(lat) > 35:
                    base_height += np.random.uniform(0.8, 2.5)
                
                if abs(lat) < 25:
                    wave_dir = np.random.normal(270, 25)
                elif lat > 25:
                    wave_dir = np.random.normal(315, 40)
                else:
                    wave_dir = np.random.normal(225, 40)
                
                wave_dir = wave_dir % 360
                period = np.clip(5 + base_height * 1.3, 5, 16)
                
                travel_direction = (wave_dir + 180) % 360
                dir_rad = np.radians(travel_direction)
                wave_speed = 9.81 * period / (2 * np.pi)
                velocity_scale = 0.001
                
                u_velocity = wave_speed * np.cos(dir_rad) * velocity_scale
                v_velocity = wave_speed * np.sin(dir_rad) * velocity_scale
                
                particles.append({
                    'lat': float(lat + np.random.uniform(-2, 2)),
                    'lon': float(lon + np.random.uniform(-4, 4)),
                    'wave_height': round(float(base_height), 2),
                    'wave_direction': round(float(wave_dir), 1),
                    'wave_period': round(float(period), 1),
                    'u_velocity': u_velocity,
                    'v_velocity': v_velocity,
                    'particle_size': max(1, min(6, base_height * 1.5)),
                    'color_intensity': min(1.0, base_height / 6.0),
                    'region': 'Pacific_Demo'
                })
        
        wave_heights = [p['wave_height'] for p in particles]
        
        return {
            'timestamp': datetime.utcnow().isoformat(),
            'data_source': 'DEMO_WAVE_PARTICLES',
            'total_particles': len(particles),
            'regions_processed': ['Atlantic_Demo', 'Pacific_Demo'],
            'wave_statistics': {
                'max_wave_height': float(max(wave_heights)),
                'min_wave_height': float(min(wave_heights)),
                'mean_wave_height': float(np.mean(wave_heights)),
                'std_wave_height': float(np.std(wave_heights))
            },
            'forecast_info': {
                'forecast_hour': forecast_hour,
                'model_run': 'DEMO',
                'forecast_valid_time': (datetime.utcnow() + timedelta(hours=forecast_hour)).isoformat(),
                'is_current': forecast_hour == 0
            },
            'particles': particles
        }

def fetch_wave_particles():
    """Fetch global wave data optimized for particle animation"""
    try:
        logger.info("🌊 Starting NEW Wave Particle Fetcher (using working GRIB logic)")
        
        fetcher = WorkingGRIBWaveFetcher()
        
        # Fetch wave data formatted for particles
        data = fetcher.fetch_global_wave_particles(forecast_hour=0)
        
        if data and data.get('particles'):
            # Cache the data
            global wave_particle_cache
            wave_particle_cache = data
            
            # Save to file (with error handling)
            try:
                os.makedirs('/tmp/wave_data', exist_ok=True)
                filename = f"/tmp/wave_data/wave_particles_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
                with open(filename, 'w') as f:
                    json.dump(data, f, indent=2)
                logger.info(f"Saved particle data to {filename}")
            except Exception as save_error:
                logger.warning(f"Could not save to /tmp/wave_data: {save_error}")
                filename = "particle_data_in_memory"
            
            # Create particle visualization
            particle_map = create_particle_wave_map(data)
            
            # Format data summary
            summary = format_particle_summary(data)
            
            return summary, particle_map, f"βœ… Particle data saved to: {filename}"
            
        else:
            return "❌ No wave particle data available", None, "Failed to fetch particle data"
            
    except Exception as e:
        logger.error(f"Error fetching wave particle data: {e}")
        return f"❌ Error: {str(e)}", None, "Failed to fetch particle data"

def create_particle_wave_map(data):
    """Create Folium map with wave layer toggles exactly like the wind app"""
    try:
        import folium
        import numpy as np
        
        particles = data.get('particles', [])
        if not particles:
            return "<div>No wave particle data available for visualization</div>"
        
        # Get data bounds
        lats = [p['lat'] for p in particles if p.get('lat')]
        lons = [p['lon'] for p in particles if p.get('lon')]
        
        if not lats or not lons:
            return "<div>No valid wave coordinates found</div>"
        
        # Create center point and bounds
        center_lat = (min(lats) + max(lats)) / 2
        center_lon = (min(lons) + max(lons)) / 2
        
        # Create Folium map exactly like wind app
        m = folium.Map(
            location=[center_lat, center_lon],
            zoom_start=4,
            tiles=None  # We'll add tiles manually
        )
        
        # Add base tile layers (like wind app)
        light_tiles = folium.TileLayer(
            'https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png',
            name='OpenStreetMap',
            attr='Β© OpenStreetMap contributors'
        )
        light_tiles.add_to(m)
        
        dark_tiles = folium.TileLayer(
            'https://{{s}}.basemaps.cartocdn.com/dark_all/{{z}}/{{x}}/{{y}}{{r}}.png',
            name='Dark',
            attr='Β© CARTO Β© OpenStreetMap contributors'
        )
        dark_tiles.add_to(m)
        
        # Sample particles for performance
        import random
        if len(particles) > 500:
            particles = random.sample(particles, 500)
        
        # Create wave height layer (like 10m wind in wind app)
        wave_points = []
        for particle in particles[::2]:  # Every 2nd particle for performance
            if particle.get('lat') and particle.get('lon') and particle.get('wave_height'):
                wave_points.append([
                    particle['lat'],
                    particle['lon'],
                    particle['wave_height']
                ])
        
        # Add wave markers as a feature group (like wind layers)
        wave_layer = folium.FeatureGroup(name="Wave Heights", show=True)
        
        for i, particle in enumerate(particles[::5]):  # Every 5th particle
            if particle.get('lat') and particle.get('lon') and particle.get('wave_height'):
                # Color based on wave height
                if particle['wave_height'] < 2:
                    color = '#0066ff'
                elif particle['wave_height'] < 4:
                    color = '#00aaff'
                elif particle['wave_height'] < 6:
                    color = '#ffaa00'
                else:
                    color = '#ff0000'
                
                popup_text = f"""
                <div style="font-family: Arial; font-size: 12px;">
                <b>🌊 Wave Data</b><br>
                <b>Height:</b> {particle['wave_height']:.2f}m<br>
                <b>Direction:</b> {particle.get('wave_direction', 0):.1f}Β°<br>
                <b>Period:</b> {particle.get('wave_period', 0):.1f}s<br>
                <b>Region:</b> {particle.get('region', 'Unknown')}
                </div>
                """
                
                folium.CircleMarker(
                    location=[particle['lat'], particle['lon']],
                    radius=max(2, min(6, particle['wave_height'] * 1.5)),
                    popup=folium.Popup(popup_text, max_width=200),
                    color=color,
                    fillColor=color,
                    fillOpacity=0.7,
                    weight=1,
                    opacity=0.8
                ).add_to(wave_layer)
        
        wave_layer.add_to(m)
        
        # Add particle flow layer (simulated particle effect)
        particle_layer = folium.FeatureGroup(name="Wave Particles", show=True)
        
        # Add direction arrows as particles
        for i, particle in enumerate(particles[::8]):  # Every 8th particle
            if all(key in particle for key in ['lat', 'lon', 'wave_direction', 'wave_height']):
                # Create arrow marker pointing in wave direction
                if particle['wave_height'] > 1:  # Only show for significant waves
                    direction = particle['wave_direction']
                    
                    # Create custom arrow icon
                    arrow_html = f'''
                    <div style="transform: rotate({direction}deg); color: rgba(100, 200, 255, 0.8); font-size: 16px;">
                        ↑
                    </div>
                    '''
                    
                    folium.Marker(
                        location=[particle['lat'], particle['lon']],
                        icon=folium.DivIcon(
                            html=arrow_html,
                            icon_size=(20, 20),
                            icon_anchor=(10, 10)
                        )
                    ).add_to(particle_layer)
        
        particle_layer.add_to(m)
        
        # Add layer control (like wind app)
        folium.LayerControl(collapsed=False).add_to(m)
        
        # Add custom controls HTML (exactly like wind app structure)
        controls_html = f'''
        <div style="position: fixed; top: 10px; left: 10px; z-index: 9999; 
                    background: rgba(255,255,255,0.95); padding: 15px; border-radius: 8px; 
                    box-shadow: 0 4px 8px rgba(0,0,0,0.2); font-family: Arial, sans-serif; font-size: 13px;">
            
            <div style="margin-bottom: 10px;">
                <strong>🌊 Wave Visualization</strong>
            </div>
            
            <div style="margin: 8px 0;">
                <label style="display: flex; align-items: center; margin-bottom: 5px;">
                    <input type="checkbox" id="waveHeightToggle" checked onchange="toggleWaveHeight()" 
                           style="margin-right: 8px;">
                    Wave Heights
                </label>
                
                <label style="display: flex; align-items: center;">
                    <input type="checkbox" id="waveParticleToggle" checked onchange="toggleWaveParticles()" 
                           style="margin-right: 8px;">
                    Wave Particles
                </label>
            </div>
            
            <div style="margin-top: 12px; padding-top: 10px; border-top: 1px solid #ddd;">
                <label style="display: flex; align-items: center;">
                    <input type="checkbox" id="darkModeToggle" onchange="toggleDarkMode()" 
                           style="margin-right: 8px;">
                    Dark Mode
                </label>
            </div>
            
            <div style="margin-top: 8px; font-size: 11px; color: #666;">
                Particles: {len(particles)} | Regions: {len(data.get('regions_processed', []))}
            </div>
        </div>
        
        <script>
        // Wait for map to be fully loaded
        setTimeout(function() {{
            // Get the map instance
            var mapElement = document.querySelector('.folium-map');
            if (mapElement && mapElement._leaflet_id) {{
                var map = window[mapElement._leaflet_id];
                window.waveMap = map;
                
                // Get layer references
                window.waveLayers = {{}};
                map.eachLayer(function(layer) {{
                    if (layer.options && layer.options.name) {{
                        window.waveLayers[layer.options.name] = layer;
                    }}
                }});
                
                console.log('Wave map initialized with layers:', Object.keys(window.waveLayers));
            }}
        }}, 1000);
        
        function toggleWaveHeight() {{
            var checkbox = document.getElementById('waveHeightToggle');
            var map = window.waveMap;
            
            if (map && window.waveLayers['Wave Heights']) {{
                if (checkbox.checked) {{
                    map.addLayer(window.waveLayers['Wave Heights']);
                }} else {{
                    map.removeLayer(window.waveLayers['Wave Heights']);
                }}
            }}
        }}
        
        function toggleWaveParticles() {{
            var checkbox = document.getElementById('waveParticleToggle');
            var map = window.waveMap;
            
            if (map && window.waveLayers['Wave Particles']) {{
                if (checkbox.checked) {{
                    map.addLayer(window.waveLayers['Wave Particles']);
                }} else {{
                    map.removeLayer(window.waveLayers['Wave Particles']);
                }}
            }}
        }}
        
        function toggleDarkMode() {{
            var checkbox = document.getElementById('darkModeToggle');
            var map = window.waveMap;
            
            if (map && window.waveLayers) {{
                if (checkbox.checked) {{
                    // Switch to dark
                    if (window.waveLayers['OpenStreetMap']) {{
                        map.removeLayer(window.waveLayers['OpenStreetMap']);
                    }}
                    if (window.waveLayers['Dark']) {{
                        map.addLayer(window.waveLayers['Dark']);
                    }}
                }} else {{
                    // Switch to light  
                    if (window.waveLayers['Dark']) {{
                        map.removeLayer(window.waveLayers['Dark']);
                    }}
                    if (window.waveLayers['OpenStreetMap']) {{
                        map.addLayer(window.waveLayers['OpenStreetMap']);
                    }}
                }}
            }}
        }}
        </script>
        '''
        
        m.get_root().html.add_child(folium.Element(controls_html))
        
        # Return HTML representation
        return m._repr_html_()
        
    except Exception as e:
        logger.error(f"Error creating wave map: {e}")
        return f"<div>Error creating wave visualization: {e}</div>"

def format_particle_summary(data):
    """Format wave particle data summary for display"""
    try:
        particles = data.get('particles', [])
        
        if not particles:
            return "No wave particle data available"
        
        wave_heights = [p.get('wave_height', 0) for p in particles if p.get('wave_height') is not None]
        wave_speeds = []
        
        for p in particles:
            if p.get('u_velocity') is not None and p.get('v_velocity') is not None:
                speed = np.sqrt(p['u_velocity']**2 + p['v_velocity']**2)
                wave_speeds.append(speed)
        
        if wave_heights:
            avg_height = np.mean(wave_heights)
            max_height = np.max(wave_heights)
            min_height = np.min(wave_heights)
        else:
            avg_height = max_height = min_height = 0
            
        if wave_speeds:
            avg_speed = np.mean(wave_speeds)
            max_speed = np.max(wave_speeds)
        else:
            avg_speed = max_speed = 0
        
        regions = data.get('regions_processed', ['Global'])
        
        summary = f"""
## 🌊 Wave Particle Animation Data

**Data Source:** {data.get('data_source', 'NOAA_GRIB')}  
**Total Particles:** {len(particles):,}  
**Timestamp:** {data.get('timestamp', 'Unknown')}  
**Regions:** {', '.join(regions)}

### Wave Statistics:
- **Average Height:** {avg_height:.2f}m
- **Maximum Height:** {max_height:.2f}m  
- **Minimum Height:** {min_height:.2f}m
- **Average Speed:** {avg_speed*1000:.2f} mm/s
- **Maximum Speed:** {max_speed*1000:.2f} mm/s

### Particle Features:
- βœ… Real-time wave direction vectors
- βœ… Velocity-based particle movement  
- βœ… Wave height color coding
- βœ… Interactive controls (play/pause, speed, colors)
- βœ… Particle trails showing wave paths

### Animation Controls:
- **Play/Pause:** Control animation
- **Speed:** Adjust particle movement speed
- **Colors:** Cycle through color schemes
- **Particles:** Adjust number of active particles

*Use the controls on the map to customize the visualization!*
"""
        return summary
        
    except Exception as e:
        logger.error(f"Error formatting particle summary: {e}")
        return f"Error formatting particle summary: {e}"

# Create Gradio interface using WORKING GRIB fetching logic
with gr.Blocks(title="🌊 Wave Particle Visualizer", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🌊 Wave Particle Animation Visualizer")
    gr.Markdown("Real-time animated wave particles using the SAME GRIB fetching logic as the working NWPS_SWAN space")
    
    with gr.Tabs():
        # Particle Animation Tab
        with gr.TabItem("🎯 Wave Particle Animation"):
            gr.Markdown("""
            **Using Working NWPS_SWAN GRIB Logic:**
            - 🌊 Same GRIB processing as your working NWPS_SWAN space
            - 🎯 Animated particles showing wave movement
            - 🎨 Multiple color schemes (Ocean, Heat Map, Plasma)
            - ⚑ Adjustable animation speed and particle count
            - πŸ“ Atlantic, Pacific, West Coast coverage (no Arctic errors)
            """)
            
            with gr.Row():
                fetch_particles_btn = gr.Button("🎯 Fetch Wave Particles", variant="primary", size="lg")
                
            with gr.Row():
                with gr.Column(scale=1):
                    particle_summary = gr.Markdown("Click 'Fetch Wave Particles' to start the animation")
                    particle_status = gr.Textbox(label="Status", value="Ready to fetch particle data")
                
                with gr.Column(scale=2):
                    particle_map = gr.HTML(label="Wave Particle Animation", value="")
            
            fetch_particles_btn.click(
                fn=fetch_wave_particles,
                outputs=[particle_summary, particle_map, particle_status]
            )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        share=False
    )