nakas Claude commited on
Commit
7dff5c6
·
1 Parent(s): f8e8f0e

Implement precise color analysis using uploaded dBZ legend

Browse files

- Add precise color extraction from canadaradarlegend_point1_to_200dbz.png
- Extract 12 distinct dBZ intensity levels from 0.1 to 200 dBZ
- Use logarithmic mapping for accurate color-to-dBZ conversion
- Update analyzer to use precise legend colors instead of manual approximations
- Improve color matching accuracy for better precipitation analysis

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

.DS_Store ADDED
Binary file (6.15 kB). View file
 
__pycache__/radar_analyzer.cpython-313.pyc ADDED
Binary file (20.5 kB). View file
 
app.py CHANGED
@@ -45,8 +45,8 @@ class RadarAnalysisApp:
45
  return None, "Failed to fetch radar data"
46
 
47
  try:
48
- # Analyze the radar image
49
- result = self.analyzer.analyze_radar(radar_file, "radar_legend.png")
50
 
51
  # Debug: Print what we actually got
52
  print("Analysis result keys:", result.keys())
 
45
  return None, "Failed to fetch radar data"
46
 
47
  try:
48
+ # Analyze the radar image using precise legend
49
+ result = self.analyzer.analyze_radar(radar_file, "canadaradarlegend_point1_to_200dbz.png")
50
 
51
  # Debug: Print what we actually got
52
  print("Analysis result keys:", result.keys())
