3 #' Transform a (potential) date index into an integer (relative to data beginning).
5 #' @param index Date (or integer) index
6 #' @param data Object of class Data, output of \code{getData()}
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),
20 error=function(e) stop("Unrecognized index format"))
21 #TODO: tz arg to difftime ?
22 integerIndex <- round( as.numeric(
23 difftime(indexAsDate, as.Date(data$getTime(1)[1])) ) ) + 1
24 if (integerIndex >= 1 && integerIndex <= data$getSize())
26 stop("Date outside data range")
28 stop("Unrecognized index format")
33 #' Transform an integer index (relative to data beginning) into a date index.
35 #' @inheritParams dateIndexToInteger
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; at least same type of day in the week:
57 #' monday=tuesday=wednesday=thursday != friday != saturday != sunday.
59 #' @param index Day index (numeric or date)
60 #' @param data Reference dataset, object output of \code{getData}
61 #' @param limit Maximum number of indices to return
62 #' @param same_season Should the indices correspond to day in same season?
63 #' @param days_in Optional set to intersect with results (NULL to discard)
66 getSimilarDaysIndices = function(index, data, limit, same_season,
67 days_in=NULL, operational=TRUE)
69 index = dateIndexToInteger(index, data)
71 # Look for similar days (optionally in same season)
73 dt_ref = as.POSIXlt(data$getTime(index)[1]) #first date-time of current day
74 day_ref = dt_ref$wday #1=monday, ..., 6=saturday, 0=sunday
75 month_ref = as.POSIXlt(data$getTime(index)[1])$mon+1 #month in 1...12
79 while (length(days) < min( limit, ifelse(is.null(days_in),Inf,length(days_in)) ))
81 if (i < 1 && j > data$getSize())
85 dt = as.POSIXlt(data$getTime(i)[1])
86 if ((is.null(days_in) || i %in% days_in) && .isSameDay(dt$wday, day_ref))
88 if (!same_season || .isSameSeason(dt$mon+1, month_ref))
93 if (!operational && j <= data$getSize())
95 dt = as.POSIXlt(data$getTime(j)[1])
96 if ((is.null(days_in) || j %in% days_in) && .isSameDay(dt$wday, day_ref))
98 if (!same_season || .isSameSeason(dt$mon+1, month_ref))
109 # Check if two months fall in the same "season" (defined by estimated pollution rate).
111 # @param month Month index to test
112 # @param month_ref Month to compare to
114 .isSameSeason = function(month, month_ref)
116 if (month_ref == 3) #TODO: same as Bruno (but weird)
117 return (month %in% c(2,3,4,9,10))
118 if (month_ref %in% c(11,12,1,2)) #~= mid-polluted
119 return (month %in% c(11,12,1,2))
120 if (month_ref %in% c(3,4,9,10)) #~= high-polluted
121 return (month %in% c(3,4,9,10))
122 return (month %in% c(5,6,7,8)) #~= non polluted
127 # Monday=Tuesday=Wednesday=Thursday ; Friday, Saturday, Sunday: specials.
129 # @param day Day index to test
130 # @param day_ref Day index to compare to
132 .isSameDay = function(day, day_ref)
134 if (day_ref %in% 1:4)
135 return (day %in% 1:4)
136 return (day == day_ref)
141 # Get indices in data of no-NA series preceded by no-NA, within [first,last] range.
143 # @inheritParams dateIndexToInteger
144 # @param first First index (included)
145 # @param last Last index (included)
147 .getNoNA2 = function(data, first, last)
149 (first:last)[ sapply(first:last, function(i)
150 !any( is.na(data$getSerie(i-1)) | is.na(data$getSerie(i)) )