How to Download Modis Images Using R: An Operational Implementation With R studio
Modis Data Portal
The Moderate Resolution Imaging Spectroradiometer (or MODIS) consists of two satellites Terra (EOS AM) and Aqua (EOS PM). The two spacecrafts have been imaging the entire earth, every 1 to 2 days, since the year 2000. The instruments provide high temporal resolution data that shows dynamics in cloud cover and radiations in the atmosphere. The instruments are also able to detect changes in oceans and on land.
Modis data is widely used by scientists, researchers, ecologists, conservationists and even hobbyists. The data is crucial for many scientists across several research domains. From those interested in applications involving analysis of changes on land, to snow cover, to oceans and even the atmosphere.
The following operational script in R can be used to download these freely available gems of data. The script has been compiled for those who have, so far, found neither the time, nor opportunity to go deeply into the study of R as a programming language and environment for statistical computing and graphic, but who want results and who are keen to obtain MODIS data for their own applications.
The reader who would like to dig to the theoretical foundations of this is advised to read Babak Naimi and join pieces of scattered information from various websites.
How to Download Modis Data
For those who are quick to cut-and-paste, kindly ensure that you have the latest version of R-Studio before you proceed. The free and open-source integrated development environment (IDE) can be installed by following this link here. Please also note that to successfully run this script, you have to install the MODIS Reprojection Tool (MRT) in your computer. The MODIS Reprojection Tool is available for use by all registered users. So go ahead to register as a user on LP DAAC online site for archive and distribution.
MRT is designed to enable users to read data files in HDF-EOS format (MODIS Level-2G, Level-3, and Level-4 land data products). The different levels of product are referenced to a global tiling scheme in which each tile is approximately 10° latitude by 10° longitude and non-overlapping. Therefore, MRT has been developed to help users to specify a geographic subset or specific science data sets as input to processing, perform geographic transformation to a different coordinate system/cartographic projection, and write the output to file formats other than HDF-EOS.
Here is the complete functional script. Remember to enter “YOUR_USER_NAME” and PASSWORD and to modify YOUR_COMPUTER_PATH where you would like to save your work. Should you run into any errors, don’t hesitate to get in touch with us.
###DOWNLOADING MODIS GEMS### #required packages install.packages('raster') install.packages('rts') #MODIS DOWNLOAD PACKAGE ITSELF install.packages('RCurl') install.packages('devtools') library (raster) library (rts) library (RCurl) library (devtools) modisProducts( ) #display available MODIS information x="MOD13A1" #Creates an object with the selected MODIS Data of interest, in this case: Vegetation Indices 16-Day L3 Global 500m #Enter the website: https://search.earthdata.nasa.gov/contact_info and create an account with a particular username and password setNASAauth(username="YOUR_USER_NAME", password = "YOUR_PASSWORD") #specify your username and password setMRTpath("YOUR_COMPUTER_PATH/MRT/bin", update = TRUE) #Specify the path for the MRT software for mosaic images and re-projection. #Please Note: Even if you are not using mosaic/re-projection, you have to specify any executable software otherwise an error will show up. install_github('babaknaimi/rts') #Install last version of rts package from Git-hub, paramount to prevent another error ###Starting MODIS DOWNLOAD### setwd("YOUR_COMPUTER_PATH/MODIS/") #specify directory to save the Data ModisDownload(x=x,h=c(16,17),v=c(6,7), dates='2005.04.23', mosaic = FALSE, proj = FALSE, version = "006") #Download the MODIS with a particular TILE (e.g. h16, v6) and a particular version without mosaic (FALSE) and without re-projection (FALSE), you will obtain the raw .hdf images. #To re-project downloaded images you can use: reprojectHDF(hdfName = 'Mosaic_2005-04-23.hdf', filename = 'ex_2.tif', MRTpath = '/YOUR_COMPUTER_PATH/MRT/bin', proj_type = 'GEO', proj_params = '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ', bands_subset = '1 1 0 0 0 0 0 0 0 0 0 0', pixel_size = 0.00416667, UL="", LR="", resample_type= 'NEAREST_NEIGHBOR', datum = 'WGS84') #If you are re-projecting to geographical coordinates (proj_type = 'GEO'), pixel_size should be approximated in degrees. In this example, the 500 m MODIS images in Africa correspond to 0.00416667 degrees. #bands_subset specify which of the 12 spectral layers from the MODIS sensor you are interested to extract from the HDF images, in this case '1 1 0 0 0 0 0 0 0 0 0 0' is obtaining NDVI and EVI layers. #proj_params are different depending on the coordinate projection, for geographic projections all should be specified in 0. You can review any particular specification in the MRT tool manual appendix C: https://lpdaac.usgs.gov/sites/default/files/public/mrt41_usermanual_032811.pdf #You can directly download and reproject images with the ModisDownload function. As a result, you will have the mosaic and the .tif reprojected images in the same folder with all the raw .hdf files. ModisDownload(x=x,h=17,v=c(8,9),MRTpath = '/YOUR_COMPUTER_PATH/MRT/bin', dates='2005.04.23', version = "006",mosaic = TRUE, bands_subset = '1 1 0 0 0 0 0 0 0 0 0 0', proj = TRUE, proj_type = 'GEO',UL="", LR="", proj_params = '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ', pixel_size = 0.00416667,resample_type = 'NEAREST_NEIGHBOR') ###COMPLETE DOWNLOAD###