intermediate: R6, too slow
[talweg.git] / pkg / R / utils.R
CommitLineData
25b75559 1#' dateIndexToInteger
3d69ff21 2#'
25b75559 3#' Transform a (potential) date index into an integer (relative to data)
3d69ff21
BA
4#'
5#' @param index Date (or integer) index
6#' @param data Object of class \code{Data}
7#'
8#' @export
9dateIndexToInteger = function(index, data)
10{
09cf9c19 11 index = index[1]
3d69ff21
BA
12 if (is.numeric(index))
13 index = as.integer(index)
3d69ff21 14 if (is.integer(index))
09cf9c19
BA
15 return (index)
16 if (inherits(index, "Date") || is.character(index))
3d69ff21 17 {
44a9990b 18 tryCatch(indexAsDate <- as.Date(index), error=function(e) stop("Unrecognized index format"))
3d69ff21 19 #TODO: tz arg to difftime ?
44a9990b
BA
20 integerIndex <- round( as.numeric(
21 difftime(indexAsDate, as.Date(data$getTime(1)[1])) ) ) + 1
22 if (integerIndex >= 1 && integerIndex <= data$getSize())
25b75559 23 return (integerIndex)
3d69ff21
BA
24 stop("Date outside data range")
25 }
26 stop("Unrecognized index format")
27}
28
25b75559 29#' integerIndexToDate
09cf9c19 30#'
25b75559 31#' Transform an integer index to date index (relative to data)
09cf9c19
BA
32#'
33#' @param index Date (or integer) index
34#' @param data Object of class \code{Data}
35#'
36#' @export
37integerIndexToDate = function(index, data)
38{
39 index = index[1]
40 if (is.numeric(index))
41 index = as.integer(index)
42 if (!is.integer(index))
43 stop("'index' should be an integer")
44 as.Date( data$getTime(index)[1] )
45}
46
25b75559 47#' getSimilarDaysIndices
3d69ff21 48#'
25b75559 49#' Find similar days indices in the past
3d69ff21
BA
50#'
51#' @param index Day index (numeric or date)
52#' @param limit Maximum number of indices to return
53#' @param same_seaon Should the indices correspond to day in same season?
54#'
55#' @export
56getSimilarDaysIndices = function(index, limit, same_season)
57{
58 index = dateIndexToInteger(index)
59
60 #TODO: mardi similaire à lundi mercredi jeudi aussi ...etc
61 if (!same_season)
62 {
63 #take all similar days in recent past
64 nb_days = min( (index-1) %/% 7, limit)
65 return ( rep(index,nb_days) - 7*seq_len(nb_days) )
66 }
67
68 #Look for similar days in similar season (+/- 30 days)
69 days = c()
70 i = index
71 while (i >= 1 && length(days) < limit)
72 {
73 if (i < index)
74 {
75 days = c(days, i)
76 #look in the "future of the past"
77 for (j in 1:4)
78 days = c(days, i+7*j)
79 }
80 #...and in the "past of the past"
81 for (j in 1:4)
82 {
83 if (i - 7*j >= 1)
84 days = c(days, i-7*j)
85 }
86 # TODO: exact computation instead of -364
87 # 364 = closest multiple of 7 to 365 - drift along the years... but not so many years so OK
88 i = i - 364
89 }
90
91 return ( days[1:min(limit,length(days))] )
92}
25b75559
BA
93
94#' getSerie
95#'
96#' Return a time-serie from its centered version + level
97#'
98#' @param data A list as returned by \code{getData}
99#' @param index The index to return
100#'
101#' @export
102getSerie = function(data, index)
103 data[[index]]$centered_serie + data[[index]]$level