Beginners Guide: Using MODIS in R

This tutorial is going to assume that you are a beginner in  R and Windows and working with MODIS. Obviously, if you already know R and Windows pretty well you can skip a bunch of steps. However, it will be good for you to walk through all the steps as I may do some things in the beginning that influence downstream work.   As an overview here is what we are going to do. First we are going to update our R tools and make sure we have the fresh bits. Then we are going to spend some time getting familiar with the bits of the windows system that matter. Basically the console. If you don’t know how to build a package in R under windows, go walk through my package building tutorial.

Then we are going to download some MODIS data by hand and get some tools and utilities for working with MODIS data. If you just have a small job sometime it may be better to work “by hand”.  At the end we will start using the MODIS package instead of doing things by hand. In very simple terms the MODIS package allows you to find and download MODIS data using “R” commands. More importantly, it allows you to control the tools, MTR and GDAL, from within R. So, first we do some things by hand downloading a file, using the tools by hand, and then we will use R programming to do the same thing. Trust me, when you have hundreds of MODIS files to download reproject and crop, working by hand is a PITA.

First things first however. I want to thank the MODIS R team for all the hard work they have done.

Matteo Mattiuzzi, Jan Verbesselt, Forrest Stevens, Tomislav Hengl, Anja Klisch, Bradley Evans and Agustin Lobo. Conrats on a great package

UPDATE R

While it’s not necessary to start  your process of building a package by updating your version of R to the current version, I started my process that way.  Ordinarily I like to stay current with R and with all my tools.  For this example I will be using version 2.15.1  The exact url for version 2.15.1 is

http://cran.r-project.org/bin/windows/base/R-2.15.1-win.exe

Its best to use this and actually see what version we are on

http://cran.r-project.org/bin/windows/base/

GET RSTUDIO (optional)

In addition to using R I also use RStudio which is a great cross platform IDE. I recommend it use.  After updating my R I updated my RStudio from the following    URL

I’m using version .96.331

When you have finished that it’s time to do a little review of your Windows System.  For some of the tools we will need  updating the PATH variables in windows may be required. If you never worked back in DOS and the early days of windows you may not be familiar with the PATH variable.

In order to build a package in R there are a couple things you need to understand about your Windows System.  These are fairly trivial but if your coming from a different platform (like MAC) then you might not know where these things are. I will take  some time to take you to a couple places on your Windows system. There are two bits that we are going to get familiar with:  The console application, and the PATH variable.

First , we are going to locate the console application. You find this application by going to your start button in the lower left hand corner of your desktop

COMMAND PROMPT

Open the start button select “All Programs” –>”Accessories” –>”Command Prompt”.  The old reliable “DOS” Prompt.

PATH

The bit of the windows we may have to track down is the PATH Variable. Again, this is an environmental variable that the windows system uses to locate programs and insure that commands get interpreted correctly. To locate your PATH variable Start by clicking on the Start button. At the start panel select the control panel. 

Next Select system and security

Next select System

Next on your left select Advanced system settings

At the bottom select Environmental Variables

In the lower Panel select the “Path Variable” and select Edit

That is how you will access your path variable when it comes time to edit it.   At this stage I would copy the PATH  variable and paste it to a simple txt document. Just in case we screw anything up and we need to recover. What we will be doing is adding various elements to that string and restarting the computer. So, make a copy now before we start. If you have none completed my tutorial on building windows packages I’ll suggest you do that now

The next step is to locate and download some sample MODIS data. later we will do this step in an automated fashion from inside R, but to gain some understanding of the external tools we will be calling from within R we will start by doing a few things manually.

Next step is here

Advertisement
  1. Barry Lesht
    November 26, 2012 at 5:05 AM

    Hi – great idea. Will the package include a facility for general hdf file access using R commands? That is, will it be possible to query the scientific data sets and attributes in the hdf files by name, etc?

    • Steven Mosher
      November 26, 2012 at 5:15 AM

      There is a facilitity for reading the HDf files and getting the SDS
      so, I have a file named “o” that is an hdf file. I use getSds(o)
      When you are running gdal, then you can specificy which SDS you want by a bit code

      getSds(HdfName=o)
      $SDSnames
      [1] “LST_Day_1km” “QC_Day” “Day_view_time”
      [4] “Day_view_angl” “LST_Night_1km” “QC_Night”
      [7] “Night_view_time” “Night_view_angl” “Emis_31”
      [10] “Emis_32” “Clear_day_cov” “Clear_night_cov”

      $SDS4gdal
      [1] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:LST_Day_1km”
      [2] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:QC_Day”
      [3] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Day_view_time”
      [4] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Day_view_angl”
      [5] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:LST_Night_1km”
      [6] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:QC_Night”
      [7] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Night_view_time”
      [8] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Night_view_angl”
      [9] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Emis_31″
      [10] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Emis_32″
      [11] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Clear_day_cov”
      [12] “HDF4_EOS:EOS_GRID:\”C:\\Users\\steve\\DOCUME~1\\MODIS_~1\\MODIS\\MOD11A1.005\\201001~1.01\\MOD11A~1.HDF\”:MODIS_Grid_Daily_1km_LST:Clear_night_cov”

  2. December 3, 2012 at 10:25 AM

    nice tutorial 🙂

  3. marcelo
    January 15, 2014 at 7:58 PM

    Great tutorial!

    I have not been able to extract a band from a MODIS MOD13C2 file in R and wonder if you can help me with that. This seems to work:

    mod<-getSds(HdfName="MOD13C2.A2002152.005.2007180044416.hdf")
    mod$SDSnames

    but when I try to access a specif band it does not work;

    raster(readGDAL(mod$SDS4gdal[2]))

    it returns this messasge:
    Error in raster(readGDAL(ww$SDS4gdal[2])) :
    error in evaluating the argument 'x' in selecting a method for function 'raster': Error in .local(.Object, …) :
    `C:\Users\M\Google Drive\NASA Maps\HDF4_EOS:EOS_GRID:"MO6356~1.HDF":MOD_Grid_monthly_CMG_VI:CMG 0.05 Deg Monthly EVI' does not exist in the file system,
    and is not recognised as a supported dataset name

    I dont now if that is related with the fact that this files are not tiles but CMGs

    Any help would be much appreciated!

    Marcelo

  4. marcelo
    January 15, 2014 at 7:58 PM

    Great tutorial!
    I have not been able to extract a band from a MODIS MOD13C2 file in R and wonder if you can help me with that. This seems to work:
    mod<-getSds(HdfName="MOD13C2.A2002152.005.2007180044416.hdf")
    mod$SDSnames
    but when I try to access a specif band it does not work;
    raster(readGDAL(mod$SDS4gdal[2]))
    it returns this messasge:
    Error in raster(readGDAL(ww$SDS4gdal[2])) :
    error in evaluating the argument 'x' in selecting a method for function 'raster': Error in .local(.Object, …) :
    `C:\Users\M\Google Drive\NASA Maps\HDF4_EOS:EOS_GRID:"MO6356~1.HDF":MOD_Grid_monthly_CMG_VI:CMG 0.05 Deg Monthly EVI' does not exist in the file system,
    and is not recognised as a supported dataset name
    I dont now if that is related with the fact that this files are not tiles but CMGs
    Any help would be much appreciated!
    Marcelo

  1. October 8, 2012 at 4:42 AM
  2. November 25, 2012 at 10:42 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: