Nothing
#' MODIS Date Conversion and Testing
#'
#' @description
#' This function converts a sequence of input dates to 'YYYY-MM-DD' and
#' 'YYYYDDD'.
#'
#' @param begin,end `Date` or `character`. Begin and end date of MODIS time
#' series, see Note. If not provided, this defaults to `"1972-01-01"` and
#' [Sys.Date()], respectively.
#'
#' @return
#' A `list` of begin and end dates formatted according to 'YYYY-MM-DD' (first
#' two slots; class `Date`) and 'YYYYDDD' (second two slots; class `character`).
#'
#' @note
#' If input dates are supplied as `character`, this function either expects
#' 7-digit strings in the MODIS intrinsic form `'\%Y\%j'` or, alternatively,
#' 10-digit strings in the form `'\%Y-\%m-\%d'` where the two field separators
#' need to be uniform (see Examples).
#'
#' @seealso [strptime()].
#'
#' @author
#' Matteo Mattiuzzi, Florian Detsch
#'
#' @examples
#' transDate()
#' transDate(begin = "2009.01.01") # ends with current date
#' transDate(end = "2009.01.01") # starts with Landsat 1
#' transDate(begin = c("2009-01-01", "2010-01-01"), end = "2011.03.16")
#'
#' @export transDate
#' @name transDate
transDate <- function(begin = NULL, end = NULL) {
begin <- if (inherits(begin, "Date")) {
format(begin, "%Y.%m.%d")
} else if (is.null(begin)) {
"1972.01.01" # start with Landsat 1
} else begin
end <- if (inherits(end, "Date")) {
format(end, "%Y.%m.%d")
} else if (is.null(end)) {
format(Sys.Date(), "%Y.%m.%d") # current date
} else end
## if 'begin' dates come in '%Y%j' format, reformat to '%Y.%m.%d'
if (any(nchar(begin) == 7)) {
# if all 'begin' dates have the same format, proceed
if (all(nchar(begin) == 7)) {
begin <- strftime(as.Date(begin, "%Y%j"), "%Y.%m.%d")
# else throw error
} else {
stop("Input dates are required to have the same format (e.g., '%Y%j').\n")
}
}
## same as above, but for 'end' dates
if (any(nchar(end) == 7)) {
if (all(nchar(end) == 7)) {
end <- strftime(as.Date(end,"%Y%j"),"%Y.%m.%d")
} else {
stop("Input dates are required to have the same format (e.g., '%Y%j').\n")
}
}
divisor <- substr(begin,5,5)
begin <- as.Date(begin, format = paste0("%Y", divisor, "%m", divisor, "%d"))
if (any(is.na(begin)))
stop("'begin' date is either in a wrong format or an invalid date.\n")
divisor <- substr(end,5,5)
end <- as.Date(end,format=paste("%Y",divisor,"%m",divisor,"%d",sep=""))
if (any(is.na(end)))
stop("'end' date is either in a wrong format or an invalid date.\n")
if (any(end < begin)) {
warning("'begin' and 'end' dates seem to be confused, reordering dates...\n")
dts <- sort(c(begin, end))
begin <- dts[1:(length(dts) - 1)]
end <- dts[length(dts)]
}
beginDOY <- format(as.Date(begin,format="%Y.%m.%d"), "%Y%j")
endDOY <- format(as.Date(end,format="%Y.%m.%d"), "%Y%j")
return(list(begin=begin,end=end,beginDOY=beginDOY,endDOY=endDOY))
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.