int readBooleanSparseMatrix_v1(int _iDatasetId, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos)
{
    hid_t obj = 0;
    hobj_ref_t pRef[2] = {0};
    herr_t status;

    /*
    * Read the data.
    */
    status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef);
    if (status < 0)
    {
        return -1;
    }

    //read Row data
    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[0]);
    status = readInteger32Matrix_v1(obj, 1, _iRows, _piNbItemRow);
    if (status < 0)
    {
        return -1;
    }

    //read cols data
    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[1]);
    status = readInteger32Matrix_v1(obj, 1, _iNbItem, _piColPos);
    if (status < 0)
    {
        return -1;
    }

    return 0;
}
示例#2
0
int readCommonSparseComplexMatrix(int _iDatasetId, int _iComplex, int _iRows, int _iCols, int _iNbItem, int *_piNbItemRow, int *_piColPos, double *_pdblReal, double *_pdblImg)
{
    hid_t obj = 0;
    hobj_ref_t pRef[3] = {0};
    herr_t status;

    /*
     * Read the data.
     */
    status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef);
    if (status < 0)
    {
        return -1;
    }

    //read Row data
    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[0]);
    status = readInteger32Matrix(obj, _piNbItemRow);
    if (status < 0)
    {
        return -1;
    }

    //read cols data
    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[1]);
    status = readInteger32Matrix(obj, _piColPos);
    if (status < 0)
    {
        return -1;
    }

    //read sparse data
    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[2]);

    if (_iComplex)
    {
        status = readDoubleComplexMatrix(obj, _pdblReal, _pdblImg);
    }
    else
    {
        status = readDoubleMatrix(obj, _pdblReal);
    }

    if (status < 0)
    {
        return -1;
    }

    status = H5Dclose(_iDatasetId);
    if (status < 0)
    {
        return -1;
    }

    return 0;
}
int readDoubleMatrix_v1(int _iDatasetId, int _iRows, int _iCols, double *_pdblData)
{
    herr_t status;

    if (_iRows != 0 && _iCols != 0)
    {
        hid_t obj;
        hobj_ref_t Ref;

        //Read the data.
        status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &Ref);
        if (status < 0)
        {
            return -1;
        }

        //Open the referenced object, get its name and type.
        obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &Ref);
        readDouble_v1(obj, _iRows, _iCols, _pdblData);
    }

    status = H5Dclose(_iDatasetId);
    if (status < 0)
    {
        return -1;
    }

    return 0;
}
示例#4
0
//-*****************************************************************************
H5Node OpenGroup( H5Node& iParent, const std::string& iName )
{
    ABCA_ASSERT( iParent.isValidObject(),
                 "Invalid parent group passed into HDF5Util OpenGroup: "
                 << iName << std::endl );

    HDF5Hierarchy* h5HPtr = iParent.getH5HPtr();

    if ( h5HPtr )
    {
        hobj_ref_t childRef = h5HPtr->getChildRef( iParent.getRef(), iName );

        hid_t childId = H5Rdereference( iParent.getObject(),
                                        H5R_OBJECT,
                                        &childRef );

        return H5Node( childId, childRef, h5HPtr );
    }
    else
    {
        hid_t childId = H5Gopen2( iParent.getObject(),
                                  iName.c_str(),
                                  H5P_DEFAULT );

        return H5Node( childId, 0, NULL );
    }
}
示例#5
0
  Object ObjectReference::dereference (const File& file) const {
    ASSERT (!isNull ());

    //return Object (Exception::check ("H5Rdereference", H5Rdereference (file.handle (), H5R_OBJECT, &value ()))); // doesn't work because H5Rdereference has non-const pointer argument
    ObjectReference cpy = *this;
    return Object (Exception::check ("H5Rdereference", H5Rdereference (file.handle (), H5R_OBJECT, &cpy.value ())));
  }
