|
|
= File handling, steps and attributes
|
|
|
= File handling
|
|
|
:TOC:
|
|
|
:numbered:
|
|
|
|
... | ... | @@ -131,186 +131,3 @@ program openclose |
|
|
end program openclose
|
|
|
----
|
|
|
|
|
|
== File attributes
|
|
|
|
|
|
=== Write file attributes
|
|
|
|
|
|
*C implementation*
|
|
|
----
|
|
|
#include "H5hut.h"
|
|
|
|
|
|
#define FNAME "example_file_attribs.h5"
|
|
|
|
|
|
#define ATTR_STRING "FileAttrString"
|
|
|
#define ATTR_INT32 "FileAttrInt32"
|
|
|
#define ATTR_INT64 "FileAttrInt64"
|
|
|
#define ATTR_FLOAT32 "FileAttrFloat32"
|
|
|
#define ATTR_FLOAT64 "FileAttrFloat64"
|
|
|
|
|
|
#define asize(array) (sizeof(array)/sizeof(array[0]))
|
|
|
|
|
|
int
|
|
|
main (
|
|
|
int argc,
|
|
|
char** argv
|
|
|
) {
|
|
|
char* string_value = "This is a string attribute bound to the file.";
|
|
|
int32_t int32_value[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144};
|
|
|
int64_t int64_value[] = {42, 43, 44, 45};
|
|
|
h5_float32_t float32_value[] = {2.71828};
|
|
|
h5_float64_t float64_value[] = {3.14159265358979323846264338327950288419716939937510};
|
|
|
|
|
|
MPI_Init (&argc, &argv);
|
|
|
|
|
|
H5AbortOnError ();
|
|
|
|
|
|
// if file properties is set to default, MPI_COMM_WORLD will be used
|
|
|
h5_file_t f = H5OpenFile (FNAME, H5_O_WRONLY, H5_PROP_DEFAULT);
|
|
|
H5WriteFileAttribString (f, ATTR_STRING, string_value);
|
|
|
H5WriteFileAttribInt32 (f, ATTR_INT32, int32_value, asize(int32_value));
|
|
|
H5WriteFileAttribInt64 (f, ATTR_INT64, int64_value, asize(int64_value));
|
|
|
H5WriteFileAttribFloat32 (f, ATTR_FLOAT32, float32_value, asize(float32_value));
|
|
|
H5WriteFileAttribFloat64 (f, ATTR_FLOAT64, float64_value, asize(float32_value));
|
|
|
|
|
|
H5CloseFile (f);
|
|
|
MPI_Finalize ();
|
|
|
return 0;
|
|
|
}
|
|
|
----
|
|
|
|
|
|
*Fortran90 implementation*
|
|
|
----
|
|
|
include 'H5hut.f90'
|
|
|
|
|
|
program write_file_attribs
|
|
|
use H5hut
|
|
|
implicit none
|
|
|
include 'mpif.h'
|
|
|
|
|
|
integer*8, parameter :: verbosity_level = 1
|
|
|
|
|
|
character (len=*), parameter :: FNAME = "example_file_attribs.h5"
|
|
|
character (len=*), parameter :: ATTR_STRING = "FileAttrString"
|
|
|
character (len=*), parameter :: ATTR_I4 = "FileAttrInt32"
|
|
|
character (len=*), parameter :: ATTR_I8 = "FileAttrInt64"
|
|
|
character (len=*), parameter :: ATTR_R4 = "FileAttrFloat32"
|
|
|
character (len=*), parameter :: ATTR_R8 = "FileAttrFloat64"
|
|
|
|
|
|
character (len=*),parameter :: string_value = "This is a string attribute bound to the file."
|
|
|
|
|
|
integer*4, parameter, dimension(*) :: i4_value = (/0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144/)
|
|
|
integer*8, parameter, dimension(*) :: i8_value = (/42, 43, 44, 45/)
|
|
|
real*4, parameter, dimension(*) :: r4_value = (/2.71828/)
|
|
|
real*8, parameter, dimension(*) :: r8_value = (/3.141592653589793238462643383279502884197169/)
|
|
|
|
|
|
integer :: ierr
|
|
|
integer*8 :: file_id, status
|
|
|
|
|
|
call mpi_init(ierr)
|
|
|
|
|
|
! abort program on any H5hut 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_WRONLY, H5_PROP_DEFAULT)
|
|
|
|
|
|
! write attributes
|
|
|
status = h5_writefileattrib_string (file_id, ATTR_STRING, string_value)
|
|
|
status = h5_writefileattrib_i4 (file_id, ATTR_I4, i4_value, int8(size(i4_value, 1)))
|
|
|
status = h5_writefileattrib_i8 (file_id, ATTR_I8, i8_value, int8(size(i8_value, 1)))
|
|
|
status = h5_writefileattrib_r4 (file_id, ATTR_R4, r4_value, int8(size(r4_value, 1)))
|
|
|
status = h5_writefileattrib_r8 (file_id, ATTR_R8, r8_value, int8(size(r8_value, 1)))
|
|
|
|
|
|
! cleanup
|
|
|
status = h5_closefile (file_id)
|
|
|
call mpi_finalize(ierr)
|
|
|
|
|
|
end program write_file_attribs
|
|
|
----
|
|
|
|
|
|
=== Read file attributes
|
|
|
|
|
|
Read file attributes `FileAttrSting` and `FileAttrInt32` from file `example_file_attribs.h5` written by above example.
|
|
|
|
|
|
*C implementation*
|
|
|
----
|
|
|
#include "H5hut.h"
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#define FNAME "example_file_attribs.h5"
|
|
|
#define ATTR_STRING "FileAttrString"
|
|
|
#define ATTR_INT32 "FileAttrInt32"
|
|
|
|
|
|
int
|
|
|
main (
|
|
|
int argc,
|
|
|
char** argv
|
|
|
) {
|
|
|
MPI_Init (&argc, &argv);
|
|
|
H5AbortOnError ();
|
|
|
|
|
|
h5_file_t f = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
|
|
|
h5_size_t len;
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_STRING, NULL, &len);
|
|
|
char* attr_string = (char*)malloc (len+1);
|
|
|
H5ReadFileAttribString (f, ATTR_STRING, attr_string);
|
|
|
/* do something with this string attribute */
|
|
|
free (attr_string);
|
|
|
|
|
|
H5GetFileAttribInfoByName (f, ATTR_INT32, NULL, &len);
|
|
|
int32_t* attr_int32 = (int32_t*)malloc (sizeof(*attr_int32)*len);
|
|
|
H5ReadFileAttribInt32 (f, ATTR_INT32, attr_int32);
|
|
|
/* do something with this 32bit signed integer array */
|
|
|
free (attr_int32);
|
|
|
|
|
|
H5CloseFile (f);
|
|
|
MPI_Finalize ();
|
|
|
return 0;
|
|
|
}
|
|
|
----
|
|
|
|
|
|
*Fortran implementation*
|
|
|
----
|
|
|
include 'H5hut.f90'
|
|
|
|
|
|
program read_step_attribs
|
|
|
use H5hut
|
|
|
implicit none
|
|
|
include 'mpif.h'
|
|
|
|
|
|
character (len=*), parameter :: FNAME = "example_file_attribs.h5"
|
|
|
character (len=*), parameter :: ATTR_STRING = "StepAttrString"
|
|
|
character (len=*), parameter :: ATTR_I4 = "StepAttrInt32"
|
|
|
character (len=128) :: fmt
|
|
|
|
|
|
! attribute values. Note: allocatable strings aren't supported in Fortran90
|
|
|
character (len=256) :: string_value
|
|
|
integer*4, allocatable :: i4_value (:)
|
|
|
|
|
|
integer :: ierr
|
|
|
integer*8 :: file_id
|
|
|
integer*8 status
|
|
|
integer*8 type
|
|
|
integer*8 len
|
|
|
|
|
|
call mpi_init (ierr)
|
|
|
call h5_abort_on_error ()
|
|
|
file_id = h5_openfile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT)
|
|
|
status = h5_setstep (file_id, int8(1))
|
|
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_STRING, type, len)
|
|
|
status = h5_readstepattrib_string (file_id, ATTR_STRING, string_value)
|
|
|
! more code here
|
|
|
|
|
|
status = h5_getstepattribinfo_by_name (file_id, ATTR_I4, type, len)
|
|
|
allocate (i4_value(len))
|
|
|
status = h5_readstepattrib_i4 (file_id, ATTR_I4, i4_value)
|
|
|
! more code here
|
|
|
|
|
|
status = h5_closefile (file_id)
|
|
|
call mpi_finalize(ierr)
|
|
|
end program read_step_attribs
|
|
|
---- |
|
|
\ No newline at end of file |