19 #include <H5Fpublic.h>
20 #include <H5Ppublic.h>
21 #include <H5Gpublic.h>
22 #include <H5Spublic.h>
23 #include <H5Tpublic.h>
24 #include <H5Apublic.h>
25 #ifdef MOAB_HAVE_HDF5_PARALLEL
39 const char** elem_type_list,
48 hid_t enum_id, group_id;
52 if( elem_list_len > 255 )
61 if( !file_ptr )
return NULL;
64 flags = overwrite ? H5F_ACC_TRUNC : H5F_ACC_EXCL;
65 file_ptr->
hdf_handle = H5Fcreate( filename, flags, H5P_DEFAULT, H5P_DEFAULT );
68 mhdf_setFail( status,
"Failed to create file \"%s\"", filename );
88 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
103 enum_id = H5Tenum_create( H5T_NATIVE_UCHAR );
106 mhdf_setFail( status,
"Failed to store elem type list." );
111 for( i = 0; i < elem_list_len; ++i )
113 if( !elem_type_list[i] || !*elem_type_list[i] )
continue;
115 idx = (
unsigned char)i;
116 if( H5Tenum_insert( enum_id, elem_type_list[i], &idx ) < 0 )
118 mhdf_setFail( status,
"Failed to store elem type list." );
124 #if defined( H5Tcommit_vers ) && H5Tcommit_vers > 1
130 mhdf_setFail( status,
"Failed to store elem type list." );
143 unsigned long* max_id_out,
152 return H5Fget_obj_count( ( (
FileHandle*)file_handle )->hdf_handle, H5F_OBJ_ALL );
155 static herr_t
get_max_id( hid_t group_id,
const char* subgroup,
const char* datatable,
unsigned long* data )
158 hid_t elem_id, conn_id, attr_id, space_id;
163 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
164 elem_id = H5Gopen2( group_id, subgroup, H5P_DEFAULT );
166 elem_id = H5Gopen( group_id, subgroup );
168 if( elem_id < 0 )
return (herr_t)-1;
170 #if defined( H5Dopen_vers ) && H5Dopen_vers > 1
171 conn_id = H5Dopen2( elem_id, datatable, H5P_DEFAULT );
173 conn_id = H5Dopen( elem_id, datatable );
176 if( conn_id < 0 )
return (herr_t)-1;
178 space_id = H5Dget_space( conn_id );
185 rank = H5Sget_simple_extent_ndims( space_id );
186 if( rank <= 0 || rank > 2 )
189 H5Sclose( space_id );
193 rval = H5Sget_simple_extent_dims( space_id, dims, NULL );
194 H5Sclose( space_id );
203 if( attr_id < 0 )
return (herr_t)-1;
205 rval = H5Aread( attr_id, H5T_NATIVE_ULONG, &
id );
207 if( rval < 0 )
return rval;
210 if(
id > *data ) *data = id;
214 static herr_t
max_id_iter( hid_t group_id,
const char* name,
void* data )
225 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
232 mhdf_setFail( status,
"Internal error - invalid file." );
237 H5Gclose( group_id );
245 H5Gclose( group_id );
246 mhdf_setFail( status,
"Internal error -- invalid file." );
254 H5Gclose( group_id );
255 mhdf_setFail( status,
"Internal error -- invalid file." );
263 H5Gclose( group_id );
267 H5Gclose( group_id );
270 mhdf_setFail( status,
"Internal error -- invalid file." );
279 unsigned long* max_id_out,
287 int check_is_hdf5 = 1;
288 #ifdef MOAB_HAVE_HDF5_PARALLEL
298 #ifdef MOAB_HAVE_HDF5_PARALLEL
299 if( access_prop != H5P_DEFAULT )
301 err = H5Pget_fapl_mpio( access_prop, &comm, &info );
313 if( check_is_hdf5 && H5Fis_hdf5( filename ) <= 0 )
315 mhdf_setFail( status,
"%s: File is not HDF5", filename );
328 flags = writable ? H5F_ACC_RDWR : H5F_ACC_RDONLY;
329 file_ptr->
hdf_handle = H5Fopen( filename, flags, access_prop );
332 mhdf_setFail( status,
"Failed to open file \"%s\"", filename );
338 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
345 mhdf_setFail( status,
"Invalid file \"%s\"\n", filename );
350 H5Gclose( group_id );
361 if( max_id_out ) *max_id_out = file_ptr->
max_id;
369 unsigned int type_index,
379 if( type_index > 255 )
389 if( enum_id < 0 )
return;
391 rval = H5Tconvert( H5T_NATIVE_UINT, H5Tget_super( enum_id ), 1, &type_index, NULL, H5P_DEFAULT );
395 mhdf_setFail( status,
"Internal error converting to enum type." );
399 rval = H5Tenum_nameof( enum_id, &type_index,
buffer, buf_size );
402 mhdf_setFail( status,
"H5Tenum_nameof failed. Invalid type index?" );
420 result = H5Fget_obj_count( file_ptr->
hdf_handle, H5F_OBJ_ALL );
424 "Cannot close file with open handles: "
425 "%d file, %d data, %d group, %d type, %d attr\n",
426 H5Fget_obj_count( file_ptr->
hdf_handle, H5F_OBJ_FILE ) - 1,
427 H5Fget_obj_count( file_ptr->
hdf_handle, H5F_OBJ_DATASET ),
428 H5Fget_obj_count( file_ptr->
hdf_handle, H5F_OBJ_GROUP ),
429 H5Fget_obj_count( file_ptr->
hdf_handle, H5F_OBJ_DATATYPE ),
430 H5Fget_obj_count( file_ptr->
hdf_handle, H5F_OBJ_ATTR ) );
460 mhdf_setFail( status,
"H5FClose failed. Invalid handle?" );
478 switch( H5Iget_type( handle ) )
481 rval = H5Gclose( handle );
484 rval = H5Tclose( handle );
487 rval = H5Sclose( handle );
490 rval = H5Dclose( handle );
498 mhdf_setFail( status,
"H5Xclose failed. Invalid handle?\n" );
510 hid_t group_id, tag_id, enum_id;
527 mhdf_setFail( status,
"Invalid character string in internal file path: \"%s\"\n", name );
531 #if defined( H5Gcreate_vers ) && H5Gcreate_vers > 1
532 group_id = H5Gcreate2( file_ptr->
hdf_handle, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
534 group_id = H5Gcreate( file_ptr->
hdf_handle, path, 3 );
538 mhdf_setFail( status,
"Creation of \"%s\" group failed.\n", path );
544 #if defined( H5Gcreate_vers ) && H5Gcreate_vers > 1
545 tag_id = H5Gcreate2( group_id,
DENSE_TAG_SUBGROUP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
551 H5Gclose( group_id );
552 mhdf_setFail( status,
"Creation of tag subgroup failed.\n" );
560 H5Gclose( group_id );
564 rval = H5Tconvert( H5T_NATIVE_UINT, H5Tget_super( enum_id ), 1, &elem_type, NULL, H5P_DEFAULT );
567 H5Gclose( group_id );
569 mhdf_setFail( status,
"Internal error converting to enum type." );
577 H5Gclose( group_id );
581 H5Gclose( group_id );
598 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
605 mhdf_setFail( status,
"Invalid file -- element group does not exist." );
609 rval = H5Gget_num_objs( group_id, &count );
612 H5Gclose( group_id );
613 mhdf_setFail( status,
"Internal error calling H5Gget_num_objs." );
618 for( i = 0; i < count; ++i )
620 rlen += H5Gget_objname_by_idx( group_id, i, NULL, 0 ) + 1;
623 length = count *
sizeof(
char* ) + rlen;
627 H5Gclose( group_id );
630 current = (
char*)(
buffer + count );
633 for( i = 0; i < count; ++i )
636 rlen = H5Gget_objname_by_idx( group_id, i, current, remaining ) + 1;
639 H5Gclose( group_id );
641 mhdf_setFail( status,
"Internal error calling H5Gget_objname_by_idx." );
650 H5Gclose( group_id );
656 const char* elem_handle,
662 hid_t elem_id, type_id, attr_id;
667 if( NULL ==
buffer || buf_len < 2 )
678 if( elem_id < 0 )
return;
684 mhdf_setFail( status,
"Missing element type attribute. Invalid file." );
688 type_id = H5Aget_type( attr_id );
689 assert( type_id > 0 );
691 rval = H5Aread( attr_id, type_id, bytes );
696 mhdf_setFail( status,
"Failed to read element type attribute. Invalid file." );
700 rval = H5Tenum_nameof( type_id, bytes,
buffer, buf_len );
704 mhdf_setFail( status,
"Invalid datatype for element type attribute. Invalid file." );
715 #if defined( H5Gcreate_vers ) && H5Gcreate_vers > 1
716 hid_t handle = H5Gcreate2( file, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
722 hid_t handle = H5Gcreate( file, path, sz );
726 mhdf_setFail( status,
"Failed to create \"%s\" group.", path );
738 static const char rval[] =
"nodes";
744 static const char rval[] =
"sets";
759 if( elem_id < 0 )
return -1;
771 hid_t data_id, type_id, space_id;
772 hsize_t
dim = (hsize_t)num_strings;
779 type_id = H5Tcopy( H5T_C_S1 );
780 if( type_id < 0 || H5Tset_size( type_id, H5T_VARIABLE ) < 0 )
782 if( type_id >= 0 ) H5Tclose( type_id );
783 mhdf_setFail( status,
"Could not create variable length string type." );
787 space_id = H5Screate_simple( 1, &
dim, NULL );
795 #if defined( H5Dcreate_vers ) && H5Dcreate_vers > 1
797 H5Dcreate2( file_ptr->
hdf_handle,
HISTORY_PATH, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
801 H5Sclose( space_id );
809 rval = H5Dwrite( data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, strings );
815 mhdf_setFail( status,
"Failed to write history data." );
826 hid_t data_id, type_id, space_id, group_id;
836 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
843 mhdf_setFail( status,
"Could not open root group. Invalid file." );
850 H5Gclose( group_id );
856 #if defined( H5Dopen_vers ) && H5Dopen_vers > 1
857 data_id = H5Dopen2( group_id,
HISTORY_NAME, H5P_DEFAULT );
861 H5Gclose( group_id );
868 space_id = H5Dget_space( data_id );
872 mhdf_setFail( status,
"Internal error calling H5Dget_space." );
876 if( 1 != H5Sget_simple_extent_ndims( space_id ) || 1 != H5Sget_simple_extent_dims( space_id, &
dim, NULL ) )
882 H5Sclose( space_id );
899 type_id = H5Tcopy( H5T_C_S1 );
900 if( type_id < 0 || H5Tset_size( type_id, H5T_VARIABLE ) < 0 )
903 if( type_id >= 0 ) H5Tclose( type_id );
904 mhdf_setFail( status,
"Could not create variable length string type." );
909 rval = H5Dread( data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, array );