h5 KeyError: "Unable to open object (object 'data' doesn't exist)"

Viewed 2529

I am taking a previously generated .h5 file which is 3D and taking a 2D slice. The new 2D h5 file looks complete and as expected, however I then pass this into some modeling software (GPRMax) and get the error KeyError: "Unable to open object (object 'data' doesn't exist)". Previous 2D and 3D .h5 files have been fine, so im not sure whats different. Below is the code used to generate the .h5 file. Any help is appreciated.

hf = h5py.File(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701.h5', 'r')

dset = hf['data']

if os.path.exists(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701-slice.h5'):
    os.remove(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701-slice.h5')

hf2 = h5py.File(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701-slice.h5','w')


dz_dy_dz = (0.001,0.001,0.001)
hf2.attrs['dx_dy_dz'] = dz_dy_dz

data = hf2.create_dataset("data", (100,1,100))

for i in range(100):
    data[i]=(dset[i,50,:])

data=data.ref
print(data)

hf2.close()
hf.close()
2 Answers

Try absolute path, please. It may works.

Here is an example that creates a ['data'] dataset in an HDF5 file, closes, then reopens (read-only) and copies the data in slices to a second HDF5 file. Maybe you will find something to help you.

with h5py.File('SO_64758047.h5','w') as h5w:
    arr = np.random.random(100*480*640).reshape(100,480,640)
    ds = h5w.create_dataset('data', data=arr)   
    
with h5py.File('SO_64758047.h5','r') as h5r:
    dset = h5r['data']

    with h5py.File('SO_64758047_2.h5','w') as h5w2:
        dz_dy_dz = (0.001,0.001,0.001)
        h5w2.attrs['dx_dy_dz'] = dz_dy_dz
        data = h5w2.create_dataset('data',(100,480,640))

        for i in range(h5r['data'].shape[0]):
            print('get slice#',str(i))
            data[i] = h5r['data'][i,:,:] 
Related