| |
| """ |
| 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...") |
| |
| |
| 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()) |
| |
| |
| 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 |
| """ |
| |
| sample_files = [ |
| "/tmp/tmp0cvj_act.grib2", |
| "/tmp/tmpd7b9xfpo.grib2" |
| ] |
| |
| 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)") |
| |
| |
| demonstrate_integration() |
|
|
| if __name__ == "__main__": |
| if len(sys.argv) > 1: |
| |
| grib_file = sys.argv[1] |
| test_arctic_file(grib_file) |
| else: |
| |
| 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") |
|
|