1 #' @title dateIndexToInteger
3 #' @description Transform a (potential) date index into an integer (relative to data)
5 #' @param index Date (or integer) index
6 #' @param data Object of class \code{Data}
9 dateIndexToInteger = function(index, data)
11 if (is.numeric(index))
12 index = as.integer(index)
13 #Undocumented "private" method to translate index --> date (is needed)
14 if (is.integer(index))
16 if (is.character(index))
18 tryCatch(dt <- as.POSIXct(index[1]), finally=print("Unrecognized index format"))
19 #TODO: tz arg to difftime ?
20 integerIndex <- as.integer( difftime(dt, data$getTime(1)) ) + 1
21 if (integerIndex > 0 && integerIndex <= data$getSize())
23 stop("Date outside data range")
25 stop("Unrecognized index format")
28 #' @title getSimilarDaysIndices
30 #' @description Find similar days indices in the past
32 #' @param index Day index (numeric or date)
33 #' @param limit Maximum number of indices to return
34 #' @param same_seaon Should the indices correspond to day in same season?
37 getSimilarDaysIndices = function(index, limit, same_season)
39 index = dateIndexToInteger(index)
41 #TODO: mardi similaire à lundi mercredi jeudi aussi ...etc
44 #take all similar days in recent past
45 nb_days = min( (index-1) %/% 7, limit)
46 return ( rep(index,nb_days) - 7*seq_len(nb_days) )
49 #Look for similar days in similar season (+/- 30 days)
52 while (i >= 1 && length(days) < limit)
57 #look in the "future of the past"
61 #...and in the "past of the past"
67 # TODO: exact computation instead of -364
68 # 364 = closest multiple of 7 to 365 - drift along the years... but not so many years so OK
72 return ( days[1:min(limit,length(days))] )