Spaces:
Sleeping
Sleeping
Streamline to fast Leaflet-Velocity only version
Browse files- Remove heavy dependencies (xarray, cfgrib, ecmwf-opendata, etc.)
- Simplify to only essential packages for ultra-fast loading
- Focus solely on Leaflet-Velocity canvas particle rendering
- Use sample wind data for immediate visualization
- Optimize Dockerfile for faster build times
- Add auto-load global map on startup
- Configurable particle display and regional views
⚡ Major performance improvements for Hugging Face Spaces deployment
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- Dockerfile +0 -11
- app.py +253 -881
- requirements.txt +1 -9
Dockerfile
CHANGED
@@ -3,14 +3,6 @@ FROM python:3.11-slim
|
|
3 |
# Set working directory
|
4 |
WORKDIR /app
|
5 |
|
6 |
-
# Install system dependencies for GRIB processing
|
7 |
-
RUN apt-get update && apt-get install -y \
|
8 |
-
libeccodes-dev \
|
9 |
-
libeccodes-tools \
|
10 |
-
wget \
|
11 |
-
curl \
|
12 |
-
&& rm -rf /var/lib/apt/lists/*
|
13 |
-
|
14 |
# Copy requirements file
|
15 |
COPY requirements.txt .
|
16 |
|
@@ -20,9 +12,6 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|
20 |
# Copy application code
|
21 |
COPY app.py .
|
22 |
|
23 |
-
# Create temp directory for GRIB files
|
24 |
-
RUN mkdir -p /tmp/ecmwf_data
|
25 |
-
|
26 |
# Set environment variables
|
27 |
ENV PYTHONUNBUFFERED=1
|
28 |
ENV GRADIO_SERVER_NAME=0.0.0.0
|
|
|
3 |
# Set working directory
|
4 |
WORKDIR /app
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Copy requirements file
|
7 |
COPY requirements.txt .
|
8 |
|
|
|
12 |
# Copy application code
|
13 |
COPY app.py .
|
14 |
|
|
|
|
|
|
|
15 |
# Set environment variables
|
16 |
ENV PYTHONUNBUFFERED=1
|
17 |
ENV GRADIO_SERVER_NAME=0.0.0.0
|
app.py
CHANGED
@@ -1,942 +1,314 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
"""
|
3 |
-
ECMWF Wind Particle Visualization
|
4 |
-
|
5 |
-
|
6 |
-
Features:
|
7 |
-
- Downloads real ECMWF 10m wind data (U and V components)
|
8 |
-
- Creates windy-style particle animation using Leaflet-Velocity
|
9 |
-
- TimestampedGeoJson animation as alternative visualization
|
10 |
-
- Interactive Folium map with time controls
|
11 |
"""
|
12 |
|
13 |
import gradio as gr
|
14 |
import numpy as np
|
15 |
-
import pandas as pd
|
16 |
-
import xarray as xr
|
17 |
import requests
|
18 |
-
import tempfile
|
19 |
-
import os
|
20 |
import json
|
21 |
-
import math
|
22 |
-
import random
|
23 |
-
from datetime import datetime, timedelta, timezone
|
24 |
-
import warnings
|
25 |
import folium
|
26 |
-
from folium.plugins import TimestampedGeoJson
|
27 |
from branca.element import Element
|
28 |
-
|
29 |
-
|
30 |
-
try:
|
31 |
-
from ecmwf.opendata import Client as OpenDataClient
|
32 |
-
OPENDATA_AVAILABLE = True
|
33 |
-
except ImportError:
|
34 |
-
OPENDATA_AVAILABLE = False
|
35 |
-
|
36 |
|
37 |
-
class
|
38 |
"""
|
39 |
-
|
40 |
-
|
41 |
"""
|
42 |
|
43 |
def __init__(self):
|
44 |
-
self.
|
45 |
-
self.client = None
|
46 |
-
if OPENDATA_AVAILABLE:
|
47 |
-
try:
|
48 |
-
self.client = OpenDataClient()
|
49 |
-
except:
|
50 |
-
self.client = None
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
date_str = run_time.strftime("%Y%m%d")
|
69 |
-
time_str = f"{run_hour:02d}"
|
70 |
-
|
71 |
-
# Test if this run is available
|
72 |
-
test_url = f"{self.aws_base_url}/{date_str}/{time_str}z/0p25/oper/"
|
73 |
-
try:
|
74 |
-
response = requests.head(test_url, timeout=10)
|
75 |
-
if response.status_code in [200, 403]:
|
76 |
-
return date_str, time_str, run_time
|
77 |
-
except:
|
78 |
-
continue
|
79 |
-
|
80 |
-
# Fallback
|
81 |
-
return now.strftime("%Y%m%d"), "12", now
|
82 |
-
|
83 |
-
except Exception:
|
84 |
-
now = datetime.utcnow()
|
85 |
-
return now.strftime("%Y%m%d"), "12", now
|
86 |
-
|
87 |
-
def download_wind_component(self, parameter, step=0, max_retries=3):
|
88 |
-
"""Download ECMWF wind component (10u or 10v)"""
|
89 |
-
|
90 |
-
date_str, time_str, run_time = self.get_latest_forecast_info()
|
91 |
-
|
92 |
-
# Method 1: Try ecmwf-opendata client
|
93 |
-
if OPENDATA_AVAILABLE and self.client:
|
94 |
-
try:
|
95 |
-
filename = os.path.join(self.temp_dir, f'ecmwf_{parameter}_{step}h_{datetime.now().strftime("%Y%m%d_%H%M%S")}.grib')
|
96 |
-
|
97 |
-
self.client.retrieve(
|
98 |
-
type="fc",
|
99 |
-
param=parameter,
|
100 |
-
step=step,
|
101 |
-
target=filename
|
102 |
-
)
|
103 |
-
|
104 |
-
if os.path.exists(filename) and os.path.getsize(filename) > 1000:
|
105 |
-
return filename, f"✅ ECMWF {parameter} data downloaded successfully!"
|
106 |
-
|
107 |
-
except Exception as e:
|
108 |
-
print(f"Client method failed: {str(e)}")
|
109 |
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
if response.status_code == 200:
|
118 |
-
local_file = os.path.join(self.temp_dir, f'ecmwf_aws_{parameter}_{step}h.grib2')
|
119 |
-
|
120 |
-
with open(local_file, 'wb') as f:
|
121 |
-
for chunk in response.iter_content(chunk_size=8192):
|
122 |
-
f.write(chunk)
|
123 |
-
|
124 |
-
if os.path.getsize(local_file) > 1000:
|
125 |
-
return local_file, f"✅ ECMWF {parameter} data downloaded via AWS S3!"
|
126 |
-
|
127 |
-
except Exception as e:
|
128 |
-
print(f"AWS method failed: {str(e)}")
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
# Download V component (10v)
|
142 |
-
v_file, v_msg = self.download_wind_component("10v", step)
|
143 |
-
|
144 |
-
if u_file and v_file:
|
145 |
-
wind_data[step] = {
|
146 |
-
'u_file': u_file,
|
147 |
-
'v_file': v_file,
|
148 |
-
'forecast_hour': step,
|
149 |
-
'valid_time': datetime.utcnow() + timedelta(hours=step)
|
150 |
-
}
|
151 |
-
print(f"✅ Successfully downloaded wind data for +{step}h")
|
152 |
-
else:
|
153 |
-
print(f"❌ Failed to download wind data for +{step}h")
|
154 |
-
|
155 |
-
except Exception as e:
|
156 |
-
print(f"Error downloading wind data for +{step}h: {str(e)}")
|
157 |
-
continue
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
"""
|
163 |
-
Convert GRIB wind files to JSON format compatible with Leaflet-Velocity
|
164 |
-
Returns grib2json-style format: [{"header": {...}, "data": [...]}, {"header": {...}, "data": [...]}]
|
165 |
"""
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
# Handle coordinates
|
182 |
-
if 'latitude' in ds_u.coords:
|
183 |
-
lats = ds_u.latitude.values
|
184 |
-
lons = ds_u.longitude.values
|
185 |
-
elif 'lat' in ds_u.coords:
|
186 |
-
lats = ds_u.lat.values
|
187 |
-
lons = ds_u.lon.values
|
188 |
-
else:
|
189 |
-
raise ValueError("Could not find latitude/longitude coordinates")
|
190 |
-
|
191 |
-
# Select first time step if multiple
|
192 |
-
if 'time' in u_data.dims and len(u_data.time) > 1:
|
193 |
-
u_values = u_data.isel(time=0).values
|
194 |
-
v_values = v_data.isel(time=0).values
|
195 |
-
elif 'valid_time' in u_data.dims:
|
196 |
-
u_values = u_data.isel(valid_time=0).values
|
197 |
-
v_values = v_data.isel(valid_time=0).values
|
198 |
-
else:
|
199 |
-
u_values = u_data.values
|
200 |
-
v_values = v_data.values
|
201 |
-
|
202 |
-
# Handle 3D data (select first level if needed)
|
203 |
-
if u_values.ndim > 2:
|
204 |
-
u_values = u_values[0]
|
205 |
-
v_values = v_values[0]
|
206 |
-
|
207 |
-
# Ensure latitude is sorted north to south for grib2json convention
|
208 |
-
if lats[0] < lats[-1]: # south to north, need to flip
|
209 |
-
lats = lats[::-1]
|
210 |
-
u_values = u_values[::-1, :]
|
211 |
-
v_values = v_values[::-1, :]
|
212 |
-
|
213 |
-
# Create header following grib2json format
|
214 |
-
ny, nx = u_values.shape
|
215 |
-
lo1 = float(lons[0])
|
216 |
-
la1 = float(lats[0]) # northernmost lat
|
217 |
-
lo2 = float(lons[-1])
|
218 |
-
la2 = float(lats[-1]) # southernmost lat
|
219 |
-
dx = float(lons[1] - lons[0]) if len(lons) > 1 else 0.25
|
220 |
-
dy = abs(float(lats[1] - lats[0])) if len(lats) > 1 else 0.25
|
221 |
-
|
222 |
-
# Common header
|
223 |
-
header_common = {
|
224 |
-
"discipline": 0,
|
225 |
-
"parameterCategory": 2,
|
226 |
-
"nx": nx,
|
227 |
-
"ny": ny,
|
228 |
-
"lo1": lo1,
|
229 |
-
"la1": la1,
|
230 |
-
"lo2": lo2,
|
231 |
-
"la2": la2,
|
232 |
-
"dx": dx,
|
233 |
-
"dy": dy,
|
234 |
-
"refTime": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
235 |
-
}
|
236 |
-
|
237 |
-
# U component header
|
238 |
-
header_u = header_common.copy()
|
239 |
-
header_u.update({
|
240 |
-
"parameterNumber": 2,
|
241 |
-
"parameterName": "UGRD",
|
242 |
-
"parameterNumberName": "eastward_wind",
|
243 |
-
"description": "U-component of wind"
|
244 |
-
})
|
245 |
-
|
246 |
-
# V component header
|
247 |
-
header_v = header_common.copy()
|
248 |
-
header_v.update({
|
249 |
-
"parameterNumber": 3,
|
250 |
-
"parameterName": "VGRD",
|
251 |
-
"parameterNumberName": "northward_wind",
|
252 |
-
"description": "V-component of wind"
|
253 |
-
})
|
254 |
-
|
255 |
-
# Flatten arrays (row-major order: west->east, north->south)
|
256 |
-
u_flat = u_values.flatten().tolist()
|
257 |
-
v_flat = v_values.flatten().tolist()
|
258 |
-
|
259 |
-
# Replace NaN values with None
|
260 |
-
u_flat = [None if (isinstance(x, float) and math.isnan(x)) else float(x) for x in u_flat]
|
261 |
-
v_flat = [None if (isinstance(x, float) and math.isnan(x)) else float(x) for x in v_flat]
|
262 |
-
|
263 |
-
# Create grib2json-style output
|
264 |
-
velocity_json = [
|
265 |
-
{
|
266 |
-
"header": header_u,
|
267 |
-
"data": u_flat
|
268 |
-
},
|
269 |
-
{
|
270 |
-
"header": header_v,
|
271 |
-
"data": v_flat
|
272 |
-
}
|
273 |
]
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
return velocity_json, f"✅ Converted GRIB to velocity JSON: {nx}x{ny} grid points"
|
280 |
-
|
281 |
-
except Exception as e:
|
282 |
-
return None, f"❌ Error converting GRIB to JSON: {str(e)}"
|
283 |
-
|
284 |
-
|
285 |
-
class GribJsonField:
|
286 |
-
"""
|
287 |
-
Bilinear-interpolated wind field for JSON exported by grib2json (UGRD/VGRD)
|
288 |
-
Based on the reference implementation for windy-style particle systems
|
289 |
-
"""
|
290 |
-
|
291 |
-
def __init__(self, records):
|
292 |
-
# Identify U (eastward) and V (northward)
|
293 |
-
rec_u = None
|
294 |
-
rec_v = None
|
295 |
-
for rec in records:
|
296 |
-
h = rec.get("header", {})
|
297 |
-
name = h.get("parameterNumberName") or h.get("parameterName") or ""
|
298 |
-
# Accept typical identifiers
|
299 |
-
if "eastward" in name or name.upper().startswith("UGRD"):
|
300 |
-
rec_u = rec
|
301 |
-
elif "northward" in name or name.upper().startswith("VGRD"):
|
302 |
-
rec_v = rec
|
303 |
-
|
304 |
-
if rec_u is None or rec_v is None:
|
305 |
-
# Fallback by parameterNumber: 2 (UGRD), 3 (VGRD)
|
306 |
-
for rec in records:
|
307 |
-
h = rec.get("header", {})
|
308 |
-
if h.get("parameterCategory") == 2:
|
309 |
-
if h.get("parameterNumber") == 2:
|
310 |
-
rec_u = rec_u or rec
|
311 |
-
if h.get("parameterNumber") == 3:
|
312 |
-
rec_v = rec_v or rec
|
313 |
-
|
314 |
-
if rec_u is None or rec_v is None:
|
315 |
-
raise ValueError("Could not find U and V components in records")
|
316 |
-
|
317 |
-
Hu = rec_u["header"]
|
318 |
-
Hv = rec_v["header"]
|
319 |
-
# Sanity: grid must match
|
320 |
-
for k in ("lo1","la1","lo2","la2","nx","ny","dx","dy"):
|
321 |
-
if Hu.get(k) != Hv.get(k):
|
322 |
-
raise ValueError(f"U/V headers differ on {k}: {Hu.get(k)} vs {Hv.get(k)}")
|
323 |
-
|
324 |
-
self.h = Hu
|
325 |
-
self.nx = int(Hu["nx"])
|
326 |
-
self.ny = int(Hu["ny"])
|
327 |
-
self.lo1 = float(Hu["lo1"])
|
328 |
-
self.la1 = float(Hu["la1"])
|
329 |
-
self.lo2 = float(Hu["lo2"])
|
330 |
-
self.la2 = float(Hu["la2"])
|
331 |
-
self.dx = float(Hu["dx"])
|
332 |
-
self.dy = float(Hu["dy"])
|
333 |
-
|
334 |
-
# Data order: i=0..nx-1 west->east, j=0..ny-1 north->south if la1>la2
|
335 |
-
self.u = rec_u["data"]
|
336 |
-
self.v = rec_v["data"]
|
337 |
-
if len(self.u) != self.nx * self.ny or len(self.v) != self.nx * self.ny:
|
338 |
-
raise ValueError("Unexpected data length for U/V")
|
339 |
-
|
340 |
-
# Pre-compute whether latitude decreases with j
|
341 |
-
self.north_to_south = self.la1 > self.la2
|
342 |
-
|
343 |
-
# Normalize longitudes to [0, 360) to simplify wrap-around if grid is global
|
344 |
-
self.global_wrap = math.isclose((self.lo2 - self.lo1 + self.dx), 360.0, rel_tol=1e-3)
|
345 |
-
|
346 |
-
def _index(self, i, j):
|
347 |
-
# row-major: for each j-row, nx x-cells
|
348 |
-
return j * self.nx + i
|
349 |
-
|
350 |
-
def _grid_coords(self, lon, lat):
|
351 |
-
"""
|
352 |
-
Map lon/lat to continuous grid indices (x, y) in [0, nx-1], [0, ny-1].
|
353 |
-
Handles increasing or decreasing latitude, and optional 0..360 wrap.
|
354 |
-
"""
|
355 |
-
L = lon
|
356 |
-
if self.global_wrap:
|
357 |
-
# normalize lon into [lo1, lo2)
|
358 |
-
span = 360.0
|
359 |
-
while L < self.lo1:
|
360 |
-
L += span
|
361 |
-
while L >= self.lo2:
|
362 |
-
L -= span
|
363 |
-
|
364 |
-
x = (L - self.lo1) / self.dx
|
365 |
-
if self.north_to_south:
|
366 |
-
y = (self.la1 - lat) / self.dy
|
367 |
else:
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
def __init__(self):
|
418 |
-
pass
|
419 |
-
|
420 |
-
def meters_per_degree_lat(self):
|
421 |
-
# ~111.32 km per degree latitude
|
422 |
-
return 111320.0
|
423 |
-
|
424 |
-
def meters_per_degree_lon(self, lat_deg):
|
425 |
-
# ~111.32 km * cos(lat)
|
426 |
-
return 111320.0 * math.cos(math.radians(lat_deg))
|
427 |
-
|
428 |
-
def rk4_step(self, field, lon, lat, dt_s):
|
429 |
-
"""
|
430 |
-
Advance (lon, lat) by dt_s seconds using RK4 on the vector field (u,v) m/s.
|
431 |
-
Convert m/s to degrees per second using local meters/degree.
|
432 |
-
"""
|
433 |
-
def uv_to_dlonlat(lon_, lat_, u, v):
|
434 |
-
if u is None or v is None:
|
435 |
-
return (None, None)
|
436 |
-
dlon_dt = u / self.meters_per_degree_lon(lat_)
|
437 |
-
dlat_dt = v / self.meters_per_degree_lat()
|
438 |
-
return (dlon_dt, dlat_dt)
|
439 |
-
|
440 |
-
vec = field.vector
|
441 |
-
# k1
|
442 |
-
uv1 = vec(lon, lat); k1 = uv_to_dlonlat(lon, lat, *(uv1 or (None,None)))
|
443 |
-
if None in k1: return (None, None)
|
444 |
-
# k2
|
445 |
-
lon2 = lon + 0.5 * dt_s * k1[0]; lat2 = lat + 0.5 * dt_s * k1[1]
|
446 |
-
uv2 = vec(lon2, lat2); k2 = uv_to_dlonlat(lon2, lat2, *(uv2 or (None,None)))
|
447 |
-
if None in k2: return (None, None)
|
448 |
-
# k3
|
449 |
-
lon3 = lon + 0.5 * dt_s * k2[0]; lat3 = lat + 0.5 * dt_s * k2[1]
|
450 |
-
uv3 = vec(lon3, lat3); k3 = uv_to_dlonlat(lon3, lat3, *(uv3 or (None,None)))
|
451 |
-
if None in k3: return (None, None)
|
452 |
-
# k4
|
453 |
-
lon4 = lon + dt_s * k3[0]; lat4 = lat + dt_s * k3[1]
|
454 |
-
uv4 = vec(lon4, lat4); k4 = uv_to_dlonlat(lon4, lat4, *(uv4 or (None,None)))
|
455 |
-
if None in k4: return (None, None)
|
456 |
-
|
457 |
-
dlon = (dt_s / 6.0) * (k1[0] + 2*k2[0] + 2*k3[0] + k4[0])
|
458 |
-
dlat = (dt_s / 6.0) * (k1[1] + 2*k2[1] + 2*k3[1] + k4[1])
|
459 |
-
return (lon + dlon, lat + dlat)
|
460 |
-
|
461 |
-
def simulate_particles(self, field, bounds, n_particles=800, steps=60, dt_minutes=5,
|
462 |
-
seed=None, respawn=True):
|
463 |
-
"""
|
464 |
-
Create Timestamped GeoJSON 'features' by advecting particles.
|
465 |
-
Each feature is a MultiPoint with a matching 'times' array.
|
466 |
-
bounds = (minLat, minLon, maxLat, maxLon)
|
467 |
"""
|
468 |
-
|
469 |
-
random.seed(seed)
|
470 |
-
|
471 |
-
minLat, minLon, maxLat, maxLon = bounds
|
472 |
-
dt_s = dt_minutes * 60
|
473 |
-
t0 = datetime.now(timezone.utc).replace(microsecond=0)
|
474 |
-
features = []
|
475 |
-
|
476 |
-
def rand_seed():
|
477 |
-
return (random.uniform(minLon, maxLon), random.uniform(minLat, maxLat))
|
478 |
-
|
479 |
-
for _ in range(n_particles):
|
480 |
-
coords = []
|
481 |
-
times = []
|
482 |
-
lon, lat = rand_seed()
|
483 |
-
for k in range(steps):
|
484 |
-
# record current position/time
|
485 |
-
coords.append([lon, lat])
|
486 |
-
times.append((t0 + timedelta(minutes=dt_minutes*k)).isoformat())
|
487 |
-
# advance
|
488 |
-
nxt = self.rk4_step(field, lon, lat, dt_s)
|
489 |
-
if nxt == (None, None):
|
490 |
-
if respawn:
|
491 |
-
lon, lat = rand_seed()
|
492 |
-
continue
|
493 |
-
else:
|
494 |
-
break
|
495 |
-
lon, lat = nxt
|
496 |
-
# keep within rough geographic bounds; respawn if out
|
497 |
-
if not (minLon <= lon <= maxLon and minLat <= lat <= maxLat):
|
498 |
-
if respawn:
|
499 |
-
lon, lat = rand_seed()
|
500 |
-
else:
|
501 |
-
break
|
502 |
-
|
503 |
-
if len(coords) >= 2:
|
504 |
-
# Color particles by wind speed at first position for visual appeal
|
505 |
-
initial_uv = field.vector(coords[0][0], coords[0][1])
|
506 |
-
if initial_uv:
|
507 |
-
wind_speed = math.sqrt(initial_uv[0]**2 + initial_uv[1]**2)
|
508 |
-
# Color code: blue (low) -> green -> yellow -> red (high)
|
509 |
-
if wind_speed < 5:
|
510 |
-
color = "#0066cc" # blue
|
511 |
-
elif wind_speed < 10:
|
512 |
-
color = "#00cc66" # green
|
513 |
-
elif wind_speed < 15:
|
514 |
-
color = "#cccc00" # yellow
|
515 |
-
else:
|
516 |
-
color = "#cc0000" # red
|
517 |
-
else:
|
518 |
-
color = "#666666" # gray for unknown
|
519 |
-
|
520 |
-
features.append({
|
521 |
-
"type": "Feature",
|
522 |
-
"geometry": {"type": "MultiPoint", "coordinates": coords},
|
523 |
-
"properties": {
|
524 |
-
"times": times,
|
525 |
-
"style": {
|
526 |
-
"color": color,
|
527 |
-
"fillColor": color,
|
528 |
-
"fillOpacity": 0.7,
|
529 |
-
"weight": 2,
|
530 |
-
"radius": 2
|
531 |
-
},
|
532 |
-
}
|
533 |
-
})
|
534 |
-
|
535 |
-
return {
|
536 |
-
"type": "FeatureCollection",
|
537 |
-
"features": features
|
538 |
-
}
|
539 |
-
|
540 |
-
|
541 |
-
class WindVisualizationApp:
|
542 |
-
"""
|
543 |
-
Main application class that ties together ECMWF data fetching and wind visualization
|
544 |
-
"""
|
545 |
-
|
546 |
-
def __init__(self):
|
547 |
-
self.wind_fetcher = ECMWFWindDataFetcher()
|
548 |
-
self.particle_animator = WindParticleAnimator()
|
549 |
-
self.current_wind_data = {}
|
550 |
-
self.current_velocity_json = None
|
551 |
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
status_msg = "🌪️ Downloading ECMWF 10m wind data...\n"
|
557 |
-
wind_data = self.wind_fetcher.download_wind_data(forecast_steps)
|
558 |
-
|
559 |
-
if not wind_data:
|
560 |
-
return "❌ Failed to download any wind data", None, None
|
561 |
-
|
562 |
-
status_msg += f"✅ Downloaded wind data for {len(wind_data)} forecast steps\n"
|
563 |
-
|
564 |
-
# Convert first available time step to velocity JSON
|
565 |
-
first_step = min(wind_data.keys())
|
566 |
-
u_file = wind_data[first_step]['u_file']
|
567 |
-
v_file = wind_data[first_step]['v_file']
|
568 |
-
|
569 |
-
status_msg += f"🔄 Converting GRIB to velocity JSON format...\n"
|
570 |
-
velocity_json, conv_msg = self.wind_fetcher.grib_to_velocity_json(u_file, v_file)
|
571 |
-
|
572 |
-
if velocity_json:
|
573 |
-
self.current_velocity_json = velocity_json
|
574 |
-
self.current_wind_data = wind_data
|
575 |
-
status_msg += conv_msg + "\n"
|
576 |
-
status_msg += f"📊 Ready for particle animation visualization!"
|
577 |
-
return status_msg, velocity_json, wind_data
|
578 |
-
else:
|
579 |
-
return f"❌ Failed to convert wind data: {conv_msg}", None, None
|
580 |
-
|
581 |
-
except Exception as e:
|
582 |
-
return f"❌ Error processing wind data: {str(e)}", None, None
|
583 |
|
584 |
-
def
|
585 |
-
"""Create
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
bounds = (25.0, -130.0, 55.0, -60.0)
|
602 |
-
center = [40, -95]
|
603 |
-
zoom = 3
|
604 |
-
elif region == "europe":
|
605 |
-
bounds = (35.0, -15.0, 65.0, 35.0)
|
606 |
-
center = [50, 10]
|
607 |
-
zoom = 4
|
608 |
-
else: # default to global
|
609 |
-
bounds = (-60.0, -180.0, 60.0, 180.0)
|
610 |
-
center = [20, 0]
|
611 |
-
zoom = 2
|
612 |
-
|
613 |
-
# Create map
|
614 |
-
m = folium.Map(
|
615 |
-
location=center,
|
616 |
-
tiles="CartoDB dark_matter",
|
617 |
-
zoom_start=zoom,
|
618 |
-
control_scale=True
|
619 |
-
)
|
620 |
-
|
621 |
-
# Add CartoDB Positron as alternative tile layer
|
622 |
-
folium.TileLayer(
|
623 |
-
tiles="CartoDB positron",
|
624 |
-
name="Light Theme",
|
625 |
-
control=True
|
626 |
-
).add_to(m)
|
627 |
-
|
628 |
-
# Add OpenStreetMap as alternative
|
629 |
-
folium.TileLayer(
|
630 |
-
tiles="OpenStreetMap",
|
631 |
-
name="OpenStreetMap",
|
632 |
-
control=True
|
633 |
-
).add_to(m)
|
634 |
-
|
635 |
-
# Create wind field from velocity JSON
|
636 |
-
field = GribJsonField(velocity_json)
|
637 |
-
|
638 |
-
# Generate particle animation using TimestampedGeoJson
|
639 |
-
geojson_data = self.particle_animator.simulate_particles(
|
640 |
-
field,
|
641 |
-
bounds=bounds,
|
642 |
-
n_particles=particle_count,
|
643 |
-
steps=50,
|
644 |
-
dt_minutes=6,
|
645 |
-
seed=42,
|
646 |
-
respawn=True
|
647 |
-
)
|
648 |
-
|
649 |
-
# Add TimestampedGeoJson layer for particle animation
|
650 |
-
ts = TimestampedGeoJson(
|
651 |
-
data=geojson_data,
|
652 |
-
period="PT6M", # 6 minutes between frames
|
653 |
-
duration=None,
|
654 |
-
add_last_point=False,
|
655 |
-
loop=True,
|
656 |
-
loop_button=True,
|
657 |
-
auto_play=True,
|
658 |
-
transition_time=300,
|
659 |
-
time_slider_drag_update=True
|
660 |
-
)
|
661 |
-
ts.add_to(m)
|
662 |
-
|
663 |
-
# Add Leaflet-Velocity plugin for canvas-based particle rendering
|
664 |
-
plugin_js = """
|
665 |
-
<script src="https://unpkg.com/leaflet-velocity/dist/leaflet-velocity.min.js"></script>
|
666 |
-
"""
|
667 |
-
m.get_root().html.add_child(Element(plugin_js))
|
668 |
-
|
669 |
-
# Save velocity JSON to a data URL for the Leaflet-Velocity layer
|
670 |
-
velocity_json_str = json.dumps(velocity_json)
|
671 |
-
|
672 |
-
map_id = m.get_name()
|
673 |
-
js = f"""
|
674 |
-
<script>
|
675 |
-
(function() {{
|
676 |
-
var map = {map_id};
|
677 |
-
var velocityData = {velocity_json_str};
|
678 |
-
|
679 |
-
// Add Leaflet-Velocity layer for windy-style canvas particles
|
680 |
-
var velocityLayer = L.velocityLayer({{
|
681 |
-
data: velocityData,
|
682 |
-
displayValues: true,
|
683 |
-
displayOptions: {{
|
684 |
-
velocityType: "Wind",
|
685 |
-
position: "bottomright",
|
686 |
-
emptyString: "No wind data",
|
687 |
-
speedUnit: "m/s",
|
688 |
-
angleConvention: "bearingCW",
|
689 |
-
showCardinal: true
|
690 |
-
}},
|
691 |
-
velocityScale: 0.008, // tune visual speed (px per m/s)
|
692 |
-
opacity: 0.85,
|
693 |
-
maxVelocity: 25, // color scale domain upper bound (m/s)
|
694 |
-
particleMultiplier: 0.006, // particle density
|
695 |
-
lineWidth: 2,
|
696 |
-
colorScale: ["#ffffff", "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#fee090", "#fdae61", "#f46d43", "#d73027", "#a50026"]
|
697 |
-
}}).addTo(map);
|
698 |
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
}};
|
703 |
|
704 |
-
|
705 |
-
|
706 |
-
L.control.layers(null, overlayMaps, {{position: 'topright'}}).addTo(map);
|
707 |
-
map._layerControlAdded = true;
|
708 |
-
}}
|
709 |
-
}})();
|
710 |
-
</script>
|
711 |
-
"""
|
712 |
-
m.get_root().html.add_child(Element(js))
|
713 |
-
|
714 |
-
# Add layer control for base maps
|
715 |
-
folium.LayerControl().add_to(m)
|
716 |
-
|
717 |
-
return m._repr_html_()
|
718 |
-
|
719 |
-
except Exception as e:
|
720 |
-
return f"""
|
721 |
-
<div style="padding: 20px; background-color: #f8d7da; border: 1px solid #f5c6cb; border-radius: 8px;">
|
722 |
-
<h4>❌ Error Creating Wind Visualization</h4>
|
723 |
-
<p>Error: {str(e)}</p>
|
724 |
-
<p>Please try downloading the wind data again or check the console for detailed error information.</p>
|
725 |
-
</div>
|
726 |
-
"""
|
727 |
-
|
728 |
-
def get_wind_info(self):
|
729 |
-
"""Get information about current wind data"""
|
730 |
-
if not self.current_wind_data:
|
731 |
-
return "No wind data loaded. Please download wind data first."
|
732 |
|
733 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
734 |
|
735 |
-
|
736 |
-
|
737 |
-
|
|
|
738 |
|
739 |
-
|
740 |
-
header
|
741 |
-
|
742 |
-
|
743 |
-
dx = header['dx']
|
744 |
-
dy = header['dy']
|
745 |
-
|
746 |
-
info += f"\n**Grid Information:**\n"
|
747 |
-
info += f"• Resolution: {dx}° × {dy}° (~{dx*111:.0f}km spacing)\n"
|
748 |
-
info += f"• Grid size: {nx} × {ny} = {nx*ny:,} points\n"
|
749 |
-
info += f"• Coverage: {header['lo1']}° to {header['lo2']}°E, {header['la2']}° to {header['la1']}°N\n"
|
750 |
-
info += f"• Data source: ECMWF IFS Operational\n"
|
751 |
-
info += f"• Parameters: 10m U-wind (eastward) and V-wind (northward)\n"
|
752 |
|
753 |
-
return
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
map_html = app.create_wind_particle_map(app.current_velocity_json, region, particle_count)
|
773 |
-
wind_info = app.get_wind_info()
|
774 |
-
return map_html, wind_info
|
775 |
-
|
776 |
|
777 |
def create_gradio_interface():
|
778 |
-
"""Create
|
779 |
|
780 |
-
with gr.Blocks(title="
|
781 |
|
782 |
gr.Markdown("""
|
783 |
-
# 🌪️
|
784 |
-
##
|
785 |
|
786 |
**Features:**
|
787 |
-
-
|
788 |
-
- 🎨 Windy-style
|
789 |
-
-
|
790 |
-
-
|
791 |
-
- 🎯 Regional and global visualization options
|
792 |
""")
|
793 |
|
794 |
-
with gr.
|
795 |
-
|
796 |
-
|
797 |
-
with gr.Column(scale=1):
|
798 |
-
gr.Markdown("### ⚙️ Controls")
|
799 |
-
|
800 |
-
download_btn = gr.Button(
|
801 |
-
"📡 Download ECMWF Wind Data",
|
802 |
-
variant="primary",
|
803 |
-
size="lg"
|
804 |
-
)
|
805 |
-
|
806 |
-
region_choice = gr.Radio(
|
807 |
-
choices=["global", "north_america", "europe"],
|
808 |
-
value="global",
|
809 |
-
label="🗺️ Region"
|
810 |
-
)
|
811 |
-
|
812 |
-
particle_count = gr.Slider(
|
813 |
-
minimum=200,
|
814 |
-
maximum=2000,
|
815 |
-
value=1000,
|
816 |
-
step=100,
|
817 |
-
label="🔵 Particle Count"
|
818 |
-
)
|
819 |
-
|
820 |
-
create_map_btn = gr.Button(
|
821 |
-
"🎨 Create Wind Visualization",
|
822 |
-
variant="secondary",
|
823 |
-
size="lg"
|
824 |
-
)
|
825 |
-
|
826 |
-
gr.Markdown("### 📊 Data Information")
|
827 |
-
wind_info = gr.Markdown("No wind data loaded yet.")
|
828 |
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
837 |
wind_map = gr.HTML(
|
838 |
label="🌪️ Wind Particle Animation",
|
839 |
value="""
|
840 |
<div style="padding: 40px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
841 |
-
border-radius: 12px; text-align: center; color: white;
|
842 |
-
|
843 |
-
<
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
|
|
|
|
|
|
850 |
</div>
|
851 |
"""
|
852 |
)
|
853 |
-
|
854 |
-
# Event handlers
|
855 |
-
download_btn.click(
|
856 |
-
download_wind_data,
|
857 |
-
outputs=[download_status, gr.JSON(visible=False), gr.JSON(visible=False)]
|
858 |
-
)
|
859 |
-
|
860 |
-
create_map_btn.click(
|
861 |
-
create_wind_map,
|
862 |
-
inputs=[region_choice, particle_count],
|
863 |
-
outputs=[wind_map, wind_info]
|
864 |
-
)
|
865 |
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
- Real-time particle advection on HTML5 canvas
|
879 |
-
- Thousands of particles animated by wind field
|
880 |
-
- Color-coded by wind speed
|
881 |
-
- Interactive controls and wind speed display
|
882 |
-
|
883 |
-
2. **TimestampedGeoJSON Animation**
|
884 |
-
- Pre-computed particle trajectories
|
885 |
-
- Time slider controls
|
886 |
-
- Smooth animation transitions
|
887 |
-
- Compatible with Folium/Leaflet ecosystem
|
888 |
-
|
889 |
-
### 📊 Technical Implementation
|
890 |
-
|
891 |
-
**Data Processing:**
|
892 |
-
- Downloads ECMWF 10m U/V wind components in GRIB2 format
|
893 |
-
- Converts to grib2json-compatible velocity JSON
|
894 |
-
- Implements bilinear interpolation for smooth field sampling
|
895 |
-
|
896 |
-
**Particle Physics:**
|
897 |
-
- RK4 (Runge-Kutta 4th order) integration for accurate advection
|
898 |
-
- Proper coordinate system handling (geographic projections)
|
899 |
-
- Particle respawning and boundary management
|
900 |
-
|
901 |
-
**Visualization:**
|
902 |
-
- Leaflet-Velocity plugin for canvas rendering
|
903 |
-
- Folium TimestampedGeoJson for time-based animation
|
904 |
-
- Multiple map tile layers and interactive controls
|
905 |
-
|
906 |
-
### 🌪️ Inspired by Windy.com
|
907 |
-
|
908 |
-
This implementation follows the same principles used by professional weather visualization platforms:
|
909 |
-
- Vector field interpolation
|
910 |
-
- Massless particle advection
|
911 |
-
- Canvas-based rendering for performance
|
912 |
-
- Color coding by wind magnitude
|
913 |
-
- Time-based animation controls
|
914 |
-
|
915 |
-
### 🛠️ Technologies Used
|
916 |
-
|
917 |
-
- **ECMWF OpenData**: Real operational forecast data
|
918 |
-
- **Leaflet-Velocity**: Canvas particle rendering
|
919 |
-
- **Folium**: Python-Leaflet bridge
|
920 |
-
- **xarray/cfgrib**: GRIB data processing
|
921 |
-
- **Gradio**: Interactive web interface
|
922 |
-
|
923 |
-
### 📚 References
|
924 |
-
|
925 |
-
- [earth.nullschool.net](https://earth.nullschool.net/) - Original particle visualization inspiration
|
926 |
-
- [Leaflet-Velocity](https://github.com/onaci/leaflet-velocity) - Canvas particle system
|
927 |
-
- [ECMWF Open Data](https://www.ecmwf.int/en/forecasts/datasets/open-data) - Data source
|
928 |
-
- [Windy.com](https://www.windy.com/) - Visual style reference
|
929 |
-
""")
|
930 |
|
931 |
gr.Markdown("""
|
932 |
---
|
933 |
-
**🌪️
|
934 |
-
*
|
935 |
""")
|
936 |
|
937 |
return interface
|
938 |
|
939 |
-
|
940 |
if __name__ == "__main__":
|
941 |
interface = create_gradio_interface()
|
942 |
interface.launch(
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
"""
|
3 |
+
Fast ECMWF Wind Particle Visualization
|
4 |
+
Simplified version using only Leaflet-Velocity for maximum performance
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
"""
|
6 |
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
|
|
|
|
9 |
import requests
|
|
|
|
|
10 |
import json
|
|
|
|
|
|
|
|
|
11 |
import folium
|
|
|
12 |
from branca.element import Element
|
13 |
+
import tempfile
|
14 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
class FastWindVisualizer:
|
17 |
"""
|
18 |
+
Fast wind visualization using sample data and Leaflet-Velocity
|
19 |
+
Optimized for speed and simplicity
|
20 |
"""
|
21 |
|
22 |
def __init__(self):
|
23 |
+
self.sample_wind_url = "https://raw.githubusercontent.com/danwild/leaflet-velocity/master/demo/wind-global.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def create_wind_map(self, region="global", show_display=True):
|
26 |
+
"""Create fast Leaflet-Velocity wind map"""
|
27 |
|
28 |
+
# Set map parameters based on region
|
29 |
+
if region == "global":
|
30 |
+
center = [20, 0]
|
31 |
+
zoom = 2
|
32 |
+
elif region == "north_america":
|
33 |
+
center = [40, -100]
|
34 |
+
zoom = 3
|
35 |
+
elif region == "europe":
|
36 |
+
center = [50, 10]
|
37 |
+
zoom = 4
|
38 |
+
else:
|
39 |
+
center = [20, 0]
|
40 |
+
zoom = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Create map with dark theme for better particle visibility
|
43 |
+
m = folium.Map(
|
44 |
+
location=center,
|
45 |
+
tiles="CartoDB dark_matter",
|
46 |
+
zoom_start=zoom,
|
47 |
+
control_scale=True
|
48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
# Add alternative tile layers
|
51 |
+
folium.TileLayer(
|
52 |
+
tiles="CartoDB positron",
|
53 |
+
name="Light Theme",
|
54 |
+
control=True
|
55 |
+
).add_to(m)
|
56 |
|
57 |
+
folium.TileLayer(
|
58 |
+
tiles="OpenStreetMap",
|
59 |
+
name="Street Map",
|
60 |
+
control=True
|
61 |
+
).add_to(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Add Leaflet-Velocity plugin
|
64 |
+
plugin_js = """
|
65 |
+
<script src="https://unpkg.com/leaflet-velocity/dist/leaflet-velocity.min.js"></script>
|
|
|
|
|
|
|
66 |
"""
|
67 |
+
m.get_root().html.add_child(Element(plugin_js))
|
68 |
+
|
69 |
+
# Configure Leaflet-Velocity with sample wind data
|
70 |
+
map_id = m.get_name()
|
71 |
+
velocity_config = {
|
72 |
+
"velocityScale": 0.01,
|
73 |
+
"opacity": 0.9,
|
74 |
+
"maxVelocity": 25,
|
75 |
+
"particleMultiplier": 0.008,
|
76 |
+
"lineWidth": 2,
|
77 |
+
"colorScale": [
|
78 |
+
"#ffffff", "#4575b4", "#74add1", "#abd9e9",
|
79 |
+
"#e0f3f8", "#fee090", "#fdae61", "#f46d43",
|
80 |
+
"#d73027", "#a50026"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
]
|
82 |
+
}
|
83 |
+
|
84 |
+
if not show_display:
|
85 |
+
velocity_config["displayValues"] = False
|
86 |
+
velocity_config["displayOptions"] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
else:
|
88 |
+
velocity_config["displayValues"] = True
|
89 |
+
velocity_config["displayOptions"] = {
|
90 |
+
"velocityType": "Wind",
|
91 |
+
"position": "bottomright",
|
92 |
+
"emptyString": "No wind data",
|
93 |
+
"speedUnit": "m/s",
|
94 |
+
"angleConvention": "bearingCW",
|
95 |
+
"showCardinal": True
|
96 |
+
}
|
97 |
+
|
98 |
+
js_code = f"""
|
99 |
+
<script>
|
100 |
+
(function() {{
|
101 |
+
var map = {map_id};
|
102 |
+
|
103 |
+
// Fetch sample wind data
|
104 |
+
fetch("{self.sample_wind_url}")
|
105 |
+
.then(r => r.json())
|
106 |
+
.then(function(data) {{
|
107 |
+
var velocityLayer = L.velocityLayer({{
|
108 |
+
data: data,
|
109 |
+
displayValues: {str(velocity_config["displayValues"]).lower()},
|
110 |
+
displayOptions: {json.dumps(velocity_config["displayOptions"])},
|
111 |
+
velocityScale: {velocity_config["velocityScale"]},
|
112 |
+
opacity: {velocity_config["opacity"]},
|
113 |
+
maxVelocity: {velocity_config["maxVelocity"]},
|
114 |
+
particleMultiplier: {velocity_config["particleMultiplier"]},
|
115 |
+
lineWidth: {velocity_config["lineWidth"]},
|
116 |
+
colorScale: {json.dumps(velocity_config["colorScale"])}
|
117 |
+
}}).addTo(map);
|
118 |
+
|
119 |
+
// Add to layer control
|
120 |
+
var overlayMaps = {{
|
121 |
+
"Wind Particles": velocityLayer
|
122 |
+
}};
|
123 |
+
|
124 |
+
if (!map._layerControlAdded) {{
|
125 |
+
L.control.layers(null, overlayMaps, {{position: 'topright'}}).addTo(map);
|
126 |
+
map._layerControlAdded = true;
|
127 |
+
}}
|
128 |
+
|
129 |
+
console.log("Wind velocity layer loaded successfully");
|
130 |
+
}})
|
131 |
+
.catch(function(error) {{
|
132 |
+
console.error("Error loading wind data:", error);
|
133 |
+
}});
|
134 |
+
}})();
|
135 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
"""
|
137 |
+
m.get_root().html.add_child(Element(js_code))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
# Add layer control for base maps
|
140 |
+
folium.LayerControl().add_to(m)
|
141 |
+
|
142 |
+
return m._repr_html_()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
+
def create_custom_wind_data(self):
|
145 |
+
"""Create simple custom wind data for demonstration"""
|
146 |
+
# Simple synthetic wind data for demonstration
|
147 |
+
# This simulates a basic circulation pattern
|
148 |
+
nx, ny = 36, 18 # 10-degree resolution
|
149 |
+
lons = np.linspace(-180, 170, nx)
|
150 |
+
lats = np.linspace(80, -80, ny)
|
151 |
+
|
152 |
+
# Create simple synthetic wind pattern
|
153 |
+
u_data = []
|
154 |
+
v_data = []
|
155 |
+
|
156 |
+
for j, lat in enumerate(lats):
|
157 |
+
for i, lon in enumerate(lons):
|
158 |
+
# Simple circulation pattern
|
159 |
+
u = 5 * np.sin(np.radians(lat)) * np.cos(np.radians(lon/2))
|
160 |
+
v = 3 * np.cos(np.radians(lat)) * np.sin(np.radians(lon/2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
# Add some randomness
|
163 |
+
u += np.random.normal(0, 1)
|
164 |
+
v += np.random.normal(0, 1)
|
|
|
165 |
|
166 |
+
u_data.append(u)
|
167 |
+
v_data.append(v)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
+
# Create grib2json-style format
|
170 |
+
header = {
|
171 |
+
"discipline": 0,
|
172 |
+
"parameterCategory": 2,
|
173 |
+
"nx": nx,
|
174 |
+
"ny": ny,
|
175 |
+
"lo1": -180,
|
176 |
+
"la1": 80,
|
177 |
+
"lo2": 170,
|
178 |
+
"la2": -80,
|
179 |
+
"dx": 10,
|
180 |
+
"dy": 10,
|
181 |
+
"refTime": "2024-08-14 12:00:00"
|
182 |
+
}
|
183 |
|
184 |
+
u_record = {
|
185 |
+
"header": {**header, "parameterNumber": 2, "parameterName": "UGRD"},
|
186 |
+
"data": u_data
|
187 |
+
}
|
188 |
|
189 |
+
v_record = {
|
190 |
+
"header": {**header, "parameterNumber": 3, "parameterName": "VGRD"},
|
191 |
+
"data": v_data
|
192 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
+
return [u_record, v_record]
|
195 |
+
|
196 |
+
# Initialize the visualizer
|
197 |
+
visualizer = FastWindVisualizer()
|
198 |
+
|
199 |
+
def create_wind_visualization(region, show_display):
|
200 |
+
"""Gradio function to create wind visualization"""
|
201 |
+
try:
|
202 |
+
map_html = visualizer.create_wind_map(region, show_display)
|
203 |
+
status = f"✅ Wind visualization created for {region.replace('_', ' ').title()}"
|
204 |
+
return map_html, status
|
205 |
+
except Exception as e:
|
206 |
+
error_html = f"""
|
207 |
+
<div style="padding: 20px; background-color: #f8d7da; border-radius: 8px;">
|
208 |
+
<h4>❌ Error Creating Visualization</h4>
|
209 |
+
<p>Error: {str(e)}</p>
|
210 |
+
</div>
|
211 |
+
"""
|
212 |
+
return error_html, f"❌ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
213 |
|
214 |
def create_gradio_interface():
|
215 |
+
"""Create fast Gradio interface"""
|
216 |
|
217 |
+
with gr.Blocks(title="Fast Wind Particle Visualization", theme=gr.themes.Soft()) as interface:
|
218 |
|
219 |
gr.Markdown("""
|
220 |
+
# 🌪️ Fast Wind Particle Visualization
|
221 |
+
## Leaflet-Velocity wind particle animation - optimized for speed
|
222 |
|
223 |
**Features:**
|
224 |
+
- ⚡ **Ultra-fast loading** with optimized dependencies
|
225 |
+
- 🎨 **Windy-style particles** using Leaflet-Velocity canvas rendering
|
226 |
+
- 🗺️ **Multiple regions** and map themes
|
227 |
+
- 🔧 **Configurable display** options
|
|
|
228 |
""")
|
229 |
|
230 |
+
with gr.Row():
|
231 |
+
with gr.Column(scale=1):
|
232 |
+
gr.Markdown("### ⚙️ Controls")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
+
region_choice = gr.Radio(
|
235 |
+
choices=["global", "north_america", "europe"],
|
236 |
+
value="global",
|
237 |
+
label="🗺️ Region"
|
238 |
+
)
|
239 |
+
|
240 |
+
show_display = gr.Checkbox(
|
241 |
+
value=True,
|
242 |
+
label="📊 Show Wind Speed Display"
|
243 |
+
)
|
244 |
+
|
245 |
+
create_btn = gr.Button(
|
246 |
+
"🌪️ Create Wind Visualization",
|
247 |
+
variant="primary",
|
248 |
+
size="lg"
|
249 |
+
)
|
250 |
+
|
251 |
+
status_output = gr.Textbox(
|
252 |
+
label="📋 Status",
|
253 |
+
lines=3
|
254 |
+
)
|
255 |
+
|
256 |
+
gr.Markdown("""
|
257 |
+
### 🎯 Quick Start
|
258 |
+
1. Select a region
|
259 |
+
2. Toggle wind speed display
|
260 |
+
3. Click "Create Wind Visualization"
|
261 |
+
4. Watch the windy-style particles!
|
262 |
+
|
263 |
+
### ⚡ Performance
|
264 |
+
- Minimal dependencies for fast loading
|
265 |
+
- Canvas-based particle rendering
|
266 |
+
- Sample global wind data
|
267 |
+
- Optimized for Hugging Face Spaces
|
268 |
+
""")
|
269 |
+
|
270 |
+
with gr.Column(scale=2):
|
271 |
wind_map = gr.HTML(
|
272 |
label="🌪️ Wind Particle Animation",
|
273 |
value="""
|
274 |
<div style="padding: 40px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
275 |
+
border-radius: 12px; text-align: center; color: white; min-height: 500px;
|
276 |
+
display: flex; align-items: center; justify-content: center;">
|
277 |
+
<div>
|
278 |
+
<h3>🌪️ Wind Particle Visualization</h3>
|
279 |
+
<p>Click "Create Wind Visualization" to see windy-style particle animation</p>
|
280 |
+
<p style="font-size: 14px; opacity: 0.8;">
|
281 |
+
• Ultra-fast Leaflet-Velocity rendering<br>
|
282 |
+
• Canvas-based particle system<br>
|
283 |
+
• Multiple map themes<br>
|
284 |
+
• Real-time wind flow visualization
|
285 |
+
</p>
|
286 |
+
</div>
|
287 |
</div>
|
288 |
"""
|
289 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
|
291 |
+
# Event handler
|
292 |
+
create_btn.click(
|
293 |
+
create_wind_visualization,
|
294 |
+
inputs=[region_choice, show_display],
|
295 |
+
outputs=[wind_map, status_output]
|
296 |
+
)
|
297 |
+
|
298 |
+
# Auto-load global map on startup
|
299 |
+
interface.load(
|
300 |
+
lambda: create_wind_visualization("global", True),
|
301 |
+
outputs=[wind_map, status_output]
|
302 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
|
304 |
gr.Markdown("""
|
305 |
---
|
306 |
+
**🌪️ Fast Wind Particle Visualization - Powered by Leaflet-Velocity**
|
307 |
+
*Optimized for speed and performance on Hugging Face Spaces*
|
308 |
""")
|
309 |
|
310 |
return interface
|
311 |
|
|
|
312 |
if __name__ == "__main__":
|
313 |
interface = create_gradio_interface()
|
314 |
interface.launch(
|
requirements.txt
CHANGED
@@ -1,13 +1,5 @@
|
|
1 |
gradio==4.44.0
|
2 |
folium==0.17.0
|
3 |
-
xarray==2024.7.0
|
4 |
-
cfgrib==0.9.11.0
|
5 |
-
ecmwf-opendata==0.3.22
|
6 |
requests==2.32.3
|
7 |
numpy==1.26.4
|
8 |
-
|
9 |
-
matplotlib==3.9.2
|
10 |
-
plotly==5.22.0
|
11 |
-
eccodes==1.7.0
|
12 |
-
branca==0.7.2
|
13 |
-
jinja2==3.1.4
|
|
|
1 |
gradio==4.44.0
|
2 |
folium==0.17.0
|
|
|
|
|
|
|
3 |
requests==2.32.3
|
4 |
numpy==1.26.4
|
5 |
+
branca==0.7.2
|
|
|
|
|
|
|
|
|
|