repDoy.R 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #' Repair MODIS "composite_day_of_the_year" SDS
  2. #'
  3. #' @description
  4. #' Currently works only for MODIS 16 days composites! In MODIS composites, the
  5. #' Julian dates inside the 'composite_day_of_the_year' SDS are referring always
  6. #' to the year they are effectively in. The problem is that the layer/SDS name
  7. #' from the last files from Terra and Aqua within a year can include dates from
  8. #' the following year and so starting again with 1. The problem comes if you
  9. #' want to sort values of a time series by date (e.g. for precise time series
  10. #' functions). This function generates a sequential vector beginning always
  11. #' with the earielst SDS/layer date and ending with the total sum of days of
  12. #' the time serie length.
  13. #'
  14. #' @param pixX \code{matrix} of values, usually derived from
  15. #' \code{\link{as.matrix}}.
  16. #' @param layerDate If \code{NULL} (default), try to autodetect layer dates. If
  17. #' you want to be sure, use the result from
  18. #' \code{extractDate(..., asDate = TRUE)} or \code{\link{orgTime}}.
  19. #' @param bias \code{integer}. Bias applied to all values in \code{pixX}.
  20. #'
  21. #' @return
  22. #' A \code{matrix} with sequential Julian dates.
  23. #'
  24. #' @author
  25. #' Matteo Mattiuzzi
  26. #'
  27. #' @examples
  28. #' \dontrun{
  29. #' runGdal(product="M.D13A2", begin="2010350", end="2011016", extent="Luxembourg",
  30. #' job="deleteme", SDSstring="100000000010")
  31. #'
  32. #' ndviFiles <- preStack(path=paste(MODIS:::.getDef()$outDirPath,"deleteme",sep="/")
  33. #' ,pattern="*_NDVI.tif$")
  34. #'
  35. #' ndvi <- stack(ndviFiles)
  36. #'
  37. #' doyFiles <- preStack(path=paste(MODIS:::.getDef()$outDirPath,"deleteme",sep="/")
  38. #' ,pattern="*_composite_day_of_the_year.tif$")
  39. #'
  40. #' doy <- stack(doyFiles)
  41. #' layerDates <- extractDate(names(doy))
  42. #'
  43. #' pixX <- 169
  44. #'
  45. #' y <- ndvi[pixX]
  46. #' x1 <- doy[pixX]
  47. #' x2 <- repDoy(doy[pixX],layerDates)
  48. #'
  49. #' x1
  50. #' x2
  51. #' # the plotting example is not really good.
  52. #' # To create a figurative example it would be necessary to dolwnload to much data!
  53. #' plot("",xlim=c(1,max(x1,x2)),ylim=c(0,2000),xlab="time",ylab="NDVI*10000")
  54. #' lines(y=y,x=x1,col="red",lwd=3)
  55. #' lines(y=y,x=x2,col="green",lwd=2)
  56. #'
  57. #' # repDoy function is thought to be enbeded in something like that:
  58. #' tr <- blockSize(ndvi)
  59. #'
  60. #' doyOk <- brick(doy)
  61. #' doyOk <- writeStart(doyOk, filename='test.tif', overwrite=TRUE)
  62. #'
  63. #' for (i in 1:tr$n)
  64. #' {
  65. #' pixX <- getValues(doy,tr$row[i],tr$nrows[i])
  66. #' ok <- repDoy(pixX,layerDates)
  67. #' doyOk <- writeValues(x=doyOk,v=ok,start=tr$row[i])
  68. #' }
  69. #' doyOk <- writeStop(doyOk)
  70. #'
  71. #' # unlink(filename(doyOk))
  72. #' }
  73. #'
  74. #' @export repDoy
  75. #' @name repDoy
  76. repDoy <- function(pixX, layerDate = NULL, bias = 0)
  77. {
  78. if (is.null(layerDate))
  79. {
  80. layerDate <- extractDate(colnames(pixX),asDate=TRUE)
  81. }
  82. if (layerDate$call$asDate)
  83. {
  84. layerDoy <- format(layerDate$inputLayerDates,"%j")
  85. layerYear <- format(layerDate$inputLayerDates,"%Y")
  86. } else
  87. {
  88. layerDoy <- substr(layerDate$inputLayerDates,5,7)
  89. layerYear <- substr(layerDate$inputLayerDates,1,4)
  90. }
  91. if (is.matrix(pixX))
  92. {
  93. pixX <- t(pixX)
  94. } else
  95. {
  96. pixX <- as.matrix(pixX) # if it is a vector
  97. }
  98. # de-bias 'end of year layers' that have 'start of year' dates
  99. mask <- pixX - as.numeric(layerDoy)
  100. mask <- sign(mask)==-1
  101. mask[is.na(mask)] <- FALSE
  102. ndays <- as.numeric(format(as.Date(paste0(layerYear,"-12-31")),"%j"))
  103. bias1 <- matrix(ndays, ncol = ncol(pixX), nrow = nrow(pixX), byrow=FALSE)
  104. pixX[mask] <- pixX[mask] + bias1[mask]
  105. # sequentialize doys
  106. ndays <- as.numeric(format(as.Date(paste0(unique(layerYear),"-12-31")),"%j"))
  107. bias1 <- cumsum(ndays) - ndays[1]
  108. counter <- as.numeric(table(layerYear)) # nlayers in Y
  109. biasN <- vector(mode="list",length=length(counter))
  110. for(i in seq_along(counter))
  111. {
  112. biasN[[i]] <- rep(bias1[i],counter[i])
  113. }
  114. pixX <- pixX + unlist(biasN) + bias
  115. return(t(pixX))
  116. }