Spaces:
Sleeping
Complete ECMWF dual wind system with enhanced color contrast
Browse files🌍 REAL ECMWF DATA INTEGRATION:
- Restored full ECMWF data downloading for both 10m and 100m winds
- Downloads real 10u, 10v, 100u, 100v components from ECMWF operational forecasts
- Processes GRIB files and converts to leaflet-velocity JSON format
- Falls back to synthetic data if ECMWF data unavailable
🎨 ENHANCED COLOR CONTRAST:
- DARK colors on LIGHT theme for maximum contrast
- LIGHT colors on DARK theme for maximum visibility
- 10m winds: Dark blue (#000066-#ccccff) vs Light blue (#ccccff-#ffffff)
- 100m winds: Dark red (#4c0000-#ff9999) vs Light red (#ff9999-#ffcccc)
🎛️ DUAL WIND TOGGLE SYSTEM:
- Independent toggle controls for 10m and 100m wind layers
- Real-time switching between different wind altitudes
- Theme-aware color adaptation on the fly
📦 UPDATED DEPENDENCIES:
- Added back xarray, cfgrib, ecmwf-opendata for GRIB processing
- Restored complete ECMWF data processing pipeline
This provides the complete solution: real ECMWF data + dual toggles + perfect contrast!
- app.py +384 -120
- requirements.txt +6 -1
@@ -1,16 +1,381 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
"""
|
3 |
-
|
4 |
-
|
5 |
"""
|
6 |
|
7 |
import gradio as gr
|
8 |
import folium
|
9 |
from branca.element import Element
|
10 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def create_wind_map(region="global"):
|
13 |
-
"""Create Leaflet-Velocity wind map with
|
14 |
|
15 |
# Set map parameters based on region
|
16 |
if region == "global":
|
@@ -43,112 +408,11 @@ def create_wind_map(region="global"):
|
|
43 |
control=True
|
44 |
).add_to(m)
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
"features": [
|
50 |
-
{
|
51 |
-
"type": "Feature",
|
52 |
-
"properties": {},
|
53 |
-
"geometry": {
|
54 |
-
"type": "Point",
|
55 |
-
"coordinates": [0, 0]
|
56 |
-
}
|
57 |
-
}
|
58 |
-
]
|
59 |
-
}
|
60 |
|
61 |
-
|
62 |
-
wind_data_10m = [
|
63 |
-
{
|
64 |
-
"header": {
|
65 |
-
"discipline": 0,
|
66 |
-
"parameterCategory": 2,
|
67 |
-
"parameterNumber": 2,
|
68 |
-
"parameterName": "UGRD",
|
69 |
-
"nx": 36,
|
70 |
-
"ny": 18,
|
71 |
-
"lo1": -180,
|
72 |
-
"la1": 80,
|
73 |
-
"lo2": 170,
|
74 |
-
"la2": -80,
|
75 |
-
"dx": 10,
|
76 |
-
"dy": 10
|
77 |
-
},
|
78 |
-
"data": [
|
79 |
-
# Simple U wind pattern (36x18 = 648 points)
|
80 |
-
*([5, 8, 3, -2, -5, -8, -3, 2] * 9 + [0] * 72) * 4,
|
81 |
-
*([2, 5, 8, 3, -2, -5, -8, -3] * 9 + [0] * 72) * 4
|
82 |
-
][:648]
|
83 |
-
},
|
84 |
-
{
|
85 |
-
"header": {
|
86 |
-
"discipline": 0,
|
87 |
-
"parameterCategory": 2,
|
88 |
-
"parameterNumber": 3,
|
89 |
-
"parameterName": "VGRD",
|
90 |
-
"nx": 36,
|
91 |
-
"ny": 18,
|
92 |
-
"lo1": -180,
|
93 |
-
"la1": 80,
|
94 |
-
"lo2": 170,
|
95 |
-
"la2": -80,
|
96 |
-
"dx": 10,
|
97 |
-
"dy": 10
|
98 |
-
},
|
99 |
-
"data": [
|
100 |
-
# Simple V wind pattern (36x18 = 648 points)
|
101 |
-
*([3, -2, -5, -8, -3, 2, 5, 8] * 9 + [0] * 72) * 4,
|
102 |
-
*([8, 3, -2, -5, -8, -3, 2, 5] * 9 + [0] * 72) * 4
|
103 |
-
][:648]
|
104 |
-
}
|
105 |
-
]
|
106 |
-
|
107 |
-
# Create 100m wind data (stronger winds at higher altitude)
|
108 |
-
wind_data_100m = [
|
109 |
-
{
|
110 |
-
"header": {
|
111 |
-
"discipline": 0,
|
112 |
-
"parameterCategory": 2,
|
113 |
-
"parameterNumber": 2,
|
114 |
-
"parameterName": "UGRD",
|
115 |
-
"nx": 36,
|
116 |
-
"ny": 18,
|
117 |
-
"lo1": -180,
|
118 |
-
"la1": 80,
|
119 |
-
"lo2": 170,
|
120 |
-
"la2": -80,
|
121 |
-
"dx": 10,
|
122 |
-
"dy": 10
|
123 |
-
},
|
124 |
-
"data": [
|
125 |
-
# Stronger U wind pattern at 100m (36x18 = 648 points)
|
126 |
-
*([8, 12, 6, -3, -8, -12, -6, 3] * 9 + [0] * 72) * 4,
|
127 |
-
*([3, 8, 12, 6, -3, -8, -12, -6] * 9 + [0] * 72) * 4
|
128 |
-
][:648]
|
129 |
-
},
|
130 |
-
{
|
131 |
-
"header": {
|
132 |
-
"discipline": 0,
|
133 |
-
"parameterCategory": 2,
|
134 |
-
"parameterNumber": 3,
|
135 |
-
"parameterName": "VGRD",
|
136 |
-
"nx": 36,
|
137 |
-
"ny": 18,
|
138 |
-
"lo1": -180,
|
139 |
-
"la1": 80,
|
140 |
-
"lo2": 170,
|
141 |
-
"la2": -80,
|
142 |
-
"dx": 10,
|
143 |
-
"dy": 10
|
144 |
-
},
|
145 |
-
"data": [
|
146 |
-
# Stronger V wind pattern at 100m (36x18 = 648 points)
|
147 |
-
*([6, -3, -8, -12, -6, 3, 8, 12] * 9 + [0] * 72) * 4,
|
148 |
-
*([12, 6, -3, -8, -12, -6, 3, 8] * 9 + [0] * 72) * 4
|
149 |
-
][:648]
|
150 |
-
}
|
151 |
-
]
|
152 |
|
153 |
# Add Leaflet-Velocity from CDN
|
154 |
velocity_css = """
|
@@ -208,31 +472,31 @@ def create_wind_map(region="global"):
|
|
208 |
if (windType === '100m') {{
|
209 |
// Red color scheme for 100m winds
|
210 |
if (currentTileLayer === 'light') {{
|
211 |
-
//
|
212 |
return [
|
213 |
-
"#
|
214 |
-
"#
|
215 |
];
|
216 |
}} else {{
|
217 |
-
//
|
218 |
return [
|
219 |
-
"#
|
220 |
-
"#
|
221 |
];
|
222 |
}}
|
223 |
}} else {{
|
224 |
// Blue color scheme for 10m winds
|
225 |
if (currentTileLayer === 'light') {{
|
226 |
-
//
|
227 |
return [
|
228 |
-
"#
|
229 |
-
"#
|
230 |
];
|
231 |
}} else {{
|
232 |
-
//
|
233 |
return [
|
234 |
-
"#
|
235 |
-
"#
|
236 |
];
|
237 |
}}
|
238 |
}}
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
"""
|
3 |
+
ECMWF Real Wind Particle Visualization with Dual Layers
|
4 |
+
Downloads current ECMWF 10m and 100m wind data and visualizes with particles
|
5 |
"""
|
6 |
|
7 |
import gradio as gr
|
8 |
import folium
|
9 |
from branca.element import Element
|
10 |
import json
|
11 |
+
import sys
|
12 |
+
import requests
|
13 |
+
import numpy as np
|
14 |
+
import xarray as xr
|
15 |
+
import tempfile
|
16 |
+
import os
|
17 |
+
from datetime import datetime, timedelta
|
18 |
+
import warnings
|
19 |
+
|
20 |
+
warnings.filterwarnings('ignore')
|
21 |
+
|
22 |
+
# Import ECMWF OpenData client
|
23 |
+
try:
|
24 |
+
from ecmwf.opendata import Client as OpenDataClient
|
25 |
+
OPENDATA_AVAILABLE = True
|
26 |
+
except ImportError:
|
27 |
+
OPENDATA_AVAILABLE = False
|
28 |
+
|
29 |
+
def log_step(step, message):
|
30 |
+
"""Log each step with clear formatting"""
|
31 |
+
print(f"🔄 STEP {step}: {message}")
|
32 |
+
sys.stdout.flush()
|
33 |
+
|
34 |
+
class ECMWFWindDataProcessor:
|
35 |
+
"""Process real ECMWF wind data for particle visualization"""
|
36 |
+
|
37 |
+
def __init__(self):
|
38 |
+
self.temp_dir = tempfile.mkdtemp()
|
39 |
+
self.client = None
|
40 |
+
if OPENDATA_AVAILABLE:
|
41 |
+
try:
|
42 |
+
self.client = OpenDataClient()
|
43 |
+
except:
|
44 |
+
self.client = None
|
45 |
+
|
46 |
+
# AWS S3 direct access URLs for ECMWF open data
|
47 |
+
self.aws_base_url = "https://ecmwf-forecasts.s3.eu-central-1.amazonaws.com"
|
48 |
+
|
49 |
+
def get_latest_forecast_info(self):
|
50 |
+
"""Get the latest available forecast run information"""
|
51 |
+
try:
|
52 |
+
# ECMWF runs at 00, 06, 12, 18 UTC
|
53 |
+
now = datetime.utcnow()
|
54 |
+
|
55 |
+
# Find the most recent model run (data available 7-9 hours after run time)
|
56 |
+
for hours_back in range(4, 24, 6): # Check recent runs
|
57 |
+
test_time = now - timedelta(hours=hours_back)
|
58 |
+
|
59 |
+
# Round to nearest 6-hour cycle
|
60 |
+
run_hour = (test_time.hour // 6) * 6
|
61 |
+
run_time = test_time.replace(hour=run_hour, minute=0, second=0, microsecond=0)
|
62 |
+
|
63 |
+
date_str = run_time.strftime("%Y%m%d")
|
64 |
+
time_str = f"{run_hour:02d}"
|
65 |
+
|
66 |
+
return date_str, time_str, run_time
|
67 |
+
|
68 |
+
# Fallback
|
69 |
+
return now.strftime("%Y%m%d"), "12", now
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
# Emergency fallback
|
73 |
+
now = datetime.utcnow()
|
74 |
+
return now.strftime("%Y%m%d"), "12", now
|
75 |
+
|
76 |
+
def download_wind_component(self, parameter="10u", step=0, max_retries=3):
|
77 |
+
"""Download ECMWF wind component data (10u, 10v, 100u, 100v)"""
|
78 |
+
|
79 |
+
date_str, time_str, run_time = self.get_latest_forecast_info()
|
80 |
+
|
81 |
+
# Method 1: Try ecmwf-opendata client (most reliable)
|
82 |
+
if OPENDATA_AVAILABLE and self.client:
|
83 |
+
try:
|
84 |
+
filename = os.path.join(self.temp_dir, f'ecmwf_{parameter}_{step}h_{datetime.now().strftime("%Y%m%d_%H%M%S")}.grib')
|
85 |
+
|
86 |
+
log_step("DOWNLOAD", f"Downloading {parameter} component via ECMWF client...")
|
87 |
+
|
88 |
+
self.client.retrieve(
|
89 |
+
type="fc", # forecast
|
90 |
+
param=parameter, # 10u, 10v, 100u, 100v
|
91 |
+
step=step, # forecast hour
|
92 |
+
target=filename
|
93 |
+
)
|
94 |
+
|
95 |
+
if os.path.exists(filename) and os.path.getsize(filename) > 1000:
|
96 |
+
log_step("SUCCESS", f"Downloaded {parameter} component ({os.path.getsize(filename)} bytes)")
|
97 |
+
return filename, f"✅ ECMWF {parameter} data downloaded successfully!\\nRun: {date_str} {time_str}z, Step: +{step}h"
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
log_step("ERROR", f"Client method failed: {str(e)}")
|
101 |
+
|
102 |
+
return None, f"❌ Unable to download ECMWF {parameter} data"
|
103 |
+
|
104 |
+
def extract_wind_data_from_grib(self, filename, parameter):
|
105 |
+
"""Extract wind data from GRIB file and return as array"""
|
106 |
+
try:
|
107 |
+
log_step("EXTRACT", f"Processing GRIB file for {parameter}...")
|
108 |
+
|
109 |
+
# Open the GRIB file with xarray
|
110 |
+
try:
|
111 |
+
ds = xr.open_dataset(filename, engine='cfgrib', backend_kwargs={'indexpath': ''})
|
112 |
+
except:
|
113 |
+
ds = xr.open_dataset(filename, engine='cfgrib')
|
114 |
+
|
115 |
+
# Find the right variable
|
116 |
+
data_vars = list(ds.data_vars.keys())
|
117 |
+
if not data_vars:
|
118 |
+
return None, None, None, "No data variables found in file"
|
119 |
+
|
120 |
+
data_var = data_vars[0]
|
121 |
+
data = ds[data_var]
|
122 |
+
|
123 |
+
# Handle coordinates
|
124 |
+
if 'latitude' in ds.coords:
|
125 |
+
lats = ds.latitude.values
|
126 |
+
lons = ds.longitude.values
|
127 |
+
elif 'lat' in ds.coords:
|
128 |
+
lats = ds.lat.values
|
129 |
+
lons = ds.lon.values
|
130 |
+
else:
|
131 |
+
return None, None, None, "Could not find latitude/longitude coordinates"
|
132 |
+
|
133 |
+
# Get the data values (select first time step if multiple)
|
134 |
+
if 'time' in data.dims and len(data.time) > 1:
|
135 |
+
values = data.isel(time=0).values
|
136 |
+
elif 'valid_time' in data.dims:
|
137 |
+
values = data.isel(valid_time=0).values
|
138 |
+
else:
|
139 |
+
values = data.values
|
140 |
+
|
141 |
+
# Handle 3D data (select first level if needed)
|
142 |
+
if values.ndim > 2:
|
143 |
+
values = values[0]
|
144 |
+
|
145 |
+
log_step("SUCCESS", f"Extracted {parameter}: {values.shape} grid, lat range: {lats.min():.1f} to {lats.max():.1f}")
|
146 |
+
|
147 |
+
ds.close()
|
148 |
+
return lats, lons, values, "Success"
|
149 |
+
|
150 |
+
except Exception as e:
|
151 |
+
return None, None, None, f"Error extracting data: {str(e)}"
|
152 |
+
|
153 |
+
def convert_to_wind_json(self, u_lats, u_lons, u_values, v_lats, v_lons, v_values, wind_level="10m"):
|
154 |
+
"""Convert ECMWF wind components to leaflet-velocity JSON format"""
|
155 |
+
try:
|
156 |
+
log_step("CONVERT", f"Converting ECMWF data to wind visualization format for {wind_level}...")
|
157 |
+
|
158 |
+
# Ensure grids match
|
159 |
+
if not (np.array_equal(u_lats, v_lats) and np.array_equal(u_lons, v_lons)):
|
160 |
+
log_step("WARNING", "U and V grids don't match exactly, using U grid as reference")
|
161 |
+
|
162 |
+
# Use U component grid as reference
|
163 |
+
lats = u_lats
|
164 |
+
lons = u_lons
|
165 |
+
|
166 |
+
# Ensure lats are in descending order (North to South) for leaflet-velocity
|
167 |
+
if lats[0] < lats[-1]:
|
168 |
+
lats = lats[::-1]
|
169 |
+
u_values = u_values[::-1, :]
|
170 |
+
v_values = v_values[::-1, :]
|
171 |
+
|
172 |
+
# Convert to lists and flatten in row-major order
|
173 |
+
u_data = u_values.flatten().tolist()
|
174 |
+
v_data = v_values.flatten().tolist()
|
175 |
+
|
176 |
+
# Replace any NaN values with 0
|
177 |
+
u_data = [0.0 if np.isnan(x) else float(x) for x in u_data]
|
178 |
+
v_data = [0.0 if np.isnan(x) else float(x) for x in v_data]
|
179 |
+
|
180 |
+
# Create grid info
|
181 |
+
ny, nx = u_values.shape
|
182 |
+
lo1 = float(lons[0])
|
183 |
+
lo2 = float(lons[-1])
|
184 |
+
la1 = float(lats[0]) # North (highest)
|
185 |
+
la2 = float(lats[-1]) # South (lowest)
|
186 |
+
dx = float(lons[1] - lons[0])
|
187 |
+
dy = float(lats[0] - lats[1]) # Should be positive since lats are descending
|
188 |
+
|
189 |
+
current_time = datetime.utcnow()
|
190 |
+
ref_time = current_time.strftime("%Y-%m-%d %H:00:00")
|
191 |
+
|
192 |
+
# Create leaflet-velocity compatible JSON structure
|
193 |
+
wind_data = [
|
194 |
+
{
|
195 |
+
"header": {
|
196 |
+
"discipline": 0,
|
197 |
+
"parameterCategory": 2,
|
198 |
+
"parameterNumber": 2,
|
199 |
+
"parameterName": "UGRD",
|
200 |
+
"parameterNumberName": "eastward_wind",
|
201 |
+
"nx": nx,
|
202 |
+
"ny": ny,
|
203 |
+
"lo1": lo1,
|
204 |
+
"la1": la1,
|
205 |
+
"lo2": lo2,
|
206 |
+
"la2": la2,
|
207 |
+
"dx": dx,
|
208 |
+
"dy": dy,
|
209 |
+
"refTime": ref_time
|
210 |
+
},
|
211 |
+
"data": u_data
|
212 |
+
},
|
213 |
+
{
|
214 |
+
"header": {
|
215 |
+
"discipline": 0,
|
216 |
+
"parameterCategory": 2,
|
217 |
+
"parameterNumber": 3,
|
218 |
+
"parameterName": "VGRD",
|
219 |
+
"parameterNumberName": "northward_wind",
|
220 |
+
"nx": nx,
|
221 |
+
"ny": ny,
|
222 |
+
"lo1": lo1,
|
223 |
+
"la1": la1,
|
224 |
+
"lo2": lo2,
|
225 |
+
"la2": la2,
|
226 |
+
"dx": dx,
|
227 |
+
"dy": dy,
|
228 |
+
"refTime": ref_time
|
229 |
+
},
|
230 |
+
"data": v_data
|
231 |
+
}
|
232 |
+
]
|
233 |
+
|
234 |
+
log_step("SUCCESS", f"Converted {wind_level} to wind JSON: {nx}x{ny} grid, {len(u_data)} points each")
|
235 |
+
|
236 |
+
return wind_data, f"Successfully converted ECMWF {wind_level} data to wind visualization format"
|
237 |
+
|
238 |
+
except Exception as e:
|
239 |
+
return None, f"Error converting data: {str(e)}"
|
240 |
+
|
241 |
+
def fetch_real_ecmwf_wind_data():
|
242 |
+
"""Download and process real ECMWF 10m and 100m wind data"""
|
243 |
+
log_step("WIND-1", "🌍 Fetching REAL ECMWF wind data (10m and 100m)...")
|
244 |
+
|
245 |
+
processor = ECMWFWindDataProcessor()
|
246 |
+
|
247 |
+
try:
|
248 |
+
# Download 10m wind components
|
249 |
+
log_step("WIND-2", "Downloading 10m U wind component...")
|
250 |
+
u10_file, u10_msg = processor.download_wind_component("10u", step=0)
|
251 |
+
|
252 |
+
log_step("WIND-3", "Downloading 10m V wind component...")
|
253 |
+
v10_file, v10_msg = processor.download_wind_component("10v", step=0)
|
254 |
+
|
255 |
+
# Download 100m wind components
|
256 |
+
log_step("WIND-4", "Downloading 100m U wind component...")
|
257 |
+
u100_file, u100_msg = processor.download_wind_component("100u", step=0)
|
258 |
+
|
259 |
+
log_step("WIND-5", "Downloading 100m V wind component...")
|
260 |
+
v100_file, v100_msg = processor.download_wind_component("100v", step=0)
|
261 |
+
|
262 |
+
# Process 10m data
|
263 |
+
wind_data_10m = None
|
264 |
+
if u10_file and v10_file:
|
265 |
+
log_step("WIND-6", "Processing 10m wind data...")
|
266 |
+
u10_lats, u10_lons, u10_values, u10_status = processor.extract_wind_data_from_grib(u10_file, "10u")
|
267 |
+
v10_lats, v10_lons, v10_values, v10_status = processor.extract_wind_data_from_grib(v10_file, "10v")
|
268 |
+
|
269 |
+
if u10_values is not None and v10_values is not None:
|
270 |
+
wind_data_10m, convert_msg = processor.convert_to_wind_json(
|
271 |
+
u10_lats, u10_lons, u10_values, v10_lats, v10_lons, v10_values, "10m"
|
272 |
+
)
|
273 |
+
|
274 |
+
# Process 100m data
|
275 |
+
wind_data_100m = None
|
276 |
+
if u100_file and v100_file:
|
277 |
+
log_step("WIND-7", "Processing 100m wind data...")
|
278 |
+
u100_lats, u100_lons, u100_values, u100_status = processor.extract_wind_data_from_grib(u100_file, "100u")
|
279 |
+
v100_lats, v100_lons, v100_values, v100_status = processor.extract_wind_data_from_grib(v100_file, "100v")
|
280 |
+
|
281 |
+
if u100_values is not None and v100_values is not None:
|
282 |
+
wind_data_100m, convert_msg = processor.convert_to_wind_json(
|
283 |
+
u100_lats, u100_lons, u100_values, v100_lats, v100_lons, v100_values, "100m"
|
284 |
+
)
|
285 |
+
|
286 |
+
# Return real data if available, otherwise fallback
|
287 |
+
if wind_data_10m is None:
|
288 |
+
wind_data_10m = generate_synthetic_wind_data("10m")
|
289 |
+
if wind_data_100m is None:
|
290 |
+
wind_data_100m = generate_synthetic_wind_data("100m")
|
291 |
+
|
292 |
+
log_step("WIND-8", f"✅ SUCCESS: Wind data ready!")
|
293 |
+
|
294 |
+
return wind_data_10m, wind_data_100m
|
295 |
+
|
296 |
+
except Exception as e:
|
297 |
+
log_step("WIND-ERROR", f"Failed to fetch real wind data: {str(e)}")
|
298 |
+
log_step("WIND-FALLBACK", "Falling back to synthetic data...")
|
299 |
+
|
300 |
+
# Fallback to synthetic data
|
301 |
+
return generate_synthetic_wind_data("10m"), generate_synthetic_wind_data("100m")
|
302 |
+
|
303 |
+
def generate_synthetic_wind_data(wind_level="10m"):
|
304 |
+
"""Generate synthetic wind data as fallback"""
|
305 |
+
log_step("GEN-1", f"Generating synthetic {wind_level} wind data...")
|
306 |
+
|
307 |
+
# Basic global grid
|
308 |
+
nx, ny = 72, 36
|
309 |
+
lon_min, lon_max = -180, 175
|
310 |
+
lat_min, lat_max = -85, 85
|
311 |
+
|
312 |
+
lons = np.linspace(lon_min, lon_max, nx)
|
313 |
+
lats = np.linspace(lat_max, lat_min, ny)
|
314 |
+
|
315 |
+
u_data = []
|
316 |
+
v_data = []
|
317 |
+
|
318 |
+
# Adjust wind strength based on level
|
319 |
+
strength_multiplier = 1.5 if wind_level == "100m" else 1.0
|
320 |
+
|
321 |
+
for j, lat in enumerate(lats):
|
322 |
+
for i, lon in enumerate(lons):
|
323 |
+
# Simple wind pattern with different strength for different levels
|
324 |
+
u = (10 * np.sin(np.radians(lon/2)) + np.random.normal(0, 3)) * strength_multiplier
|
325 |
+
v = (5 * np.cos(np.radians(lat)) + np.random.normal(0, 2)) * strength_multiplier
|
326 |
+
|
327 |
+
u_data.append(round(u, 2))
|
328 |
+
v_data.append(round(v, 2))
|
329 |
+
|
330 |
+
current_time = datetime.utcnow()
|
331 |
+
ref_time = current_time.strftime("%Y-%m-%d %H:00:00")
|
332 |
+
|
333 |
+
wind_data = [
|
334 |
+
{
|
335 |
+
"header": {
|
336 |
+
"discipline": 0,
|
337 |
+
"parameterCategory": 2,
|
338 |
+
"parameterNumber": 2,
|
339 |
+
"parameterName": "UGRD",
|
340 |
+
"parameterNumberName": "eastward_wind",
|
341 |
+
"nx": nx,
|
342 |
+
"ny": ny,
|
343 |
+
"lo1": lon_min,
|
344 |
+
"la1": lat_max,
|
345 |
+
"lo2": lon_max,
|
346 |
+
"la2": lat_min,
|
347 |
+
"dx": 5.0,
|
348 |
+
"dy": 5.0,
|
349 |
+
"refTime": ref_time
|
350 |
+
},
|
351 |
+
"data": u_data
|
352 |
+
},
|
353 |
+
{
|
354 |
+
"header": {
|
355 |
+
"discipline": 0,
|
356 |
+
"parameterCategory": 2,
|
357 |
+
"parameterNumber": 3,
|
358 |
+
"parameterName": "VGRD",
|
359 |
+
"parameterNumberName": "northward_wind",
|
360 |
+
"nx": nx,
|
361 |
+
"ny": ny,
|
362 |
+
"lo1": lon_min,
|
363 |
+
"la1": lat_max,
|
364 |
+
"lo2": lon_max,
|
365 |
+
"la2": lat_min,
|
366 |
+
"dx": 5.0,
|
367 |
+
"dy": 5.0,
|
368 |
+
"refTime": ref_time
|
369 |
+
},
|
370 |
+
"data": v_data
|
371 |
+
}
|
372 |
+
]
|
373 |
+
|
374 |
+
log_step("GEN-2", f"Generated synthetic {wind_level} wind data: {len(u_data)} points")
|
375 |
+
return wind_data
|
376 |
|
377 |
def create_wind_map(region="global"):
|
378 |
+
"""Create Leaflet-Velocity wind map with real ECMWF data"""
|
379 |
|
380 |
# Set map parameters based on region
|
381 |
if region == "global":
|
|
|
408 |
control=True
|
409 |
).add_to(m)
|
410 |
|
411 |
+
# Fetch real ECMWF wind data for both levels
|
412 |
+
log_step(5, "Fetching real ECMWF wind data...")
|
413 |
+
wind_data_10m, wind_data_100m = fetch_real_ecmwf_wind_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
|
415 |
+
log_step(6, f"Wind data ready: 10m={len(wind_data_10m)} components, 100m={len(wind_data_100m)} components")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
|
417 |
# Add Leaflet-Velocity from CDN
|
418 |
velocity_css = """
|
|
|
472 |
if (windType === '100m') {{
|
473 |
// Red color scheme for 100m winds
|
474 |
if (currentTileLayer === 'light') {{
|
475 |
+
// DARK red colors for light theme (maximum contrast on white)
|
476 |
return [
|
477 |
+
"#4c0000", "#660000", "#800000", "#990000", "#b30000",
|
478 |
+
"#cc0000", "#e60000", "#ff0000", "#ff3333", "#ff6666", "#ff9999"
|
479 |
];
|
480 |
}} else {{
|
481 |
+
// LIGHT red colors for dark theme (maximum visibility on black)
|
482 |
return [
|
483 |
+
"#ff9999", "#ff6666", "#ff3333", "#ff0000", "#e60000",
|
484 |
+
"#cc0000", "#b30000", "#990000", "#800000", "#660000", "#ffcccc"
|
485 |
];
|
486 |
}}
|
487 |
}} else {{
|
488 |
// Blue color scheme for 10m winds
|
489 |
if (currentTileLayer === 'light') {{
|
490 |
+
// DARK blue colors for light theme (maximum contrast on white)
|
491 |
return [
|
492 |
+
"#000066", "#000080", "#000099", "#0000b3", "#0000cc",
|
493 |
+
"#0000e6", "#0000ff", "#3333ff", "#6666ff", "#9999ff", "#ccccff"
|
494 |
];
|
495 |
}} else {{
|
496 |
+
// LIGHT blue colors for dark theme (maximum visibility on black)
|
497 |
return [
|
498 |
+
"#ccccff", "#9999ff", "#6666ff", "#3333ff", "#0000ff",
|
499 |
+
"#0000e6", "#0000cc", "#0000b3", "#000099", "#000080", "#ffffff"
|
500 |
];
|
501 |
}}
|
502 |
}}
|
@@ -1,3 +1,8 @@
|
|
1 |
gradio==4.44.1
|
2 |
folium==0.17.0
|
3 |
-
branca==0.7.2
|
|
|
|
|
|
|
|
|
|
|
|
1 |
gradio==4.44.1
|
2 |
folium==0.17.0
|
3 |
+
branca==0.7.2
|
4 |
+
requests==2.32.3
|
5 |
+
numpy==1.26.4
|
6 |
+
xarray==2024.6.0
|
7 |
+
cfgrib==0.9.12.0
|
8 |
+
ecmwf-opendata==0.3.22
|