Currently, the tools for downloading MODIS data are not fully functional, such as the first download of the 500-meter-resolution Lai DataSet mod15a2h, whether it is the Web download tool Daac2disk provided by the MODIS website, or the FTP protocol download provided by the R forum http. R-gis.net/?q=modisdownload, can not download mod15a2h. This shows the MODIS data using the R download HTTP protocol, which is interesting to study and post all the code:
RM (list = ls ()) setwd ("f:/modis/mod15a2h006/") library (Rcurl) library (Rgdal)
#去除网页Html标记cleanFun <-Function (htmlstring) {return (Gsub ("<.*?>", "", Htmlstring)} #R的防错机制safe_download <-function (Furl, Fsave, Fmode, Fmethod) {TryCatch ({download.file) (url = furl,destfile = Fsave,mode = Fmod E,method = fmethod)}, error = function (cond) {print ("error")})} #下载数据的地址MOD15A2H006 <-"Http://e4 ftl01.cr.usgs.gov/molt/mod15a2h.006/"gg<-GetURL (mod15a2h006) gg<-cleanfun (GG) Items <-strsplit (GG,"-\ n ") ) [[1]]items<-items[-1]items<-items[-1* nrow (items)]pp<-strsplit (items, "/") dirs <-unlist (Lapp Ly (Strsplit (Items, "/"), function (x) {x[1]})) Dates <-unlist (lapply (Strsplit (Items, "/"), Func tion (x) {x[2]})) dirsurl<-paste (mod15a2h006, dirs, "/", sep= "") #记录日志
Sink (file = "Log.log", split = T) for (I in 1:length (Dirsurl)) {print (dirsurl[i]) dirurl<-Dirsurl[i] files<-Getur L (Dirurl) files<-cleanfun (Files) BB <-strsplit (Files, "\ n") [[1]] bb<-bb[-1] bb<-bb[-1*nrow (BB)] hh <-unlist (lapply (Strsplit (BB, ""), function (x) {x[[1]]})) hdffile<-Hh[grep (hh, pattern= "mod15a2h.*.h26v05.*. HDF ") [1]] gurl = Paste (Dirurl, Hdffile," ", sep=" ") Gsave = Paste (GETWD (),"/", Hdffile, sep=" ") gmode= ' WB ' gmethod= ' WinInet
#开始下载 safe_download (furl = gurl, Fsave = gsave, Fmode = gmode, Fmethod = gmethod) print (gurl)}sink ()
R:modis Original Data download