From b4878e28dc8bf5a1cceb60e5d0e37b64e6e94c24 Mon Sep 17 00:00:00 2001 From: jim Date: Sun, 23 Nov 2014 18:44:41 -0600 Subject: [PATCH] made a test model just to figure out how to refactor things --- nd2reader/__init__.py | 2 +- nd2reader/model/__init__.py | 68 ++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/nd2reader/__init__.py b/nd2reader/__init__.py index 2bfa070..27b0b6e 100644 --- a/nd2reader/__init__.py +++ b/nd2reader/__init__.py @@ -23,7 +23,7 @@ class Nd2(BaseNd2): """ for i in range(self._image_count): - for fov in range(self._field_of_view_count): + for fov in range(self.field_of_view_count): for z_level in range(self._z_level_count): for channel in self.channels: image = self.get_image(i, fov, channel.name, z_level) diff --git a/nd2reader/model/__init__.py b/nd2reader/model/__init__.py index 56d0d90..fcb4be0 100644 --- a/nd2reader/model/__init__.py +++ b/nd2reader/model/__init__.py @@ -73,4 +73,70 @@ class Image(object): def show(self): skimage.io.imshow(self.data) - skimage.io.show() \ No newline at end of file + skimage.io.show() + + +from io import BytesIO +import array +import struct + + +class Chunkmap(object): + def __init__(self): + pass + + def read(self, filename): + with open(filename, "rb") as f: + data = f.read(-1) + self.parse(data, 1) + + def parse(self, data, count): + data = BytesIO(data) + res = {} + total_count = 0 + for c in range(count): + lastpos = data.tell() + total_count += 1 + hdr = data.read(2) + if not hdr: + break + typ = ord(hdr[0]) + bname = data.read(2*ord(hdr[1])) + name = bname.decode("utf16")[:-1].encode("utf8") + if typ == 1: + value, = struct.unpack("B", data.read(1)) + elif typ in [2, 3]: + value, = struct.unpack("I", data.read(4)) + elif typ == 5: + value, = struct.unpack("Q", data.read(8)) + elif typ == 6: + value, = struct.unpack("d", data.read(8)) + elif typ == 8: + value = data.read(2) + while value[-2:] != "\x00\x00": + value += data.read(2) + value = value.decode("utf16")[:-1].encode("utf8") + elif typ == 9: + cnt, = struct.unpack("Q", data.read(8)) + value = array.array("B", data.read(cnt)) + elif typ == 11: + curpos = data.tell() + newcount, length = struct.unpack("