Beispiel #1
0
//-*****************************************************************************
bool EquivalentDatatypes( hid_t iA, hid_t iB )
{
    if ( iA >= 0 && iB >= 0 )
    {
        if ( H5Tequal( iA, iB ) > 0 )
        {
            return true;
        }

        // If they're not equal, but they are both arrayed and
        // both have the same super type
        // and dimensions, they're equivalent
        if ( H5Tget_class( iA ) == H5T_ARRAY &&
             H5Tget_class( iB ) == H5T_ARRAY )
        {
            hid_t superA = H5Tget_super( iA );
            hid_t superB = H5Tget_super( iB );
            if ( superA >= 0 && superB >= 0 &&
                 H5Tequal( superA, superB ) > 0 )
            {
                Dimensions aDims;
                getDatatypeArrayDims( iA, aDims );
                Dimensions bDims;
                getDatatypeArrayDims( iB, bDims );
                if ( aDims == bDims )
                {
                    return true;
                }
            }
        }
    }
    return false;
}
Beispiel #2
0
/* This may be ultimately confused with nested types with 2 components
   called 'r' and 'i' and being floats, but in that case, the user
   most probably wanted to keep a complex type, so getting a complex
   instead of a nested type should not be a big issue (I hope!) :-/
   F. Alted 2005-05-23 */
int is_complex(hid_t type_id) {
  hid_t class_id, base_type_id;
  hid_t class1, class2;
  char *colname1, *colname2;
  int result = 0;
  hsize_t nfields;

  class_id = H5Tget_class(type_id);
  if (class_id == H5T_COMPOUND) {
    nfields = H5Tget_nmembers(type_id);
    if (nfields == 2) {
      colname1 = H5Tget_member_name(type_id, 0);
      colname2 = H5Tget_member_name(type_id, 1);
      if ((strcmp(colname1, "r") == 0) && (strcmp(colname2, "i") == 0)) {
        class1 = H5Tget_member_class(type_id, 0);
        class2 = H5Tget_member_class(type_id, 1);
        if (class1 == H5T_FLOAT && class2 == H5T_FLOAT)
          result = 1;
      }
      free(colname1);
      free(colname2);
    }
  }
  /* Is an Array of Complex? */
  else if (class_id == H5T_ARRAY) {
    /* Get the array base component */
    base_type_id = H5Tget_super(type_id);
    /* Call is_complex again */
    result = is_complex(base_type_id);
    H5Tclose(base_type_id);
  }
  return result;
}
Beispiel #3
0
void
mhdf_getElemName( mhdf_FileHandle file_handle,
                  unsigned int type_index,
                  char* buffer,
                  size_t buf_size,
                  mhdf_Status* status )
{
  FileHandle* file_ptr;
  herr_t rval;
  hid_t enum_id;
  API_BEGIN;
  
  if (type_index > 255)
  {
    mhdf_setFail( status, "Type index out of bounds." );
    return;
  }
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;

  enum_id = get_elem_type_enum( file_ptr, status );
  if (enum_id < 0)
    return;
  
  rval = H5Tconvert( H5T_NATIVE_UINT, H5Tget_super(enum_id), 1, &type_index, NULL, H5P_DEFAULT );
  if (rval < 0)
  {
    H5Tclose( enum_id );
    mhdf_setFail( status, "Internal error converting to enum type." );
    return;
  }
  
  rval = H5Tenum_nameof( enum_id, &type_index, buffer, buf_size );
  H5Tclose( enum_id );
  if (rval < 0)
    mhdf_setFail( status, "H5Tenum_nameof failed.  Invalid type index?" );
  else
    mhdf_setOkay( status );
    
  API_END;
}
Beispiel #4
0
/* Return the byteorder of a complex datatype.
   It is obtained from the real part, which is the first member. */
static H5T_order_t get_complex_order(hid_t type_id) {
  hid_t class_id, base_type_id;
  hid_t real_type = 0;
  H5T_order_t result = 0;

  class_id = H5Tget_class(type_id);
  if (class_id == H5T_COMPOUND) {
    real_type = H5Tget_member_type(type_id, 0);
  }
  else if (class_id == H5T_ARRAY) {
    /* Get the array base component */
    base_type_id = H5Tget_super(type_id);
    /* Get the type of real component. */
    real_type = H5Tget_member_type(base_type_id, 0);
    H5Tclose(base_type_id);
  }
  if ((class_id == H5T_COMPOUND) || (class_id == H5T_ARRAY)) {
    result = H5Tget_order(real_type);
    H5Tclose(real_type);
  }
  return result;
}
 static CollectionType* genType(hid_t datatype_id)
 {
     bool found = false;
     H5T_class_t h5_class = H5Tget_class(datatype_id);
     if(h5_class == H5T_ARRAY)
     {
         hid_t base = H5Tget_super(datatype_id);
         if(H5Tequal(base, H5T_NATIVE_HSIZE) == 1)
         {
             if(H5Tget_array_ndims(datatype_id) == 1)
             {
                 hsize_t adims_out[1];
                 H5Tget_array_dims(datatype_id, adims_out);
                 if(adims_out[0] == 3)
                     found = true;
             }
         }
         H5Tclose(base);
     }
     if(found)
         return new ColTypeDimArray;
     else
         return NULL;
 }
