简体   繁体   English

使用h5py包为python读取hdf文件时出错

[英]Error in reading hdf file using h5py package for python

I want to extract data from hdf files that I downloaded from MODIS website. 我想从我从MODIS网站下载的hdf文件中提取数据。 A sample file is provided in the link. 链接中提供了一个示例文件。 I am reading the hdf file by using the following lines of code: 我正在使用以下代码行读取hdf文件:

>>> import h5py
>>> f = h5py.File( 'MYD08_M3.A2002182.051.2008334061251.psgscs_000500751197.hdf', 'r' )

The error I am getting: 我得到的错误:

Traceback (most recent call last):
    File "<pyshell#3>", line 1, in <module>
f = h5py.File( 'MYD08_M3.A2002182.051.2008334061251.psgscs_000500751197.hdf', 'r' )
    File "C:\Python27\lib\site-packages\h5py\_hl\files.py", line 165, in __init__
fid = make_fid(name, mode, userblock_size, fapl)
    File "C:\Python27\lib\site-packages\h5py\_hl\files.py", line 57, in make_fid
fid = h5f.open(name, h5f.ACC_RDONLY, fapl=fapl)
    File "h5f.pyx", line 70, in h5py.h5f.open (h5py\h5f.c:1640)
IOError: unable to open file (File accessability: Unable to open file)

I have tried several other hdf files from different sources but I am getting the same error. 我已尝试过来自不同来源的其他几个hdf文件,但我收到同样的错误。 What seems to be the fault here? 这里似乎有什么错?

I think there could be two possible problems: 我认为可能存在两个问题:

1) As the file extension is "hdf", maybe this is a HDF4 file. 1)由于文件扩展名为“hdf”,这可能是HDF4文件。 HDF5 files normally have ".hdf5" or ".h5·" extension. HDF5文件通常具有“.hdf5”或“.h5”的扩展名。 I am not sure if h5py is able to read HDF4 files. 我不确定h5py是否能够读取HDF4文件。

2) Perhaps you have to change permissions to the file itself. 2)也许您必须更改文件本身的权限。 If you are in a linux machine try: chmod +r file.hdf 如果你在linux机器上试试: chmod +r file.hdf

You can try to open your file with HDFView . 您可以尝试使用HDFView打开文件。 This software is available in several platforms. 该软件可在多个平台上使用。 You can check the properties of the files very easily with it. 您可以使用它轻松检查文件的属性。

This sounds like a file permission error, or even file existence. 这听起来像文件权限错误,甚至文件存在。 Maybe add some checks such as 也许添加一些检查,如

import os

hdf_file = 'MYD08_M3.A2002182.051.2008334061251.psgscs_000500751197.hdf'

if not os.path.isfile(hdf_file):
    print 'file %s not found' % hdf_file

if not os.access(hdf_file, os.R_OK):
    print 'file %s not readable' % hdf_file

f = h5py.File(hdf_file, 'r')

I had the same issue, and later identified that my file had only "read-only", which for some reason stopped the h5py to read it. 我有同样的问题,后来发现我的文件只有“只读”,由于某种原因停止了h5py读取它。 After modifying the permission to "write", I was able to read it. 在修改了“写入”权限后,我能够阅读它。 Not sure why it was set up like this. 不知道为什么这样设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM