Spaces:
Sleeping
Sleeping
Added utils.py with normalization functions
Browse files
utils.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
# Normalization parameters
|
| 4 |
+
NORM_PARAMS = {
|
| 5 |
+
'u_min': -5.785097340233893,
|
| 6 |
+
'u_max': 12.700397285986645,
|
| 7 |
+
'v_min': -2.22016497137004,
|
| 8 |
+
'v_max': 22.115114215511067,
|
| 9 |
+
'u_15_min': -5.351205997176121,
|
| 10 |
+
'u_15_max': 12.59459668486086,
|
| 11 |
+
'v_15_min': -3.653644730479408,
|
| 12 |
+
'v_15_max': 21.82925627521016,
|
| 13 |
+
'u_30_min': -5.643262967391627,
|
| 14 |
+
'u_30_max': 12.55918134433948,
|
| 15 |
+
'v_30_min': -4.606536463767895,
|
| 16 |
+
'v_30_max': 21.94683289060832,
|
| 17 |
+
'u_45_min': -5.80703228733597,
|
| 18 |
+
'u_45_max': 12.59988012306978,
|
| 19 |
+
'v_45_min': -1.346510193821202,
|
| 20 |
+
'v_45_max': 21.94214151514658,
|
| 21 |
+
'x_min': -97.40959,
|
| 22 |
+
'x_max': -96.55169890366584,
|
| 23 |
+
'y_min': 32.587689999999995,
|
| 24 |
+
'y_max': 33.067024421728206
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def normalize_uv(u, v, timestep=""):
|
| 28 |
+
"""Normalize u and v components for a specific timestep."""
|
| 29 |
+
u_min_key = f'u{timestep}_min'
|
| 30 |
+
u_max_key = f'u{timestep}_max'
|
| 31 |
+
v_min_key = f'v{timestep}_min'
|
| 32 |
+
v_max_key = f'v{timestep}_max'
|
| 33 |
+
|
| 34 |
+
u_norm = (u - NORM_PARAMS[u_min_key]) / (NORM_PARAMS[u_max_key] - NORM_PARAMS[u_min_key])
|
| 35 |
+
v_norm = (v - NORM_PARAMS[v_min_key]) / (NORM_PARAMS[v_max_key] - NORM_PARAMS[v_min_key])
|
| 36 |
+
|
| 37 |
+
return u_norm, v_norm
|
| 38 |
+
|
| 39 |
+
def denormalize_uv(u_norm, v_norm, timestep=""):
|
| 40 |
+
"""Denormalize normalized u and v components for a specific timestep."""
|
| 41 |
+
u_min_key = f'u{timestep}_min'
|
| 42 |
+
u_max_key = f'u{timestep}_max'
|
| 43 |
+
v_min_key = f'v{timestep}_min'
|
| 44 |
+
v_max_key = f'v{timestep}_max'
|
| 45 |
+
|
| 46 |
+
u = u_norm * (NORM_PARAMS[u_max_key] - NORM_PARAMS[u_min_key]) + NORM_PARAMS[u_min_key]
|
| 47 |
+
v = v_norm * (NORM_PARAMS[v_max_key] - NORM_PARAMS[v_min_key]) + NORM_PARAMS[v_min_key]
|
| 48 |
+
|
| 49 |
+
return u, v
|
| 50 |
+
|
| 51 |
+
def uv_to_velocity_direction(u, v):
|
| 52 |
+
"""Convert u, v components to velocity magnitude and meteorological direction."""
|
| 53 |
+
velocity = np.sqrt(u**2 + v**2)
|
| 54 |
+
# Calculate meteorological wind direction (direction wind is coming from)
|
| 55 |
+
# Convert from mathematical angle to meteorological wind direction
|
| 56 |
+
direction = (270 - np.degrees(np.arctan2(v, u))) % 360
|
| 57 |
+
return velocity, direction
|
| 58 |
+
|
| 59 |
+
def velocity_direction_to_uv(velocity, direction):
|
| 60 |
+
"""Convert velocity magnitude and meteorological direction to u, v components."""
|
| 61 |
+
# Convert meteorological direction to mathematical angle
|
| 62 |
+
theta = np.radians((270 - direction) % 360)
|
| 63 |
+
u = velocity * np.cos(theta)
|
| 64 |
+
v = velocity * np.sin(theta)
|
| 65 |
+
return u, v
|
| 66 |
+
|
| 67 |
+
def denormalize_predictions(pred):
|
| 68 |
+
"""Denormalize model predictions to get velocity and direction."""
|
| 69 |
+
velocity = pred[:, 0] * (NORM_PARAMS['v_max'] - NORM_PARAMS['v_min']) + NORM_PARAMS['v_min']
|
| 70 |
+
direction = np.rad2deg(np.arctan2(pred[:, 1], pred[:, 2])) % 360
|
| 71 |
+
return velocity, direction
|