#' Create a date vector from \code{MODIStsp}-generated RasterStack
#'
#' This function creates a vector of class Date that corresponds to the
#' observations in MODIS data processed by \code{MODIStsp}.
#'
#' The output of this function is most useful for passing as an argument to
#' \code{bfastSpatial()}, which requires a date vector.
#'
#' @param stack A RasterStack object generated by \code{MODIStsp()}.
#' Alternatively, a character vector of file names or file paths generated by
#' \code{MODIStsp} from which dates should be extracted.
#' @return A vector of class Date containing the dates for each raster layer.
#' @examples
#' \dontrun{
#' MODIStsp_date(raster_ts)
#' }
#' @importFrom dplyr mutate
#' @importFrom magrittr %>%
#' @export
MODIStsp_date <- function(stack) {
class_stack <- class(stack)
if (class_stack=="RasterStack") {
stack <- names(stack)
} else if (class_stack=="character") {
stack <- basename(stack)
} else {
stop("Incorrect class for argument stack.")
}
dates.df <- as.data.frame(str_match(stack, "^MOD.+_([[:digit:]]{4})_([[:digit:]]{3})")) %>%
mutate(V2=as.numeric(as.character(V2)), V3=as.numeric(as.character(V3))) %>%
mutate(date=base::as.Date(V3, origin=base::as.Date(paste(V2-1, "12", "31", sep="-"))))
return(dates.df[,"date"])
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.