A possible solution ( Re: Help requested: how to read a compound )

Dear Richard,

As a relatively low-level language, C can usually do the things that other languages do, but it may take some more effort. In your case, the buffer into which you read the compound can serve as the "structure defined at runtime" that you were asking for. When your data is in the buffer (here compound_buffer is a pointer to it), you can extract fields from it with something like this:

field_offset = H5Tget_member_offset(...);
field_size = H5Tget_size(H5Tget_member_type(...));

Now, supposing that this field is of native integer type (so that field_size == sizeof(int) and byte order OK), you can extract it using memcpy:

int the_integer;

memcpy(&the_integer, (char *)compound_buffer+field_offset, field_size);

Would such an approach solve your problem?

If the data fields in the compound are of incompatible binary formats that need to be converted to native types, I think such conversions can be done by specifying a compound with the respective native types to the mem_type_id argument of H5Dread.

I remember Quincey explaining the compound conversion to me, and I think it goes like this: You would first have to iterate through the fields of the compound and create a new compound type containing the same fields with native memory types. If some fields are left out from the memory compound, it will work but the missing fields will be left unread. However, I'm not sure how the matching between the memory compound fields and file compound fields takes place: Is it by field name?

Quincey, do you read me? :wink: Please comment on my explanation on type conversion in reading compound types.

Best Regards,

Vesa Paatero

···

On Wed, 21 Oct 2009 15:23:48 -0500, Richard van Hees <R.M.van.Hees@sron.nl> wrote:
Dear Elena and Werner,

Thanks for your help and suggestions. I somehow hoped that I lacked
some knowledge as a C programmer, but I am afraid that I can not
acchieve in C what I would like to do. An alternative for my project
could be to move to c++. Would that help me?

Hi Vesa,

···

On Oct 23, 2009, at 4:58 AM, Vesa Paatero wrote:

On Wed, 21 Oct 2009 15:23:48 -0500, Richard van Hees <R.M.van.Hees@sron.nl >> > wrote:
Dear Elena and Werner,

Thanks for your help and suggestions. I somehow hoped that I lacked
some knowledge as a C programmer, but I am afraid that I can not
acchieve in C what I would like to do. An alternative for my project
could be to move to c++. Would that help me?

Dear Richard,

As a relatively low-level language, C can usually do the things that other languages do, but it may take some more effort. In your case, the buffer into which you read the compound can serve as the "structure defined at runtime" that you were asking for. When your data is in the buffer (here compound_buffer is a pointer to it), you can extract fields from it with something like this:

field_offset = H5Tget_member_offset(...);
field_size = H5Tget_size(H5Tget_member_type(...));

Now, supposing that this field is of native integer type (so that field_size == sizeof(int) and byte order OK), you can extract it using memcpy:

int the_integer;

memcpy(&the_integer, (char *)compound_buffer+field_offset, field_size);

Would such an approach solve your problem?

If the data fields in the compound are of incompatible binary formats that need to be converted to native types, I think such conversions can be done by specifying a compound with the respective native types to the mem_type_id argument of H5Dread.

I remember Quincey explaining the compound conversion to me, and I think it goes like this: You would first have to iterate through the fields of the compound and create a new compound type containing the same fields with native memory types. If some fields are left out from the memory compound, it will work but the missing fields will be left unread. However, I'm not sure how the matching between the memory compound fields and file compound fields takes place: Is it by field name?

Quincey, do you read me? :wink: Please comment on my explanation on type conversion in reading compound types.

  Yes, you are correct in your description above and it is by field name. :slight_smile:

    Quincey