read_file_attribsf.f90 4.02 KB
Newer Older
1
  !
gsell's avatar
gsell committed
2
  !  Copyright (c) 2006-2015, The Regents of the University of California,
3 4 5 6 7 8 9 10 11 12 13
  !  through Lawrence Berkeley National Laboratory (subject to receipt of any
  !  required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
  !  Institut (Switzerland).  All rights reserved.!
  !
  !  License: see file COPYING in top level of source distribution.
  !
  include 'H5hut.f90'

  program read_file_attribs
    use H5hut
    implicit none
gsell's avatar
gsell committed
14
#if defined(PARALLEL_IO)    
15
    include 'mpif.h'
gsell's avatar
gsell committed
16
#endif
17 18 19 20

    ! the file name we want to read
    character (len=*), parameter :: FNAME =               "example_file_attribs.h5"

gsell's avatar
gsell committed
21 22 23 24 25
    ! verbosity level: set it to
    ! - 1 to see error messages, if something goes wrong
    ! - 0 to get no output
    ! - a power of 2 minus one to get lot of output
    ! zeror zero
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    integer*8, parameter :: verbosity_level =             1

    ! we know the attribute names!
    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"

    ! for formated output
    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*8, allocatable :: i8_value (:)
    real*4, allocatable ::    r4_value (:)
    real*8, allocatable ::    r8_value (:)

    ! H5hut file id
    integer*8 :: file_id

    ! H5hut API status return 
    integer*8 status

    ! type of attribute
    integer*8 type

    ! len of attribute
    integer*8 len

    ! loop index
    integer*8 i

gsell's avatar
gsell committed
60 61 62 63
#if defined(PARALLEL_IO)
    ! used for mpi error return
    integer :: ierr

64
    call mpi_init (ierr)
gsell's avatar
gsell committed
65
#endif
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    ! 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_RDONLY, H5_PROP_DEFAULT)

    ! read and output string attribute
    status = h5_getfileattribinfo_by_name (file_id, ATTR_STRING, type, len)
    status = h5_readfileattrib_string (file_id, ATTR_STRING, string_value)
    write (fmt, "(a, i0, a)")  '(a', len, ')'
    write (*, "(a, ' = ')", advance='no') ATTR_STRING
    write (*, fmt) string_value

    ! read and output 32bit integer attribute
    status = h5_getfileattribinfo_by_name (file_id, ATTR_I4, type, len)
    allocate (i4_value(len))
    status = h5_readfileattrib_i4 (file_id, ATTR_I4, i4_value)
    write (fmt, "(a, i0, a)")  '(', len, 'i4)'
    write (*, "(a, ' =')", advance='no') ATTR_I4
    write (*, fmt) (i4_value(i), i = 1, len)

    ! read and output 64bit integer attribute
    status = h5_getfileattribinfo_by_name (file_id, ATTR_I8, type, len)
    allocate (i8_value(len))
    status = h5_readfileattrib_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_getfileattribinfo_by_name (file_id, ATTR_R4, type, len)
    allocate (r4_value(len))
    status = h5_readfileattrib_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_getfileattribinfo_by_name (file_id, ATTR_R8, type, len)
    allocate (r8_value(len))
    status = h5_readfileattrib_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)
gsell's avatar
gsell committed
116
#if defined(PARALLEL_IO)
117
    call mpi_finalize(ierr)
gsell's avatar
gsell committed
118
#endif
119
  end program read_file_attribs