from mdio.ncio import nc_open
import numpy as np

ncfile = 'examples/test.nc'
testcrds = np.array([28.37, 43.48, 25.27])
testbox = np.array([[59.414,  0.,     0.   ],
     [ 0.,    59.414,  0.,   ],
     [ 0.,     0.,    59.414]], dtype=np.float32)
testi = 220

f = nc_open(ncfile, 'r')
frame = f.read_frame()
assert np.allclose(frame.crds[0], testcrds) is True
assert np.allclose(frame.box, testbox) is True
i = 1
while frame is not None:
    frame = f.read_frame()
    i += 1
assert i == testi

f.close()
fin = nc_open(ncfile, 'r')
fout = nc_open('tmp.nc', 'w')
frame = fin.read_frame()
while frame is not None:
    fout.write_frame(frame)
    frame = fin.read_frame()
fin.close()
fout.close()

finA = nc_open(ncfile, 'r')
finB = nc_open('tmp.nc', 'r')
frameA = finA.read_frame()
frameB = finB.read_frame()
while frameA is not None:
    assert np.allclose(frameA.crds, frameB.crds) is True
    frameA = finA.read_frame()
    frameB = finB.read_frame()
finA.close()
finB.close()

ncfile = 'examples/test_nobox.nc'

f = nc_open(ncfile, 'r')
frame = f.read_frame()
assert np.allclose(frame.crds[0], testcrds) is True
i = 1
while frame is not None:
    frame = f.read_frame()
    i += 1
assert i == testi

f.close()
fin = nc_open(ncfile, 'r')
fout = nc_open('tmp.nc', 'w')
frame = fin.read_frame()
while frame is not None:
    fout.write_frame(frame)
    frame = fin.read_frame()
fin.close()
fout.close()

finA = nc_open(ncfile, 'r')
finB = nc_open('tmp.nc', 'r')
frameA = finA.read_frame()
frameB = finB.read_frame()
while frameA is not None:
    assert np.allclose(frameA.crds, frameB.crds) is True
    frameA = finA.read_frame()
    frameB = finB.read_frame()
finA.close()
finB.close()

