Example #1
0
/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Aget_num_attrs
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Aget_1num_1attrs
    (JNIEnv *env, jclass clss, jlong loc_id)
{
    int retVal = -1;

    retVal = H5Aget_num_attrs((hid_t)loc_id);
    if (retVal < 0)
        h5libraryError(env);

    return (jint)retVal;
} /* end Java_hdf_hdf5lib_H5_H5Aget_1num_1attrs */
Example #2
0
CPLErr HDF5Dataset::ReadGlobalAttributes(int bSUBDATASET)
{
    HDF5GroupObjects *poRootGroup =
        static_cast<HDF5GroupObjects *>(CPLCalloc(sizeof(HDF5GroupObjects), 1));

    poH5RootGroup = poRootGroup;
    poRootGroup->pszName = CPLStrdup("/");
    poRootGroup->nType = H5G_GROUP;
    poRootGroup->poHparent = nullptr;
    poRootGroup->pszPath = nullptr;
    poRootGroup->pszUnderscorePath = nullptr;

    if( hHDF5 < 0 )
    {
        CPLError(CE_Failure, CPLE_AppDefined, "hHDF5 < 0!");
        return CE_None;
    }

    H5G_stat_t oStatbuf = {{0, 0}, {0, 0}, 0, H5G_UNKNOWN, 0, 0, {0, 0, 0, 0}};

    if( H5Gget_objinfo(hHDF5, "/", FALSE, &oStatbuf) < 0 )
        return CE_Failure;
    poRootGroup->objno[0] = oStatbuf.objno[0];
    poRootGroup->objno[1] = oStatbuf.objno[1];

    if( hGroupID > 0 )
        H5Gclose(hGroupID);
    hGroupID = H5Gopen(hHDF5, "/");
    if( hGroupID < 0 )
    {
        CPLError(CE_Failure, CPLE_AppDefined, "hGroupId <0!");
        return CE_None;
    }

    poRootGroup->nbAttrs = H5Aget_num_attrs(hGroupID);

    H5Gget_num_objs(hGroupID, &(poRootGroup->nbObjs));

    if( poRootGroup->nbObjs > 0 )
    {
        poRootGroup->poHchild = static_cast<HDF5GroupObjects *>(
            CPLCalloc(static_cast<size_t>(poRootGroup->nbObjs),
                      sizeof(HDF5GroupObjects)));
        H5Giterate(hGroupID, "/", nullptr, HDF5CreateGroupObjs, poRootGroup);
    }
    else
    {
        poRootGroup->poHchild = nullptr;
    }

    HDF5ListGroupObjects(poRootGroup, bSUBDATASET);
    return CE_None;
}
Example #3
0
/*----------------------------------------------------------------------------
 * Name:        h5aget_num_attrs_c
 * Purpose:     Call H5Aget_num_attrs to determine number of
 *              attributes of an object
 * Inputs:      obj_id - object identifier
 *              attr_num - number of attributes
 * Returns:     0 on success, -1 on failure
 * Programmer:  Elena Pourmal
 *              Thursday, August 12, 1999
 * Modifications:
 *---------------------------------------------------------------------------*/
