#!/usr/bin/env python3 """ Test script to demonstrate Arctic GRIB extraction for your specific error case """ import os import sys from arctic_integration import process_arctic_grib_safe, arctic_grib_handler def test_arctic_file(grib_file_path): """ Test the Arctic extraction on your specific problematic file """ print("๐Ÿงช Testing Arctic GRIB Extraction") print("=" * 50) print(f"File: {grib_file_path}") if not os.path.exists(grib_file_path): print(f"โŒ File not found: {grib_file_path}") print("Please update the path to your Arctic GRIB file") return False try: print("\n๐Ÿ”„ Starting extraction...") # This should work even with the ECCODES polar stereographic error data = process_arctic_grib_safe(grib_file_path, 'dataframe') print(f"\nโœ… SUCCESS! Extracted {len(data)} data points") print("\n๐Ÿ“Š Data Summary:") print(f" Columns: {list(data.columns)}") if 'latitude' in data.columns: print(f" Latitude range: {data['latitude'].min():.3f}ยฐ to {data['latitude'].max():.3f}ยฐ") if 'longitude' in data.columns: print(f" Longitude range: {data['longitude'].min():.3f}ยฐ to {data['longitude'].max():.3f}ยฐ") value_col = 'value' if 'value' in data.columns else 'val' if value_col in data.columns: print(f" Data range: {data[value_col].min():.6f} to {data[value_col].max():.6f}") print(f" Valid points: {data[value_col].notna().sum()}") print(f"\n๐Ÿ“‹ First 5 data points:") print(data.head()) # Save results csv_file = grib_file_path.replace('.grib2', '_EXTRACTED.csv') data.to_csv(csv_file, index=False) print(f"\n๐Ÿ’พ Saved results to: {csv_file}") return True except Exception as e: print(f"โŒ Extraction failed: {e}") import traceback traceback.print_exc() return False def demonstrate_integration(): """ Show how to integrate this with your existing wave puller """ print("\n๐Ÿ”— Integration Example for your grib_wave_puller:") print("=" * 60) integration_code = ''' # Modify your existing Arctic processing code like this: def process_arctic_region_fixed(grib_file): """Enhanced Arctic processing that handles ECCODES errors""" try: # Your existing Arctic processing code import xarray as xr ds = xr.open_dataset(grib_file, engine='cfgrib') # ... rest of your normal processing ... except Exception as e: error_msg = str(e) # Check if it's the polar stereographic error if any(keyword in error_msg.lower() for keyword in ['polar stereographic', 'spherical earth', 'geoiterator']): print(f"ECCODES polar error detected: {error_msg}") print("Switching to alternative Arctic extraction...") # Use our Arctic extraction instead from arctic_integration import arctic_grib_handler try: result = arctic_grib_handler(grib_file) print(f"โœ… Successfully extracted {result['total_points']} Arctic points") # Convert to your expected format arctic_data = { 'latitudes': [coord[0] for coord in result['coordinates']], 'longitudes': [coord[1] for coord in result['coordinates']], 'values': result['data'], 'region': 'Arctic', 'extraction_method': 'polar_bypass' } return arctic_data except Exception as e2: print(f"โŒ Arctic extraction also failed: {e2}") raise else: # Different error, re-raise raise # Usage in your main wave puller: try: arctic_data = process_arctic_region_fixed("/tmp/tmp0cvj_act.grib2") print("Arctic processing successful!") except Exception as e: print(f"Arctic processing failed: {e}") # Continue with other regions... ''' print(integration_code) def quick_demo(): """ Quick demonstration with sample file paths """ # Common Arctic GRIB file paths from your error sample_files = [ "/tmp/tmp0cvj_act.grib2", # Your specific file "/tmp/tmpd7b9xfpo.grib2" # Your Global file (for comparison) ] print("๐Ÿš€ Quick Demo - Arctic GRIB Extraction") print("=" * 50) for grib_file in sample_files: print(f"\n๐Ÿ“ Testing: {grib_file}") if os.path.exists(grib_file): print("File exists - testing extraction...") success = test_arctic_file(grib_file) if success: print("โœ… This file can now be processed successfully!") else: print("โŒ Still having issues with this file") else: print("โ“ File not found (this is expected if temp files are cleaned up)") # Show the integration approach demonstrate_integration() if __name__ == "__main__": if len(sys.argv) > 1: # Test specific file provided as argument grib_file = sys.argv[1] test_arctic_file(grib_file) else: # Run the quick demo quick_demo() print("\n" + "=" * 60) print("๐ŸŽฏ TO USE WITH YOUR SPECIFIC ERROR:") print("=" * 60) print("1. Run: python test_arctic_extraction.py /tmp/tmp0cvj_act.grib2") print("2. Or modify your grib_wave_puller with the integration code above") print("3. The extraction will bypass ECCODES and get all lat/lon/data points") print("\n๐Ÿ’ก This solves:") print(" โ€ข ECCODES ERROR: Polar stereographic Geoiterator") print(" โ€ข Only supported for spherical earth") print(" โ€ข Unable to create iterator") print(" โ€ข Problem with calculation of geographic attributes")