cams-pollution-dashboard / diagnose_cams.py
aditya-me13's picture
integrated both the versions
610152e
raw
history blame
5.35 kB
#!/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()