have been trying to read an .hdf5 file so that I can plot some of the data, to do so I thought I would take the data in the .hdf5 file and process it into a .txt file. To check the data I ended up using the code presented in reading nested .h5 group into numpy array.
import numpy as np
import h5py
f = h5py.File('15524.h5', 'r')
list(f.keys())
dset = f['Measurement_15524']
def traverse_datasets(hdf_file):
def h5py_dataset_iterator(g, prefix=''):
for key in g.keys():
item = g[key]
path = f'{prefix}/{key}'
if isinstance(item, h5py.Dataset): # test for dataset
yield (path, item)
elif isinstance(item, h5py.Group): # test for group (go down)
yield from h5py_dataset_iterator(item, path)
for path, _ in h5py_dataset_iterator(hdf_file):
yield path
with h5py.File('15524.h5', 'r') as f:
for dset in traverse_datasets(f):
print('Path:', dset)
print('Shape:', f[dset].shape)
print('Data type:', f[dset].dtype)
This resulted in the following output:
Path: /Measurement_15524/Waveforms/waveforms_0
Shape: (200,)
Data type: [('digital_in', '<i4'), ('encoder_phi', '<f4'), ('encoder_theta', '<f4'), ('encoder_x',
'<f4'), ('encoder_y', '<f4'), ('encoder_z', '<f4'), ('id', '<i8'), ('line_index', '<i4'),
('motor_phi', '<f4'), ('motor_theta', '<f4'), ('motor_x', '<f4'), ('motor_y', '<f4'), ('motor_z',
'<f4'), ('sweep_dir', 'i1'), ('timestamp', '<f8'), ('type', 'S9'), ('waveform', '<f8', (4096,)),
('x_offset', '<f8'), ('x_spacing', '<f8')]
I believe that the data should be shaped in columns and rows, how can I visualise the output in that way?
How can I transform this into a .txt file?
I have been told that this problem could be arising from the data being str and not float, so I have attempted to put it as a lot by substituting the third line , f, by:
f= h5py.File(float('15524.h5'), 'r')
But python stated that:
ValueError: could not convert string to float: '15524.h5'
I don't programme often so my apologies if this is common knowledge.