PyHDF is a Python interface to the HDF4 library. It covers most HDF4 APIs of Scientific Data Set, Vdata, and Vgroup.
The latest version is pyhdf-0.10.5. It is last updated on May 8, 2022. The following 0.9.0 packages are provided for archiving-purpose only.
Platform | Package | Checksum |
---|---|---|
Windows | pyhdf-0.9.0.win32-py2.7.msi | SHA256 |
Unix | pyhdf-0.9.0.tar.gz | SHA256 |
The easiest way to install pyhdf will be using miniconda 32-bit. Try conda install pyhdf first. Alternatviely, try pip install pyhdf for version 0.10.x or pip install hdf4-python for version 0.9.x. If both conda and pip don't work, you can build pyhdf from source by following this guide. This installation guide is for Python 2.7. We assume that you are already familiar with Python installation and package managers such as easy_install, pip, and conda.
If you're a Docker user, please try our Docker images of Anaconda through Docker Hub that include all the required Python modules (e.g., basemap) to visualize HDF-EOS data.
C:\Anaconda
.C:\Anaconda>
. Type y for any update. Type exit to close the Anaconda command shell.INCLUDE_DIRS
environment variable. For example, if HDF4 library is installed under /usr/local, run $export INCLUDE_DIRS=/usr/local/include.LIBRARY_DIRS
environment variable. For example, if HDF4 library is installed under /usr/local, run $export LIBRARY_DIRS=/usr/local/lib.Building from source is tricky on Windows so we explain it here.
.exe
extensions.C:\Anaconda>
conda install numpy.C:\Anaconda>
conda install setuptools.C:\Anaconda\libs
.C:\pyhdf-0.9.0>
run.PyHDF is similar to HDF4 C API in that most functions have similar names and functionality. Although most function names are the same as or similar to corresponding C APIs, they are categorized into a few classes. For example, the SD API is divided into five Python classes, including SD, SDS, SDim and SDAttr.
If you have installed PyHDF successfully, you can read and visualize NASA HDF4 products. First, please make sure that you have installed basemap, matplotlib, and numpy modules and import them before pyhdf as shown in Figure 2. For example, if Python fails to load basemap module, you can install one using conda install basemap
.
Next, open the sample NASA AIRS HDF-EOS2 file, AIRS.2002.08.01.L3.RetStd_H031.v4.0.21.0.G06104133732.hdf, and read datasets as shown in Figure 3. PyHDF supports HDF4 Vgroup, SDS, and Vdata interfaces but we focus on SDS because many NASA datasets are stored in SDS.
Finally, plot the data on map using the functions in basemap and matplotlib packages as shown in Figure 4.
The complete code is here. Use right mouse button and select Save Link As to download the code. If you execute the code (e.g., python AIRS.py
) on the directory where the sample file exists, you will get the image as shown in Figure 5.
You can also write an HDF4 file. Figure 6 shows part of a program that creates an HDF4 file with an SDS dataset.
The code in Figure 6 creates an HDF4 file and an SDS object in it. This code is straightforward to those who are familiar with HDF4. As Table 1 shows, many PyHDF interfaces are equivalent to HDF4 C interfaces.
| ||||||||||||||||
Table 1 PyHDF API and equivalent HDF4 C API |
The statement starting with sd = SD()
creates an SD instance, and it
is equivalent to the SDstart()
function. The SD class implements
functions applied to a file such as creating a file and a global
attribute. The SD interface identifier that the SDstart()
API returns
does not exist because the SD class of PyHDF encapsulates the data
and possible operations.
The statement starting with sds.units
sets an attribute to
the specific SDS object. This is equivalent to the SDsetattr()
C
function. The next statement, sds[:] = data
, writes the actual values
to the file as SDwritedata()
does.
Both V API and VS API are divided into a few classes and are encapsulated like SD API. This eliminates the use of an identifier, and may improve the readability.