示例#6
0
void  H5Dataset_readPalette(H5Dataset *d, hid_t did)
{
    hid_t pal_id=-1, tid=-1;
    hobj_ref_t *refs;

    if (!d || did<=0 ) return;

    refs = H5Dataset_get_paletteRef(did);
    if (refs) {
        // use the fist palette
        pal_id =  H5Rdereference(d->fid, H5R_OBJECT, refs);
        tid = H5Dget_type(pal_id);
        if (H5Dget_storage_size(pal_id) <= 768) 
        {
            H5Attribute *attr;
            d->nattributes = 1;
            d->attributes = (H5Attribute*)malloc(sizeof(H5Attribute));
            attr = &(d->attributes[0]);
            H5Attribute_ctor(attr);
            attr->value = (unsigned char *)malloc(3*256); 
            memset(attr->value, 0, 768);
            attr->nvalue = 768;
            attr->name = (char*)malloc(20);
            strcpy(attr->name, PALETTE_VALUE);
            H5Dread( pal_id, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, attr->value);
        }

        if (tid > 0) H5Tclose(tid);
        if (pal_id > 0) H5Dclose(pal_id);
        free(refs);
    }

}
示例#7
0
H5Object & H5ReferenceData::getData(const unsigned int size, const unsigned int * index) const
{
    char * cdata = static_cast<char *>(data) + offset;
    void ** ref = 0;
    unsigned int pos = 0;
    unsigned int cumprod = 1;
    H5O_info_t info;
    H5Object * hobj;
    hid_t obj;
    hid_t file;
    ssize_t ssize;
    char * name = 0;
    std::string _name;

    for (unsigned int i = 0; i < size; i++)
    {
        pos += cumprod * index[i];
    }

    if (pos >= totalSize)
    {
        throw H5Exception(__LINE__, __FILE__, _("Invalid index."));
    }

    cdata += pos * (stride ? stride : dataSize);

    file = getFile().getH5Id();
    ref = &(((void **)cdata)[0]);
    obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref);
    if (obj < 0)
    {
        throw H5Exception(__LINE__, __FILE__, _("Cannot open object at the given position."));
    }

    ssize = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0);
    name = new char[ssize + 1];
    H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, ssize + 1);
    _name = std::string(name);
    delete[] name;

    H5Oget_info(obj, &info);
    switch (info.type)
    {
        case H5O_TYPE_GROUP:
            hobj = new H5Group(getParent(), obj, _name);
            break;
        case H5O_TYPE_DATASET:
            hobj = new H5Dataset(getParent(), obj, _name);
            break;
        case H5O_TYPE_NAMED_DATATYPE:
            hobj = new H5Type(getParent(), obj, _name);
            break;
        case H5O_TYPE_UNKNOWN:
        default:
            H5Oclose(obj);
            throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object"));
    }

    return *hobj;
}
示例#8
0
//--------------------------------------------------------------------------
// Function:	H5Location::p_dereference (protected)
// Purpose	Dereference a ref into an hdf5 object.
// Parameters
//		loc_id - IN: An hdf5 identifier specifying the location of the
//			 referenced object
//		ref - IN: Reference pointer
//		ref_type - IN: Reference type
// Exception	H5::ReferenceException
// Programmer	Binh-Minh Ribler - Oct, 2006
// Modification
//	May 2008 - BMR
//		Moved from IdComponent.
//--------------------------------------------------------------------------
hid_t H5Location::p_dereference(hid_t loc_id, const void* ref, H5R_type_t ref_type, const char* from_func)
{
   hid_t temp_id = H5Rdereference(loc_id, ref_type, ref);
   if (temp_id < 0)
   {
      throw ReferenceException(inMemFunc(from_func), "H5Rdereference failed");
   }

   return(temp_id);
}
int readCommonPolyMatrix_v1(int _iDatasetId, char *_pstVarname, int _iComplex, int _iRows, int _iCols, int *_piNbCoef, double **_pdblReal,
                            double **_pdblImg)
{
    int i = 0;
    hid_t obj = 0;
    char *pstVarName = 0;
    hobj_ref_t *pData = (hobj_ref_t *) MALLOC(_iRows * _iCols * sizeof(hobj_ref_t));
    herr_t status;

    /*
    * Read the data.
    */
    status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pData);
    if (status < 0)
    {
        FREE(pData);
        return -1;
    }

    for (i = 0; i < _iRows * _iCols; i++)
    {
        /*
        * Open the referenced object, get its name and type.
        */
        obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pData[i]);
        if (_iComplex)
        {
            status = readComplexPoly_v1(obj, &_piNbCoef[i], &_pdblReal[i], &_pdblImg[i]);
        }
        else
        {
            status = readPoly_v1(obj, &_piNbCoef[i], &_pdblReal[i]);
        }

        if (status < 0)
        {
            FREE(pData);
            return -1;
        }
    }

    pstVarName = readAttribute_v1(_iDatasetId, g_SCILAB_CLASS_VARNAME);
    strcpy(_pstVarname, pstVarName);
    FREE(pstVarName);
    status = H5Dclose(_iDatasetId);
    if (status < 0)
    {
        FREE(pData);
        return -1;
    }

    FREE(pData);

    return 0;
}
示例#10
0
//--------------------------------------------------------------------------
// Function:	H5Object::p_dereference (protected)
// Purpose	Dereference a ref into an hdf5 object.
// Parameters
//		loc_id - IN: An hdf5 identifier specifying the location of the 
//			 referenced object
//		ref - IN: Reference pointer
//		ref_type - IN: Reference type
// Exception	H5::ReferenceException
// Programmer	Binh-Minh Ribler - Oct, 2006
// Modification
//	May 2008 - BMR
//		Moved from IdComponent.
//--------------------------------------------------------------------------
hid_t H5Object::p_dereference(hid_t loc_id, const void* ref, H5R_type_t ref_type)
{
   hid_t temp_id;
   temp_id = H5Rdereference(loc_id, ref_type, ref);
   if (temp_id < 0)
   {
      throw ReferenceException("", "H5Rdereference failed");
   }

   // No failure, set id to the object
   return(temp_id);
}
int getListItemDataset_v1(int _iDatasetId, void *_piItemRef, int _iItemPos, int *_piItemDataset)
{
    hobj_ref_t poRef = ((hobj_ref_t *) _piItemRef)[_iItemPos];

    *_piItemDataset = H5Rdereference(_iDatasetId, H5R_OBJECT, &poRef);

    if (*_piItemDataset == 0)
    {
        return -1;
    }

    return 0;
}
int readDoubleComplexMatrix_v1(int _iDatasetId, int _iRows, int _iCols, double *_pdblReal, double *_pdblImg)
{
    hid_t obj;
    herr_t status;
    hobj_ref_t pRef[2] = {0};

    //Read the data.
    status = H5Dread(_iDatasetId, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, pRef);
    if (status < 0)
    {
        return -1;
    }

    //Open the referenced object, get its name and type.
    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[0]);
    status = readDouble_v1(obj, _iRows, _iCols, _pdblReal);
    if (status < 0)
    {
        return -1;
    }

    obj = H5Rdereference(_iDatasetId, H5R_OBJECT, &pRef[1]);
    status = readDouble_v1(obj, _iRows, _iCols, _pdblImg);
    if (status < 0)
    {
        return -1;
    }

    status = H5Dclose(_iDatasetId);
    if (status < 0)
    {
        return -1;
    }

    return 0;
}
示例#13
0
文件: H5Rf.c 项目: MattNapsAlot/rHDF5
/*----------------------------------------------------------------------------
 * Name:        h5rdereference_object_c
 * Purpose:     Call H5Rdereference to dereference an object
 * Inputs:      dset_id - dataset identifier
 *              ref - reference to an object
 * Outputs:     obj_id - dereferenced  object identifier
 * Returns:     0 on success, -1 on failure
 * Programmer:  Elena Pourmal
 *              Wednesday, December 1, 1999
 * Modifications:
 *---------------------------------------------------------------------------*/
