How to Read HDF-EOS2 Grid data in Fortran

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

First of all, we assume that you have installed the HDF-EOS2 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 AMSR-E AE_RnGdexternal file from NSIDC. Download the file explained in this page here.

An HDF-EOS2 file consists of multiple grids, swaths and points. 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-EOS2 API gdopen opens an existing HDF-EOS2 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-EOS2 grid file
integer*4 gridfileid
gridfileid = gdopen('AMSR_E_L3_RainGrid_B05_200707.hdf',
$DFACC_READ)

This HDF-EOS2 file has the grid object MonthlyRainTotal_GeoGrid, should be opened using the gdattach function to access all data fields, dimensions and attributes of the grid object.

Figure 2 Attaching to a grid object
integer*4 gridid
gridid = gdattach(gridfileid, 'MonthlyRainTotal_GeoGrid')
The first argument is the descriptor returned by the gdopen function. The second argument is the name of grid object.

MonthlyRainTotal_GeoGrid has two data fields: TbOceanRain and RrLandRain. Let's read data from the TbOceanRain data field. One can find the datatype, rank and dimension sizes from the HDF Viewexternal Java browser or the hdpexternalhdp command-line tool. Assuming that we know the datatype, rank and dimension sizes of TbOceanRain, data can be read using the gdrdfld function.

Figure 3 Reading data from a data field
integer status
integer*4 start(2)
integer*4 stride(2)
integer*4 edge(2)
real*4 tbocean(72,28)

start(1) = 0
stride(1) = 1
edge(1) = 72
start(2) = 0
stride(2) = 1
edge(2) = 28

status = gdrdfld(gridid, 'TbOceanRain', start,
$stride, edge, tbocean)
The first argument is the descriptor returned by the 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. We specify the entire elements by setting starting point as (0, 0), stride as (1, 1), and edge (72, 28). The last argument tbocean is the buffer for the output; the value of the data field is saved in this buffer after the gdrdfld function is called.

Note that passing insufficient buffer to the gdrdfld function results in buffer-overrun. In this example, the data field is a 72-by-28 array, and its datatype is the 32-bit floating point.

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

Figure 4 Detaching from the grid object
status = gddetach(gridid)

Now that we get all necessary data from the file, we can close the file. The gdclose function closes the file. Its argument is the descriptor returned by the gdopen function.

Figure 5 Closing the file
status = gdclose(gridfileid)

Previously, all elements of the TbOceanRain data field are stored at a variable called tbocean. This variable can be used by the application. Let's print a few elements to check if the retrieved data looks correct.

Figure 6 Using retrieved data
integer*4 i, j

do i = 1, 28
do j = 1, 72
write(*,*), tbocean(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 7 A skeleton of Makefile
HDF4_DIR=<hdf4_path>
HDFEOS2_DIR=<hdfeos2_path>

FC=$(HDF4_DIR)/bin/h4fc

FFLAGS=-fno-underscoring
LDFLAGS=-L$(HDFEOS2_DIR)/lib
LIBS=-lhdfeos -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, check here.

For more Fortran examples to access HDF-EOS2 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