int_f
nh5aget_num_attrs_c (hid_t_f *obj_id, int_f *attr_num)
{
    int_f ret_value=0;          /* Return value */

     /*
      * Call H5Aget_num_attrs function.
      */
     if ((*attr_num = (int_f)H5Aget_num_attrs((hid_t)*obj_id)) < 0)
         HGOTO_DONE(FAIL);

done:
     return ret_value;
}
Example #4
0
CPLErr HDF5Dataset::ReadGlobalAttributes(int bSUBDATASET)
{

    HDF5GroupObjects *poRootGroup;

    poRootGroup = (HDF5GroupObjects*) CPLCalloc(sizeof(HDF5GroupObjects), 1);

    poH5RootGroup=poRootGroup;
    poRootGroup->pszName   = CPLStrdup( "/" );
    poRootGroup->nType     = H5G_GROUP;
    poRootGroup->poHparent = NULL;
    poRootGroup->pszPath = NULL;
    poRootGroup->pszUnderscorePath = NULL;

    if( hHDF5 < 0 )  {
        printf( "hHDF5 <0!!\n" );
        return CE_None;
    }

    H5G_stat_t  oStatbuf;
    if( H5Gget_objinfo( hHDF5, "/", FALSE, &oStatbuf ) < 0  )
        return CE_Failure;
    poRootGroup->objno[0] = oStatbuf.objno[0];
    poRootGroup->objno[1] = oStatbuf.objno[1];

    if( hGroupID > 0 )
        H5Gclose( hGroupID );
    hGroupID = H5Gopen( hHDF5, "/" );
    if( hGroupID < 0 ){
        printf( "hGroupID <0!!\n" );
        return CE_None;
    }

    poRootGroup->nbAttrs = H5Aget_num_attrs( hGroupID );

    H5Gget_num_objs( hGroupID, &( poRootGroup->nbObjs ) );

    if( poRootGroup->nbObjs > 0 ) {
        poRootGroup->poHchild = ( HDF5GroupObjects * )
            CPLCalloc( poRootGroup->nbObjs,
            sizeof( HDF5GroupObjects ) );
        H5Giterate( hGroupID, "/", NULL,
               HDF5CreateGroupObjs, (void *)poRootGroup );
    }
    else poRootGroup->poHchild = NULL;

    HDF5ListGroupObjects( poRootGroup, bSUBDATASET );
    return CE_None;
}
Example #5
0
char HBase::hasNamedAttr(const char * attrName)
{
	hsize_t nattr = H5Aget_num_attrs(fObjectId);
	//std::cout<<"\n "<<fObjectPath<<" has "<<nattr<<" attrs\n";
	hsize_t i;
	for(i = 0; i < nattr; i++) {
		hid_t aid = H5Aopen_idx(fObjectId, (unsigned int)i );
		//std::cout<<getAttrName(aid)<<"\n";
		if(getAttrName(aid) == attrName) {
			//std::cout<<"found "<<attrName;
			H5Aclose(aid);
			return 1;
		}
		H5Aclose(aid);
	}
	return 0;
}
Example #6
0
/* Open/close the file with HDF5. */
int
readfile_hdf5(char *file_name, long long *delta, int do_inq, int num_vars)
{
   hid_t hdfid, hdf_grpid;
   hid_t fapl_id;
   struct timeval starttime, endtime;
   long long startt, endt;

   /* Start the clock. */
   gettimeofday(&starttime, NULL);

   /* Open and close the root group. */
   if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
   if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI)) ERR;
   if ((hdfid = H5Fopen(file_name, H5F_ACC_RDONLY, fapl_id)) < 0) ERR;
   if ((hdf_grpid = H5Gopen2(hdfid, "/", H5P_DEFAULT)) < 0) ERR;

   /* Do we want to do an inq? */
   if (do_inq)
   {
      if (num_vars)
      {
      }
      else /* global atts */
      {
         hsize_t num_obj;

         /* Find out how many attributes. */
         if ((num_obj = H5Aget_num_attrs(hdf_grpid)) < 0) ERR;
      }
   }

   if (H5Gclose(hdf_grpid) < 0) ERR;
   if (H5Fclose(hdfid) < 0) ERR;

   gettimeofday(&endtime, NULL);

   /* Compute the time delta */
   startt = (1000000 * starttime.tv_sec) + starttime.tv_usec;
   endt = (1000000 * endtime.tv_sec) + endtime.tv_usec;
   *delta = endt - startt;

   return 0;
}
Example #7
0
int H5Dataset_read_attribute(H5Dataset* ind, H5Dataset* outd)
{
    int ret_value=0, i=0, n=0;
    hid_t did=-1;

    if ( (did = H5Dopen(ind->fid, ind->fullpath)) < 0)
        THROW_H5LIBRARY_ERROR(ind->error,ret_value, done);

    if ( (n = H5Aget_num_attrs(did)) <=0 )
        goto done;

    ind->nattributes = n;
    ind->attributes = (H5Attribute *)malloc(n*sizeof(H5Attribute));
    for (i=0; i<n; i++) {
        H5Attribute_ctor(&(ind->attributes[i]));
        ind->attributes[i].fid = ind->fid;
        ind->attributes[i].obj_path = (char *)malloc(strlen(ind->fullpath)+1);
        strcpy(ind->attributes[i].obj_path, ind->fullpath);
        ind->attributes[i].obj_type = H5OBJECT_DATASET;
    }
    
    /* read the attribute value into memory */
    H5File_read_attribute(did, ind->attributes);

done:
    if (did > 0 ) H5Dclose(did);

#ifndef HDF5_LOCAL
    memset (outd, 0, sizeof (H5Dataset));


    /* pass on the value */
    outd->tclass = ind->tclass;
    outd->error = ind->error;
    outd->nattributes = ind->nattributes;
    outd->attributes = ind->attributes;

    ind->attributes = NULL;
    ind->nattributes = 0;
#endif

    return ret_value;
}
Example #8
0
	void Object::updateAttributes()
	{
		fAttributes.clear();

		if (fObjectId < 0)
			return;

		// get attributes
		int nAttrs = H5Aget_num_attrs(fObjectId);
		if (nAttrs < 0) {
			throw Exception("Could not retrieve number of attributes");
		}

		for (size_t iAttr = 0; iAttr < static_cast<size_t>(nAttrs); ++iAttr) {
			char* attrName;
			char objName[] = ".";
			ssize_t nameLen = H5Aget_name_by_idx(fObjectId, objName, H5_INDEX_NAME, H5_ITER_INC, iAttr, 0, 0, H5P_DEFAULT);

			if (nameLen > 0) {
				// fetch name of this attribute
				attrName = new char[++nameLen];
				H5Aget_name_by_idx(fObjectId, objName, H5_INDEX_NAME, H5_ITER_INC, iAttr, attrName, nameLen, H5P_DEFAULT);
				string sAttrName(attrName);
				delete[] attrName;

				// open attribute
				hid_t attrId = H5Aopen_by_idx(fObjectId, objName, H5_INDEX_NAME, H5_ITER_INC, iAttr, H5P_DEFAULT, H5P_DEFAULT);
				if (attrId < 0) {
					cerr << "Could not open attribute '" << sAttrName << "'" << endl;
					continue;
				}

				// parse type info
//				cout << "Reading attribute: " << sAttrName << endl;
				fAttributes[sAttrName] = llReadAttribute(attrId);

				// close attribute
				H5Aclose(attrId);
			}
		}
	}
