How to Read HDF-EOS5 Grid data in C

This page explains how to read HDF-EOS5 grid data using HDF-EOS5 C APIs. This example is very simple. It shows how to access one data field in an HDF-EOS5 file. See the full source code here.

We will use one OMI Aura data. Download the file explained in this page here.

An HDF-EOS5 file consists of multiple grids, swaths and points. A grid object consists of dimensions, data fields and attributes. Assuming that we know the name of a grid object and the name of a data field of that grid, we can access the data field through the following steps:

The following file contains the declarations of the library functions used in this example.

An HDF-EOS5 API HE5_GDopen function opens an existing HDF-EOS5 file. The first argument is the file name, and the second argument specifies the mode to access the file. In our example, we just want to read the data. So the mode is the read-only mode.

Figure 2 Opening an HDF-EOS5 grid file
hid_t gridfileid;
gridfileid = HE5_GDopen("OMI-Aura_L3-OMAEROe_2004m1001_v003-2009m0114t094640.he5", H5F_ACC_RDONLY);

This HDF-EOS5 file has the grid object ColumnAmountAerosol, which should be opened using the HE5_GDattach function to access all data fields, dimensions and attributes of the grid object.

Figure 3 Attaching to a grid object
hid_t gridid;
gridid =HE5_GDattach(gridfileid, "ColumnAmountAerosol");
The first argument is the descriptor returned by the HE5_GDopen function. The second argument is the name of grid object.

ColumnAmountAerosol has several data fields. Let's read data from TerrainReflectivity data field. One can find the datatype, rank and dimension sizes from the HDF Viewexternal Java browser or the h5dumpexternal command-line tool. Assuming that we know the datatype, rank and dimension sizes of TerrainReflectivity, data can be read using the HE5_GDreadfield function.

Figure 4 Reading data from a data field
short terrain[720][1440];
HE5_GDreadfield(gridid, "TerrainReflectivity", NULL, NULL, NULL, terrain);
The first argument is the descriptor returned by the HE5_GDattach function. The second argument specifies the name of the data field. The third, fourth and fifth arguments can specify a subset of the data field. Filling them with NULL implies reading the entire elements of the data field. The last argument terrain is the buffer for the output; the value of the data field is saved in this buffer after the HE5_GDreadfield function is called.

Note that passing insufficient buffer to the HE5_GDreadfield function results in buffer-overrun. In this example, the data field is a 720-by-1440 array, and its datatype is short integer.

After retrieving data, the grid object can be detached using the HE5_GDdetach function. Note that this function and the HE5_GDattach function form a pair. The descriptor returned by the HE5_GDattach function is the argument of the HE5_GDdetach function.

Now that we got all necessary data from the file, we can close the file. HE5_GDclose function closes the file. Its argument is the descriptor returned by the HE5_GDopen function.

Figure 6 Closing the file
HE5_GDclose(gridfileid);

Previously, all elements of TerrainReflectivity data field were stored at a variable called terrain. One can perform desired operations on this variable. Let's print a few elements to check if the retrieved data looks correct.

Figure 7 Using retrieved data
hsize_t i, j;
for (i = 0; i < 7; ++i) {
for (j = 0; j < 14; ++j) {
printf("%d ", terrain[i][j]);
}
printf("\n");
}


To get the full source code, see here. To build this program, users can use Makefile like the following:

Figure 8 A skeleton of Makefile
HDF5_DIR=<hdf5_path>
HDFEOS5_DIR=<hdfeos5_path>

CC=$(HDF5_DIR)/bin/h5cc

CFLAGS=-I$(HDFEOS5_DIR)/include
LDFLAGS=-L$(HDFEOS5_DIR)/lib
LIBS=-lhe5_hdfeos -lGctp -lm

read_grid: read_grid.c
$(CC) $(CFLAGS) $(LDFLAGS) $< $(LIBS) -o $@
Users need to adjust this file to provide proper paths. For more information about this Makefile, check here.

For more C examples of accessing HDF-EOS5 grid files, check here.


Last modified: 11/11/2020
About Us | Contact Info | Archive Info | Disclaimer
Sponsored by Subcontract number 4400528183 under Raytheon Contract number NNG15HZ39C, funded by NASA / Maintained by The HDF Group