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