MATLAB provides APIs for accessing HDF4 data including HDF-EOS2. 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) and swath (SW) data. 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.
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_RnGd 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.
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.
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
.
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.
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.
You can see the complete code from here. Figure 6 shows the result.
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.
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:
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.