Using the C HDF5 library, I’m trying to use a prepopulated vector and insert it into my HDF5 file. I would like to not have to recopy the data and just add the vector I have. Is there any way I can get H5Tarray_create to work in my case? I want to use H5Tarray because of it’s easier to debug in HDFView (and looks better). H5Tvlen_create does work, but doesn’t get the desired effect in HDFView.
struct Key {
float code, value;
};
struct Instruction {
float timestamp;
Key *keys;
};
struct v_Instruction {
float timestamp;
hvl_t keys;
};
//---
std::vector<Key> keys(2);
keys[0].code = 1;
keys[0].value = 123;
keys[1].code = 2;
keys[1].value = 234;
const hsize_t arr_dims[] = {keys.size()};
hid_t compoundKey = H5Tcreate(H5T_COMPOUND, sizeof(Key));
H5Tinsert(compoundKey, "Code", HOFFSET(Key, code), H5T_NATIVE_FLOAT);
H5Tinsert(compoundKey, "Value", HOFFSET(Key, value), H5T_NATIVE_FLOAT);
//
// Wanted: Array Method
//
hid_t key_array = H5Tarray_create(compoundKey, 1, arr_dims);
size_t s_Instruction = sizeof(Instruction::timestamp) + sizeof(Key) * keys.size();
hid_t compoundInstr = H5Tcreate(H5T_COMPOUND, s_Instruction);
H5Tinsert(compoundInstr, "timestamp", 0, H5T_NATIVE_FLOAT);
H5Tinsert(compoundInstr, "Keys", sizeof(Instruction::timestamp), key_array);
//Create Dataset
//Write Data
Instruction test;
test.timestamp = 1;
test.keys = &keys.front();
H5Dwrite(dset, compoundInstr, H5S_ALL, H5S_ALL, H5P_DEFAULT, &test); // Doesn't set data correctly
//
// V Len Method
//
hid_t vLen_key = H5Tvlen_create(compoundKey);
compoundInstr = H5Tcreate(H5T_COMPOUND, sizeof(v_Instruction));
H5Tinsert(compoundInstr, "timestamp", HOFFSET(v_Instruction, timestamp), H5T_NATIVE_FLOAT);
H5Tinsert(compoundInstr, "Keys", HOFFSET(v_Instruction, keys), vLen_key);
//Create Dataset
//Write Data
v_Instruction v_test;
v_test.timestamp = 1;
v_test.keys.len = keys.size();
v_test.keys.p = &keys.front();
H5Dwrite(dset, compoundInstr, H5S_ALL, H5S_ALL, H5P_DEFAULT, &v_test);