Skip to content

Instantly share code, notes, and snippets.

@nbassler
Created December 15, 2016 22:52
Show Gist options
  • Save nbassler/4ee2829501d91f63abe0292ed552dde6 to your computer and use it in GitHub Desktop.
Save nbassler/4ee2829501d91f63abe0292ed552dde6 to your computer and use it in GitHub Desktop.
Simple bdox reader
import sys
import numpy as np
def get_token(f):
"""
returns a tuple with 4 elements:
0: payload id
1: payload dtype string
2: payload number of elements
3: payload itself
returns None if no token was found / EOF
"""
tag = np.dtype([('pl_id', '<u8'),
('pl_type', 'S8'),
('pl_len', '<u8')])
x1 = np.fromfile(f, dtype=tag, count=1) # read the data into numpy
if not x1:
return None
else:
pl_id = x1['pl_id'][0]
pl_type = x1['pl_type'][0]
pl_len = x1['pl_len'][0]
pl = np.fromfile(f,
dtype=pl_type,
count=pl_len) # read the data into numpy
return(pl_id, pl_type, pl_len, pl)
def main(args=sys.argv[1:]):
""" Simple reader for .bdox files
"""
filename = sys.argv[1]
print(filename)
with open(filename, "rb") as f:
d1 = np.dtype([('magic', 'S6'),
('end', 'S2')])
x = np.fromfile(f, dtype=d1, count=1) # read the data into numpy
print(x['magic'][0])
print(x['end'][0])
while (f != []):
token = get_token(f)
if token is None:
break
print(token[0], token[1], token[2])
for i, j in enumerate(token[3]):
print(i, j)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment