b3e66e18d109f9bd56ece2618f204422f2bd63a0
[talweg.git] / pkg / R / utils.R
1 #' dateIndexToInteger
2 #'
3 #' Transform a (potential) date index into an integer (relative to data)
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 {
11 #works on integers too: trust input
12 if (is.numeric(index))
13 index = as.integer(index)
14 if (is.integer(index))
15 return (index)
16
17 if (inherits(index, "Date") || is.character(index))
18 {
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())
24 return (integerIndex)
25 stop("Date outside data range")
26 }
27 stop("Unrecognized index format")
28 }
29
30 #' integerIndexToDate
31 #'
32 #' Transform an integer index to date index (relative to data)
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 {
40 #works on dates too: trust input
41 if (is.character(index))
42 index = as.Date(index)
43 if (is(index,"Date"))
44 return (index)
45
46 index = index[1]
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] )
52 }
53
54 #' getSimilarDaysIndices
55 #'
56 #' Find similar days indices in the past.
57 #'
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 #'
63 #' @export
64 getSimilarDaysIndices = function(index, data, limit, same_season)
65 {
66 index = dateIndexToInteger(index, data)
67
68 # Look for similar days (optionally in same season)
69 i = index - 1
70 days = c()
71 dt_ref = as.POSIXlt(data$getTime(index)[1]) #first date-time of current day
72 day_ref = dt_ref$wday #1=monday, ..., 6=saturday, 0=sunday
73 month_ref = as.POSIXlt(data$getTime(index)[1])$mon+1 #month in 1...12
74 while (i >= 1 && length(days) < limit)
75 {
76 dt = as.POSIXlt(data$getTime(i)[1])
77 if (.isSameDay(dt$wday, day_ref))
78 {
79 if (!same_season || .isSameSeason(dt$mon+1, month_ref))
80 days = c(days, i)
81 }
82 i = i - 1
83 }
84 return ( days )
85 }
86
87 # isSameSeason
88 #
89 # Check if two months fall in the same "season" (defined by estimated pollution rate)
90 #
91 # @param month month index to test
92 # @param month_ref month to compare to
93 #
94 .isSameSeason = function(month, month_ref)
95 {
96 if (month_ref %in% c(11,12,1,2)) #~= mid-polluted
97 return (month %in% c(11,12,1,2))
98 if (month_ref %in% c(3,4,9,10)) #~= high-polluted
99 return (month %in% c(3,4,9,10))
100 return (month %in% c(5,6,7,8)) #~= non polluted
101 }
102
103 # isSameDay
104 #
105 # Monday=Tuesday=Wednesday=Thursday ; Friday, Saturday, Sunday: specials
106 #
107 # @param day day index to test
108 # @param day_ref day index to compare to
109 #
110 .isSameDay = function(day, day_ref)
111 {
112 if (day_ref == 0)
113 return (day==0)
114 if (day_ref <= 4)
115 return (day <= 4)
116 return (day == day_ref)
117 }
118
119 #' getNoNA2
120 #'
121 #' Get indices in data of no-NA series followed by no-NA, within [first,last] range.
122 #'
123 #' @param data Object of class Data
124 #' @param first First index (included)
125 #' @param last Last index (included)
126 #'
127 #' @export
128 getNoNA2 = function(data, first, last)
129 {
130 (first:last)[ sapply(first:last, function(i)
131 !any( is.na(data$getCenteredSerie(i)) | is.na(data$getCenteredSerie(i+1)) )
132 ) ]
133 }