我有一个非常大的HDF5文件,希望使用FORTRAN读取其中的一小部分。到目前为止,我的尝试都失败了,我对文档感到困惑。您可以提供给FORTRAN新手的任何指针(但合理的C / python编码器)将不胜感激。
特别是,我很难理解数据空间和内存空间是什么,在我的代码中,根据我阅读的文档,它们似乎并没有达到我的期望。不过那可能是我自己的愚蠢!
这是我正在尝试的:
integer, allocatable :: tmpdata(:,:) ! Array to contain data subset
integer(HID_T) :: fid ! HDF5 File ID
integer(HID_T) :: did ! Dataset ID
integer :: error ! Error variable
integer(HSIZE_T), dimension(1:2) :: count ! Number of px to read (x,y)
integer(HSIZE_T), dimension(1:2) :: offset ! Starting point for read (x,y)
integer(HID_T) :: dataspace ! Dataspace
integer(HID_T) :: memspace ! Memoryspace
offset=(/58000,22000/) ! Set offset in 2d dataset
count=(/1200,1200/) ! Set # pixels to read (1200x1200 slab)
allocate(tmpdata(1200,1200)) ! Allocate space to store this slab
call h5open_f(error)
call h5fopen_f ("myfile.h5", H5F_ACC_RDWR_F, fid, error) ! Open HDF5 file
call h5dopen_f(fid, "mydataset", did, error) ! Open dataset
call h5dget_space_f(did, dataspace, error) ! Retrieve dataspace
call h5screate_simple_f(2, count, memspace, error) !Create memspace, rank=2,size=1200x1200
call h5sselect_hyperslab_f(dataspace, H5S_SELECT_SET_F, offset, count, error) ! Select slab in the data
call h5dread_f(did, H5T_NATIVE_INTEGER, tmpdata, dimsm, error,memspace,dataspace) ! Read the data from the HDF5 file into the tmpdata array
! Close everything
-snip-
一切正常,直到h5dread_f调用为止。然后我遇到了段错误。如果我将tmpdata设置为等于HDF5文件中实际数据集的大小,则它可以工作,但这不是一个好的解决方案,因为对于某些文件,数据集太大而无法存储在内存中。有任何想法吗?希望我只是在做些愚蠢的事情。万一重要,我正在Ubuntu 14.04上使用ifort和HDF5-1.8.15补丁1进行编译
根据您对变量的描述和省略dimsm
,我猜您已将该变量设置为要读取的变量的完整尺寸。这些尺寸应该是您设置的超级平板的尺寸,也就是您要保存读入结果的变量的尺寸tmpdata
。您可以count
在读取调用中使用与创建超级平板相同的数组。
改变:
call h5dread_f(did, H5T_NATIVE_INTEGER, tmpdata, dimsm, error,memspace,dataspace)
到
call h5dread_f(did, H5T_NATIVE_INTEGER, tmpdata, count, error,memspace,dataspace)
并且您的阅读应该有效。
发生的情况(如果我的假设是正确的)是dimsm
包含整个数据集维度的值(或两个维度均大于1200的值),因此读取数据的调用会尝试读取数据空间范围之外的数据(设置为在给定偏移量下的1200x1200视图),如果不是segfault,则它将尝试将大于1200x1200的读数放入1200x1200变量中tmpdata
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句