While writing individual fields of Compound data type of an Attribute results other fields data got erased. Is there any way we can write individual fields at a time to an attribute compound data?
Note 1: Writing data for individual fields of a compound data type works for Dataset
Note 2: I know writing all data fields works, but we need to write individual fields
Note 3: Reading individual fields of a compound data type works for Attribute and Dataset
#define REAL_FIELD_NAME "Real"
#define IMAGINARY_FIELD_NAME "Imag"
typedef struct {
double real = 8;
double imag = 9;
} compl;
int main()
{
//Create a file
std::string _file_name("test.h5");
H5::H5File _file(_file_name, H5F_ACC_TRUNC);
//Create dataspace
hsize_t _dims = 1;
H5::DataSpace dataspace(1, &_dims);
//Create compound data type with two fields, 1. Real, 2. Imag
H5::CompType _comp_dt((2 * sizeof(double)));
_comp_dt.insertMember(REAL_FIELD_NAME, HOFFSET(compl, real), H5::PredType::NATIVE_DOUBLE);
_comp_dt.insertMember(IMAGINARY_FIELD_NAME, HOFFSET(compl, imag), H5::PredType::NATIVE_DOUBLE);
//Create Dataset with compound data type
H5::DataSet _dset(_file.createDataSet("/test_dataset", _comp_dt, dataspace));
//Create Attribute with compound data type
H5::Attribute _attr(_dset.createAttribute("/test_attr", _comp_dt, dataspace));
double _real = 9;
H5::CompType _comp_dt_real(sizeof(double));
_comp_dt_real.insertMember(REAL_FIELD_NAME, 0, H5::PredType::NATIVE_DOUBLE);
//Writing data to dtaset, only Real field
_dset.write(&_real, _comp_dt_real);
//Writing data to attribute, only Real field
_attr.write(_comp_dt_real, &_real);
double _imag = 8;
H5::CompType _comp_dt_imag(sizeof(double));
_comp_dt_imag.insertMember(IMAGINARY_FIELD_NAME, 0, H5::PredType::NATIVE_DOUBLE);
//Writing data to dtaset, only Imag field
_dset.write(&_imag, _comp_dt_imag);
//Writing data to Attribute, only Imag field, which clears Real field
_attr.write(_comp_dt_imag, &_imag);
/*following works*/
compl _real_imag;
H5::Attribute _attr_2(_dset.createAttribute("/test_attr_2", _comp_dt, dataspace));
H5::CompType _comp_dt_cmpl(sizeof(compl));
_comp_dt_cmpl.insertMember(REAL_FIELD_NAME, HOFFSET(compl, real), H5::PredType::NATIVE_DOUBLE);
_comp_dt_cmpl.insertMember(IMAGINARY_FIELD_NAME, HOFFSET(compl, imag), H5::PredType::NATIVE_DOUBLE);
_attr_2.write(_comp_dt_cmpl, &_real_imag);
return 0;
}
I got the response from HDF5 forum. Writing parts of attribute values not allowed.
Only for reference: https://forum.hdfgroup.org/t/writing-individual-fields-of-compound-data-of-an-attribute-erasing-remaining-fields/7612