Exemple #1
0
/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Rget_region
 * Signature: (JI[B)J
 */
JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Rget_1region(JNIEnv *env, jclass clss, jlong dataset, jint ref_type, jbyteArray ref)
{
    hid_t    retVal = -1;
    jboolean isCopy;
    jbyte   *refP;

    if (ref_type != H5R_DATASET_REGION)  {
        h5badArgument( env, "H5Rget_region:  bad ref_type ");
    } /* end if */
    else if (ref == NULL) {
        h5nullArgument( env, "H5Rget_region:  ref is NULL");
    } /* end if */
    else if ( ENVPTR->GetArrayLength(ENVPAR ref) != H5R_DSET_REG_REF_BUF_SIZE) {
        h5badArgument( env, "H5Rget_region:  region ref input array != H5R_DSET_REG_REF_BUF_SIZE");
    } /* end if */
    else {
        refP = (jbyte*)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy);
        if (refP == NULL) {
            h5JNIFatalError(env,  "H5Rget_region:  ref not pinned");
        } /* end if */
        else {
            retVal = H5Rget_region((hid_t)dataset, (H5R_type_t)ref_type, refP);

            ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT);

            if (retVal < 0)
                h5libraryError(env);
        } /* end else */
    } /* end else */

    return (jlong)retVal;
} /* end Java_hdf_hdf5lib_H5__1H5Rget_1region */
Exemple #2
0
//--------------------------------------------------------------------------
// Function:    H5Object::p_get_region (protected)
// Purpose      Retrieves a dataspace with the region pointed to selected.
// Parameters
//              ref_type - IN: Type of reference to get region of - default
//                              to H5R_DATASET_REGION
//              ref      - IN: Reference to get region of
// Return       Dataspace id
// Exception    H5::IdComponentException
// Programmer   Binh-Minh Ribler - May, 2004
//--------------------------------------------------------------------------
hid_t H5Object::p_get_region(void *ref, H5R_type_t ref_type) const
{
   hid_t space_id = H5Rget_region(getId(), ref_type, ref);
   if (space_id < 0)
   {
      throw IdComponentException("", "H5Rget_region failed");
   }
   return(space_id);
}
Exemple #3
0
//--------------------------------------------------------------------------
// Function:	H5Location::getRegion
///\brief	Retrieves a dataspace with the region pointed to selected.
///\param	ref	 - IN: Reference to get region of
///\param	ref_type - IN: Type of reference to get region of - default
//				to H5R_DATASET_REGION
///\return	DataSpace object
///\exception	H5::ReferenceException
// Programmer	Binh-Minh Ribler - May, 2004
//--------------------------------------------------------------------------
DataSpace H5Location::getRegion(void *ref, H5R_type_t ref_type) const
{
   hid_t space_id = H5Rget_region(getId(), ref_type, ref);
   if (space_id < 0)
   {
      throw ReferenceException(inMemFunc("getRegion"), "H5Rget_region failed");
   }
   try {
      DataSpace dataspace(space_id);
      return(dataspace);
   }
   catch (DataSpaceIException E) {
      throw ReferenceException(inMemFunc("getRegion"), E.getDetailMsg());
   }
}
Exemple #4
0
/*----------------------------------------------------------------------------
 * Name:        h5rget_region_region_object_c
 * Purpose:     Call H5Rget_region to dereference dataspace region
 * Inputs:      dset_id - dataset identifier
 *              ref - reference to the dataset region
 * Outputs:     space_id - dereferenced  dataset dataspace identifier
 * Returns:     0 on success, -1 on failure
 * Programmer:  Elena Pourmal
 *              Wednesday, December 1, 1999
 * Modifications:
 *---------------------------------------------------------------------------*/
