In [ ]:
import numpy as np
import os

Generate some basic numpy arrays for testing:


In [ ]:
1j*np.sin(np.arange(10)/np.pi)

In [ ]:
array_float_1d = np.arange(10, dtype=np.float_)
array_complex_1d = np.ones(10, dtype=np.complex_)
array_complex_1d *=np.exp(1j*np.arange(10)/np.pi)
array_complex_1d

In [ ]:
array_float_2d = np.arange(12,dtype=np.float_).reshape(4,3)
array_float_2d

In [ ]:
array_complex_2d = array_complex_1d.reshape(2,5)
array_complex_2d

In [ ]:
output_dir = 'numpy_sample_data'
if not os.path.isdir(output_dir):
    os.makedirs(output_dir)

In [ ]:
savenames = {
    'float_1d':array_float_1d,
    'float_2d': array_float_2d,
    'complex_1d': array_complex_1d,
    'complex_2d': array_complex_2d,
}
for filename, array in savenames.items():
    with open(os.path.join(output_dir,filename+'.npy'), 'wb') as f:
        np.save(f, array_complex_1d)
    with open(os.path.join(output_dir,filename+'.txt'), 'wb') as f:
        np.savetxt(f, array_complex_1d)

Quick example of multiple arrays in one npz file:


In [ ]:
mock_uvw = np.arange(30,dtype=np.float_).reshape(10,3)/2.
mock_uvw

In [ ]:
outfile = os.path.join(output_dir, 'mock_uvw_vis.npz')
with open(outfile, 'wb') as f:
    np.savez(f, uvw=mock_uvw, vis=array_complex_1d)

with open(os.path.join(output_dir,'mock_uvw.txt'), 'wb') as f:
    np.savetxt(f, mock_uvw)
with open(os.path.join(output_dir,'mock_vis.txt'), 'wb') as f:
    np.savetxt(f, array_complex_1d)

And to load:


In [ ]:
with open(os.path.join(output_dir,'complex_2d'+'.npy'), 'rb') as f:
    loaded = np.load(f)
loaded

In [ ]:
outfile = os.path.join(output_dir, 'mock_uvw_vis.npz')
with open(outfile, 'rb') as f:
    loaded = dict(np.load(f).items())
loaded.keys()

In [ ]:
loaded['uvw']

In [ ]:
loaded['vis']

In [ ]: