orgStruc.R 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #' Reorganise MODIS Files in Local Data Archive
  2. #'
  3. #' @description
  4. #' Re-organise the storage structure of your MODIS archive according to the
  5. #' settings in \code{options("MODIS_arcStructure")}. Depending on the specified
  6. #' \code{'source'} , you can also use this function to gather all MODIS grid
  7. #' files on you computer and reorganise them. The main purpose is to organise
  8. #' the archive, but it is also possible to copy a subset of files to a
  9. #' desidered location!
  10. #'
  11. #' @param from \code{character}. Local path to look for MODIS files, defaults to
  12. #' \code{options("MODIS_localArcPath")} (see \code{\link{MODISoptions}}).
  13. #' @param to \code{character}. Target folder to move (or copy) MODIS files to,
  14. #' defaults to \code{options("MODIS_localArcPath")}.
  15. #' @param structure \code{character}. Storage structure, defaults to
  16. #' \code{options("MODIS_arcStructure")} (see Examples).
  17. #' @param pattern Regular expression passed to \code{\link{list.files}}. Insert
  18. #' a pattern if you want to extract specific files from your archiv.
  19. #' @param move \code{logical}. If \code{TRUE} (default), files are moved and
  20. #' duplicated files are deleted. If \code{FALSE}, files are just copied and thus
  21. #' remain in the origin folder. Note that the copying process performs rather
  22. #' slowly when dealing with large files, e.g. 250-m 'MOD13Q1'.
  23. #' @param quiet \code{logical}, defaults to \code{FALSE}.
  24. #'
  25. #' @return
  26. #' If \code{quiet = FALSE}, information on how many files have been moved (or
  27. #' copied) and deleted is printed to the console.
  28. #'
  29. #' @author
  30. #' Matteo Mattiuzzi
  31. #'
  32. #' @examples
  33. #' \dontrun{
  34. #' # MOVE all MODIS grid data to the directory and structure as defined in
  35. #' # options("MODIS_localArcPath", "MODIS_arcStructure")
  36. #' orgStruc(move = TRUE)
  37. #'
  38. #' # COPY all MOD13Q1 from 2001 to folder "MyFiles/MOD13Q1.collection/"
  39. #' orgStruc(pattern="MOD13Q1.A2001*.",to="MyFiles",structure="PRODUCT.CCC")
  40. #'
  41. #' # COPY all MOD13Q1 to folder "MyFiles/"
  42. #' orgStruc(pattern="MOD13Q1.*.",to="MyFiles",structure="")
  43. #' }
  44. #'
  45. #' @export orgStruc
  46. #' @name orgStruc
  47. orgStruc <- function(from,to,structure, pattern, move=FALSE, quiet=FALSE)
  48. {
  49. opts <- combineOptions()
  50. if (missing(from))
  51. {
  52. from <- opts$localArcPath
  53. }
  54. if (missing(to))
  55. {
  56. to <- opts$localArcPath
  57. }
  58. to <- setPath(to)
  59. if (!missing(structure))
  60. {
  61. opts$acrStructure <- structure
  62. }
  63. ###########################
  64. if(missing(pattern))
  65. {
  66. cat(paste0("No 'pattern' set, moving/coping all MODIS grid data found in '", from,"'.\n"))
  67. avFiles <- unlist(list.files(from, pattern=".hdf$", recursive=TRUE, full.names=TRUE))
  68. } else
  69. {
  70. avFiles <- unlist(list.files(from, pattern=pattern, recursive=TRUE, full.names=TRUE))
  71. }
  72. if (length(avFiles)==0) {stop("No HDF nor HDF.XML files found!\n")}
  73. doit <- isSupported(avFiles)
  74. if (sum(doit)==0) {stop("No supported files Found")}
  75. avFiles <- avFiles[doit]
  76. if (!quiet)
  77. {
  78. cat("Found",length(avFiles),"files \n")
  79. }
  80. #########################
  81. moved <- sapply(avFiles,function(x)
  82. {
  83. orpath <- correctPath(dirname(x))
  84. fname <- basename(x)
  85. ########################
  86. # generate and create local path to file!
  87. path <- genString(x=fname,remote=FALSE,localArcPath=to)$localPath
  88. dir.create(path,showWarnings=FALSE,recursive=TRUE)
  89. ###################
  90. if (!file.exists(file.path(path,fname,fsep="/")))
  91. { # if file doesn't exist in destdir copy/move
  92. if (move)
  93. {
  94. file.rename(from=x,to=paste0(path,fname))
  95. if (file.exists(paste0(x,".xml")))
  96. {
  97. file.rename(from=paste0(x,".xml"),to=paste0(path,fname,".xml",sep=""))
  98. }
  99. moved <- 1
  100. } else
  101. {
  102. file.copy(from=x,to=paste0(path,fname),overwrite=FALSE)
  103. if (file.exists(paste0(x,".xml")))
  104. {
  105. file.copy(from=paste0(x,".xml"),to=paste0(path,fname,".xml"))
  106. }
  107. moved <- 2
  108. }
  109. } else if (file.exists(file.path(path,fname,fsep="/")) & orpath!=path)
  110. { # if file exists in destdir & inpath!=outPath...it is duplicated in 2 different locations, so remove it
  111. unlink(x)
  112. if (file.exists(paste0(x,".xml")))
  113. {
  114. unlink(paste0(x,".xml"))
  115. }
  116. moved <- 3
  117. } else
  118. {
  119. moved <- 0
  120. }
  121. if (length(list.files(orpath))==0)
  122. {
  123. if (.Platform$OS=="unix")
  124. { # I'm looking for a windows/MAC(?) eqal to the linux "rmdir -p" command!!
  125. warn <- getOption("warn")
  126. options(warn=-2)
  127. try(xxx <- invisible(system(paste0("rmdir -p --ignore-fail-on-non-empty ", orpath),intern=TRUE)),silent=TRUE)
  128. options(warn=warn)
  129. } else
  130. { # work around for rmdir -p on windows/MAC(?)
  131. unlink(orpath,recursive=TRUE)
  132. secPath <- strsplit(orpath,"/")[[1]]
  133. for (o in length(secPath):1)
  134. {
  135. delpath <- paste0(secPath[-o:-length(secPath)],collapse="/")
  136. if (length(list.files(delpath))==0)
  137. {
  138. unlink(delpath,recursive=TRUE)
  139. } else
  140. {
  141. break
  142. }
  143. }
  144. }
  145. }
  146. return(moved)
  147. })
  148. if (sum(moved==0)==length(avFiles))
  149. {
  150. cat("All files in the query are fine, no files to move or to copy!\n")
  151. } else
  152. {
  153. cat("Moved files", sum(moved==1),"\n")
  154. cat("Copied files", sum(moved==2),"\n")
  155. cat("Not moved files", sum(moved==0),"\n")
  156. cat("Deleted multiple files", sum(moved==3),"\n")
  157. }
  158. }