X-Git-Url: https://git.auder.net/pieces/Checkered/cq.svg?a=blobdiff_plain;f=pkg%2FR%2Futils.R;h=bbf138750d21b2bdca0694980e5c1e0ff85d7acb;hb=72b9c50162bcdcf6c99fbb8b2ec6ea9ba98379cb;hp=b3e66e18d109f9bd56ece2618f204422f2bd63a0;hpb=ee8b1b4e3c13f8dcf13a2c8da6a3bef1520c8252;p=talweg.git diff --git a/pkg/R/utils.R b/pkg/R/utils.R index b3e66e1..bbf1387 100644 --- a/pkg/R/utils.R +++ b/pkg/R/utils.R @@ -16,7 +16,8 @@ dateIndexToInteger = function(index, data) if (inherits(index, "Date") || is.character(index)) { - tryCatch(indexAsDate <- as.Date(index), error=function(e) stop("Unrecognized index format")) + tryCatch(indexAsDate <- as.Date(index), + error=function(e) stop("Unrecognized index format")) #TODO: tz arg to difftime ? integerIndex <- round( as.numeric( difftime(indexAsDate, as.Date(data$getTime(1)[1])) ) ) + 1 @@ -59,9 +60,10 @@ integerIndexToDate = function(index, data) #' @param data Reference dataset, object output of \code{getData} #' @param limit Maximum number of indices to return #' @param same_season Should the indices correspond to day in same season? +#' @param days_in Optional set to intersect with results (NULL to discard) #' #' @export -getSimilarDaysIndices = function(index, data, limit, same_season) +getSimilarDaysIndices = function(index, data, limit, same_season, days_in=NULL) { index = dateIndexToInteger(index, data) @@ -74,7 +76,7 @@ getSimilarDaysIndices = function(index, data, limit, same_season) while (i >= 1 && length(days) < limit) { dt = as.POSIXlt(data$getTime(i)[1]) - if (.isSameDay(dt$wday, day_ref)) + if ((is.null(days_in) || i %in% days_in) && .isSameDay(dt$wday, day_ref)) { if (!same_season || .isSameSeason(dt$mon+1, month_ref)) days = c(days, i)