R is a free software environment for statistical computing and graphics. It has a rich set of packages that can access NASA HDF products. To access HDF, you can use 3 different R packages.
You can download the latest R from here. You will need the following ncdf4 R source package to build with the custom libraries that enhance the access of NASA HDF products.
rgdal and hdf5r package will work without source customization so you don't have to download R source packages.
Package installation is case-sensitive.
The default installation of RNetCDF doesn't support reading HDF4.
For example, if you install R using apt-get install r-base
,
Ubuntu installs libnetcdf19
package.
You must remove the libnetcdf19
package first.
Installing a custom netCDF with HDF4 support is not simple.
Thus, we provide a complete GitHub Action workflow file.
Please follow the steps in the workflow.
The default installation of ncdf4 doesn't support reading HDF4 because netCDF library is not built with HDF4 support. For example, if you open an HDF4 file, you will get the following error message.
In such case, you can build netCDF library with --enable-hdf4
configuration from source and build ncdf4 R package from source. If --enable-hdf4
configuration option fails when you configure netCDF-C library, try to set the following environment variable options assuming that both HDF4 and JPEG libraries are installed under /usr/local.
To maximize the usability of NASA HDF products, we recommend you to use a special GDAL library called GEE from NASA. Please read carefully how to build GDAL with HDF4 support. Once you installed the GEE, build and install rgdal from source package by issuing the following command at the R prompt.
At the R prompt, run the following command to install binary package.
On Unix systems, hdf5r package may be installed from source after compilation. hdf5r package is based on HDF5 C++ bindings. Thus, make sure that your HDF5 library is built with --enable-cxx
configuration option.
We provide a complete sample R code example and plot on GitHub.
To use ncdf4, the first step is to include the package.
To open a NASA HDF file, use nc_open()
call with path to the file name.
To list available datasets in HDF file, type the assigned variable name.
To retrieve data from dataset, use ncvar_get()
.
If you access HDF4 dataset, it is necessary to change endianness using readBin()
because ncdf4 doesn't do it for you. You don't need the following conversion for HDF5 dataset.
To retrieve the value of attribute of a dataset, use ncatt_get()
.
To use rgdal, the first step is to include the package and other geospatial helper packages.
To open a NASA HDF file, use get_subdatasets()
with path to the file name.
To list available datasets in HDF file, type the assigned variable name.
To retrieve data from dataset, use readGDAL()
.
The RASTERXDIM, ..., RASTER4DIM options allow you to access 5-dimensional dataset and they are available only in GEE. If you use regular GDAL, you cannot access the dataset correctly.
Once data is retrieved correctly using GEE, you can assign it as a raster image and manipulate data for visualization and analysis.
To use R hdf5r package, the first step is to include the package.
To open a NASA HDF file, use h5file()
with path to the file name.
To list available datasets in HDF5 file, type the assigned variable name.
To retrieve data from dataset in a group, use readDataSet()
.
To retrieve the value of attribute of a dataset, use h5attr()
.
R provides rich graphics packages for data visualization. There are three common feature types in HDF-EOS - grid, swath, and point. We will provide a few examples of visualizing them.
To plot grid data like figure below, you can follow the AIRS L3 comprehensive example.
To plot swath data like figure below, you can follow the AIRS L2 comprehensive example.
To plot point data using symbols like figure below, you can follow the OCO2 comprehensive example.
To export the contents of a data frame (e.g., vals
in hdf5r example) to a CSV file, you can use the following code.