Example #9
0
int HBase::numAttrs()
{
	hsize_t nattr = H5Aget_num_attrs(fObjectId);
	return (int)nattr;
}
Example #10
0
herr_t HDF5CreateGroupObjs(hid_t hHDF5, const char *pszObjName, 
			   void *poHObjParent)
{
    herr_t	ret;			/* error return status */
    hid_t	hGroupID;		/* identifier of group */
    hid_t	hDatasetID;		/* identifier of dataset */
    hsize_t     nbObjs=0;		/* number of objects in a group */
    int         nbAttrs=0;		/* number of attributes in object */
    int		idx;
    int         n_dims;
    H5G_stat_t  oStatbuf;
    hsize_t     *dims=NULL;
    hsize_t     *maxdims=NULL;
    hid_t       datatype;
    hid_t       dataspace;
    hid_t       native;
    herr_t status;

    char *CreatePath( HDF5GroupObjects *poH5Object );

    HDF5GroupObjects *poHchild;
    HDF5GroupObjects *poHparent;

    poHparent = ( HDF5GroupObjects * ) poHObjParent;
    poHchild=poHparent->poHchild;

    if( H5Gget_objinfo( hHDF5, pszObjName, FALSE, &oStatbuf ) < 0  )
	return -1;

    
/* -------------------------------------------------------------------- */
/*      Look for next child                                             */
/* -------------------------------------------------------------------- */
    for( idx=0; idx < poHparent->nbObjs; idx++ )	{
	if( poHchild->pszName == NULL ) break;
	poHchild++;
    }

    if( idx == poHparent->nbObjs ) 
	return -1;  // all children parsed
    
/* -------------------------------------------------------------------- */
/*      Save child information                                          */
/* -------------------------------------------------------------------- */
    poHchild->pszName  = CPLStrdup( pszObjName );
    
    poHchild->nType  = oStatbuf.type;
    poHchild->nIndex = idx;
    poHchild->poHparent = poHparent;
    poHchild->nRank     = 0;
    poHchild->paDims    = 0;
    poHchild->HDatatype = 0;
    poHchild->objno[0]  = oStatbuf.objno[0];
    poHchild->objno[1]  = oStatbuf.objno[1];
    if( poHchild->pszPath == NULL ) {
	poHchild->pszPath  = CreatePath( poHchild );
    }
    if( poHparent->pszPath == NULL ) {
	poHparent->pszPath = CreatePath( poHparent );
    }


    switch ( oStatbuf.type ) 
	{
	case H5G_LINK:
	    poHchild->nbAttrs = 0;
	    poHchild->nbObjs = 0;
	    poHchild->poHchild = NULL;
	    poHchild->nRank      = 0;
	    poHchild->paDims    = 0;
	    poHchild->HDatatype = 0;
	    break;
	    
	case H5G_GROUP:
	    if( ( hGroupID = H5Gopen( hHDF5, pszObjName ) ) == -1  ) {
		printf( "Error: unable to access \"%s\" group.\n", 
			pszObjName );
		return -1;
	    }
	    nbAttrs          = H5Aget_num_attrs( hGroupID );
	    ret              = H5Gget_num_objs( hGroupID, &nbObjs );
	    poHchild->nbAttrs= nbAttrs;
	    poHchild->nbObjs = (int) nbObjs;
	    poHchild->nRank      = 0;
	    poHchild->paDims    = 0;
	    poHchild->HDatatype = 0;
	    
	    if( nbObjs > 0 ) {
		poHchild->poHchild =( HDF5GroupObjects * )
		    CPLCalloc( (int)nbObjs, sizeof( HDF5GroupObjects ) );
		memset( poHchild->poHchild,0,
			(size_t) (sizeof( HDF5GroupObjects ) * nbObjs) );
	    }
	    else 
		poHchild->poHchild = NULL;

            if( !HDF5GroupCheckDuplicate( poHparent, oStatbuf.objno ) )
                H5Giterate( hHDF5, pszObjName, NULL, 
                            HDF5CreateGroupObjs,  (void*) poHchild );
            else
                CPLDebug( "HDF5", "avoiding link looping on node '%s'.", 
                          pszObjName );
	    H5Gclose( hGroupID );
	    break;
	    
	case H5G_DATASET:
	    
	    if( ( hDatasetID = H5Dopen( hHDF5, pszObjName ) ) == -1  ) {
		printf( "Error: unable to access \"%s\" dataset.\n", 
		       pszObjName );
		return -1;
	    }
	    nbAttrs      = H5Aget_num_attrs( hDatasetID );
	    datatype     = H5Dget_type( hDatasetID );
	    dataspace    = H5Dget_space( hDatasetID );
	    n_dims       = H5Sget_simple_extent_ndims( dataspace );
	    native       = H5Tget_native_type( datatype, H5T_DIR_ASCEND );
	    
	    if( n_dims > 0 ) {
		dims     = (hsize_t *) CPLCalloc( n_dims,sizeof( hsize_t ) );
		maxdims  = (hsize_t *) CPLCalloc( n_dims,sizeof( hsize_t ) );
	    }
	    status     = H5Sget_simple_extent_dims( dataspace, dims, maxdims );
	    if( maxdims != NULL )
		CPLFree( maxdims );
	    
	    if( n_dims > 0 ) {
		poHchild->nRank     = n_dims;   // rank of the array
		poHchild->paDims    = dims;      // dimmension of the array.
		poHchild->HDatatype = datatype;  // HDF5 datatype
	    }
	    else  {
		poHchild->nRank     = -1;
		poHchild->paDims    = NULL;
		poHchild->HDatatype = 0;
	}
	    poHchild->nbAttrs   = nbAttrs;
	    poHchild->nbObjs    = 0;
	    poHchild->poHchild  = NULL;
	    poHchild->native    = native;
	    ret                 = H5Dclose( hDatasetID );
	    break;
	    
	case H5G_TYPE:
	    poHchild->nbAttrs = 0;
	    poHchild->nbObjs = 0;
	    poHchild->poHchild = NULL;
	    poHchild->nRank      = 0;
	    poHchild->paDims    = 0;
	    poHchild->HDatatype = 0;
	    break;
	    
	default:
	    break;
	}
    
    return 0;
}
void read_amuse_hdf5_header(char   *filename,
                             int *npart,
                             int *ncol,
                             int *ndim,
                             int *ndimV,
                             double *time,
                             int *ierr)
   {
   hid_t     file_id;
   hid_t     group_id, group_id1, group_id2;
   hid_t     attrib_id;
   herr_t    status;
   herr_t    HDF5_error = -1;
   
   *ierr = 0;
   *ndim = 0;
   *ndimV = 0;

   if (debug) printf("DEBUG: opening %s \n",filename);
   file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT);
   if (file_id == HDF5_error)
      { printf("ERROR opening %s \n",filename); *ierr = 1; return; }
   
   char *maingroup = "particles";
   /*
    * Open the "particles" dataset and read the number of particles attribute
    *
    */
   if (!checkfordataset(file_id,maingroup))
      {
         printf(" ERROR: \"%s\" dataset not found in AMUSE HDF5 file\n",maingroup);
         *ierr = 2;
         return;
      }

