def test_03_write(self): a = np.random.randint(0, 65535, (64, 64, 64), np.uint16) with make_files(1) as (dir_file, block_files): directory = Directory(1024, 1024, 1024, np.uint16, dir_file, compression=Compression.zstd, block_filenames=block_files) directory.create() directory.write_block(a, 64, 128, 192) directory.close() with open(block_files[0], "rb") as fd: block = fd.read() blosc = Blosc("zstd") a_out = np.frombuffer(blosc.decode(block), np.uint16)\ .reshape(64, 64, 64) np.testing.assert_array_equal(a, a_out)
def read_block(self, x, y, z): offset = self.offsetof(x, y, z) shape = self.get_block_size(x, y, z) idx = offset % len(self.block_filenames) directory_offset = self.directory_offset + \ offset * self.directory_entry_size if self.filesize(self.directory_filename) <\ directory_offset + self.directory_entry_size: return np.zeros(shape, self.dtype) fd = self.file_handle(self.directory_filename) fd.seek(directory_offset, os.SEEK_SET) data = fd.read(self.directory_entry_size) m = np.frombuffer(data, dtype=np.uint8) offset, size = self.decode_directory_entry(m) if size == 0: return np.zeros(shape, self.dtype) fd = self.file_handle(self.block_filenames[idx]) fd.seek(offset) uncompressed = fd.read(size) blosc = Blosc(self.compression, self.compression_level) data = blosc.decode(uncompressed) return np.frombuffer(data, self.dtype).reshape(shape)