Beispiel #6
0
void
mhdf_addElement( mhdf_FileHandle file_handle, 
                 const char* name, 
                 unsigned int elem_type,
                 mhdf_Status* status )
{
  FileHandle* file_ptr = (FileHandle*)file_handle;
  hid_t group_id, tag_id, enum_id;
  char* path, *ptr;
  size_t name_len;
  herr_t rval;
  API_BEGIN;
  
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
  
  name_len = mhdf_name_to_path( name, NULL, 0 );
  name_len += strlen(ELEMENT_GROUP) + 1;
  path = (char*)mhdf_malloc( name_len, status );
  if (!path)
    return;
  
  strcpy( path, ELEMENT_GROUP );
  ptr = path + strlen(ELEMENT_GROUP);
  if (!mhdf_path_to_name( name, ptr ))
  {
    mhdf_setFail( status, "Invalid character string in internal file path: \"%s\"\n",
      name );
    return;
  }

#if defined(H5Gcreate_vers) && H5Gcreate_vers > 1
  group_id = H5Gcreate2( file_ptr->hdf_handle, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
#else
  group_id = H5Gcreate( file_ptr->hdf_handle, path, 3 );
#endif
  if (group_id < 0)
  {
    mhdf_setFail( status, "Creation of \"%s\" group failed.\n", path );
    free( path );
    return;
  }
  free( path );
  
#if defined(H5Gcreate_vers) && H5Gcreate_vers > 1
  tag_id = H5Gcreate2( group_id, DENSE_TAG_SUBGROUP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
#else
  tag_id = H5Gcreate( group_id, DENSE_TAG_SUBGROUP, 0 );
#endif
  if (tag_id < 0)
  {
    H5Gclose( group_id );
    mhdf_setFail( status, "Creation of tag subgroup failed.\n" );
    return;
  }
  H5Gclose( tag_id );
  
  enum_id = get_elem_type_enum( file_ptr, status );
  if (enum_id < 0)
  {
    H5Gclose( group_id );
    return;
  }
  
  rval = H5Tconvert( H5T_NATIVE_UINT, H5Tget_super(enum_id), 1, &elem_type, NULL, H5P_DEFAULT );
  if (rval < 0)
  {
    H5Gclose( group_id );
    H5Tclose( enum_id );
    mhdf_setFail( status, "Internal error converting to enum type." );
    return;
  }
  
  rval = mhdf_create_scalar_attrib( group_id, ELEM_TYPE_ATTRIB, enum_id,
                                 &elem_type, status );
  H5Tclose( enum_id );
  if (rval < 0)
  {
    H5Gclose( group_id );
    return;
  }
  
  H5Gclose( group_id );
  mhdf_setOkay( status );
  API_END;
}
Beispiel #7
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() */
Beispiel #8
0
herr_t H5VLARRAYget_info( hid_t   dataset_id,
                          hid_t   type_id,
                          hsize_t *nrecords,
                          char    *base_byteorder )
{

    hid_t       space_id;
    H5T_class_t base_class_id;
    H5T_class_t atom_class_id;
    hid_t       atom_type_id;
    hid_t       base_type_id;


    /* Get the dataspace handle */
    if ( (space_id = H5Dget_space( dataset_id )) < 0 )
        goto out;

    /* Get number of records (it should be rank-1) */
    if ( H5Sget_simple_extent_dims( space_id, nrecords, NULL) < 0 )
        goto out;

    /* Terminate access to the dataspace */
    if ( H5Sclose( space_id ) < 0 )
        goto out;

    /* Get the type of the atomic component */
    atom_type_id = H5Tget_super( type_id );

    /* Get the class of the atomic component. */
    atom_class_id = H5Tget_class( atom_type_id );

    /* Check whether the atom is an array class object or not */
    if ( atom_class_id == H5T_ARRAY) {
        /* Get the array base component */
        base_type_id = H5Tget_super( atom_type_id );
        /* Get the class of base component */
        base_class_id = H5Tget_class( base_type_id );
        /* Release the datatypes */
        if ( H5Tclose(atom_type_id ) )
            goto out;
    }
    else {
        base_class_id = atom_class_id;
        base_type_id = atom_type_id;
    }

    /* Get the byteorder */
    /* Only integer, float and time classes can be byteordered */
    if ((base_class_id == H5T_INTEGER) || (base_class_id == H5T_FLOAT)
            || (base_class_id == H5T_BITFIELD) || (base_class_id == H5T_COMPOUND)
            || (base_class_id == H5T_TIME)) {
        get_order(base_type_id, base_byteorder);
    }
    else {
        strcpy(base_byteorder, "irrelevant");
    }

    /* Release the datatypes */
    if ( H5Tclose(base_type_id ) )
        goto out;

    return 0;

out:
    return -1;

}