Skip to content

Instantly share code, notes, and snippets.

@nahidakbar
Created October 27, 2014 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahidakbar/e28bd7507bd8c9033eb1 to your computer and use it in GitHub Desktop.
Save nahidakbar/e28bd7507bd8c9033eb1 to your computer and use it in GitHub Desktop.
/**
* HDS File parser
*
* @todo add 64 bit value parsing.
*/
var loadHDS = function(binary, filename)
{
try
{
var view = new DataView(binary[0].buffer);
var offset = 0;
var chunkOffset = 0;
var chunkNo = 0;
var length = binary.totalLength;
var requireSize = function(size)
{
// console.log(offset + chunkOffset + size, offset + view.buffer.byteLength);
while (chunkOffset + size >= view.buffer.byteLength && chunkNo < binary.length - 1)
{
var leftFromOldChunk = view.buffer.byteLength - chunkOffset;
var newChunkLength = binary[chunkNo + 1].length;
var newLength = leftFromOldChunk + newChunkLength;
var newArray = new Uint8Array(newLength);
newArray.set(binary[chunkNo].subarray(chunkOffset), 0);
newArray.set(binary[chunkNo + 1], leftFromOldChunk);
offset += chunkOffset;
binary[chunkNo++] = undefined;
chunkOffset = 0;
view = new DataView(newArray.buffer);
}
};
var uint32 = function()
{
requireSize(4);
var val = view.getUint32(chunkOffset, true);
chunkOffset += 4;
return val;
};
var float32 = function()
{
requireSize(4);
var val = view.getFloat32(chunkOffset, true);
chunkOffset += 4;
return val;
};
var floatArray32 = function(elements)
{
console.log(elements * 4);
requireSize(elements * 4);
var val = new Float32Array(view.buffer, chunkOffset, elements);
chunkOffset += 4 * elements;
return val;
};
var values = [];
var times = [];
var value = null;
var totalSize = 0;
var totalOffset = 0;
try
{
var lastTime = null;
while (offset + chunkOffset + 44 < length)
{
var startOffset = offset;
var simTimeStepNo = uint32();
var simStressPeriodNo = uint32();
var curStressPeriodTime = float32();
var curElapsedTime = float32();
chunkOffset += 16;
var colsCount = uint32();
var rowsCount = uint32();
var layerIndex = uint32();
if (layerIndex === 1)
{
if (value !== null)
{
values.push(value);
times.push(lastTime);
totalSize = value.length;
}
value = new Float32Array(totalSize);
totalOffset = 0;
}
if (totalSize === 0)
{
value = float32Concat(value, floatArray32(colsCount * rowsCount));
} else
{
value.set(floatArray32(colsCount * rowsCount), totalOffset);
totalOffset += colsCount * rowsCount;
}
lastTime = curElapsedTime;
// console.log(startOffset, simTimeStepNo, simStressPeriodNo, curStressPeriodTime, curElapsedTime,
// layerIndex, colsCount, rowsCount);
// console.log(values.length, times.length);
}
if (offset + chunkOffset === length)
{
values.push(value);
times.push(lastTime);
}
} catch (e)
{
console.log(e, e.stack);
}
return {
'values' : values,
'times' : times
};
} catch (e)
{
showError('Error reading', filename, e.message);
return binary;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment