No longer direct predict for Neighbors2: recollement comme Neighbors (better)
[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{
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
38integerIndexToDate = 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#'
4e25de2c 56#' Find similar days indices in the past.
3d69ff21
BA
57#'
58#' @param index Day index (numeric or date)
ee8b1b4e 59#' @param data Reference dataset, object output of \code{getData}
3d69ff21 60#' @param limit Maximum number of indices to return
4e25de2c 61#' @param same_season Should the indices correspond to day in same season?
6774e53d 62#' @param days_in Optional set to intersect with results (NULL to discard)
3d69ff21
BA
63#'
64#' @export
6774e53d 65getSimilarDaysIndices = function(index, data, limit, same_season, days_in=NULL)
3d69ff21 66{
ee8b1b4e 67 index = dateIndexToInteger(index, data)
3d69ff21 68
ee8b1b4e
BA
69 # Look for similar days (optionally in same season)
70 i = index - 1
3d69ff21 71 days = c()
ee8b1b4e
BA
72 dt_ref = as.POSIXlt(data$getTime(index)[1]) #first date-time of current day
73 day_ref = dt_ref$wday #1=monday, ..., 6=saturday, 0=sunday
74 month_ref = as.POSIXlt(data$getTime(index)[1])$mon+1 #month in 1...12
3d69ff21
BA
75 while (i >= 1 && length(days) < limit)
76 {
ee8b1b4e 77 dt = as.POSIXlt(data$getTime(i)[1])
6774e53d 78 if ((is.null(days_in) || i %in% days_in) && .isSameDay(dt$wday, day_ref))
ee8b1b4e
BA
79 {
80 if (!same_season || .isSameSeason(dt$mon+1, month_ref))
81 days = c(days, i)
82 }
83 i = i - 1
3d69ff21 84 }
a866acb3
BA
85 return ( days )
86}
3d69ff21 87
ee8b1b4e
BA
88# isSameSeason
89#
90# Check if two months fall in the same "season" (defined by estimated pollution rate)
91#
92# @param month month index to test
93# @param month_ref month to compare to
94#
95.isSameSeason = function(month, month_ref)
a866acb3 96{
ee8b1b4e 97 if (month_ref %in% c(11,12,1,2)) #~= mid-polluted
a866acb3 98 return (month %in% c(11,12,1,2))
ee8b1b4e 99 if (month_ref %in% c(3,4,9,10)) #~= high-polluted
a866acb3 100 return (month %in% c(3,4,9,10))
ee8b1b4e 101 return (month %in% c(5,6,7,8)) #~= non polluted
3d69ff21 102}
25b75559 103
ee8b1b4e
BA
104# isSameDay
105#
106# Monday=Tuesday=Wednesday=Thursday ; Friday, Saturday, Sunday: specials
107#
108# @param day day index to test
109# @param day_ref day index to compare to
110#
111.isSameDay = function(day, day_ref)
112{
113 if (day_ref == 0)
114 return (day==0)
115 if (day_ref <= 4)
116 return (day <= 4)
117 return (day == day_ref)
118}
a866acb3 119
98e958ca
BA
120#' getNoNA2
121#'
122#' Get indices in data of no-NA series followed by no-NA, within [first,last] range.
123#'
124#' @param data Object of class Data
125#' @param first First index (included)
126#' @param last Last index (included)
127#'
128#' @export
129getNoNA2 = function(data, first, last)
130{
131 (first:last)[ sapply(first:last, function(i)
132 !any( is.na(data$getCenteredSerie(i)) | is.na(data$getCenteredSerie(i+1)) )
133 ) ]
134}