... | @@ -232,23 +232,16 @@ include 'H5hut.f90' |
... | @@ -232,23 +232,16 @@ include 'H5hut.f90' |
|
|
|
|
|
=== Read file attributes
|
|
=== Read file attributes
|
|
|
|
|
|
Read and print the file attributes written by above example.
|
|
Read file attributes `FileAttrSting` and `FileAttrInt32` from file `example_file_attribs.h5` written by above example.
|
|
|
|
|
|
*C implementation*
|
|
*C implementation*
|
|
----
|
|
----
|
|
#include "H5hut.h"
|
|
#include "H5hut.h"
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
|
|
|
#define FNAME "example_file_attribs.h5"
|
|
#define FNAME "example_file_attribs.h5"
|
|
#define VERBOSITY H5_VERBOSE_ERROR
|
|
|
|
#define DEBUG_MSK 0
|
|
|
|
|
|
|
|
#define ATTR_STRING "FileAttrString"
|
|
#define ATTR_STRING "FileAttrString"
|
|
#define ATTR_INT32 "FileAttrInt32"
|
|
#define ATTR_INT32 "FileAttrInt32"
|
|
#define ATTR_INT64 "FileAttrInt64"
|
|
|
|
#define ATTR_FLOAT32 "FileAttrFloat32"
|
|
|
|
#define ATTR_FLOAT64 "FileAttrFloat64"
|
|
|
|
|
|
|
|
int
|
|
int
|
|
main (
|
|
main (
|
... | @@ -257,60 +250,22 @@ main ( |
... | @@ -257,60 +250,22 @@ main ( |
|
) {
|
|
) {
|
|
MPI_Init (&argc, &argv);
|
|
MPI_Init (&argc, &argv);
|
|
H5AbortOnError ();
|
|
H5AbortOnError ();
|
|
H5SetVerbosityLevel (VERBOSITY);
|
|
|
|
H5SetDebugMask (DEBUG_MSK);
|
|
|
|
|
|
|
|
// if file properties is set to default, MPI_COMM_WORLD will be used
|
|
|
|
h5_file_t f = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
|
|
h5_file_t f = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
|
|
h5_size_t len;
|
|
h5_size_t len;
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_STRING, NULL, &len);
|
|
H5GetFileAttribInfoByName (f, ATTR_STRING, NULL, &len);
|
|
char* attr_string = (char*)malloc (len+1);
|
|
char* attr_string = (char*)malloc (len+1);
|
|
H5ReadFileAttribString (f, ATTR_STRING, attr_string);
|
|
H5ReadFileAttribString (f, ATTR_STRING, attr_string);
|
|
printf ("%s: %s\n", ATTR_STRING, attr_string);
|
|
/* do something with this string attribute */
|
|
free (attr_string);
|
|
free (attr_string);
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_INT32, NULL, &len);
|
|
H5GetFileAttribInfoByName (f, ATTR_INT32, NULL, &len);
|
|
int32_t* attr_int32 = (int32_t*)malloc (sizeof(*attr_int32)*len);
|
|
int32_t* attr_int32 = (int32_t*)malloc (sizeof(*attr_int32)*len);
|
|
H5ReadFileAttribInt32 (f, ATTR_INT32, attr_int32);
|
|
H5ReadFileAttribInt32 (f, ATTR_INT32, attr_int32);
|
|
printf ("%s:", ATTR_INT32);
|
|
/* do something with this 32bit signed integer array */
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
printf (" %d", attr_int32[i]);
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
free (attr_int32);
|
|
free (attr_int32);
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_INT64, NULL, &len);
|
|
|
|
int64_t* attr_int64 = (int64_t*)malloc (sizeof(*attr_int64)*len);
|
|
|
|
H5ReadFileAttribInt64 (f, ATTR_INT64, attr_int64);
|
|
|
|
printf ("%s:", ATTR_INT64);
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
printf (" %lld", (long long int)attr_int64[i]);
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
free (attr_int64);
|
|
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_FLOAT32, NULL, &len);
|
|
|
|
h5_float32_t* attr_float32 = (h5_float32_t*)malloc (sizeof(*attr_float32)*len);
|
|
|
|
H5ReadFileAttribFloat32 (f, ATTR_FLOAT32, attr_float32);
|
|
|
|
printf ("%s:", ATTR_FLOAT32);
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
printf (" %f", attr_float32[i]);
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
free (attr_float32);
|
|
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_FLOAT64, NULL, &len);
|
|
|
|
h5_float64_t* attr_float64 = (h5_float64_t*)malloc (sizeof(*attr_float64)*len);
|
|
|
|
H5ReadFileAttribFloat64 (f, ATTR_FLOAT64, attr_float64);
|
|
|
|
printf ("%s:", ATTR_FLOAT64);
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
printf (" %f", attr_float64[i]);
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
free (attr_float64);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
H5CloseFile (f);
|
|
H5CloseFile (f);
|
|
MPI_Finalize ();
|
|
MPI_Finalize ();
|
|
return 0;
|
|
return 0;
|
... | @@ -329,99 +284,37 @@ include 'H5hut.f90' |
... | @@ -329,99 +284,37 @@ include 'H5hut.f90' |
|
! the file name we want to read
|
|
! the file name we want to read
|
|
character (len=*), parameter :: FNAME = "example_file_attribs.h5"
|
|
character (len=*), parameter :: FNAME = "example_file_attribs.h5"
|
|
|
|
|
|
! verbosity level: set it to a power of 2 minus one or zero
|
|
|
|
integer*8, parameter :: verbosity_level = 1
|
|
|
|
|
|
|
|
! we know the attribute names!
|
|
|
|
character (len=*), parameter :: ATTR_STRING = "StepAttrString"
|
|
character (len=*), parameter :: ATTR_STRING = "StepAttrString"
|
|
character (len=*), parameter :: ATTR_I4 = "StepAttrInt32"
|
|
character (len=*), parameter :: ATTR_I4 = "StepAttrInt32"
|
|
character (len=*), parameter :: ATTR_I8 = "StepAttrInt64"
|
|
|
|
character (len=*), parameter :: ATTR_R4 = "StepAttrFloat32"
|
|
|
|
character (len=*), parameter :: ATTR_R8 = "StepAttrFloat64"
|
|
|
|
|
|
|
|
! for formated output
|
|
|
|
character (len=128) :: fmt
|
|
character (len=128) :: fmt
|
|
|
|
|
|
! attribute values. Note: allocatable strings aren't supported in Fortran90
|
|
! attribute values. Note: allocatable strings aren't supported in Fortran90
|
|
character (len=256) :: string_value
|
|
character (len=256) :: string_value
|
|
integer*4, allocatable :: i4_value (:)
|
|
integer*4, allocatable :: i4_value (:)
|
|
integer*8, allocatable :: i8_value (:)
|
|
|
|
real*4, allocatable :: r4_value (:)
|
|
|
|
real*8, allocatable :: r8_value (:)
|
|
|
|
|
|
|
|
! used for mpi error return
|
|
|
|
integer :: ierr
|
|
integer :: ierr
|
|
|
|
|
|
! H5hut file id
|
|
|
|
integer*8 :: file_id
|
|
integer*8 :: file_id
|
|
|
|
|
|
! H5hut API status return
|
|
|
|
integer*8 status
|
|
integer*8 status
|
|
|
|
|
|
! type of attribute
|
|
|
|
integer*8 type
|
|
integer*8 type
|
|
|
|
|
|
! len of attribute
|
|
|
|
integer*8 len
|
|
integer*8 len
|
|
|
|
|
|
! loop index
|
|
|
|
integer*8 i
|
|
integer*8 i
|
|
|
|
|
|
call mpi_init (ierr)
|
|
call mpi_init (ierr)
|
|
|
|
|
|
! abort program on any H5hut error
|
|
|
|
call h5_abort_on_error ()
|
|
call h5_abort_on_error ()
|
|
|
|
|
|
call h5_set_verbosity_level (verbosity_level)
|
|
|
|
|
|
|
|
! MPI_COMM_WORLD is used, if file is opened with default properties
|
|
|
|
file_id = h5_openfile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT)
|
|
file_id = h5_openfile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT)
|
|
|
|
|
|
! open step 1
|
|
|
|
status = h5_setstep (file_id, int8(1))
|
|
status = h5_setstep (file_id, int8(1))
|
|
|
|
|
|
! read and output string attribute
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_STRING, type, len)
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_STRING, type, len)
|
|
status = h5_readstepattrib_string (file_id, ATTR_STRING, string_value)
|
|
status = h5_readstepattrib_string (file_id, ATTR_STRING, string_value)
|
|
write (fmt, "(a, i0, a)") '(a', len, ')'
|
|
! more code here
|
|
write (*, "(a, ' = ')", advance='no') ATTR_STRING
|
|
|
|
write (*, fmt) string_value
|
|
|
|
|
|
|
|
! read and output 32bit integer attribute
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_I4, type, len)
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_I4, type, len)
|
|
allocate (i4_value(len))
|
|
allocate (i4_value(len))
|
|
status = h5_readstepattrib_i4 (file_id, ATTR_I4, i4_value)
|
|
status = h5_readstepattrib_i4 (file_id, ATTR_I4, i4_value)
|
|
write (fmt, "(a, i0, a)") '(', len, 'i4)'
|
|
! more code here
|
|
write (*, "(a, ' =')", advance='no') ATTR_I4
|
|
|
|
write (*, fmt) (i4_value(i), i = 1, len)
|
|
|
|
|
|
|
|
! read and output 64bit integer attribute
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_I8, type, len)
|
|
|
|
allocate (i8_value(len))
|
|
|
|
status = h5_readstepattrib_i8 (file_id, ATTR_I8, i8_value)
|
|
|
|
write (fmt, "(a, i0, a)") '(', len, 'i4)'
|
|
|
|
write (*, "(a, ' =')", advance='no') ATTR_I8
|
|
|
|
write (*, fmt) (i8_value(i), i = 1, len)
|
|
|
|
|
|
|
|
! read and output 32bit floating point attribute
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_R4, type, len)
|
|
|
|
allocate (r4_value(len))
|
|
|
|
status = h5_readstepattrib_r4 (file_id, ATTR_R4, r4_value)
|
|
|
|
write (fmt, "(a, i0, a)") '(', len, 'f10.5)'
|
|
|
|
write (*, "(a, ' =')", advance='no') ATTR_R4
|
|
|
|
write (*, fmt) (r4_value(i), i = 1, len)
|
|
|
|
|
|
|
|
! read and output 64bit floating point attribute
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_R8, type, len)
|
|
|
|
allocate (r8_value(len))
|
|
|
|
status = h5_readstepattrib_r8 (file_id, ATTR_R8, r8_value)
|
|
|
|
write (fmt, "(a, i0, a)") '(', len, 'f10.5)'
|
|
|
|
write (*, "(a, ' =')", advance='no') ATTR_R8
|
|
|
|
write (*, fmt) (r8_value(i), i = 1, len)
|
|
|
|
|
|
|
|
! cleanup
|
|
|
|
status = h5_closefile (file_id)
|
|
status = h5_closefile (file_id)
|
|
call mpi_finalize(ierr)
|
|
call mpi_finalize(ierr)
|
|
|
|
|
|
end program read_step_attribs
|
|
end program read_step_attribs
|
|
---- |
|
---- |
|
\ No newline at end of file |
|
|