orgTime.R 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. if ( !isGeneric("orgTime") ) {
  2. setGeneric("orgTime", function(files, ...)
  3. standardGeneric("orgTime"))
  4. }
  5. #' Handle Input and Output Dates Used for Filtering
  6. #'
  7. #' @description
  8. #' This function lets you define the period to be filtered, the output temporal
  9. #' resolution, and select the required data from your input 'files'.
  10. #'
  11. #' @param files A \code{character}, \code{Date}, or \code{Raster*} object.
  12. #' Typically MODIS filenames created e.g. from \code{\link{runGdal}} or
  13. #' \code{\link{runMrt}}, but any other filenames holding date information are
  14. #' supported as well. If a \code{Raster*} object is supplied, make sure to
  15. #' adjust 'pos1', 'pos2', and 'format' according to its layer
  16. #' \code{\link[raster]{names}}.
  17. #' @param nDays Time interval for output layers. Defaults to \code{"asIn"} that
  18. #' includes the exact input dates within the period selected using \code{begin}
  19. #' and \code{end}. Can also be \code{nDays = "1 month"} or \code{"1 week"}, see
  20. #' \code{\link{seq.Date}} and Examples.
  21. #' @param begin \code{character}. Output begin date, defaults to the earliest
  22. #' input dataset.
  23. #' @param end \code{character}. Output end date, defaults to the latest input
  24. #' dataset. Note that the exact \code{end} date depends on \code{begin} and
  25. #' \code{nDays}.
  26. #' @param pillow \code{integer}. Number of days added to the beginning and end
  27. #' of a time series.
  28. #' @param pos1,pos2,format Arguments passed to \code{\link{extractDate}}.
  29. #'
  30. #' @return
  31. #' A \code{list} with the following slots (to be completed):
  32. #'
  33. #' \itemize{
  34. #' \item{\code{$inSeq}}
  35. #' \item{\code{$outSeq}}
  36. #' \item{\code{$inDoys}}
  37. #' \item{\code{$inputLayerDates}}
  38. #' \item{\code{$outputLayerDates}}
  39. #' \item{\code{$call}}
  40. #' }
  41. #'
  42. #' @seealso
  43. #' \code{\link{seq.Date}}.
  44. #'
  45. #' @author
  46. #' Matteo Mattiuzzi, Florian Detsch
  47. #'
  48. #' @examples
  49. #' # Using MODIS files
  50. #' files <- c("MOD13A2.A2010353.1_km_16_days_composite_day_of_the_year.tif",
  51. #' "MOD13A2.A2011001.1_km_16_days_composite_day_of_the_year.tif",
  52. #' "MYD13A2.A2010361.1_km_16_days_composite_day_of_the_year.tif",
  53. #' "MYD13A2.A2011009.1_km_16_days_composite_day_of_the_year.tif")
  54. #'
  55. #' orgTime(files)
  56. #' orgTime(files,nDays=2,begin="2010350",end="2011015")
  57. #'
  58. #' \dontrun{
  59. #' # Using other files, e.g. from GIMMS (Jul 1981 to Dec 1982)
  60. #' library(gimms)
  61. #'
  62. #' files.v1 <- system.file("extdata/inventory_ecv1.rds", package = "gimms")
  63. #' files.v1 <- readRDS(files.v1)[1:3]
  64. #' dates.v1 <- monthlyIndices(files.v1, timestamp = TRUE)
  65. #'
  66. #' orgTime(dates.v1)
  67. #' }
  68. #'
  69. #' @export orgTime
  70. #' @name orgTime
  71. NULL
  72. ################################################################################
  73. ### function using 'character' input ###########################################
  74. #' @aliases orgTime,character-method
  75. #' @rdname orgTime
  76. setMethod("orgTime",
  77. signature(files = "character"),
  78. function(files, nDays = "asIn", begin = NULL, end = NULL, pillow = 75
  79. , pos1, pos2, format = "%Y%j") {
  80. files <- basename(files)
  81. ## if any position indication is missing, try to retrieve it from look-up table
  82. if (any(missing(pos1), missing(pos2))) {
  83. ids = positionIndication(files)
  84. pos1 = ids[[1]]; pos2 = ids[[2]]
  85. }
  86. allDates <- sort(extractDate(files,asDate=TRUE,pos1=pos1,pos2=pos2,format=format)$inputLayerDates)
  87. datLim <- transDate(begin=begin,end=end)
  88. if (!is.null(begin))
  89. {
  90. minOUT <- datLim$begin
  91. minIN <- minOUT - pillow
  92. minHAVE <- min(allDates[allDates >= minIN])
  93. if (nDays=="asIn")
  94. {
  95. minIN <- minHAVE
  96. }
  97. } else
  98. {
  99. minIN <- minOUT <- minHAVE <- min(allDates)
  100. }
  101. if (!is.null(end))
  102. {
  103. maxOUT <- datLim$end
  104. maxIN <- maxOUT + pillow
  105. maxHAVE <- max(allDates[allDates <= maxIN])
  106. if (nDays=="asIn")
  107. {
  108. maxIN <- maxHAVE
  109. }
  110. } else
  111. {
  112. maxIN <- maxOUT <- maxHAVE <- max(allDates)
  113. }
  114. inputLayerDates <- allDates[allDates >= minHAVE & allDates <= maxHAVE]
  115. inDoys <- as.numeric(format(as.Date(inputLayerDates),"%j"))
  116. if(FALSE) # currently removed
  117. {
  118. if (minIN < minHAVE)
  119. {
  120. if (as.numeric(minHAVE - minIN) <= pillow)
  121. {
  122. warning("'begin'-date - 'pillow' is earlier by, ",as.numeric(minHAVE - minIN) ," days, than the available input dates!\nPillow at the start of the time serie is reduced to ",pillow - as.numeric(minHAVE - minIN)," days!")
  123. } else if (minOUT == minHAVE)
  124. {
  125. warning("Is is not possible to use the pillow at the begin of the time series since there is no data available before 'begin'-date!")
  126. }
  127. }
  128. if (maxIN > maxHAVE)
  129. {
  130. warning("'end'-date + 'pillow' is later by, ",as.numeric(maxIN - max(inputLayerDates)) ," days, than the available input dates!")
  131. }
  132. }
  133. if (nDays=="asIn")
  134. {
  135. outputLayerDates <- inputLayerDates[datLim$begin <= inputLayerDates & datLim$end > inputLayerDates]
  136. } else
  137. {
  138. outputLayerDates <- seq(minOUT,maxOUT,by=nDays)
  139. }
  140. t0 <- as.numeric(min(outputLayerDates,inputLayerDates)) - 1
  141. inSeq <- as.numeric(inputLayerDates) - t0
  142. outSeq <- as.numeric(outputLayerDates) - t0
  143. return(list(inSeq=inSeq,outSeq=outSeq, inDoys=inDoys, inputLayerDates=inputLayerDates,outputLayerDates=outputLayerDates,call = list(pos1=pos1,pos2=pos2,format=format,asDate=TRUE,nDays=nDays,pillow=pillow)))
  144. })
  145. ################################################################################
  146. ### function using 'Date' input ################################################
  147. #' @aliases orgTime,Date-method
  148. #' @rdname orgTime
  149. setMethod("orgTime",
  150. signature(files = "Date"),
  151. function(files,
  152. nDays = "asIn",
  153. begin = NULL,
  154. end = NULL,
  155. pillow = 75) {
  156. ## convert 'Date' to 'character'
  157. files <- as.character(files)
  158. ## invoke 'character' method
  159. orgTime(files, nDays, begin, end, pillow, pos1 = 1, pos2 = 10,
  160. format = "%Y-%m-%d")
  161. })
  162. ################################################################################
  163. ### function using 'Raster*' input #############################################
  164. #' @aliases orgTime,Raster-method
  165. #' @rdname orgTime
  166. setMethod("orgTime",
  167. signature(files = "Raster"),
  168. function(files,
  169. nDays = "asIn",
  170. begin = NULL,
  171. end = NULL,
  172. pillow = 75,
  173. pos1,
  174. pos2,
  175. format = "%Y%j") {
  176. ## extract layer names
  177. files <- names(files)
  178. ## invoke 'character' method
  179. orgTime(files, nDays, begin, end, pillow, pos1, pos2, format)
  180. })