import h5py import numpy as np f=h5py.File('small.hdf5','w') for g in ['inc0','inc1']: f.create_group(g) f[g].create_dataset('T',data=np.random.rand(2,3,4,3,3)) M = np.array([[0,1],[4,5]]).reshape(2,2) M_field = np.broadcast_to(M,(2,3,4,2,2)).copy() M_field[1,2,3]=-20. f[g].create_dataset('M',data=M_field) f[g].create_dataset('u',data=np.zeros(3*4*5*3).reshape(3,4,5,3)) f.close()