Hello all,
This is a re-ask of a question with the same title from couple of years ago which was not answered.
I need to write fixed length strings with a padding of H5T_STR_NULLTERM. I have tried using both the high and low level APIs, but unable to get the desired output. I suspect I’m missing something obvious, it’s just not obvious to me .
What I want to get:
HDF5 "desired.hdf" {
GROUP "/" {
GROUP "grp1" {
ATTRIBUTE "A1" {
DATATYPE H5T_STRING {
STRSIZE 1;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): "5", "1", "2"
}
}
}
}
}
With the high level API, the fixed length strings have STRPAD value of H5T_STR_NULLPAD, cannot find a way to change that.
With the low level API, the padding is correct, but the strings aren’t written correctly:
HDF5 "low_level.hdf" {
GROUP "/" {
GROUP "grp1" {
ATTRIBUTE "A1" {
DATATYPE H5T_STRING {
STRSIZE 1;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): "", "", ""
}
}
}
}
}
Setup: OSX Catalina 10.15.7, Python 3.7.0, h5py 3.2.1
Code to reproduce:
import numpy as np
import h5py
x = 512
x_arr = np.frombuffer(str(x).encode("UTF-8"), dtype="|S1")
attribute_name = 'A1'
type_id = h5py.h5t.TypeID.copy(h5py.h5t.C_S1)
type_id.set_size(1)
type_id.set_strpad(h5py.h5t.STR_NULLTERM)
space = h5py.h5s.create_simple((len(x_arr),))
file_name = 'low_level.hdf'
with h5py.File(file_name, "a") as f:
grp = f.create_group('grp1')
aid = h5py.h5a.create(grp.id, attribute_name.encode('utf-8'), type_id, space)
aid.write(x_arr)
file_name = 'high_level.hdf'
with h5py.File(file_name, "a") as f:
grp = f.create_group('grp1')
grp.attrs[attribute_name] = x_arr
Thanks