#if H5_VERSION_GE(1,8,0)
   group_id1 = H5Gopen2(file_id,maingroup,H5P_DEFAULT);
#else
   group_id1 = H5Gopen(file_id,maingroup);
#endif
   if (group_id1 == HDF5_error) 
      { printf("ERROR opening %s data set \n",maingroup); *ierr = 2; return; }

#if H5_VERSION_GE(1,8,0)
   group_id = H5Gopen2(group_id1,"0000000001",H5P_DEFAULT);
#else
   group_id = H5Gopen(group_id1,"0000000001");
#endif
   if (group_id == HDF5_error) 
      { printf("ERROR opening 00000000001 data set \n"); *ierr = 2; return; }

   int nattrib;
   int i;
   char name[256];
   nattrib = H5Aget_num_attrs(group_id);
   if (debug) printf("number of attributes found = %i\n",nattrib);

   /*
    * Read through all of the attributes in the header, so we
    * can still spit out the values even if they are not used by SPLASH
    */
   for(i=0; i < nattrib; i++) {
      attrib_id = H5Aopen_idx(group_id,i);
      ssize_t  attr_status;
      attr_status = H5Aget_name(attrib_id, 256, name);
      
      hid_t  type_id;
      type_id = H5Aget_type(attrib_id);
      /*type_class = H5Tget_native_type(type_id,H5T_DIR_ASCEND);*/
      if (strcmp(name,"time")==0) {
         status = H5Aread(attrib_id,H5T_NATIVE_DOUBLE,time);
      } else if (strcmp(name,"number_of_particles")==0) {
         status = H5Aread(attrib_id,H5T_NATIVE_INT,npart);
      } else {
         if (debug) printf("DEBUG: unknown attribute %s \n",name);
      }

      if (status==HDF5_error) {
         printf(" ERROR reading attribute %s \n",name);
      }

      status = H5Aclose(attrib_id);
   }

   /*
    * Now we need to get the number of data columns in the file
    * (from the number of datasets in the "attributes" group)
    */
#if H5_VERSION_GE(1,8,0)
   group_id2 = H5Gopen2(group_id,"attributes",H5P_DEFAULT);
#else
   group_id2 = H5Gopen(group_id,"attributes");
#endif
   if (group_id2 == HDF5_error) 
      { printf("ERROR opening %s data set \n","attributes"); *ierr = 2; return; }
   
   hsize_t ndatasets;
   status = H5Gget_num_objs(group_id2, &ndatasets);
   if (debug) printf("DEBUG: number of datasets = %i \n",(int)ndatasets);

   *ncol  = (int)ndatasets;
   int idim;

   /* check that coordinates are present in file */
   idim = get_rank_by_name(group_id2,"x");
   if (idim <= 0) { printf("ERROR: x positions not found\n"); *ierr = 3; }

   idim = get_rank_by_name(group_id2,"y");
   if (idim <= 0) { printf("ERROR: y positions not found\n"); *ierr = 3; }
 
   idim = get_rank_by_name(group_id2,"z");
   if (idim <= 0) { 
      printf("z positions not found, assuming file is 2D \n");
      *ndim = 2;
      *ndimV = 2;
   } else {
      *ndim = 3;
      *ndimV = 3;
   }

   /* finish, close all open datasets and close file */

   status = H5Gclose(group_id2);
   if (status == HDF5_error)
      { printf("ERROR closing attributes data set \n"); *ierr = 3; return; }

   status = H5Gclose(group_id);
   if (status == HDF5_error) 
      { printf("ERROR closing %s data set \n",maingroup); *ierr = 3; return; }

   status = H5Gclose(group_id1);
   if (status == HDF5_error) 
      { printf("ERROR closing 0001 data set \n"); *ierr = 3; return; }

   status = H5Fclose( file_id );
   if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; }
   if (debug) printf("DEBUG: finished header read \n");
   
   }
