문제

I want to extract data from hdf files that I downloaded from MODIS website. A sample file is provided in the link. I am reading the hdf file by using the following lines of code:

>>> 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. 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. HDF5 files normally have ".hdf5" or ".h5·" extension. I am not sure if h5py is able to read HDF4 files.

2) Perhaps you have to change permissions to the file itself. If you are in a linux machine try: chmod +r file.hdf

You can try to open your file with 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. After modifying the permission to "write", I was able to read it. Not sure why it was set up like this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top