preStack.R 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #' Organise (MODIS) Files in Preparation for Stacking
  2. #'
  3. #' @description
  4. #' This function lets you sort a vector of filenames according to date. It is
  5. #' thought to be used on results from \code{\link{runGdal}} or
  6. #' \code{\link{runMrt}}.
  7. #'
  8. #' @param pattern Regular expression passed to \code{\link{list.files}}
  9. #' @param path \code{character}. Location of MODIS files to stack.
  10. #' @param files \code{character} vector of filenames. If provided, arguments
  11. #' \code{pattern} and \code{path} are ignored.
  12. #' @param timeInfo Ouput from \code{\link{orgTime}}.
  13. #'
  14. #' @return
  15. #' A \code{character} vector of filenames within the query. If \code{timeInfo}
  16. #' is provided, filenames are sorted and subsetted by date.
  17. #'
  18. #' @author
  19. #' Matteo Mattiuzzi
  20. #'
  21. #' @examples
  22. #' # see Examples in ?smooth.spline.raster
  23. #'
  24. #' @export preStack
  25. #' @name preStack
  26. preStack <- function(pattern = "*", path = "./", files = NULL, timeInfo = NULL)
  27. {
  28. if (is.null(files))
  29. {
  30. fnames <- list.files( path = path, pattern = pattern, full.names = TRUE)
  31. } else
  32. {
  33. fnames <- files
  34. }
  35. if (length(fnames) == 0)
  36. {
  37. cat("No files found!\n") ; return(NULL)
  38. }
  39. if (!is.null(timeInfo))
  40. {
  41. avDates <- extractDate( basename(fnames), pos1 = timeInfo$call$pos1, pos2 = timeInfo$call$pos2, format = timeInfo$call$format, asDate = TRUE)
  42. fnames <- fnames[ order(avDates$inputLayerDates) ]
  43. avDates <- sort(avDates$inputLayerDates)
  44. begin <- min(timeInfo$inputLayerDates)
  45. end <- max(timeInfo$inputLayerDates)
  46. fnames <- fnames[avDates >= begin & avDates <= end]
  47. }
  48. cat("Found", length(fnames), "files!\n")
  49. if ( length(fnames) == 0 ) ( return(NULL) )
  50. return(fnames)
  51. }