H5LTget_attribute_double

Hi,

How can I know the size of the 'data' buffer in the below call?

herr_t H5LTget_attribute_double( hid_t loc_id, const char *obj_name, const
char *attr_name, double *data )

Thanks and regards,

···

--
Dominik Szczerba, Ph.D.
Computer Vision Lab CH-8092 Zurich
http://www.vision.ee.ethz.ch/~domi

Hi Dominik

You have to use both H5LTget_attribute_ndims and H5LTget_attribute_info, described here

http://www.hdfgroup.uiuc.edu/HDF5/doc_1.8pre/doc/HL/RM_H5LT.html

That is of course if you don't know in advance the number of elements in the attribute and its type size (if you do know, then it's just a matter of defining a static buffer to read your data).

Like the question from your previous post, an example for this behavior is not very well documented.
It is on our TO DO list to write a user's manual for the Lite API.

I wrote an example that uses both approaches , when we do know this information and when we do not. I send it here and as an attachment

let me know if you have further questions.
thanks !
Pedro

#include "hdf5.h"
#include "hdf5_hl.h"
#include <stdlib.h>

#define ATTR_SIZE 5

int main( void )
{
     hid_t fid;

     /* for dataset */
     int rank = 2;
     hsize_t dims[2] = {2,3};

     /* for attribute */
     hsize_t attr_dims[1] = { ATTR_SIZE };
     double buf[ATTR_SIZE] = {1,2,3,4,5};

     double bufr[ATTR_SIZE]; /* static buffer to read attribute */
     double *bufrd = NULL; /* dynamic buffer to read attribute */
     hsize_t *dimsr = NULL;
     int rankr;
     size_t type_size;
     H5T_class_t type_class;
     int nelmts;
     int i;

     /* create a file */
     fid = H5Fcreate("ex_lite.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

     /* make a dataset */
     if ( H5LTmake_dataset( fid, "my_dset", rank, dims, H5T_NATIVE_INT, NULL ) < 0 )
         goto out;

     /* create and write the attribute "attr1" on the dataset "my_dset" */
     if ( H5LTset_attribute_double(fid, "my_dset", "attr1", buf, ATTR_SIZE ) < 0 )
         goto out;

    /*-------------------------------------------------------------------------
     * dynamic allocation

ex_attr_dim.c (3.87 KB)

···

At 02:13 AM 1/4/2008, Dominik Szczerba wrote:

Hi,

How can I know the size of the 'data' buffer in the below call?

herr_t H5LTget_attribute_double( hid_t loc_id, const char *obj_name, const
char *attr_name, double *data )

Thanks and regards,

--
Dominik Szczerba, Ph.D.
Computer Vision Lab CH-8092 Zurich
http://www.vision.ee.ethz.ch/~domi

     *-------------------------------------------------------------------------
     */

     /* get attribute rank */
     if ( H5LTget_attribute_ndims(fid, "my_dset", "attr1", &rankr ) < 0 )
         goto out;

     printf("rank is %d\n", rankr);

     dimsr = malloc( sizeof(hsize_t) * rankr );

     /* get the type size and dimensions */
     if ( H5LTget_attribute_info(fid, "my_dset", "attr1", dimsr, &type_class, &type_size) < 0 )
         goto out;

     /* calculate number of elements */
     nelmts = 1;
     for(i = 0; i < rankr; i++)
         nelmts *= dimsr[i];

     printf("number of elements is %d and its size is %d\n", nelmts, type_size);

     /* allocate a buffer to read */
     bufrd = malloc( type_size * nelmts );

     /* get the attribute "attr1" from the dataset "my_dset" using the dynamic buffer */
     if ( H5LTget_attribute_double(fid, "my_dset", "attr1", bufrd ) < 0 )
         goto out;

     for(i = 0; i < nelmts; i++ )
         printf(" %f", bufrd[i]);
     printf("\n");

    /*-------------------------------------------------------------------------
     * static allocation
     *-------------------------------------------------------------------------
     */

     /* get the attribute "attr1" from the dataset "my_dset" using the static buffer */
     if ( H5LTget_attribute_double(fid, "my_dset", "attr1", bufr ) < 0 )
         goto out;

     for(i = 0; i < ATTR_SIZE; i++ )
         printf(" %f", bufr[i]);
     printf("\n");

     /* close file */
     H5Fclose(fid);

     if ( dimsr != NULL )
         free( dimsr );

     if ( bufrd != NULL )
         free( bufrd );

     return 1;

out:

     printf("error \n");

     return 0;
}

output is

rank is 1
number of elements is 5 and its size is 8
   1.000000 2.000000 3.000000 4.000000 5.000000

Pedro Vicente Nunes
--------------------------------------------------------------

HDF5 tools main developer
phone: (217)-265-0311
pvn@hdfgroup.org

Hi Pedro,
Now it's perfectly clear. Thanks a lot!
Dominik

···

On Friday 04 January 2008 17.55:20 Pedro Vicente Nunes wrote:

At 02:13 AM 1/4/2008, Dominik Szczerba wrote:
>Hi,
>
>How can I know the size of the 'data' buffer in the below call?
>
>herr_t H5LTget_attribute_double( hid_t loc_id, const char *obj_name, const
>char *attr_name, double *data )
>
>Thanks and regards,
>
>--
>Dominik Szczerba, Ph.D.
>Computer Vision Lab CH-8092 Zurich
>http://www.vision.ee.ethz.ch/~domi

Hi Dominik

You have to use both H5LTget_attribute_ndims and H5LTget_attribute_info,
described here

http://www.hdfgroup.uiuc.edu/HDF5/doc_1.8pre/doc/HL/RM_H5LT.html

That is of course if you don't know in advance the number of elements in
the attribute and its type size (if you do know, then it's just a matter of
defining a static buffer to read your data).

Like the question from your previous post, an example for this behavior is
not very well documented.
It is on our TO DO list to write a user's manual for the Lite API.

I wrote an example that uses both approaches , when we do know this
information and when we do not. I send it here and as an attachment

let me know if you have further questions.
thanks !
Pedro

#include "hdf5.h"
#include "hdf5_hl.h"
#include <stdlib.h>

#define ATTR_SIZE 5

int main( void )
{
     hid_t fid;

     /* for dataset */
     int rank = 2;
     hsize_t dims[2] = {2,3};

     /* for attribute */
     hsize_t attr_dims[1] = { ATTR_SIZE };
     double buf[ATTR_SIZE] = {1,2,3,4,5};

     double bufr[ATTR_SIZE]; /* static buffer to read attribute */
     double *bufrd = NULL; /* dynamic buffer to read attribute */
     hsize_t *dimsr = NULL;
     int rankr;
     size_t type_size;
     H5T_class_t type_class;
     int nelmts;
     int i;

     /* create a file */
     fid = H5Fcreate("ex_lite.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

     /* make a dataset */
     if ( H5LTmake_dataset( fid, "my_dset", rank, dims, H5T_NATIVE_INT,
NULL ) < 0 )
         goto out;

     /* create and write the attribute "attr1" on the dataset "my_dset" */
     if ( H5LTset_attribute_double(fid, "my_dset", "attr1", buf, ATTR_SIZE
) < 0 )
         goto out;

    /*-------------------------------------------------------------------------
     * dynamic allocation
     

*-------------------------------------------------------------------------

     */

     /* get attribute rank */
     if ( H5LTget_attribute_ndims(fid, "my_dset", "attr1", &rankr ) < 0 )
         goto out;

     printf("rank is %d\n", rankr);

     dimsr = malloc( sizeof(hsize_t) * rankr );

     /* get the type size and dimensions */
     if ( H5LTget_attribute_info(fid, "my_dset", "attr1", dimsr,
&type_class, &type_size) < 0 )
         goto out;

     /* calculate number of elements */
     nelmts = 1;
     for(i = 0; i < rankr; i++)
         nelmts *= dimsr[i];

     printf("number of elements is %d and its size is %d\n", nelmts,
type_size);

     /* allocate a buffer to read */
     bufrd = malloc( type_size * nelmts );

     /* get the attribute "attr1" from the dataset "my_dset" using the
dynamic buffer */
     if ( H5LTget_attribute_double(fid, "my_dset", "attr1", bufrd ) < 0 )
         goto out;

     for(i = 0; i < nelmts; i++ )
         printf(" %f", bufrd[i]);
     printf("\n");

    /*-------------------------------------------------------------------------
     * static allocation
     

*-------------------------------------------------------------------------

     */

     /* get the attribute "attr1" from the dataset "my_dset" using the
static buffer */
     if ( H5LTget_attribute_double(fid, "my_dset", "attr1", bufr ) < 0 )
         goto out;

     for(i = 0; i < ATTR_SIZE; i++ )
         printf(" %f", bufr[i]);
     printf("\n");

     /* close file */
     H5Fclose(fid);

     if ( dimsr != NULL )
         free( dimsr );

     if ( bufrd != NULL )
         free( bufrd );

     return 1;

out:

     printf("error \n");

     return 0;
}

output is

rank is 1
number of elements is 5 and its size is 8
   1.000000 2.000000 3.000000 4.000000 5.000000
   1.000000 2.000000 3.000000 4.000000 5.000000

Pedro Vicente Nunes
--------------------------------------------------------------
http://hdfgroup.org/
HDF5 tools main developer
phone: (217)-265-0311
pvn@hdfgroup.org

--
Dominik Szczerba, Ph.D.
Computer Vision Lab CH-8092 Zurich
http://www.vision.ee.ethz.ch/~domi