int_f
nh5rdereference_object_c (hid_t_f *dset_id, haddr_t_f *ref, hid_t_f *obj_id)
{
     int ret_value = -1;
     hid_t c_dset_id;
     hid_t c_obj_id;
     hobj_ref_t ref_c;

     ref_c=(hobj_ref_t)*ref;

     /*
      * Call H5Rdereference function.
      */
     c_dset_id = *dset_id;
     c_obj_id = H5Rdereference(c_dset_id, H5R_OBJECT, &ref_c);
     if(c_obj_id < 0) return ret_value;
     *obj_id = (hid_t_f)c_obj_id;
     ret_value = 0;
     return ret_value;
}
示例#14
0
文件: H5Rf.c 项目: MattNapsAlot/rHDF5
/*----------------------------------------------------------------------------
 * Name:        h5rdereference_region_c
 * Purpose:     Call H5Rdereference to dereference to dataset region
 * Inputs:      dset_id - dataset identifier
 *              ref - reference to the dataset region
 * Outputs:     obj_id - dereferenced dataset identifier
 * Returns:     0 on success, -1 on failure
 * Programmer:  Elena Pourmal
 *              Wednesday, December 1, 1999
 * Modifications:
 *---------------------------------------------------------------------------*/
int_f
nh5rdereference_region_c (hid_t_f *dset_id, int_f *ref, hid_t_f *obj_id)
{
     int ret_value = -1;
     hid_t c_dset_id;
     hdset_reg_ref_t ref_c;
     hid_t c_obj_id;

     HDmemcpy (&ref_c, ref, H5R_DSET_REG_REF_BUF_SIZE);

     /*
      * Call H5Rdereference function.
      */
     c_dset_id = *dset_id;
     c_obj_id = H5Rdereference(c_dset_id, H5R_DATASET_REGION, &ref_c);
     if(c_obj_id < 0) return ret_value;
     *obj_id = (hid_t_f)c_obj_id;
     ret_value = 0;
     return ret_value;
}
示例#15
0
int
main (void)
{
    hid_t       file, space, dset, obj, attr;   /* Handles */
    herr_t      status;
    hsize_t     dims[1] = {DIM0};
    hobj_ref_t  wdata[DIM0],                    /* Write buffer */
                *rdata;                         /* Read buffer */
    H5G_obj_t   objtype;
    ssize_t     size;
    char        *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 a scalar dataspace.
     */
    space = H5Screate (H5S_SCALAR);
    obj = H5Dcreate (file, "DS2", H5T_STD_I32LE, space, H5P_DEFAULT);
    status = H5Dclose (obj);
    status = H5Sclose (space);

    /*
     * Create a group.
     */
    obj = H5Gcreate (file, "G1", H5P_DEFAULT);
    status = H5Gclose (obj);

    /*
     * Create references to the previously created objects.  Passing -1
     * as space_id causes this parameter to be ignored.  Other values
     * besides valid dataspaces result in an error.
     */
    status = H5Rcreate (&wdata[0], file, "G1", H5R_OBJECT, -1);
    status = H5Rcreate (&wdata[1], file, "DS2", H5R_OBJECT, -1);

    /*
     * Create dataset with a scalar dataspace to serve as the parent
     * for the attribute.
     */
    space = H5Screate (H5S_SCALAR);
    dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, 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 object references to it.
     */
    attr = H5Acreate (dset, ATTRIBUTE, H5T_STD_REF_OBJ, space, H5P_DEFAULT);
    status = H5Awrite (attr, H5T_STD_REF_OBJ, wdata);

    /*
     * Close and release resources.
     */
    status = H5Aclose (attr);
    status = H5Dclose (dset);
    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);
    attr = H5Aopen_name (dset, ATTRIBUTE);

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

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

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

        /*
         * Open the referenced object, get its name and type.
         */
        obj = H5Rdereference (dset, H5R_OBJECT, &rdata[i]);
        objtype = H5Rget_obj_type (dset, H5R_OBJECT, &rdata[i]);

        /*
         * Get the length of the name, allocate space, then retrieve
         * the name.
         */
        size = 1 + H5Iget_name (obj, NULL, 0);
        if (size > 1) {
            name = (char *) malloc (size);
            size = 1 + H5Iget_name (obj, name, size);
        }

        /*
         * Print the object type and close the object.
         */
        switch (objtype) {
            case H5G_GROUP:
                printf ("Group");
                status = H5Gclose (obj);
                break;
            case H5G_DATASET:
                printf ("Dataset");
                status = H5Dclose (obj);
                break;
            case H5G_TYPE:
                printf ("Named Datatype");
                status = H5Tclose (obj);
        }

        /*
         * Print the name and deallocate space for the name.
         */
        if (size > 1) {
            printf (": %s", name);
            free (name);
        }
        printf ("\n");
    }

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

    return 0;
}
示例#16
0
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;
}   
示例#17
0
/*-------------------------------------------------------------------------
 * Function: H5IM_get_palette
 *
 * Purpose: private function that reads a palette to memory type TID
 *
 * Return: Success: 0, Failure: -1
 *
 * Programmer: Pedro Vicente Nunes, [email protected]
 *
 * Date: May 10, 2005
 *
 * Comments:
 *  This function allows reading of an 8bit palette from disk disk
 *   to memory type TID
 *  The memory datatype can be H5T_NATIVE_INT or H5T_NATIVE_UCHAR currently.
 *   the H5T_NATIVE_INT is supposed to be called from
 *   the FORTRAN interface where the image buffer is defined as type "integer"
 *
 * Comments:
 *  based on HDF5 Image and Palette Specification
 *  http://hdf.ncsa.uiuc.edu/HDF5/H5Image/ImageSpec.html
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t H5IM_get_palette(hid_t loc_id,
                         const char *image_name,
                         int pal_number,
                         hid_t tid,
                         void *pal_data)
{
 hid_t      image_id;
 int        has_pal;
 hid_t      attr_type;
 hid_t      attr_id;
 hid_t      attr_space_id;
 hid_t      attr_class;
 hssize_t   n_refs;
 hsize_t    dim_ref;
 hobj_ref_t *refbuf;     /* buffer to read references */
 hid_t      pal_id;

 /* Open the dataset. */
 if((image_id = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0)
  return -1;

 /* Try to find the attribute "PALETTE" on the >>image<< dataset */
 has_pal = H5IM_find_palette(image_id);

 if(has_pal ==  1)
 {

  if((attr_id = H5Aopen(image_id, "PALETTE", H5P_DEFAULT)) < 0)
   goto out;

  if((attr_type = H5Aget_type(attr_id)) < 0)
   goto out;

  if((attr_class = H5Tget_class(attr_type)) < 0)
   goto out;

  /* Check if it is really a reference */
  if(attr_class == H5T_REFERENCE)
  {

   /* Get the reference(s) */
   if((attr_space_id = H5Aget_space(attr_id)) < 0)
    goto out;

   n_refs = H5Sget_simple_extent_npoints(attr_space_id);

   dim_ref = n_refs;

   refbuf = malloc(sizeof(hobj_ref_t) * (int)dim_ref);

   if(H5Aread(attr_id, attr_type, refbuf) < 0)
    goto out;

   /* Get the palette id */
   if((pal_id = H5Rdereference(image_id, H5R_OBJECT, &refbuf[pal_number])) < 0)
    goto out;

   /* Read the palette dataset using the memory type TID */
   if(H5Dread(pal_id, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0)
    goto out;

   if(H5Sclose(attr_space_id) < 0)
    goto out;

   /* close the dereferenced dataset */
   if(H5Dclose(pal_id) < 0)
    goto out;

   free(refbuf);

  } /* H5T_REFERENCE */

  if(H5Tclose(attr_type) < 0)
   goto out;

  /* Close the attribute. */
  if(H5Aclose(attr_id) < 0)
   goto out;

 }

 /* Close the image dataset. */
 if(H5Dclose(image_id) < 0)
  return -1;

 return 0;

out:
 H5Dclose(image_id);
 return -1;


}
示例#18
0
static bool read_struct(int dataset, VarInfo6& info)
{
    int complex = 0;
    int size = getDimsNode(dataset, &complex, info.pdims);
    info.dims = static_cast<int>(info.pdims.size());
    info.size = 0;

    if (size == 0)
    {
        generateInfo(info);
        closeList6(dataset);
        return true;
    }
    int fieldCount = 0;
    int ret = getListDims6(dataset, &fieldCount);
    if (ret < 0)
    {
        closeList6(dataset);
        return false;
    }

    //open __refs__ node
    int refs = getDataSetIdFromName(dataset, "__refs__");
    H5O_info_t oinfo;
    for (int i = 0; i < fieldCount; ++i)
    {
        H5Oget_info_by_idx(dataset, ".", H5_INDEX_NAME, H5_ITER_NATIVE, i, &oinfo, H5P_DEFAULT);
        ssize_t len = H5Lget_name_by_idx(dataset, ".", H5_INDEX_NAME, H5_ITER_INC, i, 0, 0, H5P_DEFAULT) + 1;
        char* name = (char*)MALLOC(sizeof(char) * len);
        H5Lget_name_by_idx(dataset, ".", H5_INDEX_NAME, H5_ITER_INC, i, name, len, H5P_DEFAULT);
        std::string cname(name);
        FREE(name);

        if (cname != "__dims__" && cname != "__refs__")
        {
            int dataref = getDataSetIdFromName(dataset, cname.data());
            if (dataref < 0)
            {
                closeList6(dataset);
                return false;
            }

            int refdim = 0;
            getDatasetInfo(dataref, &complex, &refdim, NULL);
            std::vector<int> refdims(refdim);
            int refcount = getDatasetInfo(dataref, &complex, &refdim, refdims.data());
            std::vector<hobj_ref_t> vrefs(refcount);
            ret = H5Dread(dataref, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, vrefs.data());
            if (ret < 0)
            {
                return false;
            }


            //import field
            for (int j = 0; j < refcount; ++j)
            {
                int data = H5Rdereference(refs, H5R_OBJECT, &vrefs[j]);
                if (data < 0)
                {
                    return false;
                }

                VarInfo6 info2;
                ret = read_data(data, info2);
                if (ret == false)
                {
                    return false;
                }

                info.size += info2.size;

            }

            closeDataSet(dataref);
        }
    }

    generateInfo(info);
    closeList6(refs);
    closeList6(dataset);
    return true;
}
示例#19
0
/*
 * test_objnames
 * Tests that UTF-8 can be used for object names in the file.
 * Tests groups, datasets, named datatypes, and soft links.
 * Note that this test doesn't actually mark the names as being
 * in UTF-8.  At the time this test was written, that feature
 * didn't exist in HDF5, and when the character encoding property
 * was added to links it didn't change how they were stored in the file,
 * -JML 2/2/2006
 */
void test_objnames(hid_t fid, const char* string)
{
  hid_t grp_id, grp1_id, grp2_id, grp3_id;
  hid_t type_id, dset_id, space_id;
  char read_buf[MAX_STRING_LENGTH];
  char path_buf[MAX_PATH_LENGTH];
  hsize_t dims=1;
  hobj_ref_t obj_ref;
  herr_t ret;

  /* Create a group with a UTF-8 name */
  grp_id = H5Gcreate2(fid, string, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(grp_id, FAIL, "H5Gcreate2");

  /* Set a comment on the group to test that we can access the group
   * Also test that UTF-8 comments can be read.
   */
  ret = H5Oset_comment_by_name(fid, string, string, H5P_DEFAULT);
  CHECK(ret, FAIL, "H5Oset_comment_by_name");
  ret = H5Oget_comment_by_name(fid, string, read_buf, (size_t)MAX_STRING_LENGTH, H5P_DEFAULT);
  CHECK(ret, FAIL, "H5Oget_comment_by_name");

  ret = H5Gclose(grp_id);
  CHECK(ret, FAIL, "H5Gclose");

  VERIFY(HDstrcmp(string, read_buf), 0, "strcmp");

  /* Create a new dataset with a UTF-8 name */
  grp1_id = H5Gcreate2(fid, GROUP1_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(grp1_id, FAIL, "H5Gcreate2");

  space_id = H5Screate_simple(RANK, &dims, NULL);
  CHECK(space_id, FAIL, "H5Screate_simple");
  dset_id = H5Dcreate2(grp1_id, string, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(dset_id, FAIL, "H5Dcreate2");

  /* Make sure that dataset can be opened again */
  ret = H5Dclose(dset_id);
  CHECK(ret, FAIL, "H5Dclose");
  ret = H5Sclose(space_id);
  CHECK(ret, FAIL, "H5Sclose");

  dset_id = H5Dopen2(grp1_id, string, H5P_DEFAULT);
  CHECK(ret, FAIL, "H5Dopen2");
  ret = H5Dclose(dset_id);
  CHECK(ret, FAIL, "H5Dclose");
  ret = H5Gclose(grp1_id);
  CHECK(ret, FAIL, "H5Gclose");

  /* Do the same for a named datatype */
  grp2_id = H5Gcreate2(fid, GROUP2_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(grp2_id, FAIL, "H5Gcreate2");

  type_id = H5Tcreate(H5T_OPAQUE, (size_t)1);
  CHECK(type_id, FAIL, "H5Tcreate");
  ret = H5Tcommit2(grp2_id, string, type_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(type_id, FAIL, "H5Tcommit2");
  ret = H5Tclose(type_id);
  CHECK(type_id, FAIL, "H5Tclose");

  type_id = H5Topen2(grp2_id, string, H5P_DEFAULT);
  CHECK(type_id, FAIL, "H5Topen2");
  ret = H5Tclose(type_id);
  CHECK(type_id, FAIL, "H5Tclose");

  /* Don't close the group -- use it to test that object references
   * can refer to objects named in UTF-8 */

  space_id = H5Screate_simple(RANK, &dims, NULL);
  CHECK(space_id, FAIL, "H5Screate_simple");
  dset_id = H5Dcreate2(grp2_id, DSET3_NAME, H5T_STD_REF_OBJ, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(ret, FAIL, "H5Dcreate2");

  /* Create reference to named datatype */
  ret = H5Rcreate(&obj_ref, grp2_id, string, H5R_OBJECT, (hid_t)-1);
  CHECK(ret, FAIL, "H5Rcreate");
  /* Write selection and read it back*/
  ret = H5Dwrite(dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &obj_ref);
  CHECK(ret, FAIL, "H5Dwrite");
  ret = H5Dread(dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &obj_ref);
  CHECK(ret, FAIL, "H5Dread");

  /* Ensure that we can open named datatype using object reference */
  type_id = H5Rdereference(dset_id, H5R_OBJECT, &obj_ref);
  CHECK(type_id, FAIL, "H5Rdereference");
  ret = H5Tcommitted(type_id);
  VERIFY(ret, 1, "H5Tcommitted");

  ret = H5Tclose(type_id);
  CHECK(type_id, FAIL, "H5Tclose");
  ret = H5Dclose(dset_id);
  CHECK(ret, FAIL, "H5Dclose");
  ret = H5Sclose(space_id);
  CHECK(ret, FAIL, "H5Sclose");

  ret = H5Gclose(grp2_id);
  CHECK(ret, FAIL, "H5Gclose");

  /* Create "group3".  Build a hard link from group3 to group2, which has
   * a datatype with the UTF-8 name.  Create a soft link in group3
   * pointing through the hard link to the datatype.  Give the soft
   * link a name in UTF-8.  Ensure that the soft link works. */

  grp3_id = H5Gcreate2(fid, GROUP3_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(grp3_id, FAIL, "H5Gcreate2");

  ret = H5Lcreate_hard(fid, GROUP2_NAME, grp3_id, GROUP2_NAME, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(ret, FAIL, "H5Lcreate_hard");
  HDstrcpy(path_buf, GROUP2_NAME);
  HDstrcat(path_buf, "/");
  HDstrcat(path_buf, string);
  ret = H5Lcreate_hard(grp3_id, path_buf, H5L_SAME_LOC, string, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(ret, FAIL, "H5Lcreate_hard");

  /* Open named datatype using soft link */
  type_id = H5Topen2(grp3_id, string, H5P_DEFAULT);
  CHECK(type_id, FAIL, "H5Topen2");

  ret = H5Tclose(type_id);
  CHECK(type_id, FAIL, "H5Tclose");
  ret = H5Gclose(grp3_id);
  CHECK(ret, FAIL, "H5Gclose");
}
示例#20
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;
}
示例#21
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;
    }
}
示例#22
0
herr_t H5IMget_palette_info( hid_t loc_id,
                            const char *image_name,
                            int pal_number,
                            hsize_t *pal_dims )
{
    hid_t      did;
    int        has_pal;
    hid_t      atid=-1;
    hid_t      aid;
    hid_t      asid=-1;
    hssize_t   n_refs;
    hsize_t    dim_ref;
    hobj_ref_t *refbuf;     /* buffer to read references */
    hid_t      pal_id;
    hid_t      pal_space_id;
    hsize_t    pal_maxdims[2];

    /* check the arguments */
    if (image_name == NULL) 
      return -1;

    /* Open the dataset. */
    if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "PALETTE" on the >>image<< dataset */
    has_pal = H5IM_find_palette(did);

    if(has_pal ==  1)
    {
        if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if(H5Tget_class(atid) < 0)
            goto out;

        /* Get the reference(s) */
        if((asid = H5Aget_space(aid)) < 0)
            goto out;

        n_refs = H5Sget_simple_extent_npoints(asid);

        dim_ref = (hsize_t)n_refs;

        refbuf = (hobj_ref_t*)HDmalloc( sizeof(hobj_ref_t) * (size_t)dim_ref );

        if ( H5Aread( aid, atid, refbuf ) < 0)
            goto out;

        /* Get the actual palette */
        if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0)
            goto out;

        if ( (pal_space_id = H5Dget_space( pal_id )) < 0)
            goto out;

        if ( H5Sget_simple_extent_ndims( pal_space_id ) < 0)
            goto out;

        if ( H5Sget_simple_extent_dims( pal_space_id, pal_dims, pal_maxdims ) < 0)
            goto out;

        /* close */
        if (H5Dclose(pal_id)<0)
            goto out;
        if ( H5Sclose( pal_space_id ) < 0)
            goto out;
        if ( H5Sclose( asid ) < 0)
            goto out;
        if ( H5Tclose( atid ) < 0)
            goto out;
        if ( H5Aclose( aid ) < 0)
            goto out;
        HDfree( refbuf );


    }

    /* Close the image dataset. */
    if ( H5Dclose( did ) < 0)
        return -1;

    return 0;

out:
    H5Dclose( did );
    H5Sclose( asid );
    H5Tclose( atid );
    H5Aclose( aid );
    return -1;

}
示例#23
0
static types::InternalType* import_struct(int dataset)
{
    //get struct dims node
    int complex = 0;
    std::vector<int> pdims;
    int size = getDimsNode(dataset, &complex, pdims);

    types::Struct* str = new types::Struct(static_cast<int>(pdims.size()), pdims.data());
    size = str->getSize();
    if (size == 0)
    {
        //empty struct
        closeList6(dataset);
        delete str;
        return new types::Struct();
    }

    types::SingleStruct** sstr = str->get();

    int fieldCount = 0;
    int ret = getListDims6(dataset, &fieldCount);
    if (ret < 0)
    {
        closeList6(dataset);
        return str;
    }

    //get fields name
    int dfield = getDataSetIdFromName(dataset, "__fields__");
    int dim = 0;
    getDatasetInfo(dfield, &complex, &dim, NULL);
    std::vector<int> d(dim);
    size = getDatasetInfo(dfield, &complex, &dim, d.data());
    if (size < 0)
    {
        closeList6(dataset);
        delete str;
        return nullptr;
    }

    //get dims value
    std::vector<char*> fields(size);
    readStringMatrix(dfield, fields.data());

    //open __refs__ node
    int refs = getDataSetIdFromName(dataset, "__refs__");

    for (const auto & name : fields)
    {
        wchar_t* field = to_wide_string(name);
        str->addField(field);

        int dataref = getDataSetIdFromName(dataset, name);
        if (dataref < 0)
        {
            closeList6(dataset);
            freeStringMatrix(dfield, fields.data());
            FREE(field);
            delete str;
            return nullptr;
        }

        int refdim = 0;
        getDatasetInfo(dataref, &complex, &refdim, NULL);
        std::vector<int> refdims(refdim);
        int refcount = getDatasetInfo(dataref, &complex, &refdim, refdims.data());
        std::vector<hobj_ref_t> vrefs(refcount);
        ret = H5Dread(dataref, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, vrefs.data());
        if (ret < 0)
        {
            freeStringMatrix(dfield, fields.data());
            FREE(field);
            delete str;
            return nullptr;
        }


        //import field
        for (int j = 0; j < refcount; ++j)
        {
            int data = H5Rdereference(refs, H5R_OBJECT, &vrefs[j]);
            if (data < 0)
            {
                freeStringMatrix(dfield, fields.data());
                FREE(field);
                delete str;
                return nullptr;
            }

            types::InternalType* val = import_data(data);
            if (val == nullptr)
            {
                freeStringMatrix(dfield, fields.data());
                FREE(field);
                delete str;
                return nullptr;
            }

            sstr[j]->set(field, val);

        }

        FREE(field);
        closeDataSet(dataref);
    }

    freeStringMatrix(dfield, fields.data());
    closeList6(refs);
    closeList6(dataset);
    return str;
}
示例#24
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*/
示例#25
0
文件: H5IM.c 项目: Andy-Sun/VTK
herr_t H5IMget_palette( hid_t loc_id,
                       const char *image_name,
                       int pal_number,
                       unsigned char *pal_data )
{
    hid_t      did;
    int        has_pal;
    hid_t      atid=-1;
    hid_t      aid;
    hid_t      asid=-1;
    hssize_t   n_refs;
    hsize_t    dim_ref;
    hobj_ref_t *refbuf;     /* buffer to read references */
    hid_t      pal_id;

    /* Open the dataset. */
    if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "PALETTE" on the >>image<< dataset */
    has_pal = H5IM_find_palette(did);

    if(has_pal ==  1 )
    {
        if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if(H5Tget_class(atid) < 0)
            goto out;

        /* Get the reference(s) */
        if((asid = H5Aget_space(aid)) < 0)
            goto out;

        n_refs = H5Sget_simple_extent_npoints(asid);

        dim_ref = n_refs;

        refbuf = (hobj_ref_t*)malloc( sizeof(hobj_ref_t) * (int)dim_ref );

        if ( H5Aread( aid, atid, refbuf ) < 0)
            goto out;

        /* Get the palette id */
        if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0)
            goto out;

        /* Read the palette dataset */
        if ( H5Dread( pal_id, H5Dget_type(pal_id), H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data ) < 0)
            goto out;

        /* close */
        if (H5Dclose(pal_id)<0)
            goto out;
        if ( H5Sclose( asid ) < 0)
            goto out;
        if ( H5Tclose( atid ) < 0)
            goto out;
        if ( H5Aclose( aid ) < 0)
            goto out;
        free( refbuf );
    }

    /* Close the image dataset. */
    if ( H5Dclose( did ) < 0)
        return -1;

    return 0;

out:
    H5Dclose( did );
    H5Sclose( asid );
    H5Tclose( atid );
    H5Aclose( aid );
    return -1;

}