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.
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.
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 View Java browser or the h5dump command-line tool.
Assuming that we know the datatype, rank and
dimension sizes of TerrainReflectivity, data can be read using the HE5_GDreadfield
function.
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.
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.
To get the full source code, see
here.
To build this program, users can use Makefile
like the following:
Makefile
, check here.
For more C examples of accessing HDF-EOS5 grid files, check here.