How to Read HDF-EOS2 Grid data - More Examples

This page provides Makefile templates and more C and Fortran 77 examples on how to read HDF-EOS2 grid data using the HDF-EOS2 library.

HDF-EOS2 Grid data demonstrated by these examples are from products distributed by NASA GES DISC, NSIDC, LP DAAC and LaRC.

How to Build C Programs

After you write a C program, you need to build a binary using a C compiler. One easy way to build the executable is to use h4cc, which is part of the HDF4external binary distribution.

We recommend that users create Makefile to build the executable from C sources. The following shows a skeleton of Makefile that can be used to build read_grid from read_grid.c.

Figure 1 A skeleton of Makefile
HDF4_DIR=<hdf4_path>
HDFEOS2_DIR=<hdfeos2_path>

CC=$(HDF4_DIR)/bin/h4cc

CFLAGS=-I$(HDFEOS2_DIR)/include
LDFLAGS=-L$(HDFEOS2_DIR)/lib
LIBS=-lhdfeos -lGctp

read_grid: read_grid.c
$(CC) $(CFLAGS) $(LDFLAGS) $< $(LIBS) -o $@
The first two lines should be set according to the user's systems. CFLAGS and LDFLAGS set paths to the HDF-EOS2 library. Users do not need to set paths to the HDF4 library because h4cc sets them automatically. LIBS specifies names of libraries used by the program. Both hdfeos and Gctp are from HDF-EOS2. Change read_grid to your own executable name.

If users don't use h4cc, many switches may be required based on how the HDF4 library and the HDF-EOS2 library are built.

After creating your own Makefile, type make or gmake to generate the executable program.

How to Build Fortran 77 Programs

As h4cc is used in C, h4fc, which is part of the HDF4 binary distribution, can be used to conveniently compile an HDF-EOS2 Fortran program. The following shows a skeleton of Makefile that can be used to build read_grid from read_grid.f. We assumed that the Fortran 77 compiler is available.

Figure 2 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 $@
Similar to the C example, the first two lines should be adjusted. If the user does not want to use h4fc, several switches need to be passed to a compiler and a linker.

You may get linker errors regarding underscores. Probably, Fortran compilers provide several options about underscores, and users need to adjust the option. For example, we used -fno-underscoring option.

Note that h4fc is not created if the HDF4 library is built without Fortran. Still, users can build Fortran programs because the Fortran interfaces of the HDF-EOS2 library does not depend on HDF4's Fortran wrapper library. The following is one possible Makefile that builds read_grid from read_grid.f without using h4fc.

Figure 3 A skeleton of Makefile using a pure Fortran compiler
HDF4_DIR=<hdf4_path>
HDFEOS2_DIR=<hdfeos2_path>
SZIP_DIR=<szip_path>

FC=g77

FFLAGS=-fno-underscoring
LDFLAGS= -L$(HDFEOS2_DIR)/lib -L$(HDF4_DIR)/lib -L$(SZIP_DIR)/lib
LIBS=-lhdfeos -lGctp -lmfhdf -ldf -lsz -ljpeg -lz

read_grid: read_grid.f
$(FC) $(FFLAGS) $(LDFLAGS) $< $(LIBS) -o $@
In the above figure, we assumed g77external is available and the HDF4 library is built with the SZIPexternal library. If HDF4 is not built with the SZIP library, neither LDFLAGS nor LIBS needs to contain the SZIP path.

If you want to build your Fortran program with non-GNU compiler like Intel Fortran compiler or PGI Fortran compiler, do not specify any FFLAGS option in your make file. The following is an example Makefile for Intel Fortran compiler.

Figure 4 A skeleton of Makefile using an Intel Fortran compiler
HDF4_DIR=<hdf4_path>
HDFEOS2_DIR=<hdfeos2_path>
SZIP_DIR=<szip_path>

FC=ifort # FC=pgf77 will also work for PGI fortran compiler.

FFLAGS=
LDFLAGS= -L$(HDFEOS2_DIR)/lib -L$(HDF4_DIR)/lib -L$(SZIP_DIR)/lib
LIBS=-lhdfeos -lGctp -lmfhdf -ldf -lsz -ljpeg -lz

read_grid: read_grid.f
$(FC) $(FFLAGS) $(LDFLAGS) $< $(LIBS) -o $@

After creating your own Makefile, type make or gmake to generate the executable program.

AE_RnGd - AMSR-E/Aqua Monthly L3 5x5 deg Rainfall Accumulations

This data product is distributed by NSIDC. Download one file here. See C code or Fortran 77 code that reads the TbOceanRain data field from the MonthlyRainTotal_GeoGrid grid.

AIRH3QPM - AIRS/Aqua Level 3 monthly quantization in physical units (AIRS+AMSU+HSB)

This data product is distributed by GES-DISC. Download one file here. See C code or Fortran 77 code that reads the Entropy data field from the L3Quant grid.

MCD43C4 - Nadir BRDF- Adjusted Reflectance

This data product is distributed by LP-DAAC. Download one file here. See C code or Fortran 77 code that reads the Nadir_Reflectance_Band1 data field from the MCD_CMG_BRDF_0.05Deg grid.

MI3DAEF - MISR Level 3 FIRSTLOOK Component Global Aerosol Product covering a day V002

This data product is distributed by LaRC. Download one file here. See C code or Fortran 77 code that reads the Optical depth average data field from the AerosolParameterAverage grid.


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