File size: 3,411 Bytes
fe7cdf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Helper script to create US radar legend data from a cropped legend image.
Run this after the user provides a cropped US radar legend.
"""

from radar_analyzer import RadarAnalyzer
import json
import os

def create_us_legend_data(legend_image_path: str):
    """Create legend data JSON file from US radar legend image."""
    
    if not os.path.exists(legend_image_path):
        print(f"❌ Legend file not found: {legend_image_path}")
        return False
    
    print(f"🎨 Processing US radar legend: {legend_image_path}")
    
    try:
        # Create US radar analyzer
        analyzer = RadarAnalyzer(radar_type="american", legend_path=legend_image_path)
        
        # Extract colors using the precise extraction method
        color_ranges = analyzer._extract_precise_legend_colors()
        
        if color_ranges:
            print(f"βœ… Extracted {len(color_ranges)} color ranges from US legend")
            
            # Show sample colors and dBZ ranges
            print('\nπŸ“Š Sample US radar color ranges:')
            for i, cr in enumerate(color_ranges[::len(color_ranges)//10]):  # Show every 10th
                print(f'  {cr.name}: {cr.min_value:.1f}-{cr.max_value:.1f} dBZ -> RGB{cr.rgb}')
            
            # Save the US legend data
            cache_data = {
                'source_file': legend_image_path,
                'radar_type': 'american',
                'total_colors': len(color_ranges),
                'dbz_range': [color_ranges[0].min_value, color_ranges[-1].max_value] if color_ranges else [0, 0],
                'colors': []
            }
            
            for cr in color_ranges:
                cache_data['colors'].append({
                    'min_value': cr.min_value,
                    'max_value': cr.max_value,
                    'rgb': list(cr.rgb),
                    'name': cr.name,
                    'dbz_center': (cr.min_value + cr.max_value) / 2
                })
            
            # Save with descriptive filename
            output_file = 'us_radar_legend_data.json'
            with open(output_file, 'w') as f:
                json.dump(cache_data, f, indent=2)
            
            print(f'πŸ’Ύ Saved US legend data: {output_file}')
            
            # Show dBZ range
            if color_ranges:
                min_dbz = min(cr.min_value for cr in color_ranges)
                max_dbz = max(cr.max_value for cr in color_ranges)
                print(f'πŸ“Š US dBZ Range: {min_dbz:.1f} to {max_dbz:.1f}')
                
            print('\nβœ… US radar legend data created successfully!')
            print('πŸ”„ The dual radar system is now ready to use!')
            
            return True
                
        else:
            print(f'❌ No colors extracted from {legend_image_path}')
            return False
            
    except Exception as e:
        print(f'❌ Error processing US legend: {e}')
        return False

if __name__ == "__main__":
    import sys
    
    if len(sys.argv) != 2:
        print("Usage: python create_us_legend_data.py <path_to_cropped_us_legend.png>")
        print("Example: python create_us_legend_data.py us_radar_legend_cropped.png")
        sys.exit(1)
    
    legend_path = sys.argv[1]
    success = create_us_legend_data(legend_path)
    
    if success:
        sys.exit(0)
    else:
        sys.exit(1)