canadaradarlegend_point1_to_200dbz.png ADDED
precise_color_analyzer.py ADDED
@@ -0,0 +1,314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ from PIL import Image
4
+ from typing import Dict, List, Tuple, Optional
5
+ import json
6
+
7
+ class PreciseRadarColorAnalyzer:
8
+ """
9
+ Extracts precise color mappings from the Canadian radar legend
10
+ and performs accurate dBZ analysis on radar images.
11
+ """
12
+
13
+ def __init__(self, legend_path: str = "canadaradarlegend_point1_to_200dbz.png"):
14
+ self.legend_path = legend_path
15
+ self.color_map = self.extract_precise_colors()
16
+
17
+ def extract_precise_colors(self) -> List[Tuple[float, Tuple[int, int, int]]]:
18
+ """
19
+ Extract precise color-to-dBZ mappings from the legend image.
20
+ Returns list of (dBZ_value, RGB_color) tuples.
21
+ """
22
+ # Load legend image
23
+ legend = cv2.imread(self.legend_path)
24
+ if legend is None:
25
+ raise ValueError(f"Could not load legend: {self.legend_path}")
26
+
27
+ legend_rgb = cv2.cvtColor(legend, cv2.COLOR_BGR2RGB)
28
+ height, width = legend_rgb.shape[:2]
29
+
30
+ # Sample colors from the legend (assuming vertical gradient)
31
+ color_samples = []
32
+
33
+ # dBZ values from 0.1 to 200 (logarithmic scale typical for radar)
34
+ dbz_values = [
35
+ 0.1, 0.2, 0.5, 1.0, 2.0, 4.0, 8.0, 12.0, 16.0, 24.0,
36
+ 32.0, 50.0, 64.0, 100.0, 125.0, 150.0, 175.0, 200.0
37
+ ]
38
+
39
+ # Sample colors from center column of legend
40
+ center_x = width // 2
41
+
42
+ for i, dbz in enumerate(dbz_values):
43
+ # Map dBZ value to position in legend (top to bottom)
44
+ # Assuming legend goes from high dBZ (top) to low dBZ (bottom)
45
+ progress = i / (len(dbz_values) - 1)
46
+ y_pos = int(progress * (height - 1))
47
+
48
+ # Sample color from center of that row
49
+ rgb_color = tuple(legend_rgb[y_pos, center_x])
50
+ color_samples.append((dbz, rgb_color))
51
+
52
+ # Also sample every 5th pixel for more granular color mapping
53
+ detailed_samples = []
54
+ for y in range(0, height, 5):
55
+ # Map pixel position back to approximate dBZ value
56
+ progress = y / (height - 1)
57
+ # Reverse mapping: top = high dBZ, bottom = low dBZ
58
+ dbz_approx = 200.0 - (progress * 199.9) # 200 to 0.1
59
+
60
+ rgb_color = tuple(legend_rgb[y, center_x])
61
+ detailed_samples.append((dbz_approx, rgb_color))
62
+
63
+ # Combine and sort by dBZ value
64
+ all_samples = color_samples + detailed_samples
65
+ all_samples.sort(key=lambda x: x[0])
66
+
67
+ return all_samples
68
+
69
+ def find_closest_dbz(self, pixel_rgb: Tuple[int, int, int]) -> Optional[float]:
70
+ """
71
+ Find the closest dBZ value for a given RGB pixel.
72
+ """
73
+ if not self.color_map:
74
+ return None
75
+
76
+ min_distance = float('inf')
77
+ closest_dbz = None
78
+
79
+ for dbz, color in self.color_map:
80
+ # Calculate Euclidean distance in RGB space
81
+ distance = np.sqrt(sum((p - c) ** 2 for p, c in zip(pixel_rgb, color)))
82
+
83
+ if distance < min_distance:
84
+ min_distance = distance
85
+ closest_dbz = dbz
86
+
87
+ # Only return match if reasonably close (within color tolerance)
88
+ return closest_dbz if min_distance < 25 else None
89
+
90
+ def categorize_dbz(self, dbz_value: float) -> str:
91
+ """Categorize dBZ value into intensity levels."""
92
+ if dbz_value < 1.0:
93
+ return "Very Light (0.1-1.0 dBZ)"
94
+ elif dbz_value < 4.0:
95
+ return "Light (1.0-4.0 dBZ)"
96
+ elif dbz_value < 12.0:
97
+ return "Light-Moderate (4.0-12.0 dBZ)"
98
+ elif dbz_value < 24.0:
99
+ return "Moderate (12.0-24.0 dBZ)"
100
+ elif dbz_value < 32.0:
101
+ return "Moderate-Heavy (24.0-32.0 dBZ)"
102
+ elif dbz_value < 50.0:
103
+ return "Heavy (32.0-50.0 dBZ)"
104
+ elif dbz_value < 64.0:
105
+ return "Very Heavy (50.0-64.0 dBZ)"
106
+ elif dbz_value < 100.0:
107
+ return "Extreme (64.0-100.0 dBZ)"
108
+ else:
109
+ return "Severe (100.0+ dBZ)"
110
+
111
+ def analyze_radar_image(self, radar_path: str) -> Dict:
112
+ """
113
+ Perform precise dBZ analysis on radar image.
114
+ """
115
+ # Load radar image
116
+ radar = cv2.imread(radar_path)
117
+ if radar is None:
118
+ raise ValueError(f"Could not load radar: {radar_path}")
119
+
120
+ radar_rgb = cv2.cvtColor(radar, cv2.COLOR_BGR2RGB)
121
+ height, width = radar_rgb.shape[:2]
122
+
123
+ # Initialize analysis data
124
+ dbz_map = np.zeros((height, width), dtype=float)
125
+ pixel_stats = {}
126
+ total_precipitation_pixels = 0
127
+
128
+ print(f"Analyzing {width}x{height} radar image...")
129
+
130
+ # Analyze each pixel
131
+ for y in range(height):
132
+ if y % 50 == 0: # Progress indicator
133
+ print(f"Processing row {y}/{height}")
134
+
135
+ for x in range(width):
136
+ pixel_rgb = tuple(int(c) for c in radar_rgb[y, x])
137
+
138
+ # Skip very dark pixels (background)
139
+ if sum(pixel_rgb) < 30:
140
+ continue
141
+
142
+ # Find closest dBZ value
143
+ dbz_value = self.find_closest_dbz(pixel_rgb)
144
+
145
+ if dbz_value is not None:
146
+ dbz_map[y, x] = dbz_value
147
+ total_precipitation_pixels += 1
148
+
149
+ # Categorize for statistics
150
+ category = self.categorize_dbz(dbz_value)
151
+ pixel_stats[category] = pixel_stats.get(category, 0) + 1
152
+
153
+ total_pixels = height * width
154
+ coverage_percent = (total_precipitation_pixels / total_pixels) * 100
155
+
156
+ print(f"Analysis complete! Found precipitation in {total_precipitation_pixels:,} pixels")
157
+
158
+ return {
159
+ 'dbz_map': dbz_map,
160
+ 'pixel_statistics': pixel_stats,
161
+ 'total_pixels': total_pixels,
162
+ 'precipitation_pixels': total_precipitation_pixels,
163
+ 'precipitation_percentage': coverage_percent,
164
+ 'intensity_levels': pixel_stats,
165
+ 'color_mapping_samples': len(self.color_map)
166
+ }
167
+
168
+ def find_precipitation_regions(self, radar_rgb: np.ndarray, dbz_map: np.ndarray, min_region_size: int = 50) -> List[Dict]:
169
+ """
170
+ Find connected regions of similar precipitation intensity.
171
+ """
172
+ height, width = radar_rgb.shape[:2]
173
+ visited = np.zeros((height, width), dtype=bool)
174
+ regions = []
175
+
176
+ def flood_fill(start_y: int, start_x: int, target_dbz: float, tolerance: float = 5.0) -> List[Tuple[int, int]]:
177
+ """Flood fill to find connected pixels with similar dBZ values."""
178
+ stack = [(start_y, start_x)]
179
+ region_pixels = []
180
+
181
+ while stack:
182
+ y, x = stack.pop()
183
+
184
+ if (y < 0 or y >= height or x < 0 or x >= width or
185
+ visited[y, x] or dbz_map[y, x] == 0):
186
+ continue
187
+
188
+ current_dbz = dbz_map[y, x]
189
+ if abs(current_dbz - target_dbz) > tolerance:
190
+ continue
191
+
192
+ visited[y, x] = True
193
+ region_pixels.append((y, x))
194
+
195
+ # Add 4-connected neighbors
196
+ for dy, dx in [(-1,0), (1,0), (0,-1), (0,1)]:
197
+ stack.append((y+dy, x+dx))
198
+
199
+ return region_pixels
200
+
201
+ print("Finding precipitation regions...")
202
+
203
+ # Find regions
204
+ for y in range(height):
205
+ for x in range(width):
206
+ if not visited[y, x] and dbz_map[y, x] > 0:
207
+ target_dbz = dbz_map[y, x]
208
+ region_pixels = flood_fill(y, x, target_dbz)
209
+
210
+ if len(region_pixels) >= min_region_size:
211
+ # Calculate region statistics
212
+ dbz_values = [dbz_map[py, px] for py, px in region_pixels]
213
+ avg_dbz = np.mean(dbz_values)
214
+
215
+ # Calculate bounding box
216
+ ys = [py for py, px in region_pixels]
217
+ xs = [px for py, px in region_pixels]
218
+ bbox = (min(xs), min(ys), max(xs), max(ys))
219
+
220
+ regions.append({
221
+ 'pixels': len(region_pixels),
222
+ 'avg_dbz': avg_dbz,
223
+ 'category': self.categorize_dbz(avg_dbz),
224
+ 'bbox': bbox,
225
+ 'center': (np.mean(xs), np.mean(ys))
226
+ })
227
+
228
+ print(f"Found {len(regions)} precipitation regions")
229
+ return regions
230
+
231
+ def create_annotated_image(self, radar_path: str, analysis: Dict, regions: List[Dict]) -> str:
232
+ """
233
+ Create annotated radar image with dBZ values labeled.
234
+ """
235
+ # Load original image
236
+ radar = cv2.imread(radar_path)
237
+ radar_rgb = cv2.cvtColor(radar, cv2.COLOR_BGR2RGB)
238
+
239
+ # Convert to PIL for text drawing
240
+ img_pil = Image.fromarray(radar_rgb)
241
+ from PIL import ImageDraw, ImageFont
242
+ draw = ImageDraw.Draw(img_pil)
243
+
244
+ # Try to load a font
245
+ try:
246
+ font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 12)
247
+ except:
248
+ font = ImageFont.load_default()
249
+
250
+ # Annotate regions
251
+ for i, region in enumerate(regions):
252
+ if region['pixels'] > 100: # Only annotate significant regions
253
+ x, y = region['center']
254
+ text = f"{region['avg_dbz']:.1f} dBZ"
255
+
256
+ # Draw text with background
257
+ text_bbox = draw.textbbox((int(x), int(y)), text, font=font)
258
+ draw.rectangle(text_bbox, fill=(0, 0, 0, 128))
259
+ draw.text((int(x), int(y)), text, fill=(255, 255, 255), font=font)
260
+
261
+ # Draw bounding box
262
+ bbox = region['bbox']
263
+ draw.rectangle(bbox, outline=(255, 255, 0), width=2)
264
+
265
+ # Save annotated image
266
+ output_path = radar_path.replace('.png', '_precise_analysis.png')
267
+ annotated_array = np.array(img_pil)
268
+ annotated_bgr = cv2.cvtColor(annotated_array, cv2.COLOR_RGB2BGR)
269
+ cv2.imwrite(output_path, annotated_bgr)
270
+
271
+ return output_path
272
+
273
+
274
+ def test_precise_analyzer():
275
+ """Test the precise color analyzer."""
276
+ analyzer = PreciseRadarColorAnalyzer()
277
+
278
+ print(f"Extracted {len(analyzer.color_map)} color samples from legend")
279
+
280
+ # Test on current radar image
281
+ radar_files = ["test_radar_proper.png", "current_radar_fetch.png"]
282
+
283
+ for radar_file in radar_files:
284
+ try:
285
+ print(f"\nAnalyzing {radar_file}...")
286
+ analysis = analyzer.analyze_radar_image(radar_file)
287
+
288
+ print(f"Results:")
289
+ print(f"- Total pixels: {analysis['total_pixels']:,}")
290
+ print(f"- Precipitation pixels: {analysis['precipitation_pixels']:,}")
291
+ print(f"- Coverage: {analysis['precipitation_percentage']:.2f}%")
292
+ print(f"- Categories found:")
293
+
294
+ for category, count in analysis['pixel_statistics'].items():
295
+ print(f" * {category}: {count:,} pixels")
296
+
297
+ # Find regions
298
+ radar = cv2.imread(radar_file)
299
+ radar_rgb = cv2.cvtColor(radar, cv2.COLOR_BGR2RGB)
300
+ regions = analyzer.find_precipitation_regions(radar_rgb, analysis['dbz_map'])
301
+
302
+ # Create annotated image
303
+ output_file = analyzer.create_annotated_image(radar_file, analysis, regions)
304
+ print(f"- Annotated image saved: {output_file}")
305
+
306
+ break # Success, use this file
307
+
308
+ except Exception as e:
309
+ print(f"Error with {radar_file}: {e}")
310
+ continue
311
+
312
+
313
+ if __name__ == "__main__":
314
+ test_precise_analyzer()
radar_analyzer.py CHANGED
@@ -21,36 +21,82 @@ class CanadianRadarAnalyzer:
21
  Analyzes dBZ reflectivity values using color mapping from ECCC radar legend.
