File size: 7,826 Bytes
6a2b7f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | // Browser mirror of src/physics.py (8-level 240x121 schema, NOAA/Spencer TOA solar, colormaps)
export const H = 121;
export const W = 240;
export const LEVELS = [1000, 925, 850, 700, 500, 300, 200, 100];
export const N_PROG = 44; // 5 upper vars x 8 levels + 4 surface prog
export const GRAVITY = 9.80665;
// Channel indices in the 44-channel prognostic state (storage units)
export const CH = {
Z500: 4, // geopotential_500 (offset 55000, scale 1)
T850: 10, // temperature_850 (offset 273.15, scale 1 -> deg C)
Q700: 35, // specific_humidity_700 (offset 0, scale 1000 -> g/kg)
T2M: 40, // 2m_temperature (offset 273.15, scale 1 -> deg C)
U10: 41, // 10m_u_component_of_wind (m/s)
V10: 42, // 10m_v_component_of_wind (m/s)
MSLP: 43, // mean_sea_level_pressure (offset 101325, scale 1 -> Pa anomaly)
};
// Precompute lat/lon arrays matching WeatherBench 2 240x121 (lat -90..+90, lon 0..358.5)
export const LATS_DEG = new Float64Array(H);
export const LONS_DEG = new Float64Array(W);
export const SIN_LAT = new Float32Array(H);
export const COS_LAT = new Float32Array(H);
export const LON_RAD = new Float64Array(W);
export const LAT_RAD = new Float64Array(H);
for (let y = 0; y < H; y++) {
const lat = -90.0 + y * 1.5;
LATS_DEG[y] = lat;
const r = (lat * Math.PI) / 180.0;
LAT_RAD[y] = r;
SIN_LAT[y] = Math.sin(r);
COS_LAT[y] = Math.cos(r);
}
for (let x = 0; x < W; x++) {
const lon = x * 1.5;
LONS_DEG[x] = lon;
LON_RAD[x] = (lon * Math.PI) / 180.0;
}
/**
* Analytic normalized TOA solar insolation cos(zenith)+ for one Unix timestamp (seconds).
* Matches src/physics.py compute_toa_solar_np() identically.
*/
export function computeToaSolarFrame(timestampSec, out = new Float32Array(H * W)) {
const daysJ2000 = (timestampSec - 946728000.0) / 86400.0;
const gDeg = (((357.529 + 0.98560028 * daysJ2000) % 360.0) + 360.0) % 360.0;
const g = (gDeg * Math.PI) / 180.0;
const q = (((280.459 + 0.98564736 * daysJ2000) % 360.0) + 360.0) % 360.0;
const lam = ((q + 1.915 * Math.sin(g) + 0.020 * Math.sin(2.0 * g)) * Math.PI) / 180.0;
const eps = ((23.439 - 0.00000036 * daysJ2000) * Math.PI) / 180.0;
const sinDec = Math.sin(eps) * Math.sin(lam);
const cosDec = Math.sqrt(Math.max(0.0, 1.0 - sinDec * sinDec));
const invR2 = 1.0 + 0.0334 * Math.cos(g);
const secDay = ((timestampSec % 86400) + 86400) % 86400;
const utcHour = secDay / 3600.0;
const haBase = (utcHour - 12.0) * (Math.PI / 12.0);
const cosHa = new Float64Array(W);
for (let x = 0; x < W; x++) {
cosHa[x] = Math.cos(haBase + LON_RAD[x]);
}
for (let y = 0; y < H; y++) {
const sLat = SIN_LAT[y] * sinDec;
const cLat = COS_LAT[y] * cosDec;
const rowOff = y * W;
for (let x = 0; x < W; x++) {
const cz = sLat + cLat * cosHa[x];
out[rowOff + x] = cz > 0 ? cz * invR2 : 0.0;
}
}
return out;
}
// IEEE-754 float16 decode fallback when Float16Array is unavailable
const _f32Buf = new ArrayBuffer(4);
const _f32View = new Float32Array(_f32Buf);
const _u32View = new Uint32Array(_f32Buf);
export function halfToFloat(h) {
const s = (h & 0x8000) >> 15;
const e = (h & 0x7c00) >> 10;
const f = h & 0x03ff;
if (e === 0) {
return (s ? -1 : 1) * Math.pow(2, -14) * (f / 1024);
}
if (e === 31) {
return f ? NaN : (s ? -Infinity : Infinity);
}
_u32View[0] = (s << 31) | ((e + 112) << 23) | (f << 13);
return _f32View[0];
}
export function decodeFloat16Buffer(arrayBuffer) {
if (typeof Float16Array !== "undefined") {
const f16 = new Float16Array(arrayBuffer);
const out = new Float32Array(f16.length);
for (let i = 0; i < f16.length; i++) out[i] = f16[i];
return out;
}
const u16 = new Uint16Array(arrayBuffer);
const out = new Float32Array(u16.length);
for (let i = 0; i < u16.length; i++) out[i] = halfToFloat(u16[i]);
return out;
}
export function float32ToFloat16Array(f32) {
if (typeof Float16Array !== "undefined") {
return Float16Array.from(f32);
}
// Fallback Uint16Array bit-pack for ORT float16 tensor
const u16 = new Uint16Array(f32.length);
for (let i = 0; i < f32.length; i++) {
_f32View[0] = f32[i];
const x = _u32View[0];
const sign = (x >> 16) & 0x8000;
let exp = ((x >> 23) & 0xff) - 112;
let mant = (x >> 13) & 0x03ff;
if (exp <= 0) {
u16[i] = sign;
} else if (exp >= 31) {
u16[i] = sign | 0x7c00;
} else {
u16[i] = sign | (exp << 10) | mant;
}
}
return u16;
}
// Perceptually crafted scientific color ramps
export const LAYERS = {
t2m: {
id: "t2m",
name: "2 m temperature",
short: "Temp 2 m",
unit: "°C",
min: -35,
max: 45,
stops: [
[0.0, [24, 28, 92]],
[0.18, [34, 94, 198]],
[0.36, [44, 178, 214]],
[0.50, [92, 206, 138]],
[0.65, [238, 216, 86]],
[0.80, [242, 122, 46]],
[0.92, [206, 36, 52]],
[1.0, [118, 12, 44]],
],
},
wspd10: {
id: "wspd10",
name: "10 m wind speed",
short: "Wind 10 m",
unit: "m/s",
min: 0,
max: 26,
stops: [
[0.0, [12, 22, 46]],
[0.2, [22, 68, 132]],
[0.42, [34, 152, 178]],
[0.64, [114, 214, 124]],
[0.82, [248, 196, 52]],
[0.94, [238, 86, 52]],
[1.0, [214, 48, 168]],
],
},
mslp: {
id: "mslp",
name: "Sea-level pressure",
short: "Pressure",
unit: "hPa",
min: 970,
max: 1040,
stops: [
[0.0, [158, 24, 146]],
[0.20, [58, 72, 204]],
[0.40, [44, 154, 214]],
[0.58, [124, 186, 174]],
[0.74, [232, 214, 122]],
[0.88, [236, 132, 64]],
[1.0, [186, 44, 48]],
],
},
z500: {
id: "z500",
name: "500 hPa height",
short: "Z500",
unit: "dam",
min: 500,
max: 595,
stops: [
[0.0, [32, 16, 86]],
[0.22, [44, 78, 176]],
[0.45, [36, 164, 168]],
[0.68, [132, 206, 98]],
[0.86, [246, 198, 56]],
[1.0, [226, 72, 42]],
],
},
tp6h: {
id: "tp6h",
name: "6-hour rain",
short: "Rain 6 h",
unit: "mm",
min: 0,
max: 25,
stops: [
[0.0, [10, 18, 34]],
[0.08, [18, 46, 88]],
[0.22, [34, 118, 198]],
[0.42, [56, 196, 172]],
[0.64, [222, 224, 68]],
[0.84, [246, 122, 42]],
[1.0, [228, 42, 152]],
],
},
t850: {
id: "t850",
name: "850 hPa temperature",
short: "Temp 850",
unit: "°C",
min: -30,
max: 32,
stops: [
[0.0, [26, 32, 108]],
[0.22, [36, 104, 206]],
[0.45, [52, 186, 202]],
[0.64, [154, 214, 112]],
[0.82, [246, 168, 52]],
[1.0, [202, 34, 52]],
],
},
};
export function sampleRamp(stops, t, outRgb) {
const c = t <= 0 ? 0 : t >= 1 ? 1 : t;
for (let i = 0; i < stops.length - 1; i++) {
const [t0, c0] = stops[i];
const [t1, c1] = stops[i + 1];
if (c <= t1 || i === stops.length - 2) {
const f = (c - t0) / Math.max(t1 - t0, 1e-6);
outRgb[0] = (c0[0] + (c1[0] - c0[0]) * f) | 0;
outRgb[1] = (c0[1] + (c1[1] - c0[1]) * f) | 0;
outRgb[2] = (c0[2] + (c1[2] - c0[2]) * f) | 0;
return;
}
}
}
// Bilinear sample from (H, W) array at (latDeg in [-90, 90], lonDeg in [-180, 180] or [0, 360))
export function sampleFieldLatLon(arr, latDeg, lonDeg) {
const lon360 = ((lonDeg % 360) + 360) % 360;
const fy = Math.max(0, Math.min(H - 1, (latDeg + 90.0) / 1.5));
const fx = (lon360 / 1.5) % W;
const y0 = Math.floor(fy);
const y1 = Math.min(H - 1, y0 + 1);
const x0 = Math.floor(fx) % W;
const x1 = (x0 + 1) % W;
const wy = fy - y0;
const wx = fx - Math.floor(fx);
const v00 = arr[y0 * W + x0];
const v10 = arr[y0 * W + x1];
const v01 = arr[y1 * W + x0];
const v11 = arr[y1 * W + x1];
return (v00 * (1 - wx) + v10 * wx) * (1 - wy) + (v01 * (1 - wx) + v11 * wx) * wy;
}
|