Skip to content

Instantly share code, notes, and snippets.

@andycasey
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save andycasey/596e1a45806c1a0fec22 to your computer and use it in GitHub Desktop.

Select an option

Save andycasey/596e1a45806c1a0fec22 to your computer and use it in GitHub Desktop.
IDL sucks ass
import struct
def chunk(l, n):
if n < 1:
n = 1
return [l[i:i + n] for i in range(0, len(l), n)]
def _read_binary_line_list(cls, filename):
with open(filename, "rb") as fp:
# Skip the header (this is also length 15200)
fp.seek(0x3b60)
contents = fp.read()
size = 15200
return sum([_read_binary_structure(contents[i*size:]) \
for i in xrange(len(contents)/size)], [])
def _read_binary_structure(cls, contents):
data_format = "<200d200d200f200f200f200f200f200f200f200f200f200l"
data = struct.unpack_from(data_format, contents)
# bytes
offset = struct.calcsize(data_format)
sizes = (5, 3, 3, 3, 3)
byte_format = "<1000s600s600s600s600s"
str_data = [chunk(d, s) for d, s in \
zip(struct.unpack_from(byte_format, contents[offset:]), sizes)]
offset += struct.calcsize(byte_format)
element_fmt = "<200b200b200b"
element_data = struct.unpack_from(element_fmt, contents[offset:])
# Join back together
records = []
for i in xrange(200):
record = list(data[i::200])
record.extend([each[i] for each in str_data])
record.extend(element_data[i::200])
records.append(tuple(record))
return records
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment