How to Read and Visualize HDF-EOS2 data Using MATLAB

MATLABexternal provides APIsexternal for accessing HDF4external data including HDF-EOS2external. In order to use these APIs, user must be familiar with the HDF4 and HDF-EOS2 library. This page presents a few examples on how to read and visualize HDF-EOS2 data via MATLAB.

MATLAB provides functions to read HDF-EOS grid (GD)external and swath (SW) dataexternal. Since each of them is equivalent to an HDF-EOS2 C API, those who are familiar with the HDF-EOS2 library can easily learn how to handle HDF-EOS2 files in MATLAB.

Grid

HDF-EOS grid data is special in that it does not store longitude and latitude values. Instead of storing those values, HDF-EOS grid keeps only several parameters that can generate the entire longitude and latitude values. This can save much space, but this also makes it difficult to retrieve those values unless HDF-EOS API is used.

The degree of difficulty of obtaining longitude and latitude values depends on the projection method. The simplest projection is the geographic projection. We will cover this case using a real AMSR-E AE_RnGdexternal file from NSIDC. You can download the file here.

To access a data field in a grid data, the enclosing HDF-EOS2 file should be opened using gd.open, first. Using the descriptor returned by that API, the grid should be opened. In this example, we will access a grid named MonthlyRainTotal_GeoGrid in an HDF-EOS2 file named AMSR_E_L3_RainGrid_V06_200206.hdf.

Figure 1 Accessing a grid
FILE_NAME='AMSR_E_L3_RainGrid_V06_200206.hdf'
GRID_NAME='MonthlyRainTotal_GeoGrid'

file_id = gd.open(FILE_NAME, 'rdonly')
grid_id = gd.attach(file_id, GRID_NAME)
gd is the function which MATLAB provides to read data from an HDF-EOS2 grid file. The first argument that it takes always reflects the task it is supposed to do. In the figure above, gd.open would open the file which is similar to GDopen API from the HDF-EOS2 C library. It will create a handle which is stored in file_id. Using this file_id descriptor, the grid is opened in the next step using gd.attach.

Then, data fields, dimensions and attributes in this grid can be accessed.

Figure 2 Reading a data field in a grid
DATAFIELD_NAME='TbOceanRain'
[data1, lat, lon] = gd.readField(grid_id, DATAFIELD_NAME, [], [], [])
The data in a grid can be read out by using gd.readField. The name of the data field being read is TbOceanRain. Note that grid_id, is the descriptor returned by gd.attach. data1 is the buffer where the data is stored. Unlike using C APIs, users do not need to allocate memory for this buffer; memory will be automatically allocated. Also, users do not need to specify the type of data1 because MATLAB is a dynamically typed language.

After finishing reading all fields, one needs to close the grid using gd.detach. Similarly, if accessing the grid file is done, one needs to close the file using gd.close.

Figure 3 Closing the grid and the file
gd.detach(grid_id);
gd.close(file_id);
grid_id is what gd.attach returned, and file_id is what gd.open returned.

It is necessary to change type for plotting and handle fill value.

Figure 4 Handling fill value
data=double(data1);
data(data==-1) = NaN;

Finally, one can use the surfm function to draw a plot using data, lon and lat. Since many options are provided for using the surfm function, users may need to refer to MATLAB's detailed document for more information about this function.

Figure 5 Visualizing a data field
surfm(lat, lon, data, 'LineStyle', 'none');

You can see the complete code from here. Figure 6 shows the result.

Swath

Unlike grid data, swath data contains geo-location fields such as Longitude and Latitude. This allows users to draw plots without the extra step to calculate longitude and latitude values; these values can be read from geo-location fields. However, swath data may contain dimension maps that allow the size of geo-location fields smaller or larger than that of the related data fields. If this is the case, retrieving longitude and latitude values becomes complicated. We will not cover the case when dimension maps are used to handle a swath.

We will use one swath file from MODIS/Terra Snow Cover 5-Min L2 Swath 500m V006 product. You can get one sample file here.

The step to open, read and close is similar.

Figure 7 Accessing a swath
FILE_NAME='MOD10_L2.A2000065.0040.006.2016058071909.hdf'
SWATH_NAME='MOD_Swath_Snow'

file_id = sw.open(FILE_NAME, 'rdonly');
swath_id = sw.attach(file_id, SWATH_NAME);

DATAFIELD_NAME='NDSI_Snow_Cover'
data = sw.readField(swath_id, DATAFIELD_NAME, [], [], []);
lon = sw.readField(swath_id, 'Longitude', [], [], []);
lat = sw.readField(swath_id, 'Latitude', [], [], []);

sw.detach(swath_id);
sw.close(file_id);
The name of APIs is slightly different; sw is used instead of gd. As the function name implies, functions that contain sw are for swath, and the functions that contain gd are for grid. The above code reads one data field NDSI_Snow_Cover and two geo-location fields, Latitude and Longitude. Usually, a swath data contains both geo-location fields, and they provide longitude values and latitude values.

As the gd.readField function used to handle a grid file, the sw.readField function for the swath file in the above piece of code stores the data elements in the variable data. Now that lat and lon contain the latitude and longitude values respectively, one can pass those variables to contour function as the following arguments:

In this example, the first and second arguments of surfm are two-dimensional because both Longitude and Latitude are two-dimensional. Then, each element of data is mapped to each element of lat and lon.

You can see the complete code here. Figure 9 shows the result.

See Also

Reference

  1. Equirectangular projection. Wikipedia. http://en.wikipedia.org/wiki/Geographic_projection
  2. GDdefpixreg 2-123 HDF-EOS Library User's Guide for the EMD Project Volume 2. Raytheon Company
  3. 6.5.4 Projection Parameters HDF-EOS Library User's Guide for the EMD Project Volume 1. Raytheon Company


Last modified: 12/02/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