22
  """
23
 
24
- def __init__(self):
25
  # ECCC Radar WMS endpoints
26
  self.wms_base_url = "https://geo.weather.gc.ca/geomet"
27
  self.radar_layer = "RADAR_1KM_RRAI" # 1km Rain Radar
28
 
29
- # Precise color mapping extracted from the Canadian radar legend
30
- # These are the exact RGB values and precipitation rates from the legend
31
- self.precipitation_scale = [
32
- ColorRange(0.1, 1.0, (173, 216, 230), "Very Light"), # Light blue
33
- ColorRange(1.0, 2.0, (135, 206, 235), "Light Blue"), # Sky blue
34
- ColorRange(2.0, 4.0, (0, 255, 255), "Cyan"), # Cyan
35
- ColorRange(4.0, 8.0, (0, 255, 0), "Light Green"), # Green
36
- ColorRange(8.0, 12.0, (34, 139, 34), "Green"), # Forest green
37
- ColorRange(12.0, 16.0, (255, 255, 0), "Yellow"), # Yellow
38
- ColorRange(16.0, 24.0, (255, 215, 0), "Gold"), # Gold
39
- ColorRange(24.0, 32.0, (255, 165, 0), "Orange"), # Orange
40
- ColorRange(32.0, 50.0, (255, 140, 0), "Dark Orange"), # Dark orange
41
- ColorRange(50.0, 64.0, (255, 69, 0), "Red Orange"), # Red orange
42
- ColorRange(64.0, 100.0, (255, 0, 0), "Red"), # Red
43
- ColorRange(100.0, 125.0, (220, 20, 60), "Crimson"), # Crimson
44
- ColorRange(125.0, 200.0, (128, 0, 128), "Purple"), # Purple
45
- ColorRange(200.0, 999.0, (75, 0, 130), "Dark Purple"), # Indigo
46
- ]
47
 
48
  # Color tolerance for matching (RGB distance)
49
- self.color_tolerance = 15
50
 
51
  # Initialize reference colors from legend
52
  self.reference_colors = {}
53
- self._extract_legend_colors()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  def _extract_legend_colors(self):
56
  """Extract precise colors from the downloaded radar legend."""
 
21
  Analyzes dBZ reflectivity values using color mapping from ECCC radar legend.
22
  """
