delHdf.R 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #' Delete Local MODIS Grid Files
  2. #'
  3. #' @description
  4. #' Delete MODIS grid files to reduce the local storage.
  5. #'
  6. #' @param product \code{character}, see \code{\link{getProduct}}.
  7. #' @param collection \code{character} or \code{integer}, see
  8. #' \code{\link{getCollection}}.
  9. #' @param extent Extent information, defaults to \code{'global'}. See
  10. #' \code{\link{getTile}}.
  11. #' @param tileH \code{numeric} or \code{character}. Horizontal tile number,
  12. #' see \code{\link{getTile}}.
  13. #' @param tileV \code{numeric} or \code{character}. Vertical tile number(s),
  14. #' see \code{tileH}.
  15. #' @param begin \code{character}. Begin date of MODIS time series, see
  16. #' \code{\link{transDate}} for formatting.
  17. #' @param end Same for end date.
  18. #' @param ask \code{logical}. If \code{TRUE} (default), the user is being asked
  19. #' for deletion after checking.
  20. #' @param ... Arguments passed to \code{\link{MODISoptions}}, particularly
  21. #' \code{localArcPath}.
  22. #'
  23. #' @author
  24. #' Matteo Mattiuzzi
  25. #'
  26. #' @examples
  27. #' \dontrun{
  28. #'
  29. #' # YOU ARE ASKED TO CONFIM THE DELETION! BUT IF THE EXAMPLE THIS FOR YOU SENSITIVE DATA CHANGE IT!
  30. #'
  31. #' # REMOVE "MYD11A2" from specific date range and area subset:
  32. #' # delHdf(product="MYD11A2",begin="2010001",end="2010.02.01",extent="austria")
  33. #' # or
  34. #' # delHdf(product="MYD11A2",begin="2010001",end="2010.02.01",tileV=18:19,tileH=4)
  35. #'
  36. #' # REMOVE "MOD11A2" and "MYD11A2" from specific date range but globaly:
  37. #' # delHdf(product="M.D11A2",begin="2010001",end="2010.02.01")
  38. #'
  39. #' # REMOVE ALL "MOD11A2" from local archive:
  40. #' # delHdf(product="MOD11A2")
  41. #' }
  42. #'
  43. #' @export delHdf
  44. #' @name delHdf
  45. delHdf <- function(product, collection=NULL, extent="global", tileV=NULL, tileH=NULL, begin=NULL, end=NULL, ask=TRUE,...)
  46. {
  47. if (!ask)
  48. {
  49. doit <- "Y"
  50. }
  51. summaries <- 0
  52. opts <- combineOptions(...)
  53. # product/dates/extent
  54. product <- getProduct(x=product,quiet=TRUE)
  55. product$CCC <- getCollection(product=product,collection=collection,quiet=TRUE)
  56. info <- list()
  57. for (z in seq_along(product$PRODUCT))
  58. {
  59. info[[z]] <- paste0(product$PRODUCT[z],".",product$CCC[[which(names(product$CCC)==product$PRODUCT[z])]])
  60. }
  61. cat("\nYou are about to delete\n - products:", paste(unlist(info),collapse=", "),"\n")
  62. rm(info)
  63. if (!is.null(tileV)&!is.null(tileH))
  64. {
  65. ext <- getTile(tileV=tileV,tileH=tileH)
  66. } else if (extent[1]!="global")
  67. {
  68. ext <- getTile(x = extent)
  69. } else
  70. {
  71. ext <- list()
  72. ext$tile <- extent
  73. }
  74. cat(" - tiles:", paste0(unlist(ext$tile),collapse=", "),"\n")
  75. if (is.null(begin) & is.null(end))
  76. {
  77. cat(" - dates: all dates\n")
  78. } else {
  79. tLimits <- transDate(begin=begin,end=end)
  80. cat(" - date range: from",as.character(tLimits$begin),"to",as.character(tLimits$end),"\n")
  81. }
  82. if (ask)
  83. {
  84. doit <- toupper(readline("\nAre you sure you want proceed? [y/n]: "))
  85. }
  86. if (doit %in% c("N","NO"))
  87. {
  88. return("Ok deleting abborted!")
  89. } else if (doit %in% c("Y","YES"))
  90. {
  91. # bypass checks if a complete product has to be deleted!
  92. if (is.null(begin) & is.null(end) & ext$tile[1]=="global")
  93. {
  94. for (z in seq_along(product$PRODUCT))
  95. {
  96. todo <- paste0(product$PRODUCT[z],".",product$CCC[[which(names(product$CCC)==product$PRODUCT[z])]])
  97. for(u in seq_along(todo))
  98. {
  99. path <- genString(x=strsplit(todo[u],"\\.")[[1]][1],collection=strsplit(todo[u],"\\.")[[1]][2],date=NULL,remote=FALSE,opts)$localPath
  100. path <- strsplit(path,"/")[[1]]
  101. path <- paste0(path[-length(path)],collapse="/")
  102. allLocal <- list.files(path,recursive=TRUE)
  103. summaries <- fileSize(allLocal,units="MB") + sum(summaries)
  104. unlink(path,recursive=TRUE)
  105. }
  106. }
  107. cat("Deleted:", todo[u],"\n")
  108. } else
  109. {
  110. for (z in seq_along(product$PRODUCT))
  111. {
  112. todo <- paste0(product$PRODUCT[z],".",product$CCC[[which(names(product$CCC)==product$PRODUCT[z])]])
  113. for(u in seq_along(todo))
  114. {
  115. path <- genString(x=strsplit(todo[u],"\\.")[[1]][1],collection=strsplit(todo[u],"\\.")[[1]][2],date=NULL,remote=FALSE,opts)$localPath
  116. path <- strsplit(path,"/")[[1]]
  117. path <- paste0(path[-length(path)],collapse="/")
  118. allLocal <- list.files(path=path,pattern=".hdf",recursive=TRUE,full.names=TRUE)
  119. if (length(allLocal)!=0)
  120. {
  121. # remove out of querry dates
  122. locDates <- as.Date(sapply(allLocal,function(x)
  123. {
  124. date <- strsplit(normalizePath(dirname(x),winslash="/"),"/")[[1]]
  125. date <- date[length(date)]
  126. return(date)
  127. }),"%Y.%m.%d")
  128. allLocal <- allLocal[as.Date(tLimits$begin) <= locDates & as.Date(tLimits$end) >= locDates]
  129. if (length(allLocal)==0)
  130. {
  131. break
  132. }
  133. subprod <- getProduct(allLocal[1])
  134. if (subprod$TYPE=="CMG")
  135. {
  136. useExt <- "global"
  137. } else {
  138. useExt <- ext$tile
  139. }
  140. if (useExt=="global")
  141. {
  142. summaries <- fileSize(allLocal,units="MB") + sum(summaries)
  143. unlink(allLocal,recursive=TRUE)
  144. } else {
  145. for(i in seq_along(useExt))
  146. {
  147. summaries <- fileSize(allLocal,units="MB") + sum(summaries)
  148. unlink(grep(allLocal,pattern=useExt[i],value=TRUE),recursive=TRUE)
  149. }
  150. }
  151. dirs <- unique(dirname(allLocal))
  152. for (i in seq_along(dirs))
  153. {
  154. if (length(list.files(dirs[i]))==0)
  155. {
  156. if (.Platform$OS=="unix") # I'm looking for a windows/MAC(?) eqal to the linux "rmdir -p" command!!
  157. {
  158. warn <- options()$warn
  159. options(warn=-2)
  160. try(xxx <- invisible(system(paste0("rmdir -p --ignore-fail-on-non-empty ", dirs[i]),intern=TRUE)),silent=TRUE)
  161. options(warn=warn)
  162. } else
  163. { # work arount for rmdir -p on Windows
  164. unlink(dirs[i],recursive=TRUE)
  165. secPath <- strsplit(dirs[i],"/")[[1]]
  166. for (o in length(secPath):1)
  167. {
  168. delpath <- paste0(secPath[-o:-length(secPath)],collapse="/")
  169. if (length(list.files(delpath))==0)
  170. {
  171. unlink(delpath,recursive=TRUE)
  172. } else {
  173. break
  174. }
  175. }
  176. }
  177. }
  178. }
  179. cat("Deleted subset of:", todo[u],"\n")
  180. } else {
  181. cat("No files in querry for:", todo[u],"\n")
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return(summaries)
  188. }