| |
| """ |
| Test script to verify pygrib installation and Arctic GRIB file processing |
| """ |
| import sys |
| import os |
|
|
| def test_pygrib_import(): |
| """Test if pygrib can be imported successfully""" |
| print("π Testing pygrib import...") |
| try: |
| import pygrib |
| print(f"β
pygrib imported successfully, version: {pygrib.__version__}") |
| return True |
| except ImportError as e: |
| print(f"β pygrib import failed: {e}") |
| return False |
|
|
| def test_eccodes_import(): |
| """Test if eccodes can be imported successfully""" |
| print("π Testing eccodes import...") |
| try: |
| import eccodes |
| print(f"β
eccodes imported successfully") |
| return True |
| except ImportError as e: |
| print(f"β eccodes import failed: {e}") |
| return False |
|
|
| def test_arctic_grib_processing(): |
| """Test Arctic GRIB file processing with pygrib""" |
| arctic_file = "arctic_manual_20250828_12z.grib2" |
| |
| if not os.path.exists(arctic_file): |
| print(f"β οΈ Arctic test file not found: {arctic_file}") |
| return False |
| |
| print(f"π§ͺ Testing Arctic GRIB processing with pygrib...") |
| |
| try: |
| import pygrib |
| |
| |
| grbs = pygrib.open(arctic_file) |
| print(f"π Successfully opened Arctic GRIB file") |
| |
| |
| msg_count = grbs.messages |
| print(f"π Total messages in file: {msg_count}") |
| |
| |
| grbs.rewind() |
| wave_params_found = 0 |
| |
| for i, grb in enumerate(grbs): |
| if i >= 10: |
| break |
| |
| param_name = grb.name |
| short_name = grb.shortName if hasattr(grb, 'shortName') else 'unknown' |
| |
| print(f" Message {i+1}: {param_name} ({short_name})") |
| |
| |
| if any(keyword in param_name.lower() for keyword in ['wave', 'swell', 'height', 'period']): |
| wave_params_found += 1 |
| print(f" π Found wave parameter!") |
| |
| try: |
| |
| lats, lons = grb.latlons() |
| values = grb.values |
| |
| print(f" π Grid shape: {values.shape}") |
| print(f" π Value range: {values.min():.3f} to {values.max():.3f}") |
| print(f" πΊοΈ Lat range: {lats.min():.2f}Β° to {lats.max():.2f}Β°") |
| print(f" πΊοΈ Lon range: {lons.min():.2f}Β° to {lons.max():.2f}Β°") |
| |
| |
| arctic_points = (lats >= 50.0).sum() |
| print(f" π§ Arctic points (β₯50Β°N): {arctic_points:,}") |
| |
| except Exception as coord_error: |
| print(f" β Coordinate extraction failed: {coord_error}") |
| continue |
| |
| grbs.close() |
| print(f"β
Arctic GRIB processing test completed") |
| print(f"π Wave parameters found: {wave_params_found}") |
| |
| return wave_params_found > 0 |
| |
| except Exception as e: |
| print(f"β Arctic GRIB processing failed: {e}") |
| import traceback |
| traceback.print_exc() |
| return False |
|
|
| def main(): |
| """Run all pygrib tests""" |
| print("π§ͺ PyGRIB Setup Test Suite") |
| print("=" * 50) |
| |
| tests_passed = 0 |
| total_tests = 3 |
| |
| |
| if test_pygrib_import(): |
| tests_passed += 1 |
| |
| print() |
| |
| |
| if test_eccodes_import(): |
| tests_passed += 1 |
| |
| print() |
| |
| |
| if test_arctic_grib_processing(): |
| tests_passed += 1 |
| |
| print() |
| print("=" * 50) |
| print(f"β
Tests passed: {tests_passed}/{total_tests}") |
| |
| if tests_passed == total_tests: |
| print("π All tests passed! pygrib is working correctly.") |
| else: |
| print("β οΈ Some tests failed. Check error messages above.") |
| |
| return tests_passed == total_tests |
|
|
| if __name__ == "__main__": |
| success = main() |
| sys.exit(0 if success else 1) |