63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
/**
|
|
* Handles Binary Data Ingestion via Fetch.
|
|
*/
|
|
(function (global) {
|
|
global.DataLoader = {
|
|
/**
|
|
* Fetches and parses the binary simulation data.
|
|
* @param {string} url - URL to the binary file.
|
|
* @returns {Promise<Object>} - { labels, data } or error.
|
|
*/
|
|
async loadBinary(url = 'simulation.bin') {
|
|
try {
|
|
const response = await fetch(url, { cache: "no-store" });
|
|
if (!response.ok) throw new Error(`HTTP Error: ${response.status}`);
|
|
|
|
const buffer = await response.arrayBuffer();
|
|
return this.parseBuffer(buffer);
|
|
} catch (err) {
|
|
console.error("Load Failed:", err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
parseBuffer(buffer) {
|
|
const dataView = new DataView(buffer);
|
|
let offset = 0;
|
|
|
|
// 1. Read Header
|
|
const magic = dataView.getUint32(offset, true); // Little Endian
|
|
offset += 4;
|
|
|
|
const count = dataView.getUint32(offset, true);
|
|
offset += 4;
|
|
|
|
if (magic !== 0xFEAA01) {
|
|
throw new Error(`Invalid Magic Number: 0x${magic.toString(16)}`);
|
|
}
|
|
|
|
// 2. Read X Data
|
|
const xData = [];
|
|
for (let i = 0; i < count; i++) {
|
|
xData.push(dataView.getFloat32(offset, true));
|
|
offset += 4;
|
|
}
|
|
|
|
// 3. Read Y Data
|
|
const yData = [];
|
|
for (let i = 0; i < count; i++) {
|
|
yData.push(dataView.getFloat32(offset, true));
|
|
offset += 4;
|
|
}
|
|
|
|
// Format for Plotter (Labels as strings/numbers, Data as numbers)
|
|
// Plotter expects labels, but for scatter/line with x,y numbers we might adjust plotter.
|
|
// For now, mapping X to labels is fine for simple line plots.
|
|
return {
|
|
labels: xData.map(x => x.toFixed(2)),
|
|
data: yData
|
|
};
|
|
}
|
|
};
|
|
})(window);
|