56 lines
2.5 KiB
TypeScript
56 lines
2.5 KiB
TypeScript
import { File, Paths } from "expo-file-system";
|
|
|
|
export type CanonicalWindFrame = Readonly<{
|
|
label: "T0" | "T1" | "INVALID";
|
|
metadataJson: string;
|
|
payloadFileUri: string;
|
|
generation: number;
|
|
validTime: number;
|
|
}>;
|
|
|
|
const width = 4;
|
|
const height = 3;
|
|
const bytesPerCell = 2 * Float32Array.BYTES_PER_ELEMENT;
|
|
const validTimeT0 = 1767225600000;
|
|
const validTimeT1 = validTimeT0 + 3_600_000;
|
|
|
|
function metadata(label: "T0" | "T1" | "INVALID", generation: number, validTime: number, byteLength: number) {
|
|
return JSON.stringify({
|
|
schema: "maplibre-particle-field/v1", fieldId: `rn-step061-wind-${label.toLowerCase()}`,
|
|
fieldType: "WIND", displayDomain: "GLOBAL", generation, validTime, dataVersion: 1,
|
|
bounds: { west: -180, south: -85, east: 180, north: 85 }, grid: { width, height },
|
|
vector: { layout: "interleaved-uv", encoding: "float32-le", noData: -9999 }, payload: { byteLength },
|
|
});
|
|
}
|
|
|
|
function interleavedUvBytes(frameOffset: number) {
|
|
const bytes = new ArrayBuffer(width * height * bytesPerCell);
|
|
const view = new DataView(bytes);
|
|
for (let cell = 0; cell < width * height; cell += 1) {
|
|
const offset = cell * bytesPerCell;
|
|
// Canonical order is U=eastward followed by V=northward. `true` freezes LE storage.
|
|
view.setFloat32(offset, 4 + frameOffset + cell * 0.1, true);
|
|
view.setFloat32(offset + Float32Array.BYTES_PER_ELEMENT, -1 + frameOffset * 0.25, true);
|
|
}
|
|
return new Uint8Array(bytes);
|
|
}
|
|
|
|
function writeFrame(label: "T0" | "T1" | "INVALID", generation: number, validTime: number, bytes: Uint8Array, declaredByteLength = bytes.byteLength): CanonicalWindFrame {
|
|
const file = new File(Paths.cache, `maplibre-step061-${label.toLowerCase()}.uvf`);
|
|
file.write(bytes);
|
|
return { label, metadataJson: metadata(label, generation, validTime, declaredByteLength), payloadFileUri: file.uri, generation, validTime };
|
|
}
|
|
|
|
/** Produces independent canonical frames; the Core timeline alone selects T0/T1/MIX. */
|
|
export function writeCanonicalWindFixture(): readonly [CanonicalWindFrame, CanonicalWindFrame, CanonicalWindFrame] {
|
|
const t0Bytes = interleavedUvBytes(0);
|
|
const t1Bytes = interleavedUvBytes(1);
|
|
// Deliberately declare the normal byte length while writing one byte fewer: Core must reject it.
|
|
const invalidBytes = t0Bytes.slice(0, t0Bytes.byteLength - 1);
|
|
return [
|
|
writeFrame("T0", 1, validTimeT0, t0Bytes),
|
|
writeFrame("T1", 2, validTimeT1, t1Bytes),
|
|
writeFrame("INVALID", 3, validTimeT1 + 3_600_000, invalidBytes, t0Bytes.byteLength),
|
|
];
|
|
}
|