Example #12
0
herr_t HDF5CreateGroupObjs( hid_t hHDF5, const char *pszObjName,
                            void *poHObjParent)
{
    HDF5GroupObjects *const poHparent =
        static_cast<HDF5GroupObjects *>(poHObjParent);
    HDF5GroupObjects *poHchild = poHparent->poHchild;
    H5G_stat_t oStatbuf;

    if( H5Gget_objinfo(hHDF5, pszObjName, FALSE, &oStatbuf) < 0  )
        return -1;

    // Look for next child.
    unsigned idx = 0;  // idx is used after the for loop.
    for( ; idx < poHparent->nbObjs; idx++ )
    {
        if( poHchild->pszName == nullptr ) break;
        poHchild++;
    }

    if( idx == poHparent->nbObjs )
        return -1;  // All children parsed.

    // Save child information.
    poHchild->pszName = CPLStrdup(pszObjName);

    poHchild->nType = oStatbuf.type;
    poHchild->nIndex = idx;
    poHchild->poHparent = poHparent;
    poHchild->nRank = 0;
    poHchild->paDims = nullptr;
    poHchild->HDatatype = 0;
    poHchild->objno[0] = oStatbuf.objno[0];
    poHchild->objno[1] = oStatbuf.objno[1];
    if( poHchild->pszPath == nullptr )
    {
        CreatePath(poHchild);
    }
    if( poHparent->pszPath == nullptr )
    {
        CreatePath(poHparent);
    }

    switch( oStatbuf.type )
    {
    case H5G_LINK:
    {
        poHchild->nbAttrs = 0;
        poHchild->nbObjs = 0;
        poHchild->poHchild = nullptr;
        poHchild->nRank = 0;
        poHchild->paDims = nullptr;
        poHchild->HDatatype = 0;
        break;
    }
    case H5G_GROUP:
    {
        hid_t hGroupID = H5I_INVALID_HID;  // Identifier of group.
        if( (hGroupID = H5Gopen(hHDF5, pszObjName)) == -1)
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                     "unable to access \"%s\" group.", pszObjName);
            return -1;
        }
        // Number of attributes in object.
        const int nbAttrs = H5Aget_num_attrs(hGroupID);
        hsize_t nbObjs = 0;  // Number of objects in a group.
        H5Gget_num_objs(hGroupID, &nbObjs);
        poHchild->nbAttrs = nbAttrs;
        poHchild->nbObjs = static_cast<int>(nbObjs);
        poHchild->nRank = 0;
        poHchild->paDims = nullptr;
        poHchild->HDatatype = 0;

        if( nbObjs > 0 )
        {
            poHchild->poHchild = static_cast<HDF5GroupObjects *>(
                CPLCalloc(static_cast<int>(nbObjs), sizeof(HDF5GroupObjects)));
            memset(poHchild->poHchild, 0,
                   static_cast<size_t>(sizeof(HDF5GroupObjects) * nbObjs));
        }
        else
        {
            poHchild->poHchild = nullptr;
        }

        if( !HDF5GroupCheckDuplicate(poHparent, oStatbuf.objno) )
            H5Giterate(hHDF5, pszObjName, nullptr, HDF5CreateGroupObjs, poHchild);
        else
            CPLDebug("HDF5", "avoiding link looping on node '%s'.", pszObjName);

        H5Gclose(hGroupID);
        break;
    }
    case H5G_DATASET:
    {
        hid_t hDatasetID = H5I_INVALID_HID;  // Identifier of dataset.
        if( (hDatasetID = H5Dopen(hHDF5, pszObjName)) == -1)
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                     "unable to access \"%s\" dataset.", pszObjName);
            return -1;
        }
        const int nbAttrs = H5Aget_num_attrs(hDatasetID);
        const hid_t datatype = H5Dget_type(hDatasetID);
        const hid_t dataspace = H5Dget_space(hDatasetID);
        const int n_dims = H5Sget_simple_extent_ndims(dataspace);
        const hid_t native = H5Tget_native_type(datatype, H5T_DIR_ASCEND);
        hsize_t *maxdims = nullptr;
        hsize_t *dims = nullptr;

        if( n_dims > 0 )
        {
            dims = static_cast<hsize_t *>(CPLCalloc(n_dims, sizeof(hsize_t)));
            maxdims =
                static_cast<hsize_t *>(CPLCalloc(n_dims, sizeof(hsize_t)));
        }
        H5Sget_simple_extent_dims(dataspace, dims, maxdims);
        if( maxdims != nullptr )
            CPLFree(maxdims);

        if( n_dims > 0 )
        {
            poHchild->nRank = n_dims;        // rank of the array
            poHchild->paDims = dims;         // dimension of the array.
            poHchild->HDatatype = datatype;  // HDF5 datatype
        }
        else
        {
            poHchild->nRank = -1;
            poHchild->paDims = nullptr;
            poHchild->HDatatype = 0;
        }
        poHchild->nbAttrs = nbAttrs;
        poHchild->nbObjs = 0;
        poHchild->poHchild = nullptr;
        poHchild->native = native;
        H5Tclose(datatype);
        H5Sclose(dataspace);
        H5Dclose(hDatasetID);
        break;
    }
    case H5G_TYPE:
    {
        poHchild->nbAttrs = 0;
        poHchild->nbObjs = 0;
        poHchild->poHchild = nullptr;
        poHchild->nRank = 0;
        poHchild->paDims = nullptr;
        poHchild->HDatatype = 0;
        break;
    }
    default:
        break;
    }

    return 0;
}
Example #13
0
void rdiff(const char *name, hid_t f1, hid_t f2) {
  hid_t g1 = H5Gopen(f1, name, H5P_DEFAULT);
  hid_t g2 = H5Gopen(f2, name, H5P_DEFAULT);
  if (g1 >= 0 && g2 >= 0) {
    int n1 = H5Aget_num_attrs(g1);
    for (int i = 0; i < n1; i++) {
      char aname[MAXNAME];
      hid_t a1 = H5Aopen_idx(g1, i);
      assert(H5Aget_name(a1, MAXNAME, aname) < MAXNAME);
      H5Aclose(a1);
      if (!H5LTfind_attribute(g2, aname)) {
	printf("Only in %s[%s%s]\n", file1, name, aname);
	continue;
      }
      int d1, d2;
      H5LTget_attribute_ndims(f1, name, aname, &d1);
      H5LTget_attribute_ndims(f2, name, aname, &d2);
      assert(d1 <= 1 && d2 <= 1);
      hsize_t dims1, dims2;
      H5T_class_t t1, t2;
      size_t ts1, ts2;
      H5LTget_attribute_info(f1, name, aname, &dims1, &t1, &ts1);
      H5LTget_attribute_info(f2, name, aname, &dims2, &t2, &ts2);
      assert(t1 == t2);
      assert(t1 == H5T_INTEGER || t1 == H5T_FLOAT || t1 == H5T_STRING);
      if (t1 == H5T_INTEGER) {
	assert(d1==0 || (dims1 == 1 && dims2 == 1));
	assert(ts1 == 4 && ts2 == 4);
	int v1, v2;
	H5LTget_attribute_int(f1, name, aname, &v1);
	H5LTget_attribute_int(f2, name, aname, &v2);
	if (v1 != v2) {
	  printf("%s[%s%s]=%d %s[%s%s]=%d\n", file1, name, aname, v1, file2, name, aname, v2);
	}
      }
      if (t1 == H5T_FLOAT) {
	assert(d1==0 || (dims1 == 1 && dims2 == 1));
	assert(ts1 == 4 && ts2 == 4);
	float v1, v2;
	H5LTget_attribute_float(f1, name, aname, &v1);
	H5LTget_attribute_float(f2, name, aname, &v2);
	if (v1 != v2) {
	  printf("%s[%s%s]=%g %s[%s%s]=%g\n", file1, name, aname, v1, file2, name, aname, v2);
	}
      }
      if (t1 == H5T_STRING) {
	assert(ts1 < 256 && ts2 < 256);
	char buf1[256];
	char buf2[256];
	H5LTget_attribute_string(f1, name, aname, buf1);
	H5LTget_attribute_string(f2, name, aname, buf2);
	if (strcmp(buf1, buf2)) {
	  printf("%s[%s%s]=%s %s[%s%s]=%s\n", file1, name, aname, buf1, file2, name, aname, buf2);
	}
      }
    }
    int n2 = H5Aget_num_attrs(g2);
    for (int i = 0; i < n2; i++) {
      char aname[MAXNAME];
      hid_t a2 = H5Aopen_idx(g2, i);
      assert(H5Aget_name(a2, MAXNAME, aname) < MAXNAME);
      H5Aclose(a2);
      if (!H5LTfind_attribute(g1, aname)) {
	printf("Only in %s[%s%s]\n", file2, name, aname);
	continue;
      }
    }

    hsize_t nobj;
    H5Gget_num_objs(g1, &nobj);
    for (int i = 0; i < nobj; i++) {
      char oname[MAXNAME];
      assert(H5Gget_objname_by_idx(g1, i, oname, MAXNAME) < MAXNAME);
      int otype = H5Gget_objtype_by_idx(g1, i);
      assert(otype == H5G_DATASET);
      if (!H5LTfind_dataset(g2, oname)) {
	printf("Only in %s[%s%s]\n", file1, name, oname);
	continue;
      }
      hsize_t dims1[2], dims2[2];
      H5T_class_t t1, t2;
      size_t ts1, ts2;
      H5LTget_dataset_info(g1, oname, dims1, &t1, &ts1);
      H5LTget_dataset_info(g2, oname, dims2, &t2, &ts2);
      if (dims1[0] != dims2[0] || dims1[1] != dims2[1]) {
	printf("%s[%s%s](%d,%d) != %s[%s%s](%d,%d)\n", 
	       file1, name, oname, dims1[1], dims1[0],
	       file2, name, oname, dims2[1], dims2[0]);
	continue;
      }
      float *data1 = malloc(dims1[0]*dims1[1]*sizeof(float));
      float *data2 = malloc(dims1[0]*dims1[1]*sizeof(float));
      H5LTread_dataset_float(g1, oname, data1);
      H5LTread_dataset_float(g2, oname, data2);
      float maxdiff = 0;
      for (int i = dims1[0]*dims1[1]-1; i >= 0; i--) {
	float d = data1[i] - data2[i];
	if (d < 0) d = -d;
	if (d > maxdiff) maxdiff = d;
      }
      printf("max |%s[%s%s] - %s[%s%s]| = %g\n",
	     file1, name, oname, file2, name, oname, maxdiff);
      free(data1); free(data2);
    }
    H5Gget_num_objs(g2, &nobj);
    for (int i = 0; i < nobj; i++) {
      char oname[MAXNAME];
      assert(H5Gget_objname_by_idx(g2, i, oname, MAXNAME) < MAXNAME);
      int otype = H5Gget_objtype_by_idx(g2, i);
      assert(otype == H5G_DATASET);
      if (!H5LTfind_dataset(g1, oname)) {
	printf("Only in %s[%s%s]\n", file2, name, oname);
	continue;
      }
    }
    H5Gclose(g1);
    H5Gclose(g2);
  } else if (g1 >= 0) {
    printf("Only in %s:%s\n", file1, name);
    H5Gclose(g1);
  } else if (g2 >= 0) {
    printf("Only in %s:%s\n", file2, name);
    H5Gclose(g2);
  } else {
    printf("Group %s does not exist in either file.\n", name);
  }
}
Example #14
0
/* ****************************************************************************************************************************** */
int main(int argc, char *argv[]) {
  hid_t   fileID, dataSetID;
  herr_t  hErrVal;
  int     i;
  hsize_t dims[1024], maxDims[1024];
  H5T_class_t class;
  char classStr[32];
  hid_t dataTypeID;
  size_t dataSize;
  H5T_order_t order;
  int rank; /* Note this is an int, not an hssize_t */
  int intVal;
  hid_t dataSpaceID;
  hid_t rootGroupID;
  hsize_t numInRootGrp, firstDataSetIdx, foundFirstDataSet;
  char attrName[1024], firstDataSetName[1024];
  ssize_t objectNameSize, attrNameSize;
  H5G_stat_t objectStatInfo;
  int numAttrs;
  int curAttrIdx;
  hid_t attrID;
  hsize_t numDataPoints;
  unsigned majnum, minnum, relnum;

  /* Load the library -- not required most platforms. */
  hErrVal = H5open();
  mjrHDF5_chkError(hErrVal);

  /* Get the library version */
  hErrVal = H5get_libversion(&majnum, &minnum, &relnum);
  mjrHDF5_chkError(hErrVal);
  printf("Lib Version: v%lu.%lur%lu\n", (unsigned long)majnum, (unsigned long)minnum, (unsigned long)relnum);

  /*  Open an existing file. */
  fileID = H5Fopen(TST_FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT);
  mjrHDF5_chkError(fileID);

  /*  Get the ID for the "root" group -- every HDF5 has one */
  rootGroupID = H5Gopen(fileID, "/", H5P_DEFAULT);
  mjrHDF5_chkError(rootGroupID);

  /* Get the number of objects in the root group. */
  hErrVal = H5Gget_num_objs(rootGroupID, &numInRootGrp);
  mjrHDF5_chkError(hErrVal);

  printf("The root group contains %lu object%c\n", (unsigned long)numInRootGrp, (numInRootGrp==1?' ':'s'));

  if(numInRootGrp < 1) {
    printf("As the file contains NO objects, I have nothing left to do...\n");
    exit(1);
  } /* end if */

  /*  Find the first dataset in the root group. */
  for(foundFirstDataSet=0,firstDataSetIdx=0; (!foundFirstDataSet)&&(firstDataSetIdx<numInRootGrp); firstDataSetIdx++) {
    /* Get object name from the index. */
    objectNameSize = H5Gget_objname_by_idx(rootGroupID, firstDataSetIdx, firstDataSetName, 1024);
    mjrHDF5_chkError(objectNameSize);
    if(objectNameSize == 0) { /* Need to check for zero return too */
      printf("ERROR: Object with index %lu doesn't exist in root group!\n", (unsigned long)firstDataSetIdx);
      exit(1);
    } /* end if */
    /*  Now use the object name to get info about the object... */
    hErrVal = H5Gget_objinfo(rootGroupID, firstDataSetName, 0, &objectStatInfo);
    mjrHDF5_chkError(hErrVal);
    /* If the object is a dataset, then print out some info. */
    if(objectStatInfo.type == H5G_DATASET) {
      printf("Object %luth (%s) is a dataset!\n", (unsigned long)firstDataSetIdx, firstDataSetName);
      printf("The name of the %luth object of the root group is: %s\n", (unsigned long)firstDataSetIdx, firstDataSetName);
      foundFirstDataSet = 1;
      printf("Info for the %s dataset:\n", firstDataSetName);
      printf("  Modify time: %lu\n", (unsigned long)objectStatInfo.mtime);
      printf("  Type: %lu\n", (unsigned long)objectStatInfo.type);
      printf("  Link count: %lu\n", (unsigned long)objectStatInfo.nlink);
    } /* end if */
  } /* end for */
  /* Note: At this point index of the dataset will be: firstDataSetIdx-- */
  if(!foundFirstDataSet) {
    printf("ERROR: Could not find a dataset in the root group\n");
    exit(1);
  } /* end if */

  /* Open the dataset we found -- we know it exists. */
  dataSetID = H5Dopen(rootGroupID, firstDataSetName, H5P_DEFAULT);
  mjrHDF5_chkError(dataSetID);

  /* ****************************************************************************************************************************** */
  /* Get some info regarding the TYPE of the dataset. */
  dataTypeID  = H5Dget_type(dataSetID);
  mjrHDF5_chkError(dataTypeID);
  /*  Get the class of the data */
  class = H5Tget_class(dataTypeID);
  mjrHDF5_Tclass2str(class, classStr);
  printf("  Object class: %s\n", classStr);
  /*  Get the size of the type */
  dataSize = H5Tget_size(dataTypeID);
  if(dataSize == 0) {
    printf("ERROR: Failure in H5Tget_size().\n");
    exit(1);
  } /* end if */
  printf("  Size of data type: %lu\n", (unsigned long)dataSize);
  /*  Get the byte order */
  order = H5Tget_order(dataTypeID);
  printf("  Byte Order: ");
  switch(order) {
    case H5T_ORDER_ERROR  : printf("ERROR\n");            break;
    case H5T_ORDER_LE     : printf("Little Endian\n");    break;
    case H5T_ORDER_BE     : printf("Big Endian\n");       break;
    case H5T_ORDER_VAX    : printf("VAX mixed endian\n"); break;
    case H5T_ORDER_MIXED  : printf("Mixed endian\n"); break;
    case H5T_ORDER_NONE   : printf("particular order\n"); break;
  } /* end switch */
  /*  We are done with the datatype. */
  hErrVal = H5Tclose(dataTypeID);
  mjrHDF5_chkError(hErrVal);

  /* ****************************************************************************************************************************** */
  /* Figure out the size of the dataset. */
  dataSpaceID = H5Dget_space(dataSetID);
  mjrHDF5_chkError(dataSpaceID);
  /*  Get the number of dims. */
  rank = H5Sget_simple_extent_ndims(dataSpaceID);
  mjrHDF5_chkError(rank);
  if(rank > 1024) {
    /*  This can't really happen (limit is 32) */
    printf("ERROR: rank too large.\n");
    exit(1);
  } /* end if */
  /* Get the size of each dim. */
  intVal = H5Sget_simple_extent_dims(dataSpaceID, dims, maxDims);
  mjrHDF5_chkError(intVal);
  printf("  Dataspace Rank %lu\n", (unsigned long)rank);
  printf("  Dim Lengths: ");
  for(i=0; i<rank; i++)
    if(dims[i] == H5S_UNLIMITED) {
      printf("%s ", "UNLIMITED");
    } else {
      printf("%ld ", (long)(dims[i]));
    } /* end if/else */
  printf("\n");
  printf("  Max Dim Lengths: ");
  for(i=0; i<rank; i++)
    if(maxDims[i] == H5S_UNLIMITED) {
      printf("%s ", "UNLIMITED");
    } else {
      printf("%ld ", (long)(maxDims[i]));
    } /* end if/else */
  printf("\n");
  numDataPoints = H5Sget_simple_extent_npoints(dataSpaceID);
  if(numDataPoints == 0) {
    printf("ERROR: Call to H5Sget_simple_extent_npoints failed.\n");
    exit(1);
  } /* end if */
  printf("Number of data points: %lu\n", (unsigned long)numDataPoints);

  /* We are done with the dataSpaceID */
  hErrVal = H5Sclose(dataSpaceID);
  mjrHDF5_chkError(hErrVal);

  /* Get the number of attributes for the dataSet. */
  numAttrs = H5Aget_num_attrs(dataSetID);
  mjrHDF5_chkError(numAttrs);

  printf("  Number of attrs: %lu\n", (unsigned long)numAttrs);

  /* If we have any attributes, we get info for them */
  if(numAttrs > 0) {
    printf("  Attribute info:\n");

    for(curAttrIdx=0; curAttrIdx<numAttrs; curAttrIdx++) {
      attrID = H5Aopen_idx(dataSetID, curAttrIdx);
      mjrHDF5_chkError(attrID);

      attrNameSize = H5Aget_name(attrID, 1024, attrName);
      mjrHDF5_chkError(attrNameSize);

      printf("    Number %3lu:  ", (unsigned long)curAttrIdx);

	  dataTypeID  = H5Aget_type(attrID);
	  mjrHDF5_chkError(dataTypeID);
	  /* Get the class for the type. */
	  class = H5Tget_class(dataTypeID);
	  mjrHDF5_Tclass2str(class, classStr);
	  printf(" Class: %-16s", classStr);
	  /*  Get the size of the type */
	  dataSize = H5Tget_size(dataTypeID);
	  if(dataSize == 0) {
		printf("ERROR: Failure in H5Tget_size().\n");
		exit(1);
	  } /* end if */
	  printf(" Size: %3lu ", (unsigned long)dataSize);
	  hErrVal = H5Tclose(dataTypeID);
	  mjrHDF5_chkError(hErrVal);

      printf(" Name: %s \n", attrName);

      hErrVal = H5Aclose(attrID);
	  mjrHDF5_chkError(hErrVal);
    } /* end for */
  } /* end if */