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