File size: 5,350 Bytes
610152e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/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()