I am trying to write a compound type that contains a variable length string as a field to HDF5. Following is the approach that I took but the strings written to the H5 file are garbled as shown below:
Creating compound types with non-string types works without any issues. Any pointers on what might be the issue? Thanks.
using HDF5
struct Foo
x::String
end
function HDF5.datatype(::Type{Foo})
# compound dtype
dtype = HDF5.h5t_create(HDF5.H5T_COMPOUND, sizeof(Foo))
# var len string
varlenstr_dtype = HDF5Datatype(HDF5.h5t_copy(HDF5.H5T_C_S1))
HDF5.h5t_set_size(varlenstr_dtype.id, HDF5.H5T_VARIABLE)
HDF5.h5t_insert(dtype, "x", 0, varlenstr_dtype)
HDF5.HDF5Datatype(dtype)
end
data = map(Foo, ["hi", "there"])
h5open("/tmp/test.h5", "w") do file
g = g_create(file, "testGroup")
dtype = datatype(Foo)
size = length(data)
dset = d_create(g, "testDataset", HDF5.HDF5Datatype(dtype), dataspace(data))
HDF5.h5d_write(dset, dtype, HDF5.H5P_DEFAULT, HDF5.H5P_DEFAULT, HDF5.H5P_DEFAULT, data)
end
Output of the H5 file:
HDF5 "/tmp/test.h5" {
GROUP "/" {
GROUP "testGroup" {
DATASET "testDataset" {
DATATYPE H5T_COMPOUND {
H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
} "x";
}
DATASPACE SIMPLE { ( 2 ) / ( 2 ) }
DATA {
(0): {
"0Mc\006\37777777764\177"
},
(1): {
"PMc\006\37777777764\177"
}
}
}
}
}
}