Header

Import MODIS data in GRASS using r.in.gdal

Author: Paulo van Breugel
Updated on: 2016-05-26

Introduction

NASA offers access to its MODIS data sets through Reverb|Echo. MODIS (or Moderate Resolution Imaging Spectroradiometer) is a key instrument aboard the Terra (originally known as EOS AM-1) and Aqua (originally known as EOS PM-1) satellites. These satellites cover the entire Earth's surface every 1 to 2 days, acquiring data in 36 spectral bands, or groups of wavelengths (see MODIS Technical Specifications). The data is useful to study or monitor global dynamics and processes occurring on the land, in the oceans, and in the lower atmosphere.

The data comes in HDF format and uses the Sinusoidal grid tiling system. HDF4 (also known as HDF) is a library and multi-object file format for storing and managing data between machines. There are two versions of HDF technologies that are completely different: HDF4 and HDF5. HDF4 is the first HDF format. If your gdal is compiled with HDF4 support (use ./configure –with-hdf4), you can use gdal, or any software that uses gdal, to open the downloaded MODIS tiles directly. For example in QGIS as explained here or in GRASS GIS. In GRASS you can use the r.in.gdal function.

Preparation

The first step is to create a location and mapset with the Sinusoidal projection used for the MODIS data. The proj4 definition is +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs. Or you can create a new location and mapset using the MODIS Sinusoidal GIS shape file (Created by Markus Neteler and made available under the CC-BY-SA license).

The next, tricky, part is that HDF4 data files can contain multiple data arrays or bands and we need to specify which of these bands we want to extract. You can use the gdalinfo function for that.

gdalinfo MOD14A1.A2012313.h22v10.hdf
...
Subdatasets:
  SUBDATASET_1_NAME=HDF4_EOS:EOS_GRID: "MOD14A1.A2012313.h22v10.hdf":MODIS_Grid_Daily_Fire:FireMask
  SUBDATASET_1_DESC=[8x1200x1200] FireMask MODIS_Grid_Daily_Fire (8-bit unsigned integer)
  SUBDATASET_2_NAME=HDF4_EOS:EOS_GRID: "MOD14A1.A2012313.h22v10.hdf":MODIS_Grid_Daily_Fire:QA
  SUBDATASET_2_DESC=[8x1200x1200] QA MODIS_Grid_Daily_Fire (8-bit unsigned integer)
  SUBDATASET_3_NAME=HDF4_EOS:EOS_GRID: "MOD14A1.A2012313.h22v10.hdf":MODIS_Grid_Daily_Fire:MaxFRP
  SUBDATASET_3_DESC=[8x1200x1200] MaxFRP MODIS_Grid_Daily_Fire (32-bit integer)
  SUBDATASET_4_NAME=HDF4_EOS:EOS_GRID: "MOD14A1.A2012313.h22v10.hdf":MODIS_Grid_Daily_Fire:sample
  SUBDATASET_4_DESC=[8x1200x1200] sample MODIS_Grid_Daily_Fire (16-bit unsigned integer)
...

In the example above, only the relevant part of the output of gdalinfo is given; the bands included and their names. The actual information is much longer so you’ll have to look through the output to find the relevant section. To import one of these bands in GRASS, you need the name as specified by gdalinfo.

Importing the data

For example, you have the file MOD14A1.A2012313.h22v10.hdf and you want to extract the FireMask band. That means you need to name of the first band, which is “HDF4_EOS:EOS_GRID:”MOD14A1.A2012313.h22v10.hdf”:MODIS_Grid_Daily_Fire:FireMask”

You can do this manually, but that can become a bit tedious. Easier is to use the command line utilities grep and sed, like in the example below. It is a little bit more involving. However, the advantage is that it is relatively easy to create a script based on these few lines for batch importing data.

inputmap=`gdalinfo MOD14A1.A2003097.h21v07.hdf | grep 'SUBDATASET_1_NAME=' | sed -e 's/SUBDATASET_1_NAME=//'`
outputmap=`echo $inputmap |  awk -F\" '{ print $2 }' | awk -F. '{ print $1"_"$2"_"$3 }' | sed -e 's/A//'`
r.in.gdal $inputmap out=$outputmap

The first line extracts the name of SUBDATASET_1_NAME. The second creates the name of the imported layer in your GRASS database (MOD141_A2003097_h21v07 in the example above). The third line is the actual r.in.gdal command, which imports the first band of the file in the current mapset.

Additional information

Check also out the GRASS GIS Wiki, where you can find a few alternative ways to import the MODIS data layers. There is furthermore the r.modis addon, a toolset to import MODIS satellite data into GRASS GIS. You might also be interested to read this article on spatial-analyst.net for tools to download MODIS data in R. And of course, NASA offers the Modis Reprojection Tool (MRT), to reproject and mosaic the MODIS hdf data tiles.