3

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?

Yash
  • 689
  • 4
  • 11
  • 26

3 Answers3

5

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.

  • Thank for your reply. I used HDFView to visualize the data from the hdf files and it works and the files are of hdf4 format. So as you said, the problem must be with h5py not being able to support hdf4 format. I came across pyhdf that supports hdf4 format. I guess it will solve my problem. By the way, I did not chmod, I am using windows. – Yash May 21 '13 at 14:45
  • 1
    That's great! Hope you can do your job with `pyhdf`. – Iñigo Hernáez Corres May 22 '13 at 15:03
  • 2
    An alternative is to use `h4toh5` to convert the files to HDF5. This allows to rely on the latest tools. Tool available at the [project home](http://www.hdfgroup.org/h4toh5/). – Eric Platon Apr 13 '14 at 02:31
2

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')
danodonovan
  • 19,636
  • 10
  • 70
  • 78
  • Thanks for the reply. The file exists, in fact all 139 of them, but to make sure I performed the checks you suggested, looping over every file in the folder to find that the files exists and are accessible. – Yash May 21 '13 at 11:10
0

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.

Philip
  • 1