CoolFace
Apppublic

nakas/ecmwf_open_data_forcast

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
test_arctic_extraction.py174 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3Test script to demonstrate Arctic GRIB extraction for your specific error case4"""5 6import os7import sys8from arctic_integration import process_arctic_grib_safe, arctic_grib_handler9 10def test_arctic_file(grib_file_path):11    """12    Test the Arctic extraction on your specific problematic file13    """14    print("๐Ÿงช Testing Arctic GRIB Extraction")15    print("=" * 50)16    print(f"File: {grib_file_path}")17    18    if not os.path.exists(grib_file_path):19        print(f"โŒ File not found: {grib_file_path}")20        print("Please update the path to your Arctic GRIB file")21        return False22    23    try:24        print("\n๐Ÿ”„ Starting extraction...")25        26        # This should work even with the ECCODES polar stereographic error27        data = process_arctic_grib_safe(grib_file_path, 'dataframe')28        29        print(f"\nโœ… SUCCESS! Extracted {len(data)} data points")30        print("\n๐Ÿ“Š Data Summary:")31        print(f"  Columns: {list(data.columns)}")32        33        if 'latitude' in data.columns:34            print(f"  Latitude range: {data['latitude'].min():.3f}ยฐ to {data['latitude'].max():.3f}ยฐ")35        if 'longitude' in data.columns:36            print(f"  Longitude range: {data['longitude'].min():.3f}ยฐ to {data['longitude'].max():.3f}ยฐ")37        38        value_col = 'value' if 'value' in data.columns else 'val'39        if value_col in data.columns:40            print(f"  Data range: {data[value_col].min():.6f} to {data[value_col].max():.6f}")41            print(f"  Valid points: {data[value_col].notna().sum()}")42        43        print(f"\n๐Ÿ“‹ First 5 data points:")44        print(data.head())45        46        # Save results47        csv_file = grib_file_path.replace('.grib2', '_EXTRACTED.csv')48        data.to_csv(csv_file, index=False)49        print(f"\n๐Ÿ’พ Saved results to: {csv_file}")50        51        return True52        53    except Exception as e:54        print(f"โŒ Extraction failed: {e}")55        import traceback56        traceback.print_exc()57        return False58 59def demonstrate_integration():60    """61    Show how to integrate this with your existing wave puller62    """63    print("\n๐Ÿ”— Integration Example for your grib_wave_puller:")64    print("=" * 60)65    66    integration_code = '''67# Modify your existing Arctic processing code like this:68 69def process_arctic_region_fixed(grib_file):70    """Enhanced Arctic processing that handles ECCODES errors"""71    72    try:73        # Your existing Arctic processing code74        import xarray as xr75        ds = xr.open_dataset(grib_file, engine='cfgrib')76        # ... rest of your normal processing ...77        78    except Exception as e:79        error_msg = str(e)80        81        # Check if it's the polar stereographic error82        if any(keyword in error_msg.lower() for keyword in 83               ['polar stereographic', 'spherical earth', 'geoiterator']):84            85            print(f"ECCODES polar error detected: {error_msg}")86            print("Switching to alternative Arctic extraction...")87            88            # Use our Arctic extraction instead89            from arctic_integration import arctic_grib_handler90            91            try:92                result = arctic_grib_handler(grib_file)93                print(f"โœ… Successfully extracted {result['total_points']} Arctic points")94                95                # Convert to your expected format96                arctic_data = {97                    'latitudes': [coord[0] for coord in result['coordinates']],98                    'longitudes': [coord[1] for coord in result['coordinates']],99                    'values': result['data'],100                    'region': 'Arctic',101                    'extraction_method': 'polar_bypass'102                }103                104                return arctic_data105                106            except Exception as e2:107                print(f"โŒ Arctic extraction also failed: {e2}")108                raise109        else:110            # Different error, re-raise111            raise112 113# Usage in your main wave puller:114try:115    arctic_data = process_arctic_region_fixed("/tmp/tmp0cvj_act.grib2")116    print("Arctic processing successful!")117    118except Exception as e:119    print(f"Arctic processing failed: {e}")120    # Continue with other regions...121'''122    123    print(integration_code)124 125def quick_demo():126    """127    Quick demonstration with sample file paths128    """129    # Common Arctic GRIB file paths from your error130    sample_files = [131        "/tmp/tmp0cvj_act.grib2",  # Your specific file132        "/tmp/tmpd7b9xfpo.grib2"   # Your Global file (for comparison)133    ]134    135    print("๐Ÿš€ Quick Demo - Arctic GRIB Extraction")136    print("=" * 50)137    138    for grib_file in sample_files:139        print(f"\n๐Ÿ“ Testing: {grib_file}")140        141        if os.path.exists(grib_file):142            print("File exists - testing extraction...")143            success = test_arctic_file(grib_file)144            if success:145                print("โœ… This file can now be processed successfully!")146            else:147                print("โŒ Still having issues with this file")148        else:149            print("โ“ File not found (this is expected if temp files are cleaned up)")150    151    # Show the integration approach152    demonstrate_integration()153 154if __name__ == "__main__":155    if len(sys.argv) > 1:156        # Test specific file provided as argument157        grib_file = sys.argv[1]158        test_arctic_file(grib_file)159    else:160        # Run the quick demo161        quick_demo()162        163        print("\n" + "=" * 60)164        print("๐ŸŽฏ TO USE WITH YOUR SPECIFIC ERROR:")165        print("=" * 60)166        print("1. Run: python test_arctic_extraction.py /tmp/tmp0cvj_act.grib2")167        print("2. Or modify your grib_wave_puller with the integration code above")168        print("3. The extraction will bypass ECCODES and get all lat/lon/data points")169        print("\n๐Ÿ’ก This solves:")170        print("   โ€ข ECCODES ERROR: Polar stereographic Geoiterator")171        print("   โ€ข Only supported for spherical earth")172        print("   โ€ข Unable to create iterator")173        print("   โ€ข Problem with calculation of geographic attributes")174