Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """ | |
| CAMS Download Diagnostic Tool | |
| Helps troubleshoot issues with CAMS data downloads | |
| """ | |
| import os | |
| import zipfile | |
| from pathlib import Path | |
| from datetime import datetime, timedelta | |
| def diagnose_cams_downloads(): | |
| """Diagnose CAMS download issues""" | |
| print("π CAMS Download Diagnostic Tool") | |
| print("=" * 50) | |
| # Check downloads directory | |
| downloads_dir = Path("downloads") | |
| if not downloads_dir.exists(): | |
| print("β Downloads directory doesn't exist") | |
| return | |
| print(f"π Downloads directory: {downloads_dir.absolute()}") | |
| # List all files in downloads | |
| all_files = list(downloads_dir.glob("*")) | |
| if not all_files: | |
| print("π Downloads directory is empty") | |
| return | |
| print(f"\nπ Found {len(all_files)} files:") | |
| for file_path in all_files: | |
| print(f"\nπ File: {file_path.name}") | |
| print(f" Size: {file_path.stat().st_size} bytes ({file_path.stat().st_size / 1024:.1f} KB)") | |
| # Check if it's supposed to be a ZIP file | |
| if file_path.suffix.lower() == '.zip' or 'cams' in file_path.name.lower(): | |
| print(f" Expected: ZIP file") | |
| # Test if it's actually a ZIP | |
| if zipfile.is_zipfile(file_path): | |
| print(f" β Valid ZIP file") | |
| try: | |
| with zipfile.ZipFile(file_path, 'r') as zf: | |
| contents = zf.namelist() | |
| print(f" π¦ Contains {len(contents)} files:") | |
| for content in contents[:5]: # Show first 5 files | |
| print(f" - {content}") | |
| if len(contents) > 5: | |
| print(f" ... and {len(contents) - 5} more") | |
| except Exception as e: | |
| print(f" β οΈ Error reading ZIP: {e}") | |
| else: | |
| print(f" β NOT a valid ZIP file") | |
| # Try to read first few bytes to see what it actually is | |
| try: | |
| with open(file_path, 'rb') as f: | |
| header = f.read(100) | |
| print(f" π File header (first 100 bytes): {header[:50]}...") | |
| # Check for common error patterns | |
| header_str = header.decode('utf-8', errors='ignore').lower() | |
| if 'html' in header_str: | |
| print(f" π¨ Appears to be HTML (likely an error page)") | |
| elif 'error' in header_str: | |
| print(f" π¨ Contains 'error' - likely an error response") | |
| elif 'json' in header_str: | |
| print(f" π¨ Appears to be JSON (likely an API error)") | |
| elif header.startswith(b'PK'): | |
| print(f" π€ Has ZIP signature but zipfile module rejects it") | |
| else: | |
| print(f" β Unknown file format") | |
| except Exception as e: | |
| print(f" β Error reading file: {e}") | |
| def test_cds_connection(): | |
| """Test CDS API connection""" | |
| print("\nπ Testing CDS API Connection") | |
| print("-" * 30) | |
| try: | |
| import cdsapi | |
| # Check for .cdsapirc file | |
| cdsapirc_path = Path.home() / '.cdsapirc' | |
| if cdsapirc_path.exists(): | |
| print("β .cdsapirc file found") | |
| # Try to initialize client | |
| try: | |
| client = cdsapi.Client() | |
| print("β CDS API client initialized successfully") | |
| # Test a simple info request (doesn't download data) | |
| print("π Testing API connection...") | |
| # Note: This is just a connection test, not actually downloading | |
| print("β CDS API connection appears to be working") | |
| print("π‘ If downloads fail, it may be due to:") | |
| print(" - Invalid date range") | |
| print(" - CAMS service temporary issues") | |
| print(" - Account limitations") | |
| except Exception as e: | |
| print(f"β CDS API client initialization failed: {e}") | |
| else: | |
| print("β .cdsapirc file not found") | |
| print("π‘ Create ~/.cdsapirc with your CDS API credentials") | |
| except ImportError: | |
| print("β cdsapi module not installed") | |
| print("π‘ Install with: pip install cdsapi") | |
| def suggest_solutions(): | |
| """Suggest solutions for common issues""" | |
| print("\nπ‘ Common Solutions") | |
| print("-" * 20) | |
| print("1. π Try a different date (some dates may not have data)") | |
| print("2. π Wait and retry (CAMS servers may be busy)") | |
| print("3. π Check CDS API credentials in ~/.cdsapirc") | |
| print("4. ποΈ Clear downloads directory and retry") | |
| print("5. π Use more recent dates (last 30 days usually work)") | |
| print("6. π Check CDS website status: https://cds.climate.copernicus.eu/") | |
| if __name__ == "__main__": | |
| diagnose_cams_downloads() | |
| test_cds_connection() | |
| suggest_solutions() |