How to read HDF-EOS2 Swath Data in C

This page explains how to read data fields and geolocation fields from HDF-EOS2 swath data using HDF-EOS2 C APIs. The example used below will simply read the entire elements in one data field and two geolocation fields, and dump several elements. To get the full source code, see here.

An HDF-EOS2 file consists of multiple grids, swaths and points. A swath object consists of dimensions, data fields, attributes, geolocation fields and dimension maps. Note that a swath object can have geolocation fields and dimension maps, which are not allowed in a grid object. Geo-location fields provide spatial and/or temporal information about the data fields.

In most cases, a swath object has at least two geolocation fields: Longitude and Latitude. As their names indicate, both fields are about geolocations where physical data (represented by data fields) were measured.

Briefly speaking, dimension maps can be used to save space by keeping only small number of elements in geolocation fields. The trade-off is that associating geolocation fields with data fields is complicated. For more information about the concept of the dimension map in swath, refer to HDF-EOS Library User's Guideexternal.

We will use one AMSR-E data AE_L2Aexternal at NSIDC. Download the file explained in this page here. This file does not use dimension maps.

Assuming that we know the swath object name, the data field name, and the geolocation field name, we can access the data field through the following steps:

For those who are familiar with the HDF-EOS2 grid interface, the above steps are straightforward. All grid interfaces start with GD, and all swath interfaces start with SW.

The following files contain the declarations of the library functions used in this example.

Figure 1 Required header files to include
#include <mfhdf.h>
#include <hdf.h>
#include <HdfEosDef.h>

The HDF-EOS2 API SWopen function 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 2 Opening an HDF-EOS2 swath file
int32 swathfileid;
swathfileid = SWopen("AMSR_E_L2A_BrightnessTemperatures_V09_200206190029_D.hdf", DFACC_RDONLY);

This HDF-EOS2 file has three swath objects. We will open the swath object Low_Res_Swath using the SWattach function to access its data fields, attributes and geolocation fields.

Figure 3 Attaching to a swath object
int32 swathid;
swathid = SWattach(swathfileid, "Low_Res_Swath");
The first argument is the descriptor returned by the SWopen function. The second argument is the name of swath object.

Low_Res_Swath has several data fields. Let's read data from the 23.8H_Approx._Res.3_TB_(not-resampled) data field. One can find the datatype, rank and dimension sizes using the HDF Viewexternal Java browser or the hdpexternal command-line tool. Assuming that we know the datatype, rank and dimension sizes of this data field, data can be read using the SWreadfield function.

Figure 4 Reading data from a data field
int16 approxres[1997][243];
SWreadfield(swathid, "23.8H_Approx._Res.3_TB_(not-resampled)", NULL, NULL, NULL, approxres);
The first argument is the descriptor returned by the SWattach 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 approxres is the buffer for the output; the value of the data field is saved in this buffer after the GDreadfield function is called.

Note that passing insufficient buffer to the SWreadfield function results in buffer-overrun. In this example, the data field is a 1997-by-243 array, and its datatype is the short(16-bit for most systems) integer.

The Low_Res_Swath swath has three geolocation fields: Time, Longitude and Latitude. We will retrieve elements from Longitude and Latitude. The same API, SWreadfield, can be used to read data in a geolocation field.

Figure 5 Reading data from geolocation fields
float32 longitude[1997][243];
SWreadfield(swathid, "Longitude", NULL, NULL, NULL, longitude);

float32 latitude[1997][243];
SWreadfield(swathid, "Latitude", NULL, NULL, NULL, latitude);

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

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

Figure 7 Closing the file
SWclose(swathfileid);

We have read one data field 23.8H_Approx._Res.3_TB_(not-resampled), and two geolocation fields, Longitude and Latitude, and stored them at approxres, longitude and latitude, respectively. One can print their values as follows:

Figure 8 Using retrieved data
int32 i, j;
for (i = 0; i < 1997; ++i) {
for (j = 0; j < 243; ++j) {
printf("%d was measured at %f, %f\n", approxres[i][j], longitude[i][j], latitude[i][j]);
}
}
This code assumes that those three fields have the same rank and number of elements. Note that this is not the case if dimension maps are used. If dimension maps are used, users need to do interpolation or sampling to get the location where an element of data fields was measured.

Also, one needs to know that data fields can have more dimensions than geolocation fields. For example, the data field Land/Ocean_Flag_for_6_10_18_23_36_50_89A is a 1997-by-243-by-7 array. Given that Longitude and Latitude are 1997-by-243 arrays, we can conclude that the first two dimensions of the data field are related to both Longitude and Latitude, and the third dimension is related to something else. This implies that seven elements are measured at the same location.

To get the full source code, see here. For more C examples of accessing HDF-EOS2 swath data and how to build this code, see 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