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.
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.
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.
gdopenfunction. 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 View Java browser or the hdphdp command-line tool.
Assuming that we know the datatype, rank and
dimension sizes of TbOceanRain, data can be read using the
gdattachfunction. 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
tboceanis the buffer for the output; the value of the data field is saved in this buffer after the
gdrdfldfunction 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
Note that this function and the
gdattach function form a pair. The descriptor returned
gdattach function is the argument of the
Now that we get all necessary data from the file, we can close the file.
gdclose function closes the file. Its argument is the descriptor returned by
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.
As the above code omits some parts, see
to get the full source code.
To build this program, users can use
Makefile like the following:
Makefile, check here.
For more Fortran examples to access HDF-EOS2 grid files, click here.