使用Fortran读取一组HDF5的数据集

时间:2019-07-05 07:01:42

标签: fortran hdf5

我无法使用Fortran读取一组HDF5文件的成员数据集。

我能够列出我的HDF5组的成员。但是我无法访问组中成员的数据,

program sds_info
use hdf5
implicit none


! Variables declaration
  CHARACTER*100 :: file_name
  CHARACTER*100 :: sds_name
  CHARACTER*100 :: gr_name
  INTEGER(HID_T):: file_id, gr_id, dset_id, attr_id
  INTEGER :: status, error, storage, nlinks,max_corder, attr_num

  REAL, DIMENSION(1) :: dset_data, data_out
  INTEGER, DIMENSION(1)   ::    buf

  INTEGER(HSIZE_T), DIMENSION(1)::  data_dims
  INTEGER(HSIZE_T), DIMENSION(1) ::dims
!
! varaibles to read a dataset in a group
  CHARACTER*100 :: ap_name
  integer(HID_T):: ap_id
  real, allocatable, dimension(:) :: ap
  integer(HSIZE_T), dimension(15624960) :: ap_dim
  integer :: nmembers ! Number of group members
  CHARACTER(LEN=20) :: name_buffer ! Buffer to hold object's name
  integer :: i
  integer :: type
! 
! Variables initalization
  file_name = "PVAR8.h5"
  sds_name = "time"
  gr_name = "part"
  attr_name = "attr1"
  ap_name="ap"

! Initialize the interface
  call h5open_f(status)
! Open an hdf5 file
  call h5fopen_f(file_name, H5F_ACC_RDWR_F, file_id, status)


! Open a group
  call h5gopen_f(file_id, gr_name, gr_id, status )
!  
! Open a dataset
  call h5dopen_f(file_id, sds_name, dset_id, error)
! Get the number of attributes
  call h5aget_num_attrs_f(dset_id, attr_num, error)
  print *, "attr_num ",attr_num
! Read the dataset
  call h5dread_f(dset_id, H5T_NATIVE_REAL, data_out, data_dims, error)
  print *, "data_out ",data_out


! Terminate access to the group
  call h5gclose_f(gr_id, error)
! Terminate access to the dataset
  call h5dclose_f(dset_id, error)
! Terminate access to the file
  call h5fclose_f(file_id, error)
! Close FORTRAN interface.
  call h5close_f(status)


end program sds_info

我可以读取组,但是如何使用Fortran在HDF5中访问和读取组成员的数据?

1 个答案:

答案 0 :(得分:1)

如果您不打算使用特定技术,请查看HDFql来解决您的问题。

使用Fortran中的HDFql,您可以从文件time读取组part中存储的数据集PVAR8.h5(数据类型为实数),如下所示:

PROGRAM Test

    ! use HDFql module (make sure it can be found by the Fortran compiler)
    USE HDFql

    ! declare variables
    REAL(KIND = 8) :: value
    INTEGER :: state

    ! register variable "value" for subsequent use (by HDFql)
    state = hdfql_variable_transient_register(value)

    ! select (i.e. read) data from dataset "time" and populate variable "value" with it
    state = hdfql_execute("SELECT FROM PVAR8.h5 /part/time INTO MEMORY 0")

    ! display content of variable "value"
    WRITE(*, *) "Dataset value:", value

END PROGRAM

可以在here中找到其他HDFql示例。