Spaces:
Sleeping
Sleeping
| #!/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) |