nakas Claude commited on
Commit
056a863
·
1 Parent(s): 8c2a426

Add RRFS radar viewer with Leaflet map integration

Browse files

- Implement Gradio app for viewing RRFS composite reflectivity
- Fetch real RRFS data from NOAA AWS S3 bucket using Herbie
- Display radar data on interactive Leaflet map with Folium
- Support multiple forecast hours (0-18) and run times
- Add NEXRAD-style color scheme for reflectivity visualization
- Include geographic bounds for CONUS domain

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

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

Files changed (3) hide show
  1. .gitignore +28 -0
  2. app.py +363 -0
  3. requirements.txt +10 -0
.gitignore ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+ *.so
5
+ .DS_Store
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+ .env
23
+ .venv
24
+ env/
25
+ venv/
26
+ ENV/
27
+ env.bak/
28
+ venv.bak/
app.py ADDED
@@ -0,0 +1,363 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import folium
3
+ from folium import plugins
4
+ import matplotlib.pyplot as plt
5
+ import matplotlib.colors as mcolors
6
+ import numpy as np
7
+ from datetime import datetime, timedelta
8
+ from io import BytesIO
9
+ import base64
10
+ from PIL import Image
11
+ import requests
12
+ from herbie import Herbie
13
+ import warnings
14
+ warnings.filterwarnings('ignore')
15
+
16
+ # RRFS domain bounds (approximate CONUS)
17
+ CONUS_BOUNDS = [[21.14, -122.7], [47.86, -60.9]]
18
+
19
+ def get_available_runs():
20
+ """Get list of available RRFS run times from the past week"""
21
+ runs = []
22
+ # RRFS retrospective data from May 2024
23
+ base_dates = [
24
+ "2024-05-02 12:00",
25
+ "2024-05-02 00:00",
26
+ "2024-05-01 12:00",
27
+ "2024-05-01 00:00",
28
+ ]
29
+ return base_dates
30
+
31
+ def create_radar_colormap():
32
+ """Create NEXRAD-like color map for composite reflectivity"""
33
+ colors = [
34
+ '#646464', # No echo
35
+ '#04e9e7', # Light blue
36
+ '#019ff4', # Blue
37
+ '#0300f4', # Dark blue
38
+ '#02fd02', # Green
39
+ '#01c501', # Dark green
40
+ '#008e00', # Darker green
41
+ '#fdf802', # Yellow
42
+ '#e5bc00', # Orange-yellow
43
+ '#fd9500', # Orange
44
+ '#fd0000', # Red
45
+ '#d40000', # Dark red
46
+ '#bc0000', # Darker red
47
+ '#f800fd', # Magenta
48
+ '#9854c6', # Purple
49
+ ]
50
+ bounds = [-30, -20, -10, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 75]
51
+ cmap = mcolors.ListedColormap(colors)
52
+ norm = mcolors.BoundaryNorm(bounds, cmap.N)
53
+ return cmap, norm
54
+
55
+ def fetch_rrfs_data(run_time, forecast_hour):
56
+ """
57
+ Fetch RRFS composite reflectivity data from NOAA
58
+
59
+ Args:
60
+ run_time: Model run time (YYYYMMDDHH format or datetime string)
61
+ forecast_hour: Forecast hour (0-18)
62
+
63
+ Returns:
64
+ tuple: (data_array, lats, lons) or (None, None, None) if fetch fails
65
+ """
66
+ try:
67
+ # Parse run time
68
+ if isinstance(run_time, str):
69
+ if '-' in run_time:
70
+ dt = datetime.strptime(run_time, "%Y-%m-%d %H:%M")
71
+ else:
72
+ dt = datetime.strptime(run_time, "%Y%m%d%H")
73
+ else:
74
+ dt = run_time
75
+
76
+ # Initialize Herbie for RRFS
77
+ # Using AWS S3 bucket for retrospective data
78
+ H = Herbie(
79
+ dt,
80
+ model='rrfs',
81
+ product='nat',
82
+ fxx=forecast_hour,
83
+ priority=['aws'],
84
+ )
85
+
86
+ # Try to get composite reflectivity
87
+ # REFC = Composite Reflectivity
88
+ try:
89
+ ds = H.xarray('REFC:entire')
90
+ except:
91
+ # If REFC not available, try alternative variable names
92
+ try:
93
+ ds = H.xarray('REFD')
94
+ except:
95
+ return None, None, None
96
+
97
+ # Extract data
98
+ if 'refc' in ds:
99
+ refc = ds['refc']
100
+ elif 'refd' in ds:
101
+ refc = ds['refd']
102
+ else:
103
+ # Get first variable
104
+ var_name = list(ds.data_vars)[0]
105
+ refc = ds[var_name]
106
+
107
+ # Get coordinates
108
+ lats = refc.latitude.values if hasattr(refc, 'latitude') else refc.lat.values
109
+ lons = refc.longitude.values if hasattr(refc, 'longitude') else refc.lon.values
110
+
111
+ # Get reflectivity values
112
+ refc_data = refc.values
113
+
114
+ return refc_data, lats, lons
115
+
116
+ except Exception as e:
117
+ print(f"Error fetching RRFS data: {e}")
118
+ return None, None, None
119
+
120
+ def create_radar_overlay(refc_data, lats, lons):
121
+ """
122
+ Create a PNG overlay image from radar data
123
+
124
+ Args:
125
+ refc_data: Composite reflectivity data array
126
+ lats: Latitude coordinates
127
+ lons: Longitude coordinates
128
+
129
+ Returns:
130
+ tuple: (image_base64, bounds) for folium ImageOverlay
131
+ """
132
+ # Create figure
133
+ fig, ax = plt.subplots(figsize=(10, 8), dpi=150)
134
+
135
+ # Get colormap
136
+ cmap, norm = create_radar_colormap()
137
+
138
+ # Plot data
139
+ im = ax.pcolormesh(lons, lats, refc_data, cmap=cmap, norm=norm, shading='auto')
140
+
141
+ # Remove axes
142
+ ax.axis('off')
143
+ plt.tight_layout(pad=0)
144
+
145
+ # Save to bytes
146
+ buf = BytesIO()
147
+ plt.savefig(buf, format='png', transparent=True, bbox_inches='tight', pad_inches=0)
148
+ plt.close(fig)
149
+ buf.seek(0)
150
+
151
+ # Convert to base64
152
+ img_base64 = base64.b64encode(buf.read()).decode()
153
+
154
+ # Get bounds for overlay
155
+ bounds = [
156
+ [float(np.min(lats)), float(np.min(lons))],
157
+ [float(np.max(lats)), float(np.max(lons))]
158
+ ]
159
+
160
+ return f"data:image/png;base64,{img_base64}", bounds
161
+
162
+ def generate_map(run_time, forecast_hour):
163
+ """
164
+ Generate Folium map with RRFS radar overlay
165
+
166
+ Args:
167
+ run_time: Model run time
168
+ forecast_hour: Forecast hour
169
+
170
+ Returns:
171
+ folium.Map object
172
+ """
173
+ # Create base map centered on CONUS
174
+ m = folium.Map(
175
+ location=[39.0, -98.0],
176
+ zoom_start=4,
177
+ tiles='OpenStreetMap'
178
+ )
179
+
180
+ # Add tile layer options
181
+ folium.TileLayer('Cartodb Positron').add_to(m)
182
+ folium.TileLayer('Cartodb dark_matter').add_to(m)
183
+
184
+ # Fetch RRFS data
185
+ status_html = """
186
+ <div style='position: fixed; bottom: 20px; left: 20px; z-index: 1000;
187
+ background-color: white; padding: 10px; border-radius: 5px;
188
+ border: 2px solid #333; font-family: Arial;'>
189
+ <b>RRFS Composite Reflectivity</b><br>
190
+ Run: {run_time}<br>
191
+ Forecast Hour: F{fxx:03d}<br>
192
+ Valid: {valid_time}<br>
193
+ <span style='color: #0066cc;'>Source: NOAA RRFS (AWS S3)</span>
194
+ </div>
195
+ """
196
+
197
+ try:
198
+ # Parse run time
199
+ if isinstance(run_time, str):
200
+ if '-' in run_time:
201
+ dt = datetime.strptime(run_time, "%Y-%m-%d %H:%M")
202
+ else:
203
+ dt = datetime.strptime(run_time, "%Y%m%d%H")
204
+ else:
205
+ dt = run_time
206
+
207
+ valid_time = dt + timedelta(hours=int(forecast_hour))
208
+
209
+ # Add status info
210
+ m.get_root().html.add_child(folium.Element(
211
+ status_html.format(
212
+ run_time=dt.strftime("%Y-%m-%d %H:00 UTC"),
213
+ fxx=int(forecast_hour),
214
+ valid_time=valid_time.strftime("%Y-%m-%d %H:00 UTC")
215
+ )
216
+ ))
217
+
218
+ # Fetch and overlay data
219
+ refc_data, lats, lons = fetch_rrfs_data(run_time, int(forecast_hour))
220
+
221
+ if refc_data is not None:
222
+ # Create overlay
223
+ img_data, bounds = create_radar_overlay(refc_data, lats, lons)
224
+
225
+ # Add image overlay
226
+ folium.raster_layers.ImageOverlay(
227
+ image=img_data,
228
+ bounds=bounds,
229
+ opacity=0.6,
230
+ name='RRFS Composite Reflectivity'
231
+ ).add_to(m)
232
+
233
+ # Add colorbar legend
234
+ legend_html = '''
235
+ <div style="position: fixed; top: 20px; right: 20px; z-index: 1000;
236
+ background-color: white; padding: 10px; border-radius: 5px;
237
+ border: 2px solid #333; font-family: Arial; font-size: 12px;">
238
+ <b>Reflectivity (dBZ)</b><br>
239
+ <div style="display: flex; flex-direction: column; margin-top: 5px;">
240
+ <div><span style="background: #9854c6; width: 20px; height: 15px; display: inline-block;"></span> 60-75</div>
241
+ <div><span style="background: #f800fd; width: 20px; height: 15px; display: inline-block;"></span> 50-60</div>
242
+ <div><span style="background: #bc0000; width: 20px; height: 15px; display: inline-block;"></span> 45-50</div>
243
+ <div><span style="background: #d40000; width: 20px; height: 15px; display: inline-block;"></span> 40-45</div>
244
+ <div><span style="background: #fd0000; width: 20px; height: 15px; display: inline-block;"></span> 35-40</div>
245
+ <div><span style="background: #fd9500; width: 20px; height: 15px; display: inline-block;"></span> 30-35</div>
246
+ <div><span style="background: #e5bc00; width: 20px; height: 15px; display: inline-block;"></span> 25-30</div>
247
+ <div><span style="background: #fdf802; width: 20px; height: 15px; display: inline-block;"></span> 20-25</div>
248
+ <div><span style="background: #008e00; width: 20px; height: 15px; display: inline-block;"></span> 15-20</div>
249
+ <div><span style="background: #01c501; width: 20px; height: 15px; display: inline-block;"></span> 10-15</div>
250
+ <div><span style="background: #02fd02; width: 20px; height: 15px; display: inline-block;"></span> 5-10</div>
251
+ <div><span style="background: #0300f4; width: 20px; height: 15px; display: inline-block;"></span> 0-5</div>
252
+ </div>
253
+ </div>
254
+ '''
255
+ m.get_root().html.add_child(folium.Element(legend_html))
256
+ else:
257
+ # Add error message
258
+ error_html = """
259
+ <div style='position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
260
+ z-index: 1001; background-color: #ffcccc; padding: 20px;
261
+ border-radius: 10px; border: 2px solid #ff0000; font-family: Arial;'>
262
+ <h3>Data Not Available</h3>
263
+ <p>Unable to fetch RRFS data for the selected time.</p>
264
+ <p><b>Note:</b> Real-time RRFS output ceased in December 2024 for testing.<br>
265
+ Retrospective data from May 2024 is available.</p>
266
+ </div>
267
+ """
268
+ m.get_root().html.add_child(folium.Element(error_html))
269
+
270
+ except Exception as e:
271
+ # Add error message
272
+ error_html = f"""
273
+ <div style='position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
274
+ z-index: 1001; background-color: #ffcccc; padding: 20px;
275
+ border-radius: 10px; border: 2px solid #ff0000; font-family: Arial;'>
276
+ <h3>Error Loading Data</h3>
277
+ <p>Error: {str(e)}</p>
278
+ <p>Please try a different run time or forecast hour.</p>
279
+ </div>
280
+ """
281
+ m.get_root().html.add_child(folium.Element(error_html))
282
+
283
+ # Add layer control
284
+ folium.LayerControl().add_to(m)
285
+
286
+ return m
287
+
288
+ def create_interface():
289
+ """Create Gradio interface"""
290
+
291
+ with gr.Blocks(title="RRFS Radar Viewer") as demo:
292
+ gr.Markdown("""
293
+ # RRFS Composite Reflectivity Viewer
294
+
295
+ View NOAA Rapid Refresh Forecast System (RRFS) composite reflectivity data on an interactive map.
296
+
297
+ **Data Source:** NOAA RRFS retrospective data from AWS S3 (May 2024)
298
+
299
+ **Note:** Real-time RRFS output ceased in December 2024 for final testing before operational deployment in 2026.
300
+ """)
301
+
302
+ with gr.Row():
303
+ run_time = gr.Dropdown(
304
+ choices=get_available_runs(),
305
+ value=get_available_runs()[0],
306
+ label="Model Run Time (UTC)",
307
+ info="Select RRFS model initialization time"
308
+ )
309
+
310
+ forecast_hour = gr.Slider(
311
+ minimum=0,
312
+ maximum=18,
313
+ step=1,
314
+ value=0,
315
+ label="Forecast Hour",
316
+ info="Hours from model initialization (0-18)"
317
+ )
318
+
319
+ load_btn = gr.Button("Load Radar Data", variant="primary", size="lg")
320
+
321
+ map_output = gr.HTML(label="RRFS Radar Map")
322
+
323
+ def load_map(run_time, forecast_hour):
324
+ m = generate_map(run_time, int(forecast_hour))
325
+ return m._repr_html_()
326
+
327
+ load_btn.click(
328
+ fn=load_map,
329
+ inputs=[run_time, forecast_hour],
330
+ outputs=map_output
331
+ )
332
+
333
+ gr.Markdown("""
334
+ ---
335
+ ### About RRFS
336
+
337
+ The Rapid Refresh Forecast System (RRFS) is NOAA's next-generation convection-allowing ensemble prediction system.
338
+ It provides high-resolution weather forecasts at 3 km grid spacing over North America.
339
+
340
+ **Composite Reflectivity** shows the maximum radar reflectivity in a vertical column, useful for identifying
341
+ precipitation intensity and convective weather features.
342
+
343
+ ### Data Information
344
+
345
+ - **Model:** RRFS (Rapid Refresh Forecast System)
346
+ - **Grid Spacing:** 3 km
347
+ - **Domain:** North America (CONUS shown)
348
+ - **Forecast Length:** 18 hours
349
+ - **Update Frequency:** Retrospective runs from May 2024
350
+ - **Source:** NOAA AWS S3 Bucket (noaa-rrfs-pds)
351
+
352
+ ### References
353
+
354
+ - [RRFS Information (NOAA)](https://rapidrefresh.noaa.gov/RRFS/)
355
+ - [RRFS on AWS Open Data](https://registry.opendata.aws/noaa-rrfs/)
356
+ - [NOAA Global Systems Laboratory](https://gsl.noaa.gov/focus-areas/unified_forecast_system/rrfs)
357
+ """)
358
+
359
+ return demo
360
+
361
+ if __name__ == "__main__":
362
+ demo = create_interface()
363
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio==5.49.1
2
+ herbie-data==2024.12.0
3
+ cartopy==0.23.0
4
+ matplotlib==3.9.3
5
+ numpy==1.26.4
6
+ xarray==2024.11.0
7
+ cfgrib==0.9.14.1
8
+ folium==0.18.0
9
+ Pillow==11.0.0
10
+ requests==2.32.3