3 #' 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)
11 #works on integers too: trust input
12 if (is.numeric(index))
13 index = as.integer(index)
14 if (is.integer(index))
17 if (inherits(index, "Date") || is.character(index))
19 tryCatch(indexAsDate <- as.Date(index), error=function(e) stop("Unrecognized index format"))
20 #TODO: tz arg to difftime ?
21 integerIndex <- round( as.numeric(
22 difftime(indexAsDate, as.Date(data$getTime(1)[1])) ) ) + 1
23 if (integerIndex >= 1 && integerIndex <= data$getSize())
25 stop("Date outside data range")
27 stop("Unrecognized index format")
32 #' Transform an integer index to date index (relative to data)
34 #' @param index Date (or integer) index
35 #' @param data Object of class \code{Data}
38 integerIndexToDate = function(index, data)
40 #works on dates too: trust input
41 if (is.character(index))
42 index = as.Date(index)
47 if (is.numeric(index))
48 index = as.integer(index)
49 if (!is.integer(index))
50 stop("'index' should be a date or integer")
51 as.Date( data$getTime(index)[1] )
54 #' getSimilarDaysIndices
56 #' Find similar days indices in the past.
58 #' @param index Day index (numeric or date)
59 #' @param data Reference dataset, object output of \code{getData}
60 #' @param limit Maximum number of indices to return
61 #' @param same_season Should the indices correspond to day in same season?
62 #' @param days_in Optional set to intersect with results (NULL to discard)
65 getSimilarDaysIndices = function(index, data, limit, same_season, days_in=NULL)
67 index = dateIndexToInteger(index, data)
69 # Look for similar days (optionally in same season)
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
75 while (i >= 1 && length(days) < limit)
77 dt = as.POSIXlt(data$getTime(i)[1])
78 if ((is.null(days_in) || i %in% days_in) && .isSameDay(dt$wday, day_ref))
80 if (!same_season || .isSameSeason(dt$mon+1, month_ref))
90 # Check if two months fall in the same "season" (defined by estimated pollution rate)
92 # @param month month index to test
93 # @param month_ref month to compare to
95 .isSameSeason = function(month, month_ref)
97 if (month_ref %in% c(11,12,1,2)) #~= mid-polluted
98 return (month %in% c(11,12,1,2))
99 if (month_ref %in% c(3,4,9,10)) #~= high-polluted
100 return (month %in% c(3,4,9,10))
101 return (month %in% c(5,6,7,8)) #~= non polluted
106 # Monday=Tuesday=Wednesday=Thursday ; Friday, Saturday, Sunday: specials
108 # @param day day index to test
109 # @param day_ref day index to compare to
111 .isSameDay = function(day, day_ref)
117 return (day == day_ref)
122 #' Get indices in data of no-NA series followed by no-NA, within [first,last] range.
124 #' @param data Object of class Data
125 #' @param first First index (included)
126 #' @param last Last index (included)
129 getNoNA2 = function(data, first, last)
131 (first:last)[ sapply(first:last, function(i)
132 !any( is.na(data$getCenteredSerie(i)) | is.na(data$getCenteredSerie(i+1)) )