Exemple #1
0
/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Aget_name
 * Signature: (J)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL
Java_hdf_hdf5lib_H5_H5Aget_1name
    (JNIEnv *env, jclass clss, jlong attr_id)
{
    char    *aName;
    jstring  str = NULL;
    ssize_t  buf_size;

    /* get the length of the name */
    buf_size = H5Aget_name((hid_t)attr_id, 0, NULL);
    if (buf_size <= 0) {
        h5badArgument(env, "H5Aget_name:  buf_size <= 0");
    } /* end if */
    else {
        buf_size++; /* add extra space for the null terminator */
        aName = (char*)HDmalloc(sizeof(char) * (size_t)buf_size);
        if (aName == NULL) {
            h5outOfMemory(env, "H5Aget_name:  malloc failed");
        } /* end if */
        else {
            buf_size = H5Aget_name((hid_t)attr_id, (size_t)buf_size, aName);
            if (buf_size < 0) {
                HDfree(aName);
                h5libraryError(env);
            } /* end if */
            else {
                /* save the string; */
                str = ENVPTR->NewStringUTF(ENVPAR aName);
                HDfree(aName);
            } /* end else */
        } /* end else */
    } /* end else */
    return str;
} /* end Java_hdf_hdf5lib_H5_H5Aget_1name */
Exemple #2
0
/*----------------------------------------------------------------------------
 * Name:        h5aget_name_c
 * Purpose:     Call H5Aget_name to get attribute's name
 * Inputs:      attr_id - attribute identifier
 *              bufsize -size of the buffer
 * Outputs:     buf - buffer to hold the name
 * Returns:     0 on success, -1 on failure
 * Programmer:  Elena Pourmal
 *              Thursday, August 12, 1999
 * Modifications:
 *---------------------------------------------------------------------------*/
int_f
nh5aget_name_c(hid_t_f *attr_id, size_t_f *bufsize, _fcd buf)
{
    char *c_buf=NULL;           /* Buffer to hold C string */
    int_f ret_value=0;          /* Return value */

     /*
      * Allocate buffer to hold name of an attribute
      */
     if ((c_buf = HDmalloc((size_t)*bufsize +1)) == NULL)
         HGOTO_DONE(FAIL);

     /*
      * Call H5Aget_name function
      */
     if ((ret_value = (int_f)H5Aget_name((hid_t)*attr_id, (size_t)*bufsize, c_buf)) < 0)
         HGOTO_DONE(FAIL);

     /*
      * Convert C name to FORTRAN and place it in the given buffer
      */
      HD5packFstring(c_buf, _fcdtocp(buf), (size_t)*bufsize);

done:
      if(c_buf) HDfree(c_buf);
      return ret_value;
}
Exemple #3
0
extern hid_t get_attribute_handle(hid_t parent, char *name)
{
    char buf[MAX_ATTR_NAME+1];
    int nattr, i, len;
    hid_t aid;
    H5O_info_t object_info;

    if (parent < 0) {
        debug3("PROFILE: parent is not HDF5 object");
        return -1;
    }

    H5Oget_info(parent, &object_info);
    nattr = object_info.num_attrs;
    for (i = 0; (nattr>0) && (i<nattr); i++) {
        aid = H5Aopen_by_idx(parent, ".", H5_INDEX_NAME, H5_ITER_INC,
                             i, H5P_DEFAULT, H5P_DEFAULT);
        // Get the name of the attribute.
        len = H5Aget_name(aid, MAX_ATTR_NAME, buf);
        if (len < MAX_ATTR_NAME) {
            if (strcmp(buf, name) == 0) {
                return aid;
            }
        }
        H5Aclose(aid);
    }
    debug3("PROFILE: failed to find HDF5 attribute=%s\n", name);

    return -1;
}
Exemple #4
0
std::string HBase::getAttrName(hid_t attrId)
{
	char memb_name[1024];
	H5Aget_name(attrId, (size_t)1024, 
		memb_name );
	std::stringstream sst;
	sst.str("");
	sst<<memb_name;
	return sst.str();
}
Exemple #5
0
void
HDF5Attribute::write(const HDF5Id mem_type_id, const void *attribute_data) const
{
    i32 res = H5Awrite(m_id, mem_type_id, attribute_data);
    if (res < 0) {
        char attrName[256];
        H5Aget_name(m_id, 256, attrName);
        THROW(Iex::IoExc, "Could not write attribute" << attrName);
    }
}
Exemple #6
0
/*
 * test_attrname
 * Test that attributes can deal with UTF-8 strings
 */
