#!/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()