genString.R 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # Author: Matteo Mattiuzzi, [email protected]
  2. # Date : February 2012
  3. # 'date' is the date of an existing file! result from getStruc() and passed as single date! For format see ?transDate
  4. genString <- function(x, collection=NULL, date=NULL, what="images", local=TRUE, remote=TRUE,
  5. opts = NULL, ...)
  6. {
  7. if (is.null(opts))
  8. opts <- combineOptions(checkTools = FALSE, ...)
  9. product <- getProduct(x=x,quiet=TRUE)
  10. if(length(product$PRODUCT)>1)
  11. {
  12. warning("genString() does not support multiple products! Generating 'path' for the first product: ", product$PRODUCT[1], "\n")
  13. product <- lapply(product,function(x){x[1]}) # take only the first argument
  14. }
  15. product$CCC = if (is.null(collection)) {
  16. unlist(getCollection(product = product$PRODUCT, checkTools = FALSE))
  17. } else {
  18. sprintf("%03d",as.numeric(unlist(collection)[1]))
  19. }
  20. if (!is.null(date))
  21. {
  22. product$DATE <- list(paste0("A",transDate(date)$beginDOY)) # generates MODIS file date format "AYYYYDDD"
  23. }
  24. ## if options have not been passed down, create them from '...'
  25. opts$auxPath <- setPath(opts$auxPath)
  26. remotePath <- localPath <- NULL
  27. if (is.null(product$DATE)) # if x is a PRODUCT and date is not provided
  28. {
  29. if (local)
  30. {
  31. tempString <- strsplit(opts$arcStructure,"/")[[1]]
  32. string <- list()
  33. l=0
  34. for (i in 1:length(tempString))
  35. {
  36. s <- strsplit(tempString[i],"\\.")[[1]]
  37. if (length(s)>0)
  38. {
  39. tmp <- list()
  40. for (u in 1:length(s))
  41. {
  42. if (s[u] %in% c("DATE","YYYY","DDD"))
  43. {
  44. tmp[[u]] <- s[u]
  45. } else
  46. {
  47. tmp[[u]] <- getPart(x=product,s[u])
  48. }
  49. }
  50. if (length(tmp)>0)
  51. {
  52. l=l+1
  53. string[[l]] <- paste0(unlist(tmp),collapse=".")
  54. }
  55. }
  56. }
  57. localPath <- setPath(path.expand(paste0(opts$localArcPath,paste0(unlist(string),collapse="/")))
  58. , mkdir = FALSE)
  59. }
  60. if (remote)
  61. {
  62. namesFTP <- names(MODIS_FTPinfo)
  63. Hmany <- grep(namesFTP,pattern="^ftpstring*.")
  64. remotePath <- list()
  65. n = 0
  66. for (e in Hmany)
  67. {
  68. stringX <- MODIS_FTPinfo[[e]]
  69. if(length(grep(product$SOURCE,pattern=stringX$name))>0 & what %in% stringX$content)
  70. {
  71. n=n+1
  72. if(is.null(stringX$variablepath))
  73. {
  74. remotePath[[n]] <- stringX$basepath
  75. } else
  76. {
  77. struc <- stringX$variablepath
  78. tempString <- strsplit(struc,"/")[[1]]
  79. string <- list()
  80. l=0
  81. for (i in 1:length(tempString))
  82. {
  83. s <- strsplit(tempString[i],"\\.")[[1]]
  84. if (length(s)> 0)
  85. {
  86. l=l+1
  87. tmp <- list()
  88. for (u in 1:length(s))
  89. {
  90. if (s[u] %in% c("DATE","YYYY","DDD"))
  91. {
  92. tmp[[u]] <- s[u]
  93. } else
  94. {
  95. tmp[[u]] <- getPart(x=product,s[u])
  96. }
  97. }
  98. string[[l]] <- paste0(unlist(tmp),collapse=".")
  99. ## append '_MERRAGMAO' if product is hosted at NTSG
  100. if ("NTSG" == stringX$name & i == 2)
  101. string[[l]] <- gsub("(\\.){1}(\\d){3}$", ".105_MERRAGMAO"
  102. , string[[l]])
  103. }
  104. }
  105. remotePath[[n]] <- path.expand(paste(stringX$basepath,paste0(unlist(string),collapse="/"),sep="/"))
  106. }
  107. names(remotePath)[n] <- stringX$name
  108. }
  109. }
  110. }
  111. } else
  112. { # if x is a file name
  113. if (local)
  114. {
  115. tempString <- strsplit(opts$arcStructure,"/")[[1]]
  116. string <- list()
  117. l=0
  118. for (i in 1:length(tempString))
  119. {
  120. s <- strsplit(tempString[i],"\\.")[[1]]
  121. if (length(s)>0)
  122. {
  123. l=l+1
  124. tmp <- list()
  125. for (u in seq_along(s))
  126. {
  127. tmp[[u]] <- getPart(x=product,s[u])
  128. }
  129. string[[l]] <- paste0(unlist(tmp),collapse=".")
  130. }
  131. }
  132. localPath <- setPath(path.expand(paste0(opts$localArcPath,paste0(unlist(string),collapse="/"))))
  133. }
  134. if (remote)
  135. {
  136. if (!what %in% c("images","metadata"))
  137. {
  138. stop("Parameter 'what' must be 'images' or 'metadata'")
  139. }
  140. namesFTP <- names(MODIS_FTPinfo)
  141. Hmany <- grep(namesFTP,pattern="^ftpstring*.") # get ftpstrings in ./MODIS_Opts.R
  142. remotePath <- list()
  143. n = 0
  144. for (e in Hmany)
  145. {
  146. stringX <- MODIS_FTPinfo[[e]]
  147. if(length(grep(product$SOURCE,pattern=stringX$name))>0 & what %in% stringX$content)
  148. {
  149. struc <- stringX$variablepath
  150. tempString <- strsplit(struc,"/")[[1]]
  151. string <- list()
  152. l=0
  153. for (i in 1:length(tempString))
  154. {
  155. s <- strsplit(tempString[i],"\\.")[[1]]
  156. if (length(s)>0)
  157. {
  158. l=l+1
  159. tmp <- list()
  160. for (u in seq_along(s))
  161. {
  162. tmp[[u]] <- getPart(x=product,s[u])
  163. }
  164. string[[l]] <- paste0(unlist(tmp),collapse=".")
  165. ## if working on NTSG server
  166. if ("NTSG" == stringX$name) {
  167. # add '_MERRAGMAO' suffix
  168. if (i == 2)
  169. string[[l]] <- gsub("(\\.){1}(\\d){3}$", ".105_MERRAGMAO"
  170. , string[[l]])
  171. # add leading 'Y' to year
  172. if (i == 3)
  173. string[[l]] <- paste0("Y", string[[l]])
  174. # add leading 'D' to day of year
  175. if (i == 4)
  176. # MOD16A2
  177. if (product$PRODUCT == "MOD16A2")
  178. string[[l]] <- paste0("D", string[[l]])
  179. else
  180. string[[l]] <- ""
  181. }
  182. }
  183. }
  184. n=n+1
  185. remotePath[[n]] <- path.expand(paste(stringX$basepath,paste0(unlist(string),collapse="/"),sep="/"))
  186. names(remotePath)[n] <- stringX$name
  187. }
  188. }
  189. }
  190. }
  191. return(list(localPath=correctPath(localPath), remotePath=remotePath))
  192. }