Download MODIS images with R
Actualizado: mar 7
The following is an operational implementation of the script developed and published by Babak Naimi, thus, further information can be found here.
Some important pieces of information and solutions to common errors are scattered in different websites; this script compile all that solutions into the present file.
Before fully using the script, you have to install the MODIS Reprojection Tool (MRT) in your system. A tutorial for MRT installation in MAC systems can be found here.
You can copy and paste the following script into R-studio/R and start downloading MODIS images.
Any suggestion or improvement would be highly appreciated.
MODIS DOWNLOAD SCRIPT:
library (raster) library (rts) library (RCurl) library (devtools)
#display available MODIS information
x="MOD13A1" #Creates an object with the selected MODIS of interest, in this case: Vegetation Indices 16-Day L3 Global 500m #More information on vegetation indexes products: https://modis.gsfc.nasa.gov/data/dataprod/mod13.php
#Enter the website: https://search.earthdata.nasa.gov/contact_info and create an account with a particular username and password
setNASAauth(username="danielromeroa", password = "OdetoJoy9.1")
#specify username and password for downloading with the R script
setMRTpath("/Users/daniel/Downloads/MRT/bin", update = TRUE) #Specify the path for the NASA MRT software for mosaic images and reprojection. #Even if you are not using mosaic/reprojection, you have to specify any executable software otherwise an error will show up. #If a different software from MRT was defined, to update for the correct path you should clear objects from the dataset and restart the script. #MRT installation guide for MAC can be found here:
#Install last version of rts package from Git-hub, paramount to prevent another error #More details on this error: https://gis.stackexchange.com/questions/260049/downloading-modis-products-with-r-package-rts/263996 #This updated should be installed only the first time using the script
####MODIS DOWNLOAD EXAMPLE#### setwd("/Users/daniel/Documents/ANTHRAX/GLOBAL_PROJECT/Envs/MODIS/")
#specify directory to save the MODIS
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 reprojection (FALSE), you will obtain the raw .hdf images.
#To reproject downloaded images you can use:
reprojectHDF(hdfName = 'Mosaic_2005-04-23.hdf', filename = 'ex_2.tif', MRTpath = '/Users/daniel/Downloads/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 reprojecting 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 = '/Users/daniel/Downloads/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')
#If you want to delete .hdf files after reprojection you can automatically do it with 'delete = TRUE'.
ModisDownload(x=x,h=16,v=c(8,9),MRTpath = '/Users/daniel/Downloads/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', delete = TRUE)
#More examples can be found using ?ModisDownload
#Or visiting: http://r-gis.net/?q=ModisDownload