transDate.R 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #' MODIS Date Conversion and Testing
  2. #'
  3. #' @description
  4. #' This function converts a sequence of input dates to 'YYYY-MM-DD' and
  5. #' 'YYYYDDD'.
  6. #'
  7. #' @param begin,end \code{character} or \code{Date}. Begin (end) date of MODIS
  8. #' time series, see Note. If not provided, this defaults to \code{"1972-01-01"}
  9. #' (\code{Sys.Date()}).
  10. #'
  11. #' @return
  12. #' A \code{list} of begin and end dates formatted according to 'YYYY-MM-DD'
  13. #' (first two slots; class \code{Date}) and 'YYYYDDD' (second two slots; class
  14. #' \code{character}).
  15. #'
  16. #' @note
  17. #' If input dates are supplied as \code{character}, this function either expects
  18. #' 7-digit strings in the MODIS intrinsic form \code{'\%Y\%j'} or, alternatively,
  19. #' 10-digit strings in the form \code{'\%Y-\%m-\%d'} where the two field separators
  20. #' need to be uniform (see Examples).
  21. #'
  22. #' @seealso \code{\link{strptime}}.
  23. #'
  24. #' @author
  25. #' Matteo Mattiuzzi, Florian Detsch
  26. #'
  27. #' @examples
  28. #' transDate()
  29. #' transDate(begin = "2009.01.01") # ends with current date
  30. #' transDate(end = "2009.01.01") # starts with Landsat 1
  31. #' transDate(begin = c("2009-01-01", "2010-01-01"), end = "2011.03.16")
  32. #'
  33. #' @export transDate
  34. #' @name transDate
  35. transDate <- function(begin = NULL, end = NULL) {
  36. begin <- if (inherits(begin, "Date")) {
  37. format(begin, "%Y.%m.%d")
  38. } else if (is.null(begin)) {
  39. "1972.01.01" # start with Landsat 1
  40. } else begin
  41. end <- if (inherits(end, "Date")) {
  42. format(end, "%Y.%m.%d")
  43. } else if (is.null(end)) {
  44. format(Sys.Date(), "%Y.%m.%d") # current date
  45. } else end
  46. ## if 'begin' dates come in '%Y%j' format, reformat to '%Y.%m.%d'
  47. if (any(nchar(begin) == 7)) {
  48. # if all 'begin' dates have the same format, proceed
  49. if (all(nchar(begin) == 7)) {
  50. begin <- strftime(as.Date(begin, "%Y%j"), "%Y.%m.%d")
  51. # else throw error
  52. } else {
  53. stop("Input dates are required to have the same format (e.g., '%Y%j').\n")
  54. }
  55. }
  56. ## same as above, but for 'end' dates
  57. if (any(nchar(end) == 7)) {
  58. if (all(nchar(end) == 7)) {
  59. end <- strftime(as.Date(end,"%Y%j"),"%Y.%m.%d")
  60. } else {
  61. stop("Input dates are required to have the same format (e.g., '%Y%j').\n")
  62. }
  63. }
  64. divisor <- substr(begin,5,5)
  65. begin <- as.Date(begin, format = paste0("%Y", divisor, "%m", divisor, "%d"))
  66. if (any(is.na(begin)))
  67. stop("'begin' date is either in a wrong format or an invalid date.\n")
  68. divisor <- substr(end,5,5)
  69. end <- as.Date(end,format=paste("%Y",divisor,"%m",divisor,"%d",sep=""))
  70. if (any(is.na(end)))
  71. stop("'end' date is either in a wrong format or an invalid date.\n")
  72. if (any(end < begin)) {
  73. warning("'begin' and 'end' dates seem to be confused, reordering dates...\n")
  74. dts <- sort(c(begin, end))
  75. begin <- dts[1:(length(dts) - 1)]
  76. end <- dts[length(dts)]
  77. }
  78. beginDOY <- format(as.Date(begin,format="%Y.%m.%d"), "%Y%j")
  79. endDOY <- format(as.Date(end,format="%Y.%m.%d"), "%Y%j")
  80. return(list(begin=begin,end=end,beginDOY=beginDOY,endDOY=endDOY))
  81. }