"""
Implement a simple copy command that exercises the new code.
"""
import numpy as np
import os, sys
from ..api import ZgyReader, ZgyWriter, ProgressWithDots
from ..test.utils import SDCredentials
from ..iterator import readall
def copy_open_file(r, w, progress):
"""Simple example of manually iterating over files."""
def _roundup(x, step): return ((x + step - 1) // step) * step
blocksize = (r.bricksize[0], r.bricksize[1],
_roundup(r.size[2], r.bricksize[2]))
total = (((r.size[0] + blocksize[0] - 1) // blocksize[0]) *
((r.size[1] + blocksize[1] - 1) // blocksize[1]))
done = 0
data = np.zeros(blocksize, dtype=np.float32)
for ii in range(0, r.size[0], blocksize[0]):
for jj in range(0, r.size[1], blocksize[1]):
r.read((ii, jj, 0), data)
w.write((ii, jj, 0), data)
done += 1
if progress: progress(done, total)
def copy_open_v2(r, w, progress):
"""Fewer lines of code but more going on behind the scenes."""
for datastart, datasize, data in readall(r, progress=progress):
w.write(datastart, data)
def copy(srcfilename, dstfilename):
with ZgyReader(srcfilename, iocontext = SDCredentials()) as r:
with ZgyWriter(dstfilename, templatename=srcfilename,
iocontext = SDCredentials()) as w:
copy_open_file(r, w, progress=ProgressWithDots())
w.finalize(progress=ProgressWithDots())
if __name__ == "__main__":
np.seterr(all='raise')
copy(sys.argv[1], sys.argv[2])