1

Using the h5py module I'm trying to simply read in data from one h5 file, do some basic calculations with the data and write it back into a new h5 file. All is well, except when trying to write the dataset.

so far I have:

f = h5py.File(inData,'r')
dset = f['/DATA/DATA/']
HH = dset[...,0]

HHdB = (10*numpy.log10(HH*HH)) - 83

outfile = h5py.File(outData, 'w')
f.create_dataset('/DATA/DATA/', data=(HHdB))

This returns me the error: "ValueError: unable to create dataset (Dataset: Unable to initialize object)", which I don't understand.

Im a newbie so any help would be much appreciated!

1 Answer 1

1

f.create_dataset should be outfile.create_dataset, since f is the File opened in read mode, while outfile is a File opened in write mode.

By the way, if you use the h5py.Files as a context manager in a with-statement, the file will automatically be closed for you (and written to disk) when Python leaves the with-statement.

import numpy
import h5py

with h5py.File(inData,'r') as f:
    dset = f['/DATA/DATA/']

HH = dset[...,0]
HHdB = (10*numpy.log10(HH*HH)) - 83

with h5py.File(outData, 'w') as outfile:
    outfile.create_dataset('/DATA/DATA/', data=HHdB)
Sign up to request clarification or add additional context in comments.

2 Comments

Well thats embarrassing. It was some code I copied from somewhere else and I forgot to change it. Thanks for the help (and for not laughing at me)!
was going to mention the with-statement, though on some older machines (at work or so), this might not yet work (with-statement was introduced in python 2.5)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.