目前下载MODIS数据的工具功能不够完整,比如先要下载500米分辨率的LAI数据集MOD15A2H, 无论是MODIS官网提供的网页下载工具daac2disk,还是R论坛提供的ftp协议下载包http://r-gis.net/?q=ModisDownload, 都不能下载MOD15A2H。 这里展示使用R下载http协议的MODIS数据,有兴趣的可以自己研究一下,贴出全部代码:
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 = fmode,method = fmethod) }, error = function(cond) { print("error") } ) } #下载数据的地址 MOD15A2H006 <- "http://e4ftl01.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(lapply(strsplit(items, "/ "), function(x){x[1]})) dates <- unlist(lapply(strsplit(items, "/ "), function(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<- getURL(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()
时间: 2024-10-06 23:07:16