[GIS] processing MODIS hdf files with gdal

gdalmodispythonremote sensingwindows

I'm trying to open an MODIS file (hdf format) with gdal in a python script under windows OS. I use the HDF4 driver, as read at http://www.gdal.org/frmt_hdf4.html.
But when I run it I got this error message:

ERROR 4: `MOD10A1.A2000064.h12v04.005.2007164045648.hdf' not recognised as a supported file format. 

I have tried with the gdal.ALLRegister() statement with the same result. My goal is to obtain the properties of the image using GeoTransform, RasterXSize, etc. and reproject it afterward. If you could provide me with any help it would be much appreciated.

ps. I know I can use the Modis Reprojection Tool (MRT) but I need it to be included in my script as it will iterate over a folder containing a lot of images.

thanks

Best Answer

HDF4 support is not available in GDAL by default. If you're using the GDAL binaries and python libraries from GISInternals, these do not have HDF4 support compiled in (at the time this answer was originally posted in July 2013, however HDF4 support was added to the GISInternal GDAL build in Nov. 2013). HDF4 support is compiled in the OSGeo4W GDAL binaries.

You can test if you have HDF4 support with the gdalinfo command: gdalinfo --format hdf4

You can also batch the MRT from within your python script using the subprocess module, see the "Command Line Interface" section of the MRT User Manual.

Edit: the subprocess module is easy to use, see below. See also the "Automated Batch Processing" section in the MRT User Manual.

import subprocess
def runcmd(cmd)
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    stdout,stderr=proc.communicate()
    exit_code=proc.wait()
    return exit_code,stdout,stderr

cmd = 'mrt command line'
exit_code,stdout,stderr = runcmd(cmd)
if exit_code:raise RuntimeError(stderr)
else:print stdout