void test_attrname(hid_t fid, const char * string)
{
  hid_t group_id, attr_id;
  hid_t dtype_id, space_id;
  hsize_t dims=1;
  char read_buf[MAX_STRING_LENGTH];
  herr_t ret;

 /* Create a new group and give it an attribute whose
  * name and value are UTF-8 strings.
  */
  group_id = H5Gcreate2(fid, GROUP4_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(group_id, FAIL, "H5Gcreate2");

  space_id = H5Screate_simple(RANK, &dims, NULL);
  CHECK(space_id, FAIL, "H5Screate_simple");
  dtype_id = H5Tcopy(H5T_C_S1);
  CHECK(dtype_id, FAIL, "H5Tcopy");
  ret = H5Tset_size(dtype_id, (size_t)MAX_STRING_LENGTH);
  CHECK(ret, FAIL, "H5Tset_size");

  /* Create the attribute and check that its name is correct */
  attr_id = H5Acreate2(group_id, string, dtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
  CHECK(attr_id, FAIL, "H5Acreate2");
  ret = H5Aget_name(attr_id, (size_t)MAX_STRING_LENGTH, read_buf);
  CHECK(ret, FAIL, "H5Aget_name");
  ret = strcmp(read_buf, string);
  VERIFY(ret, 0, "strcmp");
  read_buf[0] = '\0';

  /* Try writing and reading from the attribute */
  ret = H5Awrite(attr_id, dtype_id, string);
  CHECK(ret, FAIL, "H5Awrite");
  ret = H5Aread(attr_id, dtype_id, read_buf);
  CHECK(ret, FAIL, "H5Aread");
  ret = strcmp(read_buf, string);
  VERIFY(ret, 0, "strcmp");

  /* Clean up */
  ret = H5Aclose(attr_id);
  CHECK(ret, FAIL, "H5Aclose");
  ret = H5Tclose(dtype_id);
  CHECK(ret, FAIL, "H5Tclose");
  ret = H5Sclose(space_id);
  CHECK(ret, FAIL, "H5Sclose");
  ret = H5Gclose(group_id);
  CHECK(ret, FAIL, "H5Gclose");
}
Exemple #7
0
void
HDF5Attribute::read(HDF5Id mem_type_id, void *buf) const
{
    // make sure it's a valid attribute id
    if (!isValid()) {
        THROW(Iex::IoExc, "Trying to read invalid attribute");
    }

    // read in the attribute
    HDF5Err res = H5Aread(m_id, mem_type_id, buf);

    // check if read was successful
    if (res < 0) {
        char attrName[256];
        H5Aget_name(m_id, 256, attrName);
        THROW(Iex::IoExc, "Couldn't read attribute " << attrName);
    }
}
void h5_att_str(hid_t file_id, char *group, char *name, char *value)
{
  int ii, kk;
  void *buf = NULL;
  hid_t attr_id;
  H5O_info_t object_info;
  char *attr_name = (char *) MALLOC(sizeof(char)*50);

  H5Oget_info_by_name(file_id, group, &object_info, H5P_DEFAULT);
  for (ii=0; ii<object_info.num_attrs; ii++) {
    attr_id = H5Aopen_by_idx(file_id, group, H5_INDEX_NAME, H5_ITER_NATIVE, ii,
      H5P_DEFAULT, H5P_DEFAULT);
    H5Aget_name(attr_id, 50, attr_name);
    if (strcmp_case(name, attr_name) == 0) {
      hid_t attr_type = H5Aget_type(attr_id);
      H5T_class_t data_type = H5Tget_native_type(attr_type, H5T_DIR_DEFAULT);
      size_t data_size = H5Tget_size(data_type);
      hid_t attr_space = H5Aget_space(attr_id);
      hsize_t dims[H5S_MAX_RANK];
      int rank = H5Sget_simple_extent_dims(attr_space, dims, NULL);
      hsize_t elements = 1;
      for (kk=0; kk<rank; kk++)
        elements *= dims[kk];
      buf = (void *) MALLOC((unsigned)(elements*data_size));
      H5Aread(attr_id, attr_type, buf);
      H5Tclose(attr_type);
      H5Tclose(data_type);
      H5Sclose(attr_space);
    }
    else {
      H5Aclose(attr_id);
      continue;
    }
    H5Aclose(attr_id);
  }
  if (buf) {
    strcpy(value, buf);
    FREE(buf);
  }
  else
    strcpy(value, "???");
  FREE(attr_name);
}
	std::vector<std::string> list_attributes(hdf5dataset& hdataset)
	{
		H5O_info_t info;
		H5Oget_info(hdataset.handle(), &info);
		std::size_t num_attr = info.num_attrs;

		std::vector<std::string> result(num_attr);

		const int MAX_LEN = 256;
		char attr_name[MAX_LEN];

		for(std::size_t i = 0; i < num_attr; ++i)
		{
			hid_t attribute_id = H5Aopen_idx(hdataset.handle(), i);
			H5Aget_name(attribute_id, MAX_LEN, attr_name);
			result[i] = std::string(attr_name);
			H5Aclose(attribute_id);
		}

		return result;
	}
Exemple #10
0
/*-------------------------------------------------------------------------
 * Function: build_match_list_attrs
 *
 * Purpose: get list of matching attribute name from obj1 and obj2
 *
 * Note:
 *  Find common attribute; the algorithm for search is referred from 
 *  build_match_list() in h5diff.c .
 *
 * Parameter:
 *  table_out [OUT] : return the list
 *
 * Programmer: Jonathan Kim
 *
 * Date: March 15, 2011
 *------------------------------------------------------------------------*/
static herr_t build_match_list_attrs(hid_t loc1_id, hid_t loc2_id, table_attrs_t ** table_out,  diff_opt_t *options)
{
    H5O_info_t oinfo1, oinfo2;  /* Object info */
    hid_t      attr1_id=-1;     /* attr ID */
    hid_t      attr2_id=-1;     /* attr ID */
    size_t curr1 = 0;
    size_t curr2 = 0;
    unsigned infile[2];
    char  name1[ATTR_NAME_MAX];
    char  name2[ATTR_NAME_MAX];
    int cmp;
    unsigned i;
    table_attrs_t *table_lp = NULL;

    if(H5Oget_info(loc1_id, &oinfo1) < 0)
        goto error;
    if(H5Oget_info(loc2_id, &oinfo2) < 0)
        goto error;

    table_attrs_init( &table_lp );

    
   /*--------------------------------------------------
    * build the list
    */
    while(curr1 < oinfo1.num_attrs && curr2 < oinfo2.num_attrs)
    {
        /*------------------ 
         * open attribute1 */
        if((attr1_id = H5Aopen_by_idx(loc1_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr1, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        /* get name */
        if(H5Aget_name(attr1_id, (size_t)ATTR_NAME_MAX, name1) < 0)
            goto error;

        /*------------------ 
         * open attribute2 */
        if((attr2_id = H5Aopen_by_idx(loc2_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr2, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        /* get name */
        if(H5Aget_name(attr2_id, (size_t)ATTR_NAME_MAX, name2) < 0)
            goto error;

        /* criteria is string compare */
        cmp = HDstrcmp(name1, name2);

        if(cmp == 0) 
        {
            infile[0] = 1;
            infile[1] = 1;
            table_attr_mark_exist(infile, name1, table_lp);
            curr1++;
            curr2++;
        }
        else if(cmp < 0)
        {
            infile[0] = 1;
            infile[1] = 0;
            table_attr_mark_exist(infile, name1, table_lp);
            table_lp->nattrs_only1++;
            curr1++;
        }
        else
        {
            infile[0] = 0;
            infile[1] = 1;
            table_attr_mark_exist(infile, name2, table_lp);
            table_lp->nattrs_only2++;
            curr2++;
        }

        /* close for next turn */
        H5Aclose(attr1_id);
        attr1_id = -1;
        H5Aclose(attr2_id);
        attr2_id = -1;
    } /* end while */

    /* list1 did not end */
    infile[0] = 1;
    infile[1] = 0;
    while(curr1 < oinfo1.num_attrs)
    {
        /*------------------ 
         * open attribute1 */
        if((attr1_id = H5Aopen_by_idx(loc1_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr1, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        /* get name */
        if(H5Aget_name(attr1_id, (size_t)ATTR_NAME_MAX, name1) < 0)
            goto error;

        table_attr_mark_exist(infile, name1, table_lp);
        table_lp->nattrs_only1++;
        curr1++;

        /* close for next turn */
        H5Aclose(attr1_id);
        attr1_id = -1;
    }

    /* list2 did not end */
    infile[0] = 0;
    infile[1] = 1;
    while(curr2 < oinfo2.num_attrs)
    {
        /*------------------ 
         * open attribute2 */
        if((attr2_id = H5Aopen_by_idx(loc2_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr2, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        /* get name */
        if(H5Aget_name(attr2_id, (size_t)ATTR_NAME_MAX, name2) < 0)
            goto error;

        table_attr_mark_exist(infile, name2, table_lp);
        table_lp->nattrs_only2++;
        curr2++;

        /* close for next turn */
        H5Aclose(attr2_id);
    }

    /*------------------------------------------------------
    * print the list
    */
    if(options->m_verbose_level == 2)
    {
        /* if '-v2' is detected */
        parallel_print("   obj1   obj2\n");
        parallel_print(" --------------------------------------\n");
        for(i = 0; i < (unsigned int) table_lp->nattrs; i++) 
        {
            char c1, c2;
            c1 = (table_lp->attrs[i].exist[0]) ? 'x' : ' ';
            c2 = (table_lp->attrs[i].exist[1]) ? 'x' : ' ';
            parallel_print("%5c %6c    %-15s\n", c1, c2, table_lp->attrs[i].name);
        } /* end for */
    }

    if(options->m_verbose_level >= 1)
    {
        parallel_print("Attributes status:  %d common, %d only in obj1, %d only in obj2\n", table_lp->nattrs - table_lp->nattrs_only1 - table_lp->nattrs_only2, table_lp->nattrs_only1, table_lp->nattrs_only2 );
    }

    *table_out = table_lp;

    return 0;

error:
    if (0 < attr1_id)
        H5Aclose(attr1_id);
    if (0 < attr2_id)
        H5Aclose(attr2_id);

    return -1;
}
//-*****************************************************************************
ArImpl::~ArImpl()
{

    m_data.reset();

    if ( m_file >= 0 )
    {
        int dsetCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATASET);
        int grpCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_GROUP );
        int dtypCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATATYPE );
        int attrCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_ATTR );

        int objCount = dsetCount + grpCount + dtypCount + attrCount;

        if ( objCount != 0 )
        {
            std::stringstream strm;
            strm << "Open HDF5 handles detected during reading:" << std::endl
                 << "DataSets: " << dsetCount
                 << ", Groups: " << grpCount
                 << ", DataTypes: " << dtypCount
                 << ", Attributes: " << attrCount;

            std::vector< hid_t > objList;

            // when getting the name corresponding to a hid_t, the get_name
            // functions always append a NULL character, which we strip off
            // when injecting into out stream.
            std::string name;

            if ( dsetCount > 0 )
            {
                strm << std::endl << "DataSets: " << std::endl;
                objList.resize( dsetCount );
                H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_DATASET,
                    dsetCount, &objList.front() );
                for ( int i = 0; i < dsetCount; ++i )
                {
                    int strLen = H5Iget_name( objList[i], NULL, 0 ) + 1;
                    name.resize( strLen );
                    H5Iget_name( objList[i], &(name[0]), strLen );
                    strm << name.substr(0, name.size() - 1) << std::endl;
                }
            }

            if ( grpCount > 0 )
            {
                strm << std::endl << std::endl << "Groups:" << std::endl;
                objList.resize( grpCount );
                H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_GROUP,
                    grpCount, &objList.front() );
                for ( int i = 0; i < grpCount; ++i )
                {
                    int strLen = H5Iget_name( objList[i], NULL, 0 ) + 1;
                    name.resize( strLen );
                    H5Iget_name( objList[i], &(name[0]), strLen );
                    strm << std::endl << name.substr(0, name.size() - 1);
                }
            }

            if ( attrCount > 0 )
            {
                strm << std::endl << std::endl << "Attrs:" << std::endl;
                objList.resize( attrCount );
                H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_ATTR,
                    attrCount, &objList.front() );
                for ( int i = 0; i < attrCount; ++i )
                {
                    int strLen = H5Aget_name( objList[i], 0, NULL ) + 1;
                    name.resize( strLen );
                    H5Aget_name( objList[i], strLen, &(name[0]) );
                    strm << std::endl << name.substr(0, name.size() - 1);
                }
            }

            // just for formatting purposes
            if ( dtypCount > 0 )
            {
                strm << std::endl;
            }

            m_file = -1;
            ABCA_THROW( strm.str() );
        }

        H5Fclose( m_file );
        m_file = -1;
    }
}
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");
   
   }
Exemple #13
0
/*-------------------------------------------------------------------------
 * Function: copy_attr
 *
 * Purpose: copy attributes located in LOC_IN, which is obtained either from
 * loc_id = H5Gopen2( fid, name);
 * loc_id = H5Dopen2( fid, name);
 * loc_id = H5Topen2( fid, name);
 *
 * Return: 0, ok, -1 no
 *-------------------------------------------------------------------------
 */
int
copy_attr(hid_t loc_in, hid_t loc_out, named_dt_t **named_dt_head_p,
        trav_table_t *travt, pack_opt_t *options)
{
    int         ret_value = 0;
    hid_t       attr_id = -1;  /* attr ID */
    hid_t       attr_out = -1; /* attr ID */
    hid_t       space_id = -1; /* space ID */
    hid_t       ftype_id = -1; /* file type ID */
    hid_t       wtype_id = -1; /* read/write type ID */
    size_t      msize;         /* size of type */
    void       *buf = NULL;    /* data buffer */
    hsize_t     nelmts;        /* number of elements in dataset */
    int         rank;          /* rank of dataset */
    htri_t      is_named;      /* Whether the datatype is named */
    hsize_t     dims[H5S_MAX_RANK];/* dimensions of dataset */
    char        name[255];
    H5O_info_t  oinfo;         /* object info */
    int         j;
    unsigned    u;
    hbool_t     is_ref = 0;
    H5T_class_t type_class = -1;

    if (H5Oget_info(loc_in, &oinfo) < 0)
        HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Oget_info failed");

    /*-------------------------------------------------------------------------
     * copy all attributes
     *-------------------------------------------------------------------------
     */
    for (u = 0; u < (unsigned) oinfo.num_attrs; u++) {
        /* open attribute */
        if ((attr_id = H5Aopen_by_idx(loc_in, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t) u, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aopen_by_idx failed");

        /* get name */
        if (H5Aget_name(attr_id, (size_t) 255, name) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed");

        /* get the file datatype  */
        if ((ftype_id = H5Aget_type(attr_id)) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aget_type failed");

        /* Check if the datatype is committed */
        if ((is_named = H5Tcommitted(ftype_id)) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tcommitted failed");
        if (is_named && travt) {
            hid_t fidout = -1;

            /* Create out file id */
            if ((fidout = H5Iget_file_id(loc_out)) < 0)
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Iget_file_id failed");

            /* Copy named dt */
            if ((wtype_id = copy_named_datatype(ftype_id, fidout, named_dt_head_p, travt, options)) < 0) {
                H5Fclose(fidout);
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "copy_named_datatype failed");
            } /* end if */

            if (H5Fclose(fidout) < 0)
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Fclose failed");
        } /* end if */
        else {
            if (options->use_native == 1)
                wtype_id = H5Tget_native_type(ftype_id, H5T_DIR_DEFAULT);
            else
                wtype_id = H5Tcopy(ftype_id);
        } /* end else */

        /* get the dataspace handle  */
        if ((space_id = H5Aget_space(attr_id)) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aget_space failed");

        /* get dimensions  */
        if ((rank = H5Sget_simple_extent_dims(space_id, dims, NULL)) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Sget_simple_extent_dims failed");

        nelmts = 1;
        for (j = 0; j < rank; j++)
            nelmts *= dims[j];

        if ((msize = H5Tget_size(wtype_id)) == 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tget_size failed");

        /*-------------------------------------------------------------------------
         * object references are a special case. We cannot just copy the buffers,
         * but instead we recreate the reference.
         * This is done on a second sweep of the file that just copies the referenced
         * objects at copy_refs_attr()
         *-------------------------------------------------------------------------
         */
        type_class = H5Tget_class(wtype_id);
        is_ref = (type_class == H5T_REFERENCE);
        if (type_class == H5T_VLEN || type_class == H5T_ARRAY) {
            hid_t base_type = -1;

            base_type = H5Tget_super(ftype_id);
            is_ref = (is_ref || (H5Tget_class(base_type) == H5T_REFERENCE));
            if (H5Tclose(base_type) < 0)
                H5TOOLS_INFO(H5E_tools_min_id_g, "H5Tclose base_type failed");
        }

        if (type_class == H5T_COMPOUND) {
            int nmembers = H5Tget_nmembers(wtype_id);

            for (j = 0; j < nmembers; j++) {
                hid_t mtid = H5Tget_member_type(wtype_id, (unsigned)j);
                H5T_class_t mtclass = H5Tget_class(mtid);
                if (H5Tclose(mtid) < 0)
                    H5TOOLS_INFO(H5E_tools_min_id_g, "H5Tclose mtid failed");

                if (mtclass == H5T_REFERENCE) {
                    is_ref = 1;
                    break;
                }
            } /* for (j=0; i<nmembers; j++) */
        } /* if (type_class == H5T_COMPOUND) */

        if (!is_ref) {
            /*-------------------------------------------------------------------------
             * read to memory
             *-------------------------------------------------------------------------
             */

            buf = (void *)HDmalloc((size_t)(nelmts * msize));
            if (buf == NULL) {
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "HDmalloc failed");
            } /* end if */
            if (H5Aread(attr_id, wtype_id, buf) < 0)
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aread failed");

            /*-------------------------------------------------------------------------
             * copy
             *-------------------------------------------------------------------------
             */

            if ((attr_out = H5Acreate2(loc_out, name, wtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT)) < 0)
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Acreate2 failed on ,%s>", name);
            if (H5Awrite(attr_out, wtype_id, buf) < 0)
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Awrite failed");

            /*close*/
            if (H5Aclose(attr_out) < 0)
                HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aclose failed");

            /* Check if we have VL data and string in the attribute's  datatype that must
             * be reclaimed */
            if (TRUE == h5tools_detect_vlen(wtype_id))
                H5Dvlen_reclaim(wtype_id, space_id, H5P_DEFAULT, buf);
            HDfree(buf);
            buf = NULL;
        } /*H5T_REFERENCE*/

        if (options->verbose)
            printf(FORMAT_OBJ_ATTR, "attr", name);

        /*-------------------------------------------------------------------------
         * close
         *-------------------------------------------------------------------------
         */
        if (H5Sclose(space_id) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Sclose failed");
        space_id = -1;
        if (H5Tclose(wtype_id) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tclose failed");
        wtype_id = -1;
        if (H5Tclose(ftype_id) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tclose failed");
        ftype_id = -1;
        if (H5Aclose(attr_id) < 0)
            HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aclose failed");
        attr_id = -1;
    } /* for u */

done:
    H5E_BEGIN_TRY {
        if (buf) {
            /* Check if we have VL data and string in the attribute's  datatype that must
            * be reclaimed */
            if (TRUE == h5tools_detect_vlen(wtype_id))
                H5Dvlen_reclaim(wtype_id, space_id, H5P_DEFAULT, buf);

            /* Free buf */
            HDfree(buf);
        } /* end if */

        H5Aclose(attr_out);
        H5Sclose(space_id);
        H5Tclose(wtype_id);
        H5Tclose(ftype_id);
        H5Aclose(attr_id);
    } H5E_END_TRY;

    return ret_value;
} /* end copy_attr() */
Exemple #14
0
int H5Attribute_init(H5Attribute *a, hid_t aid) {
    int ret_value=0, i=0;
    hsize_t dims[H5S_MAX_RANK];
    hid_t sid=-1, tid=-1, ftid=-1;
    unsigned int npoints;

    if (NULL == a || aid < 0) {
        ret_value = -1;
        goto done;
    }

    if (a->space.rank > 0)
        goto done; /* already called */

    ftid = H5Aget_type(aid);
    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    if (ftid > 0) H5Tclose(ftid);

    a->name = (char *)malloc(MAX_NAME_LEN);
    H5Aget_name(aid, MAX_NAME_LEN, a->name);

    a->tclass = a->type.tclass = (H5Datatype_class_t)H5Tget_class(tid);
    a->type.size = H5Tget_size(tid);

    if  ( a->type.tclass == H5DATATYPE_INTEGER
         || a->type.tclass == H5DATATYPE_FLOAT
         || a->type.tclass == H5DATATYPE_BITFIELD
         || a->type.tclass == H5DATATYPE_REFERENCE
         || a->type.tclass == H5DATATYPE_ENUM ) {
        a->type.order = (H5Datatype_order_t)H5Tget_order(tid);
        if (a->type.tclass == H5DATATYPE_INTEGER) {
            a->type.sign = (H5Datatype_sign_t)H5Tget_sign(tid);
        }
    }
    else if (a->type.tclass == H5DATATYPE_COMPOUND) {
        a->type.nmembers = H5Tget_nmembers(tid );
        a->type.mnames = (char **)malloc(a->type.nmembers*sizeof(char*));
        a->type.mtypes = (int *)malloc(a->type.nmembers*sizeof(int));
        for (i=0; i<a->type.nmembers; i++) {
            a->type.mnames[i] = H5Tget_member_name(tid, i);
            a->type.mtypes[i] = H5Tget_class(H5Tget_member_type(tid, i));
        }
    }

    sid = H5Aget_space(aid);
    a->space.rank = H5Sget_simple_extent_ndims(sid);
    if ( H5Sget_simple_extent_dims(sid, dims, NULL) < 0 )
        THROW_H5LIBRARY_ERROR(a->error,ret_value, done);

    if (a->space.rank<=0)
    {
        a->space.rank = 1;
        dims[0] = 1;
    }

    npoints = 1;
    for (i=0; i<a->space.rank; i++)
    {
        a->space.dims[i] = dims[i];
        a->space.start[i] = 0;
        a->space.stride[i] = 1;
        a->space.count[i] = dims[i];
        npoints *= dims[i];
    }
    a->space.npoints = npoints;

done:
    if (sid > 0 ) H5Sclose(sid);
    if (tid > 0 ) H5Tclose(tid);

    return ret_value;
}
Exemple #15
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);
  }
}
// Read all optional attributes
char AH5_read_opt_attrs(hid_t loc_id, const char *path, AH5_opt_attrs_t *opt_attrs, char mandatory_attrs[][AH5_ATTR_LENGTH], size_t nb_mandatory_attrs)
{
    char success = AH5_FALSE, is_mandatory;
    H5O_info_t object_info;
    hsize_t i, j, k = 0;
    hid_t attr_id, type_id, memtype;
    float buf[2];
    hsize_t nb_present_mandatory_attrs = 0;
    char temp_name[AH5_ATTR_LENGTH];

    if (AH5_path_valid(loc_id, path))
    {
        // Check presence of all mandatory attributes
        for (i = 0; i < (hsize_t) nb_mandatory_attrs; i++)
            if (H5Aexists_by_name(loc_id, path, mandatory_attrs[i], H5P_DEFAULT) > 0)
                nb_present_mandatory_attrs++;
        H5Oget_info_by_name(loc_id, path, &object_info, H5P_DEFAULT);
        opt_attrs->nb_instances = object_info.num_attrs - nb_present_mandatory_attrs;
        if (opt_attrs->nb_instances > 0)
            opt_attrs->instances = (AH5_attr_instance_t *) malloc ((size_t) opt_attrs->nb_instances * sizeof(AH5_attr_instance_t));
        for (i = 0; i < object_info.num_attrs; i++)
        {
            is_mandatory = AH5_FALSE;
            attr_id = H5Aopen_by_idx(loc_id, path, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, i, H5P_DEFAULT, H5P_DEFAULT);
            H5Aget_name(attr_id, AH5_ATTR_LENGTH, temp_name);
            for (j = 0; j < nb_mandatory_attrs; j++)
                if (strcmp(temp_name, mandatory_attrs[j]) == 0)
                    is_mandatory = AH5_TRUE;
            if (!is_mandatory)
            {
                opt_attrs->instances[k].name = strdup(temp_name);
                type_id = H5Aget_type(attr_id);
                opt_attrs->instances[k].type = H5Tget_class(type_id);
                H5Tclose(type_id);
                switch (opt_attrs->instances[k].type)
                {
                case H5T_INTEGER:
                    opt_attrs->instances[k].value.i = 0;
                    if (H5Aread(attr_id, H5T_NATIVE_INT, &(opt_attrs->instances[k].value.i)) >= 0)
                        success = AH5_TRUE;
                    break;
                case H5T_FLOAT:
                    opt_attrs->instances[k].value.f = 0;
                    if (H5Aread(attr_id, H5T_NATIVE_FLOAT, &(opt_attrs->instances[k].value.f)) >= 0)
                        success = AH5_TRUE;
                    break;
                case H5T_COMPOUND:
                    opt_attrs->instances[k].value.c = AH5_set_complex(0, 0);
                    type_id = AH5_H5Tcreate_cpx_memtype();
                    if (H5Aread(attr_id, type_id, buf) >= 0)
                    {
                        opt_attrs->instances[k].value.c = AH5_set_complex(buf[0], buf[1]);
                        success = AH5_TRUE;
                    }
                    H5Tclose(type_id);
                    break;
                case H5T_STRING:
                    opt_attrs->instances[k].value.s = NULL;
                    memtype = H5Tcopy(H5T_C_S1);
                    H5Tset_size(memtype, AH5_ATTR_LENGTH);
                    opt_attrs->instances[k].value.s = (char *) malloc(AH5_ATTR_LENGTH * sizeof(char));
                    if (H5Aread(attr_id, memtype, opt_attrs->instances[k].value.s) >= 0)
                        success = AH5_TRUE;
                    H5Tclose(memtype);
                    break;
                default:
                    opt_attrs->instances[k].type = H5T_NO_CLASS;
                    printf("***** WARNING: Unsupported type of attribute \"%s@%s\". *****\n\n", path, opt_attrs->instances[k].name);
                    break;
                }
                k++;
            }
            H5Aclose(attr_id);
        }
    }
    if (!success)
    {
        opt_attrs->instances = NULL;
        opt_attrs->nb_instances = 0;
    }
    return success;
}
Exemple #17
0
hsize_t diff_attr(hid_t loc1_id,
                  hid_t loc2_id,
                  const char *path1,
                  const char *path2,
                  diff_opt_t *options)
{
    hid_t      attr1_id=-1;     /* attr ID */
    hid_t      attr2_id=-1;     /* attr ID */
    hid_t      space1_id=-1;    /* space ID */
    hid_t      space2_id=-1;    /* space ID */
    hid_t      ftype1_id=-1;    /* file data type ID */
    hid_t      ftype2_id=-1;    /* file data type ID */
    hid_t      mtype1_id=-1;    /* memory data type ID */
    hid_t      mtype2_id=-1;    /* memory data type ID */
    size_t     msize1;          /* memory size of memory type */
    size_t     msize2;          /* memory size of memory type */
    void       *buf1=NULL;      /* data buffer */
    void       *buf2=NULL;      /* data buffer */
    hsize_t    nelmts1;         /* number of elements in dataset */
    int        rank1;           /* rank of dataset */
    int        rank2;           /* rank of dataset */
    hsize_t    dims1[H5S_MAX_RANK];/* dimensions of dataset */
    hsize_t    dims2[H5S_MAX_RANK];/* dimensions of dataset */
    char       name1[512];
    char       name2[512];
    char       np1[512];
    char       np2[512];
    H5O_info_t oinfo1, oinfo2;     /* Object info */
    unsigned   u;                  /* Local index variable */
    hsize_t    nfound = 0;
    hsize_t    nfound_total = 0;
    int       j;

    if(H5Oget_info(loc1_id, &oinfo1) < 0)
        goto error;
    if(H5Oget_info(loc2_id, &oinfo2) < 0)
        goto error;

    if(oinfo1.num_attrs != oinfo2.num_attrs)
        return 1;

    for( u = 0; u < (unsigned)oinfo1.num_attrs; u++)
    {
        /* reset buffers for every attribute, we might goto out and call free */
        buf1 = NULL;
        buf2 = NULL;

        /* open attribute */
        if((attr1_id = H5Aopen_by_idx(loc1_id, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)u, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        /* get name */
        if(H5Aget_name(attr1_id, 255, name1) < 0)
            goto error;

        /* use the name on the first file to open the second file */
        H5E_BEGIN_TRY
        {
            if((attr2_id = H5Aopen(loc2_id, name1, H5P_DEFAULT)) < 0)
                goto error;
        } H5E_END_TRY;

        /* get name */
        if(H5Aget_name(attr2_id, 255, name2) < 0)
            goto error;

        /* get the datatypes  */
        if ((ftype1_id = H5Aget_type(attr1_id)) < 0)
            goto error;
        if ((ftype2_id = H5Aget_type(attr2_id)) < 0)
            goto error;
        if ((mtype1_id = h5tools_get_native_type(ftype1_id))<0)
            goto error;
        if ((mtype2_id = h5tools_get_native_type(ftype2_id))<0)
            goto error;
        if ((msize1 = H5Tget_size(mtype1_id))==0)
            goto error;
        if ((msize2 = H5Tget_size(mtype2_id))==0)
            goto error;

        /* get the dataspace   */
        if ((space1_id = H5Aget_space(attr1_id)) < 0)
            goto error;
        if ((space2_id = H5Aget_space(attr2_id)) < 0)
            goto error;

        /* get dimensions  */
        if ( (rank1 = H5Sget_simple_extent_dims(space1_id, dims1, NULL)) < 0 )
            goto error;
        if ( (rank2 = H5Sget_simple_extent_dims(space2_id, dims2, NULL)) < 0 )
            goto error;


       /*-------------------------------------------------------------------------
        * check for comparable TYPE and SPACE
        *-------------------------------------------------------------------------
        */

        if ( msize1 != msize2
            ||
            diff_can_type(ftype1_id,
            ftype2_id,
            rank1,
            rank2,
            dims1,
            dims2,
            NULL,
            NULL,
            name1,
            name2,
            options,
            0)!=1)
        {


            if (H5Tclose(ftype1_id)<0)
                goto error;
            if (H5Tclose(ftype2_id)<0)
                goto error;
            if (H5Sclose(space1_id)<0)
                goto error;
            if (H5Sclose(space2_id)<0)
                goto error;
            if (H5Aclose(attr1_id)<0)
                goto error;
            if (H5Aclose(attr2_id)<0)
                goto error;
            if (H5Tclose(mtype1_id)<0)
                goto error;
            if (H5Tclose(mtype2_id)<0)
                goto error;

            continue;


        }


        /*-------------------------------------------------------------------------
        * read
        *-------------------------------------------------------------------------
        */
        nelmts1=1;
        for (j=0; j<rank1; j++)
            nelmts1*=dims1[j];

        buf1=(void *) HDmalloc((unsigned)(nelmts1*msize1));
        buf2=(void *) HDmalloc((unsigned)(nelmts1*msize2));
        if ( buf1==NULL || buf2==NULL){
            parallel_print( "cannot read into memory\n" );
            goto error;
        }
        if (H5Aread(attr1_id,mtype1_id,buf1)<0)
            goto error;
        if (H5Aread(attr2_id,mtype2_id,buf2)<0)
            goto error;

        /* format output string */
        sprintf(np1,"%s of <%s>",name1,path1);
        sprintf(np2,"%s of <%s>",name2,path2);

        /*-------------------------------------------------------------------------
        * array compare
        *-------------------------------------------------------------------------
        */

        /* always print name */
        if (options->m_verbose)
        {
            do_print_objname ("attribute", np1, np2);
            nfound = diff_array(buf1,
                buf2,
                nelmts1,
                (hsize_t)0,
                rank1,
                dims1,
                options,
                np1,
                np2,
                mtype1_id,
                attr1_id,
                attr2_id);
            print_found(nfound);

        }
        /* check first if we have differences */
        else
        {
            if (options->m_quiet==0)
            {
                /* shut up temporarily */
                options->m_quiet=1;
                nfound = diff_array(buf1,
                    buf2,
                    nelmts1,
                    (hsize_t)0,
                    rank1,
                    dims1,
                    options,
                    np1,
                    np2,
                    mtype1_id,
                    attr1_id,
                    attr2_id);
                /* print again */
                options->m_quiet=0;
                if (nfound)
                {
                    do_print_objname ("attribute", np1, np2);
                    nfound = diff_array(buf1,
                        buf2,
                        nelmts1,
                        (hsize_t)0,
                        rank1,
                        dims1,
                        options,
                        np1,
                        np2,
                        mtype1_id,
                        attr1_id,
                        attr2_id);
                    print_found(nfound);
                } /*if*/
            } /*if*/
            /* in quiet mode, just count differences */
            else
            {
                nfound = diff_array(buf1,
                    buf2,
                    nelmts1,
                    (hsize_t)0,
                    rank1,
                    dims1,
                    options,
                    np1,
                    np2,
                    mtype1_id,
                    attr1_id,
                    attr2_id);
            } /*else quiet */
        } /*else verbose */


       /*-------------------------------------------------------------------------
        * close
        *-------------------------------------------------------------------------
        */

        if (H5Tclose(ftype1_id)<0)
            goto error;
        if (H5Tclose(ftype2_id)<0)
            goto error;
        if (H5Sclose(space1_id)<0)
            goto error;
        if (H5Sclose(space2_id)<0)
            goto error;
        if (H5Aclose(attr1_id)<0)
            goto error;
        if (H5Aclose(attr2_id)<0)
            goto error;
        if (H5Tclose(mtype1_id)<0)
            goto error;
        if (H5Tclose(mtype2_id)<0)
            goto error;

        if (buf1)
            HDfree(buf1);
        if (buf2)
            HDfree(buf2);

        nfound_total += nfound;
 } /* u */

 return nfound_total;

error:
 H5E_BEGIN_TRY {
     H5Tclose(ftype1_id);
     H5Tclose(ftype2_id);
     H5Tclose(mtype1_id);
     H5Tclose(mtype2_id);
     H5Sclose(space1_id);
     H5Sclose(space2_id);
     H5Aclose(attr1_id);
     H5Aclose(attr2_id);
     if (buf1)
         HDfree(buf1);
     if (buf2)
         HDfree(buf2);
 } H5E_END_TRY;

 options->err_stat=1;
 return nfound_total;
}
Exemple #18
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 */