How to Read HDF-EOS5 Grid data in Fortran

This page explains how to read HDF-EOS5 grid data using HDF-EOS5 Fortran APIs. One Fortran program that reads a data field and dumps its elements is presented. This example is written in Fortran 77.

First of all, we assume that you have installed the HDF-EOS5 Fortran library correctly. Please make sure that you included -Df2cFortran in the compiler option when you built it. You may want to review our How to build HDF-EOS page.

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

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

The 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 1 Opening an HDF-EOS5 grid file
integer gridfileid
gridfileid = he5_gdopen('OMI-Aura_L3-OMAEROe_2004m1001_v003-2009m0114t094640.he5',
$HE5F_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 2 Attaching to a grid object
integer 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 the grid object.

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

Figure 3 Reading data from a data field
integer status
integer*4 start(2)
integer*4 stride(2)
integer*4 edge(2)
integer*2 terrain(1440, 720)

start(1) = 0
stride(1) = 1
edge(1) = 1440
start(2) = 0
stride(2) = 1
edge(2) = 720

status = he5_gdrdfld(gridid, 'TerrainReflectivity', start,
$stride, edge, terrain)
The first argument is the descriptor returned by the he5_gdattach function. The second argument specifies the name of data field. The third, fourth and fifth arguments are used to specify the range of elements. Here, we specify the entire elements by setting starting point as (0, 0), stride as (1, 1), and edge (1440, 720). The last argument terrain is the buffer for the output; he5_gdrdfld function fills this.

If you use a 64-bit machine, you need to change the type of integer*4 to integer*8 for start, stride and edge variables.

Figure 4 Reading data from a data field on 64-bit machine
integer status
integer*8 start(2)
integer*8 stride(2)
integer*8 edge(2)
integer*2 terrain(1440, 720)

start(1) = 0
stride(1) = 1
edge(1) = 1440
start(2) = 0
stride(2) = 1
edge(2) = 720

status = he5_gdrdfld(gridid, 'TerrainReflectivity', start,
$stride, edge, terrain)
Otherwise, you'll get a run time error even if the unmodified program compiles without any warning.

Note that passing insufficient buffer to the he5_gdrdfld function results in buffer-overrun. In this example, the data field is a 1440-by-720 array, and its datatype is the short(16-bit for most systems) 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.

Figure 5 Detaching from the grid object
status = he5_gddetach(gridid)

Now that we get 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
status = he5_gdclose(gridfileid)

Previously, all elements of TerrainReflectivity data field are 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
integer*4 i, j

do i = 1, 720
do j = 1, 1440
write(*,*), terrain(j,i)
enddo
write(*,*)
enddo

As the above code omits some parts, see here. to get the full source code. 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>

FC=$(HDF5_DIR)/bin/h5fc

FFLAGS=-fno-underscoring
LDFLAGS=-L$(HDFEOS5_DIR)/lib
LIBS=-lhe5_hdfeos -lGctp

read_grid: read_grid.f
$(FC) $(FFLAGS) $(LDFLAGS) $< $(LIBS) -o $@
Users need to adjust this file to provide proper paths. For more information about this Makefile, click here.

For more Fortran examples of accessing HDF-EOS5 grid files, click 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