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)
12 if (is.numeric(index))
13 index = as.integer(index)
14 if (is.integer(index))
16 if (inherits(index, "Date") || is.character(index))
18 tryCatch(dt <- as.POSIXct(index), error=function(e) stop("Unrecognized index format"))
19 #TODO: tz arg to difftime ?
20 integerIndex <- round( (as.numeric( difftime(dt, data$getTime(1)) ))[1] ) + 1
21 if (integerIndex > 0 && integerIndex <= data$getSize())
23 #WARNING: if series start at date >0h, result must be shifted
24 date1 = as.POSIXlt(data$getTime(1)[1])
25 date2 = as.POSIXlt(data$getTime(2)[1])
26 shift = (date1$year==date2$year && date1$mon==date2$mon && date1$mday==date2$mday)
27 return (integerIndex + ifelse(shift,1,0))
29 stop("Date outside data range")
31 stop("Unrecognized index format")
34 #' @title integerIndexToDate
36 #' @description Transform an integer index to date index (relative to data)
38 #' @param index Date (or integer) index
39 #' @param data Object of class \code{Data}
42 integerIndexToDate = function(index, data)
45 if (is.numeric(index))
46 index = as.integer(index)
47 if (!is.integer(index))
48 stop("'index' should be an integer")
49 as.Date( data$getTime(index)[1] )
52 #' @title getSimilarDaysIndices
54 #' @description Find similar days indices in the past
56 #' @param index Day index (numeric or date)
57 #' @param limit Maximum number of indices to return
58 #' @param same_seaon Should the indices correspond to day in same season?
61 getSimilarDaysIndices = function(index, limit, same_season)
63 index = dateIndexToInteger(index)
65 #TODO: mardi similaire à lundi mercredi jeudi aussi ...etc
68 #take all similar days in recent past
69 nb_days = min( (index-1) %/% 7, limit)
70 return ( rep(index,nb_days) - 7*seq_len(nb_days) )
73 #Look for similar days in similar season (+/- 30 days)
76 while (i >= 1 && length(days) < limit)
81 #look in the "future of the past"
85 #...and in the "past of the past"
91 # TODO: exact computation instead of -364
92 # 364 = closest multiple of 7 to 365 - drift along the years... but not so many years so OK
96 return ( days[1:min(limit,length(days))] )