Writing individual fields of Compound data of an Attribute erasing remaining fields


#1

Attribute with compound data, while writing data to individual fields erasing data from other fields (Same is working for dataset, but not working for attribute). Following is the sample code.
Note:I know writing all fields at a time works, but I want to write individual fields

Blockquote
#define REAL_FIELD_NAME “Real”
#define IMAGINARY_FIELD_NAME “Imag”
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, 0, H5::PredType::NATIVE_DOUBLE);
    _comp_dt.insertMember(IMAGINARY_FIELD_NAME, sizeof(double), 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*/
    typedef  struct {
        double real = 8;
        double imag = 9;
    } compl;

    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;
}

> Blockquote


#2

The documentation might be a little unclear at this point, but the ability to write only parts of an attribute value is not supported. That’s one of the differences between datasets and attributes. Both are (logically) array variables and the whole gamut of HDF5 datatypes is available for elements, but there are functional differences/they play different roles: For example, datasets are linked into the hierarchy while attributes decorate/describe other objects. You can perform partial I/O on datasets (dataspace selections and user-visible datatype fields), but attribute values are treated as indivisible entities. G.


#3

Thank you @gheber

FYI: it looks like reading partial data from an attribute is supported (but not writing), I am able to read individual fields of the compound attribute .