int_f
nh5rget_region_region_c (hid_t_f *dset_id, int_f *ref, hid_t_f *space_id)
{
     int ret_value = -1;
     hid_t c_dset_id;
     hid_t c_space_id;
     hdset_reg_ref_t ref_c;

     HDmemcpy (&ref_c, ref, H5R_DSET_REG_REF_BUF_SIZE);

     /*
      * Call H5Rget_region function.
      */
     c_dset_id = *dset_id;
     c_space_id = H5Rget_region(c_dset_id, H5R_DATASET_REGION, &ref_c);
     if(c_space_id < 0) return ret_value;
     *space_id = (hid_t_f)c_space_id;
     ret_value = 0;
     return ret_value;
}
Exemple #5
0
int do_copy_refobjs(hid_t fidin,
                    hid_t fidout,
                    trav_table_t *travt,
                    pack_opt_t *options) /* repack options */
{
    hid_t     grp_in=(-1);            /* read group ID */
    hid_t     grp_out=(-1);           /* write group ID */
    hid_t     dset_in=(-1);           /* read dataset ID */
    hid_t     dset_out=(-1);          /* write dataset ID */
    hid_t     type_in=(-1);           /* named type ID */
    hid_t     dcpl_id=(-1);           /* dataset creation property list ID */
    hid_t     space_id=(-1);          /* space ID */
    hid_t     ftype_id=(-1);          /* file data type ID */
    hid_t     mtype_id=(-1);          /* memory data type ID */
    size_t    msize;                  /* memory size of memory type */
    hsize_t   nelmts;                 /* number of elements in dataset */
    int       rank;                   /* rank of dataset */
    hsize_t   dims[H5S_MAX_RANK];     /* dimensions of dataset */
    unsigned int i, j;
    int       k;
    
    /*-------------------------------------------------------------------------
    * browse
    *-------------------------------------------------------------------------
    */
    
    for ( i = 0; i < travt->nobjs; i++)
    {
        switch ( travt->objs[i].type )
        {
        /*-------------------------------------------------------------------------
        * H5G_GROUP
        *-------------------------------------------------------------------------
        */
        case H5G_GROUP:
            
        /*-------------------------------------------------------------------------
        * copy referenced objects in attributes
        *-------------------------------------------------------------------------
        */
            
            if ((grp_out=H5Gopen(fidout,travt->objs[i].name))<0)
                goto error;
            if((grp_in = H5Gopen (fidin,travt->objs[i].name))<0)
                goto error;
            if (copy_refs_attr(grp_in,grp_out,options,travt,fidout)<0)
                goto error;
            if (H5Gclose(grp_out)<0)
                goto error;
            if (H5Gclose(grp_in)<0)
                goto error;
            
           /*-------------------------------------------------------------------------
            * check for hard links
            *-------------------------------------------------------------------------
            */
            
            if (travt->objs[i].nlinks)
            {
                for ( j=0; j<travt->objs[i].nlinks; j++)
                {
                    H5Glink(fidout,
                        H5G_LINK_HARD,
                        travt->objs[i].name,
                        travt->objs[i].links[j].new_name);
                }
            }
            
            break;
            
       /*-------------------------------------------------------------------------
       * H5G_DATASET
       *-------------------------------------------------------------------------
       */
        case H5G_DATASET:
            
            if ((dset_in=H5Dopen(fidin,travt->objs[i].name))<0)
                goto error;
            if ((space_id=H5Dget_space(dset_in))<0)
                goto error;
            if ((ftype_id=H5Dget_type (dset_in))<0)
                goto error;
            if ((dcpl_id=H5Dget_create_plist(dset_in))<0)
                goto error;
            if ( (rank=H5Sget_simple_extent_ndims(space_id))<0)
                goto error;
            if ( H5Sget_simple_extent_dims(space_id,dims,NULL)<0)
                goto error;
            nelmts=1;
            for (k=0; k<rank; k++)
                nelmts*=dims[k];
            
            if ((mtype_id=h5tools_get_native_type(ftype_id))<0)
                goto error;
            
            if ((msize=H5Tget_size(mtype_id))==0)
                goto error;
            
            
           /*-------------------------------------------------------------------------
           * check if the dataset creation property list has filters that
           * are not registered in the current configuration
           * 1) the external filters GZIP and SZIP might not be available
           * 2) the internal filters might be turned off
           *-------------------------------------------------------------------------
           */
            if (h5tools_canreadf((NULL),dcpl_id)==1)
            {
               /*-------------------------------------------------------------------------
                * test for a valid output dataset
                *-------------------------------------------------------------------------
                */
                dset_out = FAIL;
                
                /*-------------------------------------------------------------------------
                * object references are a special case
                * we cannot just copy the buffers, but instead we recreate the reference
                *-------------------------------------------------------------------------
                */
                if (H5Tequal(mtype_id, H5T_STD_REF_OBJ))
                {
                    H5G_obj_t1       obj_type;
                    hid_t            refobj_id;
                    hobj_ref_t       *refbuf=NULL; /* buffer for object references */
                    hobj_ref_t       *buf=NULL;
                    const char*      refname;
                    unsigned         u;
                    
                    /*-------------------------------------------------------------------------
                    * read to memory
                    *-------------------------------------------------------------------------
                    */
                    
                    if (nelmts)
                    {
                        buf=(void *) HDmalloc((unsigned)(nelmts*msize));
                        if ( buf==NULL){
                            printf( "cannot read into memory\n" );
                            goto error;
                        }
                        if (H5Dread(dset_in,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf)<0)
                            goto error;
                        
                        if ((obj_type = H5Rget_obj_type(dset_in,H5R_OBJECT,buf))<0)
                            goto error;
                        refbuf=HDcalloc((unsigned)nelmts,msize);
                        if ( refbuf==NULL){
                            printf( "cannot allocate memory\n" );
                            goto error;
                        }
                        for ( u=0; u<nelmts; u++)
                        {
                            H5E_BEGIN_TRY {
                                if ((refobj_id = H5Rdereference(dset_in,H5R_OBJECT,&buf[u]))<0)
                                    continue;
                            } H5E_END_TRY;
                            /* get the name. a valid name could only occur in the
                            second traversal of the file */
                            if ((refname=MapIdToName(refobj_id,travt))!=NULL)
                            {
                                /* create the reference, -1 parameter for objects */
                                if (H5Rcreate(&refbuf[u],fidout,refname,H5R_OBJECT,-1)<0)
                                    goto error;
                                if(options->verbose)
                                {
                                    
                                    
                                    printf(FORMAT_OBJ,"dset",travt->objs[i].name );
                                    printf("object <%s> object reference created to <%s>\n",
                                        travt->objs[i].name,
                                        refname);
                                }
                            }/*refname*/
                            close_obj(obj_type,refobj_id);
                        }/*  u */
                    }/*nelmts*/
                    
                     /*-------------------------------------------------------------------------
                     * create/write dataset/close
                     *-------------------------------------------------------------------------
                    */
                    if ((dset_out=H5Dcreate(fidout,travt->objs[i].name,mtype_id,space_id,dcpl_id))<0)
                        goto error;
                    if (nelmts) {
                        if (H5Dwrite(dset_out,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,refbuf)<0)
                            goto error;
                    }
                    
                    if (buf)
                        free(buf);
                    if (refbuf)
                        free(refbuf);
                    
                }/*H5T_STD_REF_OBJ*/
                
                 /*-------------------------------------------------------------------------
                 * dataset region references
                 *-------------------------------------------------------------------------
                */
                else if (H5Tequal(mtype_id, H5T_STD_REF_DSETREG))
                {
                    H5G_obj_t1       obj_type;
                    hid_t            refobj_id;
                    hdset_reg_ref_t  *refbuf=NULL; /* input buffer for region references */
                    hdset_reg_ref_t  *buf=NULL;    /* output buffer */
                    const char*      refname;
                    unsigned         u;
                    
                    /*-------------------------------------------------------------------------
                    * read input to memory
                    *-------------------------------------------------------------------------
                    */
                    if (nelmts)
                    {
                        buf=(void *) HDmalloc((unsigned)(nelmts*msize));
                        if ( buf==NULL){
                            printf( "cannot read into memory\n" );
                            goto error;
                        }
                        if (H5Dread(dset_in,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf)<0)
                            goto error;
                        if ((obj_type = H5Rget_obj_type(dset_in,H5R_DATASET_REGION,buf))<0)
                            goto error;
                        
                        /*-------------------------------------------------------------------------
                        * create output
                        *-------------------------------------------------------------------------
                        */
                        
                        refbuf=HDcalloc(sizeof(hdset_reg_ref_t),(size_t)nelmts); /*init to zero */
                        if ( refbuf==NULL){
                            printf( "cannot allocate memory\n" );
                            goto error;
                        }
                        for ( u=0; u<nelmts; u++)
                        {
                            H5E_BEGIN_TRY {
                                if ((refobj_id = H5Rdereference(dset_in,H5R_DATASET_REGION,&buf[u]))<0)
                                    continue;
                            } H5E_END_TRY;
                            
                            /* get the name. a valid name could only occur in the
                            second traversal of the file */
                            if ((refname=MapIdToName(refobj_id,travt))!=NULL)
                            {
                                hid_t region_id;    /* region id of the referenced dataset */
                                if ((region_id = H5Rget_region(dset_in,H5R_DATASET_REGION,&buf[u]))<0)
                                    goto error;
                                /* create the reference, we need the space_id */
                                if (H5Rcreate(&refbuf[u],fidout,refname,H5R_DATASET_REGION,region_id)<0)
                                    goto error;
                                if (H5Sclose(region_id)<0)
                                    goto error;
                                if(options->verbose)
                                {
                                    
                                    
                                    
                                    printf(FORMAT_OBJ,"dset",travt->objs[i].name );
                                    printf("object <%s> region reference created to <%s>\n",
                                        travt->objs[i].name,
                                        refname);
                                }
                            }/*refname*/
                            close_obj(obj_type,refobj_id);
                        }/*  u */
                    }/*nelmts*/
                    
                     /*-------------------------------------------------------------------------
                     * create/write dataset/close
                     *-------------------------------------------------------------------------
                    */
                    if ((dset_out=H5Dcreate(fidout,travt->objs[i].name,mtype_id,space_id,dcpl_id))<0)
                        goto error;
                    if (nelmts) {
                        if (H5Dwrite(dset_out,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,refbuf)<0)
                            goto error;
                    }
                    
                    if (buf)
                        free(buf);
                    if (refbuf)
                        free(refbuf);
                } /* H5T_STD_REF_DSETREG */
                
                
               /*-------------------------------------------------------------------------
                * not references, open previously created object in 1st traversal
                *-------------------------------------------------------------------------
                */
                else
                {
                    if ((dset_out=H5Dopen(fidout,travt->objs[i].name))<0)
                        goto error;
                }
                
                assert(dset_out!=FAIL);
                
                /*-------------------------------------------------------------------------
                * copy referenced objects in attributes
                *-------------------------------------------------------------------------
                */
                if (copy_refs_attr(dset_in,dset_out,options,travt,fidout)<0)
                    goto error;
                
                
                /*-------------------------------------------------------------------------
                * check for hard links
                *-------------------------------------------------------------------------
                */
                if (travt->objs[i].nlinks)
                {
                    for ( j=0; j<travt->objs[i].nlinks; j++){
                        H5Glink(fidout,
                            H5G_LINK_HARD,
                            travt->objs[i].name,
                            travt->objs[i].links[j].new_name);
                    }
                }
                
                if (H5Dclose(dset_out)<0)
                    goto error;
                
   }/*can_read*/
int 
main(void)
{
    hid_t		fid1;		/* HDF5 File IDs		*/
    hid_t		dset1,	/* Dataset ID			*/
                dset2;      /* Dereferenced dataset ID */
    hid_t		sid1,       /* Dataspace ID	#1		*/
                sid2;       /* Dataspace ID	#2		*/
    hsize_t *   coords;             /* Coordinate buffer */
    hsize_t		low[SPACE2_RANK];   /* Selection bounds */
    hsize_t		high[SPACE2_RANK];     /* Selection bounds */
    hdset_reg_ref_t      *rbuf;      /* buffer to to read disk */
    int    *drbuf;      /* Buffer for reading numeric data from disk */
    int        i, j;          /* counting variables */
    herr_t		ret;		/* Generic return value		*/

    /* Output message about test being performed */

    /* Allocate write & read buffers */
    rbuf=malloc(sizeof(hdset_reg_ref_t)*SPACE1_DIM1);
    drbuf=calloc(sizeof(int),SPACE2_DIM1*SPACE2_DIM2);

    /* Open the file */
    fid1 = H5Fopen(FILE2, H5F_ACC_RDWR, H5P_DEFAULT);

    /* Open the dataset */
    dset1=H5Dopen(fid1,"/Dataset1");

    /* Read selection from disk */
    ret=H5Dread(dset1,H5T_STD_REF_DSETREG,H5S_ALL,H5S_ALL,H5P_DEFAULT,rbuf);

    /* Try to open objects */
    dset2 = H5Rdereference(dset1,H5R_DATASET_REGION,&rbuf[0]);

    /* Check information in referenced dataset */
    sid1 = H5Dget_space(dset2);

    ret=H5Sget_simple_extent_npoints(sid1);
    printf(" Number of elements in the dataset is : %d\n",ret);

    /* Read from disk */
    ret=H5Dread(dset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,drbuf);

    for(i=0; i<SPACE2_DIM1; i++) {
        for (j=0; j<SPACE2_DIM2; j++) printf (" %d ", drbuf[i*SPACE2_DIM2+j]);
        printf("\n"); }

    /* Get the hyperslab selection */
    sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[0]);

    /* Verify correct hyperslab selected */
    ret = H5Sget_select_npoints(sid2);
    printf(" Number of elements in the hyperslab is : %d \n", ret);
    ret = H5Sget_select_hyper_nblocks(sid2);
    coords=malloc(ret*SPACE2_RANK*sizeof(hsize_t)*2); /* allocate space for the hyperslab blocks */
    ret = H5Sget_select_hyper_blocklist(sid2,0,ret,coords);
    printf(" Hyperslab coordinates are : \n");
    printf (" ( %lu , %lu ) ( %lu , %lu ) \n", \
(unsigned long)coords[0],(unsigned long)coords[1],(unsigned long)coords[2],(unsigned long)coords[3]); 
    free(coords);
    ret = H5Sget_select_bounds(sid2,low,high);

    /* Close region space */
    ret = H5Sclose(sid2);

    /* Get the element selection */
    sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[1]);

    /* Verify correct elements selected */
    ret = H5Sget_select_elem_npoints(sid2);
    printf(" Number of selected elements is : %d\n", ret);

    /* Allocate space for the element points */
    coords= malloc(ret*SPACE2_RANK*sizeof(hsize_t)); 
    ret = H5Sget_select_elem_pointlist(sid2,0,ret,coords);
    printf(" Coordinates of selected elements are : \n");
    for (i=0; i<2*NPOINTS; i=i+2) 
         printf(" ( %lu , %lu ) \n", (unsigned long)coords[i],(unsigned long)coords[i+1]); 
          
    free(coords);
    ret = H5Sget_select_bounds(sid2,low,high);

    /* Close region space */
    ret = H5Sclose(sid2);

    /* Close first space */
    ret = H5Sclose(sid1);

    /* Close dereferenced Dataset */
    ret = H5Dclose(dset2);

    /* Close Dataset */
    ret = H5Dclose(dset1);

    /* Close file */
    ret = H5Fclose(fid1);

    /* Free memory buffers */
    free(rbuf);
    free(drbuf);
    return 0;
}   
Exemple #7
0
int
main (void)
{
    hid_t               file, space, memspace, dset, dset2, attr;
                                                    /* Handles */
    herr_t              status;
    hsize_t             dims[1] = {DIM0},
                        dims2[2] = {DS2DIM0, DS2DIM1},
                        coords[4][2] = { {0,  1},
                                         {2, 11},
                                         {1,  0},
                                         {2,  4} },
                        start[2] = {0, 0},
                        stride[2] = {2, 11},
                        count[2] = {2, 2},
                        block[2] = {1, 3};
    hssize_t            npoints;
    hdset_reg_ref_t     wdata[DIM0],                /* Write buffer */
                        *rdata;                     /* Read buffer */
    ssize_t             size;
    char                wdata2[DS2DIM0][DS2DIM1] = {"The quick brown",
                                                    "fox jumps over ",
                                                    "the 5 lazy dogs"},
                        *rdata2,
                        *name;
    int                 ndims,
                        i;

    /*
     * Create a new file using the default properties.
     */
    file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /*
     * Create a dataset with character data.
     */
    space = H5Screate_simple (2, dims2, NULL);
    dset2 = H5Dcreate (file, DATASET2, H5T_STD_I8LE, space, H5P_DEFAULT,
                H5P_DEFAULT, H5P_DEFAULT);
    status = H5Dwrite (dset2, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                wdata2);

    /*
     * Create reference to a list of elements in dset2.
     */
    status = H5Sselect_elements (space, H5S_SELECT_SET, 4, coords[0]);
    status = H5Rcreate (&wdata[0], file, DATASET2, H5R_DATASET_REGION, space);

    /*
     * Create reference to a hyperslab in dset2, close dataspace.
     */
    status = H5Sselect_hyperslab (space, H5S_SELECT_SET, start, stride, count,
                block);
    status = H5Rcreate (&wdata[1], file, DATASET2, H5R_DATASET_REGION, space);
    status = H5Sclose (space);

    /*
     * Create dataset with a null dataspace to serve as the parent for
     * the attribute.
     */
    space = H5Screate (H5S_NULL);
    dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT,
                H5P_DEFAULT, H5P_DEFAULT);
    status = H5Sclose (space);

    /*
     * Create dataspace.  Setting maximum size to NULL sets the maximum
     * size to be the current size.
     */
    space = H5Screate_simple (1, dims, NULL);

    /*
     * Create the attribute and write the region references to it.
     */
    attr = H5Acreate (dset, ATTRIBUTE, H5T_STD_REF_DSETREG, space, H5P_DEFAULT,
                    H5P_DEFAULT);
    status = H5Awrite (attr, H5T_STD_REF_DSETREG, wdata);

    /*
     * Close and release resources.
     */
    status = H5Aclose (attr);
    status = H5Dclose (dset);
    status = H5Dclose (dset2);
    status = H5Sclose (space);
    status = H5Fclose (file);


    /*
     * Now we begin the read section of this example.  Here we assume
     * the attribute has the same name and rank, but can have any size.
     * Therefore we must allocate a new array to read in data using
     * malloc().
     */

    /*
     * Open file, dataset, and attribute.
     */
    file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
    dset = H5Dopen (file, DATASET, H5P_DEFAULT);
    attr = H5Aopen (dset, ATTRIBUTE, H5P_DEFAULT);

    /*
     * Get dataspace and allocate memory for read buffer.
     */
    space = H5Aget_space (attr);
    ndims = H5Sget_simple_extent_dims (space, dims, NULL);
    rdata = (hdset_reg_ref_t *) malloc (dims[0] * sizeof (hdset_reg_ref_t));
    status = H5Sclose (space);

    /*
     * Read the data.
     */
    status = H5Aread (attr, H5T_STD_REF_DSETREG, rdata);

    /*
     * Output the data to the screen.
     */
    for (i=0; i<dims[0]; i++) {
        printf ("%s[%d]:\n  ->", ATTRIBUTE, i);

        /*
         * Open the referenced object, retrieve its region as a
         * dataspace selection.
         */
        dset2 = H5Rdereference (dset, H5P_DEFAULT, H5R_DATASET_REGION, &rdata[i]);
        space = H5Rget_region (dset, H5R_DATASET_REGION, &rdata[i]);

        /*
         * Get the length of the object's name, allocate space, then
         * retrieve the name.
         */
        size = 1 + H5Iget_name (dset2, NULL, 0);
        name = (char *) malloc (size);
        size = H5Iget_name (dset2, name, size);

        /*
         * Allocate space for the read buffer.  We will only allocate
         * enough space for the selection, plus a null terminator.  The
         * read buffer will be 1-dimensional.
         */
        npoints = H5Sget_select_npoints (space);
        rdata2 = (char *) malloc (npoints + 1);

        /*
         * Read the dataset region, and add a null terminator so we can
         * print it as a string.
         */
        memspace = H5Screate_simple (1, (hsize_t *) &npoints, NULL);
        status = H5Dread (dset2, H5T_NATIVE_CHAR, memspace, space, H5P_DEFAULT,
                    rdata2);
        rdata2[npoints] = '\0';

        /*
         * Print the name and region data, close and release resources.
         */
        printf (" %s: %s\n", name, rdata2);
        free (rdata2);
        free (name);
        status = H5Sclose (space);
        status = H5Sclose (memspace);
        status = H5Dclose (dset2);
    }

    /*
     * Close and release resources.
     */
    free (rdata);
    status = H5Aclose (attr);
    status = H5Dclose (dset);
    status = H5Fclose (file);

    return 0;
}
void H5ReferenceData::printData(std::ostream & os, const unsigned int pos, const unsigned int indentLevel) const
{
    char * cdata = static_cast<char *>(data) + offset + pos * (stride ? stride : dataSize);
    void ** ref = &(((void **)cdata)[0]);
    hid_t file = getFile().getH5Id();
    hid_t obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref);
    if (obj < 0)
    {
        os << "NULL";
        return;
    }

    ssize_t size = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0);
    char * name = 0;
    H5O_info_t info;

    if (size != -1)
    {
        name = new char[size + 1];
        H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, size + 1);

        if (datasetReference == H5R_OBJECT)
        {
            H5Oget_info(obj, &info);
            H5Oclose(obj);

            switch (info.type)
            {
                case H5O_TYPE_GROUP:
                    os << "GROUP ";
                    break;
                case H5O_TYPE_DATASET:
                    os << "DATASET ";
                    break;
                case H5O_TYPE_NAMED_DATATYPE:
                    os << "DATATYPE ";
                    break;
                case H5O_TYPE_UNKNOWN:
                default:
                    if (size != -1)
                    {
                        delete[] name;
                    }
                    throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object"));
            }

            os << (haddr_t)(*ref) << " " << name;
        }
        else
        {
            hid_t space = H5Rget_region(file, H5R_DATASET_REGION, ref);
            hssize_t npoints = H5Sget_select_elem_npoints(space);
            hsize_t ndims = H5Sget_simple_extent_dims(space, 0, 0);
            H5Oclose(obj);

            os << "DATASET " << name << " {";

            if (npoints >= 0)
            {
                const hsize_t N = ndims * npoints;
                hsize_t * buf = new hsize_t[N];
                herr_t err =  H5Sget_select_elem_pointlist(space, 0, npoints, buf);
                for (hssize_t i = 0; i < (hssize_t)N; i += ndims)
                {
                    os << "(";
                    for (unsigned int j = 0; j < ndims - 1; j++)
                    {
                        os << buf[i + j] << ",";
                    }
                    os << buf[i + ndims - 1] << ")";

                    if (i != N - ndims)
                    {
                        os << ", ";
                    }
                    else
                    {
                        os << "}";
                    }
                }
                delete[] buf;
            }
            else
            {
                hssize_t nblocks = H5Sget_select_hyper_nblocks(space);
                if (nblocks >= 0)
                {
                    const hsize_t N = 2 * ndims * nblocks;
                    hsize_t * buf = new hsize_t[N];
                    herr_t err = H5Sget_select_hyper_blocklist(space, 0, nblocks, buf);
                    for (hssize_t i = 0; i < (hssize_t)N; i += 2 * ndims)
                    {
                        os << "(";
                        for (unsigned int j = 0; j < ndims - 1; j++)
                        {
                            os << buf[i + j] << ",";
                        }
                        os << buf[i + ndims - 1] << ")-(";
                        for (unsigned int j = 0; j < ndims - 1; j++)
                        {
                            os << buf[i + ndims + j] << ",";
                        }
                        os << buf[i + 2 * ndims - 1] << ")";

                        if (i != N - 2 * ndims)
                        {
                            os << ", ";
                        }
                        else
                        {
                            os << "}";
                        }
                    }

                    delete[] buf;
                }
            }

            H5Sclose(space);
        }

        delete[] name;
    }
}
int main(void)
{
    hid_t file_id;        /* file identifier */
    hid_t space_id;       /* dataspace identifiers */
    hid_t spacer_id;
    hid_t dsetv_id;       /*dataset identifiers*/
    hid_t dsetr_id;
    hsize_t dims[2] =  {2,9};
    hsize_t dimsr[1] =  {2};
    int rank = 2;
    int rankr =1;
    herr_t status;
    hdset_reg_ref_t ref[2];
    hdset_reg_ref_t ref_out[2];
    int data[2][9] = {{1,1,2,3,3,4,5,5,6},{1,2,2,3,4,4,5,6,6}};
    int data_out[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
    hsize_t start[2];
    hsize_t count[2];
    hsize_t coord[2][3] = {{0, 0, 1}, {6, 0, 8}};
    unsigned num_points = 3;
    int i, j;
    size_t name_size1, name_size2;
    char buf1[10], buf2[10];

    /*
     * Create file with default file access and file creation properties.
     */
    file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /*
     * Create dataspace for datasets.
     */
    space_id = H5Screate_simple(rank, dims, NULL);
    spacer_id = H5Screate_simple(rankr, dimsr, NULL);

    /*
     * Create integer dataset.
     */
    dsetv_id = H5Dcreate2(file_id, dsetnamev, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /*
     * Write data to the dataset.
     */
    status = H5Dwrite(dsetv_id, H5T_NATIVE_INT, H5S_ALL , H5S_ALL, H5P_DEFAULT,data);
    status = H5Dclose(dsetv_id);

    /*
     * Dataset with references.
     */
    dsetr_id = H5Dcreate2(file_id, dsetnamer, H5T_STD_REF_DSETREG, spacer_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /*
     * Create a reference to the hyperslab.
     */
    start[0] = 0;
    start[1] = 3;
    count[0] = 2;
    count[1] = 3;
    status = H5Sselect_hyperslab(space_id, H5S_SELECT_SET, start, NULL, count, NULL);
    status = H5Rcreate(&ref[0], file_id, dsetnamev, H5R_DATASET_REGION, space_id);

    /*
     * Create a reference to elements selection.
     */
    status = H5Sselect_none(space_id);
    status = H5Sselect_elements(space_id, H5S_SELECT_SET, num_points, (const hsize_t *)coord);
    status = H5Rcreate(&ref[1], file_id, dsetnamev, H5R_DATASET_REGION, space_id);

    /*
     * Write dataset with the references.
     */
    status = H5Dwrite(dsetr_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT,ref);

    /*
     * Close all objects.
     */
    status = H5Sclose(space_id);
    status = H5Sclose(spacer_id);
    status = H5Dclose(dsetr_id);
    status = H5Fclose(file_id);

    /*
     * Reopen the file to read selections back.
     */
    file_id = H5Fopen(filename, H5F_ACC_RDWR,  H5P_DEFAULT);

    /*
     * Reopen the dataset with object references and read references
     * to the buffer.
     */
    dsetr_id = H5Dopen2(file_id, dsetnamer, H5P_DEFAULT);

    status = H5Dread(dsetr_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL,
                   H5P_DEFAULT, ref_out);

    /*
     * Dereference the first reference.
     */
    dsetv_id = H5Rdereference2(dsetr_id, H5P_DEFAULT, H5R_DATASET_REGION, &ref_out[0]);
    /*
     * Get name of the dataset the first region reference points to
     * using H5Rget_name
     */
    name_size1 = H5Rget_name(dsetr_id, H5R_DATASET_REGION, &ref_out[0], (char*)buf1, 10);
    printf(" Dataset's name (returned by H5Rget_name) the reference points to is %s, name length is %d\n", buf1, (int)name_size1);
    /*
     * Get name of the dataset the first region reference points to
     * using H5Iget_name
     */
    name_size2 = H5Iget_name(dsetv_id, (char*)buf2, 10);
    printf(" Dataset's name (returned by H5Iget_name) the reference points to is %s, name length is %d\n", buf2, (int)name_size2);

    space_id = H5Rget_region(dsetr_id, H5R_DATASET_REGION,&ref_out[0]);

    /*
     * Read and display hyperslab selection from the dataset.
     */

    status = H5Dread(dsetv_id, H5T_NATIVE_INT, H5S_ALL, space_id,
                   H5P_DEFAULT, data_out);
    printf("Selected hyperslab: ");
    for (i = 0; i <= 1; i++)
    {
        printf("\n");
        for (j = 0; j <= 8; j++)
            printf("%d ", data_out[i][j]);
    }
    printf("\n");

    /*
     * Close dataspace and the dataset.
     */
    status = H5Sclose(space_id);
    status = H5Dclose(dsetv_id);

    /*
     * Initialize data_out array again to get point selection.
     */
    for (i = 0; i <= 1; i++)
        for (j = 0; j <= 8; j++)
            data_out[i][j] = 0;

    /*
     * Dereference the second reference.
     */
    dsetv_id = H5Rdereference2(dsetr_id, H5P_DEFAULT, H5R_DATASET_REGION, &ref_out[1]);
    space_id = H5Rget_region(dsetv_id, H5R_DATASET_REGION,&ref_out[1]);

    /*
     * Read selected data from the dataset.
     */

    status = H5Dread(dsetv_id, H5T_NATIVE_INT, H5S_ALL, space_id,
                   H5P_DEFAULT, data_out);
    printf("Selected points: ");
    for (i = 0; i <= 1; i++)
    {
        printf("\n");
        for (j = 0; j <= 8; j++)
            printf("%d ", data_out[i][j]);
    }
    printf("\n");

    /*
     * Close dataspace and the dataset.
     */
    status = H5Sclose(space_id);
    status = H5Dclose(dsetv_id);
    status = H5Dclose(dsetr_id);
    status = H5Fclose(file_id);

    return 0;
}