UNPKG

mnemo-dmp

Version:

Library for parsing menmo (cave survey tools) dmp files

130 lines 4.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.surveyListToByteArray = exports.surveyListFromByteArray = exports.dmpFromByteArray = exports.dmpToByteArray = exports.ShotType = exports.Direction = void 0; const shot_length = 16; const header_length = 10; var Direction; (function (Direction) { Direction[Direction["In"] = 0] = "In"; Direction[Direction["Out"] = 1] = "Out"; })(Direction = exports.Direction || (exports.Direction = {})); var ShotType; (function (ShotType) { ShotType[ShotType["CSA"] = 0] = "CSA"; ShotType[ShotType["CSB"] = 1] = "CSB"; ShotType[ShotType["STD"] = 2] = "STD"; ShotType[ShotType["EOC"] = 3] = "EOC"; })(ShotType = exports.ShotType || (exports.ShotType = {})); function read_shot(buf, at) { return { type: buf.readUInt8(at + 0), head_in: buf.readInt16BE(at + 1) / 10, head_out: buf.readInt16BE(at + 3) / 10, length: buf.readInt16BE(at + 5) / 100, depth_in: buf.readInt16BE(at + 7) / 100, depth_out: buf.readInt16BE(at + 9) / 100, pitch_in: buf.readInt16BE(at + 11) / 10, pitch_out: buf.readInt16BE(at + 13) / 10, marker: buf.readUInt8(at + 14) }; } function write_shot(buf, at, shot) { buf.writeUInt8(shot.type, at + 0); buf.writeInt16BE(shot.head_in * 10, at + 1); buf.writeInt16BE(shot.head_out * 10, at + 3); buf.writeInt16BE(Math.round(shot.length * 100), at + 5); buf.writeInt16BE(Math.round(shot.depth_in * 100), at + 7); buf.writeInt16BE(Math.round(shot.depth_out * 100), at + 9); buf.writeInt16BE(shot.pitch_in * 10, at + 11); buf.writeInt16BE(shot.pitch_out * 10, at + 13); buf.writeUInt8(shot.marker, at + 14); } function read_shots(buf, at) { const shots = []; for (let i = 0; i < buf.length; i += shot_length) { const shot = read_shot(buf, i + at); shots.push(shot); if (shot.type === ShotType.EOC) break; } return shots; } function write_shots(buf, at, shots) { //todo: assert last and only last is EOC shots.forEach((shot, i) => { write_shot(buf, at + (i * shot_length), shot); }); } function read_survey(buf, at) { const magic = buf.readUInt8(at + 0); const year = buf.readUInt8(at + 1); if (magic !== 2) { throw new Error("Invalid file format"); } if (year > 100 || year < 16) { throw new Error("Invalid year"); } return { date: new Date(year + 2000, buf.readUInt8(at + 2), buf.readUInt8(at + 3), buf.readUInt8(at + 4), buf.readUInt8(at + 5)), name: buf.toString("ascii", at + 6, at + 9), direction: buf.readUInt8(at + 9), shots: read_shots(buf, at + header_length) }; } function write_survey(buf, at, survey) { buf.writeUInt8(2, at + 0); buf.writeUInt8(survey.date.getFullYear() - 2000, at + 1); buf.writeUInt8(survey.date.getMonth(), at + 2); buf.writeUInt8(survey.date.getDay() - 1, at + 3); buf.writeUInt8(survey.date.getHours(), at + 4); buf.writeUInt8(survey.date.getMinutes(), at + 5); buf.write(survey.name, at + 6, 3, "ascii"); buf.writeUInt8(survey.direction, at + 9); write_shots(buf, at + header_length, survey.shots); } function binary_size_of(survey) { return header_length + (survey.shots.length * shot_length); } function read_surveys(buf) { const surveys = []; let at = 0; while (at + header_length < buf.length) { const survey = read_survey(buf, at); surveys.push(survey); at += binary_size_of(survey); } return surveys; } function write_surveys(buf, surveys) { let at = 0; surveys.forEach((survey) => { write_survey(buf, at, survey); at += binary_size_of(survey); }); } function dmpToByteArray(dmp_data) { return new Uint8Array(dmp_data.split(';').map(x => parseInt(x))); } exports.dmpToByteArray = dmpToByteArray; function dmpFromByteArray(arr) { let s = ""; arr.forEach((x) => { s = `${s}${(x << 24 >> 24).toString()};`; }); return s + "\n"; } exports.dmpFromByteArray = dmpFromByteArray; function surveyListFromByteArray(raw) { return read_surveys(Buffer.from(raw)); } exports.surveyListFromByteArray = surveyListFromByteArray; function surveyListToByteArray(surveys) { const size = surveys .map(x => binary_size_of(x)) .reduce((acc, x) => acc + x, 0); const buf = Buffer.alloc(size); write_surveys(buf, surveys); return buf; } exports.surveyListToByteArray = surveyListToByteArray; //# sourceMappingURL=index.js.map