Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in reading hdf file using h5py package for python

Tags:

python

h5py

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?

like image 762
Yash Avatar asked Feb 04 '26 08:02

Yash


2 Answers

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.

like image 141
Iñigo Hernáez Corres Avatar answered Feb 06 '26 22:02

Iñigo Hernáez Corres


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')
like image 25
danodonovan Avatar answered Feb 06 '26 22:02

danodonovan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!