mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 16:52:20 +00:00
Each /api/wind refresh decoded two or three 0.25 degree GRIB2 messages with ecCodes (WASM), validated ~1.04 M doubles per message and resampled them, all synchronously on the server thread. Measured with cached upstream bytes: 43 to 164 ms of event-loop block per warm refresh, 230 to 381 ms for the first one in a process, 316 to 974 ms when all six keys refresh together. Decode, validation and resampling now run on one lazily started worker thread, which transfers back only the display-grid samples. The server keeps the sampled U/V of the last forecast object per model (1.04 MB each), so an overlay change, or a refresh within the same forecast step, fetches and decodes only the scalar. Manifests and grid bodies are byte-identical to before for the same upstream bytes. Co-Authored-By: Claude Fable 5.2 <[email protected]>
69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
import { sampleWindField } from './grid.js';
|
|
|
|
/** Lazy shared ecCodes WASM instance, created only for an active wind request. */
|
|
let eccodesPromise = null;
|
|
function loadEccodes() {
|
|
eccodesPromise ??= import('@meri-imperiumi/eccodes-wasm')
|
|
.then((module) => module.createEccodes())
|
|
.catch((error) => {
|
|
eccodesPromise = null;
|
|
throw error;
|
|
});
|
|
return eccodesPromise;
|
|
}
|
|
|
|
/** Decode one bounded message in WASM memory without mounting the host filesystem. */
|
|
export async function decodeWindGribMessage(buffer, { eccodesModule } = {}) {
|
|
if (buffer.byteLength > 8 * 1024 * 1024)
|
|
throw new Error('Wind GRIB exceeds byte budget');
|
|
const eccodes = eccodesModule ?? (await loadEccodes());
|
|
// Everything after this await is synchronous. A shared virtual file is safe
|
|
// across U/V and model requests and its storage is released after each decode.
|
|
const file = '/gev-wind.grib2';
|
|
let handle = null;
|
|
try {
|
|
eccodes.writeFile(file, buffer);
|
|
handle = eccodes.openGrib(file);
|
|
const ni = handle.getLong('Ni');
|
|
const nj = handle.getLong('Nj');
|
|
if (
|
|
!Number.isInteger(ni) ||
|
|
!Number.isInteger(nj) ||
|
|
ni < 1 ||
|
|
nj < 1 ||
|
|
ni * nj > 2_000_000
|
|
)
|
|
throw new Error('Invalid wind GRIB geometry');
|
|
return {
|
|
ni,
|
|
nj,
|
|
lo1: handle.getDouble('longitudeOfFirstGridPointInDegrees'),
|
|
la1: handle.getDouble('latitudeOfFirstGridPointInDegrees'),
|
|
di: handle.getDouble('iDirectionIncrementInDegrees'),
|
|
dj: handle.getDouble('jDirectionIncrementInDegrees'),
|
|
values: handle.getDoubleArray('values'),
|
|
shortName: handle.getString('shortName'),
|
|
level: handle.getLong('level'),
|
|
units: handle.getString('units'),
|
|
};
|
|
} finally {
|
|
handle?.delete();
|
|
eccodes.writeFile(file, new Uint8Array(0));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Worker side of ./decoder.js: decode each posted message, validate and sample
|
|
* it to the display grid, and transfer back only the samples.
|
|
*/
|
|
export function serveWindDecodes(port, decodeImpl = decodeWindGribMessage) {
|
|
port.on('message', async ({ id, bytes, targetDx }) => {
|
|
try {
|
|
const field = sampleWindField(await decodeImpl(bytes), targetDx);
|
|
port.postMessage({ id, field }, [field.samples.buffer]);
|
|
} catch (error) {
|
|
port.postMessage({ id, error: String(error?.message ?? error) });
|
|
}
|
|
});
|
|
}
|