Skip to content

Instantly share code, notes, and snippets.

@hhrhhr
Last active August 25, 2019 23:16
Show Gist options
  • Save hhrhhr/21b301137d95864b8bd3b356d3cf0d6f to your computer and use it in GitHub Desktop.
Save hhrhhr/21b301137d95864b8bd3b356d3cf0d6f to your computer and use it in GitHub Desktop.
парсер .70 файлов в текстовое представление
-- 1 2 3 4 5 6 7 8
---------+---------+---------+---------+---------+---------+---------+---------+
--[[ constants ]]
local YEAR = 2016 -- 1451509200
local EPOCH = os.time({year = YEAR, month = 1, day = 0, hour = 0})
--[[ funcs ]]
local r = {}
r.read = function(fmt, count)
local res, _ = string.unpack(fmt, r.h:read(count))
return res
end
-- big endian!
function r.u8() return r.read("B" , 1) end
function r.u16() return r.read(">H" , 2) end
function r.u32() return r.read(">I4", 4) end
function r.f() return r.read(">f" , 4) end
function r.d() return r.read(">d" , 8) end
r.h = nil -- file handle
local function epoch2unix(time)
local unixtime = EPOCH + time
return os.date("%H:%M:%S", unixtime)
end
--[[ main ]]
local fn = arg[1]
r.h = io.open(fn, "rb")
local sz = r.h:seek("end")
r.h:seek("set")
local csv1 = {}
local csv2 = {}
while r.h:seek() < sz do
local t1, t2 = {}, {}
-- 64 bytes, unknown data
for i = 1, 22 do
t1[i] = r.u8()
end
t1[23] = r.u8() -- HH, UTC+3 (MSK)
t1[24] = r.u8() -- MM
t1[25] = r.u8() -- SS
t1[26] = r.u32() -- ms
for i = 27, 61 do
t1[i] = r.u8()
end
local time = t1[23] * 3600
time = time + t1[24] * 60
time = time + t1[25]
time = time + tonumber("0." .. t1[26]) -- seconds from 00:00:00
table.insert(csv1, {time})
-- 64 bytes
t2[1] = epoch2unix(r.u32()) -- time1
t2[2] = r.u16() -- year
t2[3] = r.u16() -- day from year
t2[4] = epoch2unix(r.u32()) -- time2
t2[5] = epoch2unix(r.u32()) -- time3
t2[6] = r.f() -- L0
t2[7] = r.f() -- L1
t2[8] = r.f() -- L2
t2[9] = r.f() -- L3
t2[10] = epoch2unix(r.u32()) -- time4
t2[11] = r.d() -- X
t2[12] = r.d() -- Y
t2[13] = r.d() -- Z
assert(0 == r.u32())-- zero
table.insert(csv2, t2)
end
r.h:close()
-- write output
local w = io.open(fn:sub(1, -3) .. "csv", "w+b")
w:write("t0;t1;year;day;t2;t3;qw;qx;qy;qz;t4;x;y;z\n")
for i = 1, #csv2 do
local t = csv1[i]
w:write(table.concat(t, ";"):gsub("%.", ","), ";")
t = csv2[i]
w:write(table.concat(t, ";"):gsub("%.", ","), "\n")
end
w:close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment