简体   繁体   中英

writing an array to h5 in python

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!

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.File s 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM