Reading dataset from h5 with low level API

Hi guys,

I’m new in a using of h5py low level API. The h5 file is created by nastran. I try to get results from datasets for further processing. My script is opening the file, open the dataset, creates a numpy array (set all values to 1) for the data from dataset, sizes are match, and without any error message, reads the dataset, but the result array stays untouched.
Dataset type is compound. 3 columns of int64. 71 row in the dataset.

Variable sizes during the script runs:
data_butter: (71,)
dataspace: (71,)
fspace: (71,)
mspace: (71,)

please help me, what I’m doing wrong, or what is missing.

Thanks,
Tibor
Here is my script:

import h5py
import numpy as np

h5Path=r"C:\analysis_software\msc_h5reader_speed_check\for_h5py_forum"
fileName=“test_Nastran2023.3_result.h5”
h5Path+=“\”

Open the HDF5 file using the low-level API

h5file = h5py.h5f.open((h5Path + fileName).encode(), h5py.h5f.ACC_RDONLY)

Open the dataset using its path

h5GroupPath = b’/INDEX/NASTRAN/RESULT/ELEMENTAL/ELEMENT_FORCE/BEAM/’
dataset = h5py.h5d.open(h5file, h5GroupPath)

Get the dataspace (shape of the dataset)

dataspace = dataset.get_space()
dims = dataspace.get_simple_extent_dims()

Get the datatype

datatype = dataset.get_type()

Determine datatypes of the actual dataset (for type Compound)

i=0
dataStructure=
while True:
try:
datatype.get_member_class(i)
except ValueError:
break
dataStructure.append(datatype.get_member_type(i).dtype)
i+=1

Manually define a NumPy structured dtype

np_dtype = np.dtype([(“field1”, dataStructure[0]), (“field2”, dataStructure[1]), (“field3”, dataStructure[2])]) # Replace fields based on actual structure

Create a NumPy array with the correct dtype and shape

data_buffer = np.empty(dims, dtype=np_dtype)
data_buffer = np.full(dims,1,dtype=np_dtype)
print(“prefilled Data:”, data_buffer)

fspace = dataset.get_space()
fspace.select_all()
mspace = h5py.h5s.create_simple(dataset.shape)

Read data into the NumPy structured array

dataset.read(mspace, fspace, data_buffer)

Print structured data

print(“Structured Data:”, data_buffer)

Close dataset

dataset.close()

Close h5 file

h5file.close()

Hi all,

I found what was the issue. Looks like when the field names are not match in numpy and h5 dataset, then read function is not working.
After I replaced the following line in the code:

np_dtype = np.dtype([(“field1”, dataStructure[0]), (“field2”, dataStructure[1]), (“field3”, dataStructure[2])]) # Replace fields based on actual structure

to this:

np_dtype= dataset.dtype

the content of the dataset is correctly copied by read function into the memory.

Note: same issue occuring in case of high level api using read_direct function.