1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
# Authors: Denis A. Engemann <denis.engemann@gmail.com>
# simplified BSD-3 license
import struct
import numpy as np
from ...externals.six import b
def _unpack_matrix(fid, format, rows, cols, dtype):
""" Aux Function """
out = np.zeros((rows, cols), dtype=dtype)
bsize = struct.calcsize(format)
string = fid.read(bsize)
data = struct.unpack(format, string)
iter_mat = [(r, c) for r in range(rows) for c in range(cols)]
for idx, (row, col) in enumerate(iter_mat):
out[row, col] = data[idx]
return out
def _unpack_simple(fid, format, count):
""" Aux Function """
bsize = struct.calcsize(format)
string = fid.read(bsize)
data = list(struct.unpack(format, string))
out = data if count < 2 else list(data)
if len(out) > 0:
out = out[0]
return out
def read_str(fid, count=1):
""" Read string """
format = '>' + ('c' * count)
data = list(struct.unpack(format, fid.read(struct.calcsize(format))))
bytestr = b('').join(data[0:data.index(b('\x00')) if b('\x00') in data else
count])
return str(bytestr.decode('ascii')) # Return native str type for Py2/3
def read_char(fid, count=1):
" Read character from bti file """
return _unpack_simple(fid, '>' + ('c' * count), count)
def read_bool(fid, count=1):
""" Read bool value from bti file """
return _unpack_simple(fid, '>' + ('?' * count), count)
def read_uint8(fid, count=1):
""" Read unsigned 8bit integer from bti file """
return _unpack_simple(fid, '>' + ('B' * count), count)
def read_int8(fid, count=1):
""" Read 8bit integer from bti file """
return _unpack_simple(fid, '>' + ('b' * count), count)
def read_uint16(fid, count=1):
""" Read unsigned 16bit integer from bti file """
return _unpack_simple(fid, '>' + ('H' * count), count)
def read_int16(fid, count=1):
""" Read 16bit integer from bti file """
return _unpack_simple(fid, '>' + ('H' * count), count)
def read_uint32(fid, count=1):
""" Read unsigned 32bit integer from bti file """
return _unpack_simple(fid, '>' + ('I' * count), count)
def read_int32(fid, count=1):
""" Read 32bit integer from bti file """
return _unpack_simple(fid, '>' + ('i' * count), count)
def read_uint64(fid, count=1):
""" Read unsigned 64bit integer from bti file """
return _unpack_simple(fid, '>' + ('Q' * count), count)
def read_int64(fid, count=1):
""" Read 64bit integer from bti file """
return _unpack_simple(fid, '>' + ('q' * count), count)
def read_float(fid, count=1):
""" Read 32bit float from bti file """
return _unpack_simple(fid, '>' + ('f' * count), count)
def read_double(fid, count=1):
""" Read 64bit float from bti file """
return _unpack_simple(fid, '>' + ('d' * count), count)
def read_int16_matrix(fid, rows, cols):
""" Read 16bit integer matrix from bti file """
format = '>' + ('h' * rows * cols)
return _unpack_matrix(fid, format, rows, cols, np.int16)
def read_float_matrix(fid, rows, cols):
""" Read 32bit float matrix from bti file """
format = '>' + ('f' * rows * cols)
return _unpack_matrix(fid, format, rows, cols, 'f4')
def read_double_matrix(fid, rows, cols):
""" Read 64bit float matrix from bti file """
format = '>' + ('d' * rows * cols)
return _unpack_matrix(fid, format, rows, cols, 'f8')
def read_transform(fid):
""" Read 64bit float matrix transform from bti file """
format = '>' + ('d' * 4 * 4)
return _unpack_matrix(fid, format, 4, 4, 'f8')
|