H5LTget_attribute_double

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

H5LTget_attribute_double

Dominik Szczerba-2
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20080104/bde5af1f/attachment.bin>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

H5LTget_attribute_double

Pedro Vicente Nunes
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 at hdfgroup.org
-------------- next part --------------
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help at hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

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


-------------- next part --------------
----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

H5LTget_attribute_double

Dominik Szczerba-2
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 at hdfgroup.org
>



--
Dominik Szczerba, Ph.D.
Computer Vision Lab CH-8092 Zurich
http://www.vision.ee.ethz.ch/~domi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20080104/c87fc8cc/attachment.bin>

Loading...