#' Given a RasterStack or RasterBrick, convert the names to POSIXct dates
#'
#' @export
#' @param x RasterStack or RasterBrick with names like \code{A20150123}. It is
#' an error to provide a stack/brick without proper names.
#' @param form character either 'Date' (default) or 'POSIXct'
#' @param tz the timezone, default \code{UTC}
#' @param time character defaults to \code{12:00:00}
#' @param fmt the format string to use
#' @return vector of Date or POSIXct dates
stack_dates = function(x, form = 'Date',
tz = 'UTC', time = '12:00:00', fmt = 'A%Y%j %H:%M:%S'){
if (!(inherits(x, 'RasterBrick') || inherits(x, 'RasterStack')))
stop("input must be of class RasterBrick or RasterStack")
switch(tolower(form[1]),
'posixct' = try(as.POSIXct(paste(names(x), time), tz = tz, format = fmt)),
try(as.Date(names(x), format = 'A%Y%j'))
)
}
#' Given a RasterStack or RasterBrick list the days missing from the sequence.
#'
#' @export
#' @param x the RasterStack or RasterBrick
#' @param ... further arguments for \code{stack_dates()}
#' @return vector of zero or more Date or POSIXct dates missing
stack_missing_days = function(x, ...){
d = stack_dates(x, ...)
r = range(d)
dd = seq(from = r[1], to = r[2], by = 'day')
dd[!(dd %in% d)]
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.