【问题标题】:Read dataset of a group of HDF5 using Fortran使用 Fortran 读取一组 HDF5 的数据集
【发布时间】:2019-11-15 19:02:09
【问题描述】:

我无法使用 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 中访问和读取组成员的数据?

【问题讨论】:

  • 程序在什么时候没有按预期工作?程序的错误信息和/或输出是什么?

标签: fortran hdf5


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 2016-01-24
    • 2015-09-20
    • 2019-06-24
    • 2017-09-24
    • 2014-06-11
    • 2012-11-29
    • 2015-04-17
    • 2019-06-09
    相关资源
    最近更新 更多