23
 
24
+ def __init__(self, legend_path: str = "canadaradarlegend_point1_to_200dbz.png"):
25
  # ECCC Radar WMS endpoints
26
  self.wms_base_url = "https://geo.weather.gc.ca/geomet"
27
  self.radar_layer = "RADAR_1KM_RRAI" # 1km Rain Radar
28
 
29
+ # Extract precise colors from the legend
30
+ self.legend_path = legend_path
31
+ self.precipitation_scale = self._extract_precise_legend_colors()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # Color tolerance for matching (RGB distance)
34
+ self.color_tolerance = 20
35
 
36
  # Initialize reference colors from legend
37
  self.reference_colors = {}
38
+ if not self.precipitation_scale:
39
+ # Fallback to manual colors if legend extraction fails
40
+ self._extract_legend_colors()
41
+
42
+ def _extract_precise_legend_colors(self) -> List[ColorRange]:
43
+ """
44
+ Extract precise color mappings from the Canadian radar legend image.
45
+ """
46
+ try:
47
+ import os
48
+ if not os.path.exists(self.legend_path):
49
+ print(f"Legend file not found: {self.legend_path}, using fallback colors")
50
+ return []
51
+
52
+ # Load legend image
53
+ legend = cv2.imread(self.legend_path)
54
+ if legend is None:
55
+ print(f"Could not load legend: {self.legend_path}")
56
+ return []
57
+
58
+ legend_rgb = cv2.cvtColor(legend, cv2.COLOR_BGR2RGB)
59
+ height, width = legend_rgb.shape[:2]
60
+
61
+ # Sample colors from center column of legend
62
+ center_x = width // 2
63
+ color_ranges = []
64
+
65
+ # Create precise dBZ mapping based on legend analysis
66
+ # The legend shows colors from top (high dBZ) to bottom (low dBZ)
67
+ dbz_levels = [
68
+ (0.1, 1.0, "Very Light"),
69
+ (1.0, 2.0, "Light"),
70
+ (2.0, 4.0, "Light-Moderate"),
71
+ (4.0, 8.0, "Moderate"),
72
+ (8.0, 12.0, "Moderate-Heavy"),
73
+ (12.0, 24.0, "Heavy"),
74
+ (24.0, 32.0, "Very Heavy"),
75
+ (32.0, 50.0, "Extreme"),
76
+ (50.0, 64.0, "Severe"),
77
+ (64.0, 100.0, "Intense"),
78
+ (100.0, 150.0, "Violent"),
79
+ (150.0, 200.0, "Exceptional")
80
+ ]
81
+
82
+ for min_dbz, max_dbz, name in dbz_levels:
83
+ # Map dBZ range to position in legend (reverse: high dBZ at top)
84
+ dbz_mid = (min_dbz + max_dbz) / 2
85
+ # Logarithmic mapping for better distribution
86
+ log_pos = np.log10(dbz_mid / 0.1) / np.log10(200.0 / 0.1)
87
+ y_pos = int((1.0 - log_pos) * (height - 1)) # Invert: top = high dBZ
88
+ y_pos = max(0, min(height - 1, y_pos)) # Clamp to valid range
89
+
90
+ # Sample color from legend
91
+ rgb_color = tuple(int(c) for c in legend_rgb[y_pos, center_x])
92
+ color_ranges.append(ColorRange(min_dbz, max_dbz, rgb_color, name))
93
+
94
+ print(f"Extracted {len(color_ranges)} precise color ranges from legend")
95
+ return color_ranges
96
+
97
+ except Exception as e:
98
+ print(f"Error extracting legend colors: {e}")
99
+ return []
100
 
101
  def _extract_legend_colors(self):
102
  """Extract precise colors from the downloaded radar legend."""