getStruc.R 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # Author: Matteo Mattiuzzi, Florian Detsch
  2. # Date: January 2018
  3. getStruc <- function(product, collection = NULL, server = NULL, begin = NULL
  4. , end = NULL, forceCheck = FALSE, ...)
  5. {
  6. opts <- combineOptions(...)
  7. sturheit <- stubborn(level = opts$stubbornness)
  8. if (is.null(server)) {
  9. server = opts$MODISserverOrder[1]
  10. }
  11. setPath(opts$auxPath, ask=FALSE)
  12. #########################
  13. # Check Platform and product
  14. inp = product
  15. product <- getProduct(x=product,quiet=TRUE)
  16. if (is.null(product)) {
  17. stop("Product '", inp, "' not recognized. See getProduct() for a list of "
  18. , "available products.")
  19. } else rm(inp)
  20. # Check collection
  21. product$CCC = getCollection(product = product, collection = collection
  22. , forceCheck = forceCheck)
  23. dates <- transDate(begin=begin,end=end)
  24. todoy <- format(as.Date(format(Sys.time(),"%Y-%m-%d")),"%Y%j")
  25. ########################
  26. # load aux
  27. col <- product$CCC[[1]]
  28. basnam <- paste0(product$PRODUCT[1],".",product$CCC[[1]],".",server)
  29. info <- list.files(path=opts$auxPath,pattern=paste0(basnam,".*.txt"),full.names=TRUE)[1]
  30. output <- list(dates=NULL,source=server,online=NA)
  31. class(output) <- "MODISonlineFolderInfo"
  32. ## no local structure
  33. if (is.na(info)) {
  34. getIT <- TRUE
  35. online <- TRUE
  36. ## if local structure exists, check if
  37. } else {
  38. lastcheck <- as.Date(strsplit(basename(info),"\\.")[[1]][4],"%Y%j")
  39. output$dates <- na.omit(as.Date(read.table(info,stringsAsFactors=FALSE)[,1]))
  40. # end date in local structure is younger than user-defined end date
  41. if (max(output$dates,na.rm=TRUE) > dates$end) {
  42. getIT <- FALSE
  43. online <- "up-to-date"
  44. } else {
  45. # last check is older than 24 hours
  46. if (lastcheck < as.Date(todoy,"%Y%j")) {
  47. getIT <- TRUE
  48. online <- TRUE
  49. # last check is not older than 24 hours, but end date in local structure
  50. # is older than user-defined end date
  51. } else {
  52. getIT <- FALSE
  53. online <- "up-to-date"
  54. }
  55. }
  56. }
  57. if (getIT | forceCheck)
  58. {
  59. lockfile <- paste0(opts$auxPath, basnam,".lock")[[1]]
  60. if(file.exists(lockfile))
  61. {
  62. if(as.numeric(Sys.time() - file.info(lockfile)$mtime) > 10)
  63. {
  64. unlink(lockfile)
  65. } else
  66. {
  67. readonly <- TRUE
  68. }
  69. } else
  70. {
  71. zz <- file(description=lockfile, open="wt") # open an output file connection
  72. write('deleteme',zz)
  73. close(zz)
  74. readonly <- FALSE
  75. on.exit(unlink(lockfile))
  76. }
  77. path <- genString(x=product$PRODUCT[1], collection=col, local=FALSE)
  78. cat("Downloading structure on '",server,"' for: ",product$PRODUCT[1],".",col,"\n",sep="")
  79. if(exists("FtpDayDirs"))
  80. {
  81. rm(FtpDayDirs)
  82. }
  83. if (server=="LPDAAC")
  84. {
  85. startPath <- strsplit(path$remotePath$LPDAAC,"DATE")[[1]][1] # cut away everything behind DATE
  86. for (g in 1:sturheit)
  87. {
  88. cat("Try:",g," \r")
  89. FtpDayDirs <- try(filesUrl(startPath))
  90. cat(" \r")
  91. if(exists("FtpDayDirs"))
  92. {
  93. break
  94. }
  95. Sys.sleep(opts$wait)
  96. }
  97. FtpDayDirs <- as.Date(as.character(FtpDayDirs),"%Y.%m.%d")
  98. } else if (server=="LAADS")
  99. {
  100. startPath <- strsplit(path$remotePath$LAADS,"YYYY")[[1]][1] # cut away everything behind YYYY
  101. opt <- options("warn")
  102. options("warn"=-1)
  103. rm(years)
  104. once <- TRUE
  105. for (g in 1:sturheit)
  106. {
  107. cat("Downloading structure from 'LAADS'-server! Try:",g,"\r")
  108. years <- try(filesUrl(startPath))
  109. years <- as.character(na.omit(as.numeric(years))) # removes folders/files probably not containing data
  110. if(g < (sturheit/2))
  111. {
  112. Sys.sleep(opts$wait)
  113. } else
  114. {
  115. if(once & (30 > opts$wait)) {cat("Server problems, trying with 'wait=",max(30,opts$wait),"\n")}
  116. once <- FALSE
  117. Sys.sleep(max(30,opts$wait))
  118. }
  119. if(exists("years"))
  120. {
  121. break
  122. }
  123. cat(" \r")
  124. }
  125. options("warn"=opt$warn)
  126. Ypath <- paste0(startPath,years,"/")
  127. ouou <- vector(length=length(years),mode="list")
  128. for(ix in seq_along(Ypath))
  129. {
  130. cat("Downloading structure of '",years[ix],"' from '",server,"'-server. \r",sep="")
  131. ouou[[ix]] <- paste0(years[ix], filesUrl(Ypath[ix]))
  132. }
  133. cat(" \r")
  134. FtpDayDirs <- as.Date(unlist(ouou),"%Y%j")
  135. } else if (server == "NTSG") {
  136. startPath <- strsplit(path$remotePath$NTSG,"YYYY")[[1]][1] # cut away everything behind YYYY
  137. opt <- options("warn")
  138. options("warn"=-1)
  139. once <- TRUE
  140. for (g in 1:sturheit) {
  141. cat("Downloading structure from 'NTSG'-server! Try:", g, "\n")
  142. years <- try(filesUrl(startPath), silent = TRUE)
  143. if (!inherits(years, "try-error")) {
  144. years_new <- gsub("^Y", "", years)
  145. break
  146. } else {
  147. if (g < (sturheit/2)) {
  148. Sys.sleep(opts$wait)
  149. } else {
  150. if (once & (30 > opts$wait)) {
  151. cat("Encountering server problems, now trying with 'wait = 30'...\n")
  152. }
  153. once <- FALSE
  154. Sys.sleep(max(30,opts$wait))
  155. }
  156. }
  157. }
  158. options("warn"=opt$warn)
  159. if (product$PRODUCT != "MOD16A3") {
  160. Ypath <- paste0(startPath,years,"/")
  161. ouou <- vector(length=length(years),mode="list")
  162. cat("Downloading structure from", years_new[1], "to", years_new[length(years_new)], "from", server, "file server.\n")
  163. for(ix in seq_along(Ypath))
  164. {
  165. ouou[[ix]] <- paste(years[ix], filesUrl(Ypath[ix]), sep = "/")
  166. }
  167. cat(" \r")
  168. FtpDayDirs <- as.Date(unlist(ouou),"Y%Y/D%j")
  169. ## if product is 'MOD16A3', no daily sub-folders exist
  170. } else {
  171. FtpDayDirs <- as.Date(paste(years_new, "12", "31", sep = "-"))
  172. }
  173. }
  174. }
  175. if(!exists("FtpDayDirs"))
  176. {
  177. if (online == "up-to-date") {
  178. cat("Local structure is up-to-date. Using offline information!\n")
  179. output$online <- TRUE
  180. } else {
  181. cat("Couldn't get structure from", server, "server. Using offline information!\n")
  182. output$online <- FALSE
  183. }
  184. } else if (FtpDayDirs[1]==FALSE)
  185. {
  186. cat("Couldn't get structure from", server, "server. Using offline information!\n")
  187. output$online <- FALSE
  188. } else
  189. {
  190. output$dates <- FtpDayDirs
  191. output$online <- TRUE
  192. }
  193. if (exists("readonly")) {
  194. if(!readonly)
  195. {
  196. unlink(list.files(path=opts$auxPath, pattern=paste0(basnam,".*.txt"), full.names=TRUE))
  197. unlink(lockfile)
  198. write.table(output$dates, paste0(opts$auxPath,basnam,".",todoy,".txt"), row.names=FALSE, col.names=FALSE)
  199. }
  200. }
  201. return(output)
  202. }