intermediate: R6, too slow
[talweg.git] / pkg / R / getData.R
1 #' @title Acquire data in a clean format
2 #'
3 #' @description Take in input data frames and/or files containing raw data, and timezones, and
4 #' output a Data object, roughly corresponding to a list where each cell contains all value
5 #' for one day (see \code{?Data}).
6 #'
7 #' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time,
8 #' second is value) or a CSV file
9 #' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn is dates,
10 #' next block are measurements for the day, and final block are exogenous forecasts
11 #' @param input_tz Timezone in the input files ("GMT" or e.g. "Europe/Paris")
12 #' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
13 #' see \code{strptime})
14 #' @param working_tz Timezone to work with ("GMT" or e.g. "Europe/Paris")
15 #' @param predict_at When does the prediction take place ? Integer, in hours. Default: 0
16 #' @param limit Number of days to extract (default: Inf, for "all")
17 #'
18 #' @return A list where data[[i]] contains
19 #' \itemize{
20 #' \item time: vector of times
21 #' \item centered_serie: centered serie
22 #' \item level: corresponding level
23 #' \item exo: exogenous variables
24 #' \item exo_hat: predicted exogenous variables
25 #' }
26 #'
27 #' @examples
28 #' ts_data = read.csv(system.file("extdata","pm10_mesures_H_loc.csv",package="talweg"))
29 #' exo_data = read.csv(system.file("extdata","meteo_extra_noNAs.csv",package="talweg"))
30 #' data = getData(ts_data, exo_data, limit=120)
31 #' @export
32 getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%M",
33 working_tz="GMT", predict_at=0, limit=Inf)
34 {
35 # Sanity checks (not full, but sufficient at this stage)
36 if (!is.character(input_tz) || !is.character(working_tz))
37 stop("Bad timezone (see ?timezone)")
38 input_tz = input_tz[1]
39 working_tz = working_tz[1]
40 if ( (!is.data.frame(ts_data) && !is.character(ts_data)) ||
41 (!is.data.frame(exo_data) && !is.character(exo_data)) )
42 stop("Bad time-series / exogenous input (data frame or CSV file)")
43 if (is.character(ts_data))
44 ts_data = ts_data[1]
45 if (is.character(exo_data))
46 exo_data = exo_data[1]
47 predict_at = as.integer(predict_at)[1]
48 if (predict_at<0 || predict_at>23)
49 stop("Bad predict_at (0-23)")
50 if (!is.character(date_format))
51 stop("Bad date_format (character)")
52 date_format = date_format[1]
53
54 ts_df =
55 if (is.character(ts_data))
56 read.csv(ts_data)
57 else
58 ts_data
59 exo_df =
60 if (is.character(exo_data))
61 read.csv(exo_data)
62 else
63 exo_data
64 # Convert to the desired timezone (usually "GMT" or "Europe/Paris")
65 formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz)
66 ts_df[,1] = format(as.POSIXct(formatted_dates_POSIXlt), tz=working_tz, usetz=TRUE)
67
68 line = 1 #index in PM10 file (24 lines for 1 cell)
69 nb_lines = nrow(ts_df)
70 nb_exos = ( ncol(exo_df) - 1 ) / 2
71 data = list()
72 i = 1 #index of a cell in data
73 while (line <= nb_lines)
74 {
75 time = c()
76 serie = c()
77 repeat
78 {
79 {
80 time = c(time, ts_df[line,1])
81 serie = c(serie, ts_df[line,2])
82 line = line + 1
83 };
84 if (line >= nb_lines + 1 || as.POSIXlt(ts_df[line-1,1])$hour == predict_at)
85 break
86 }
87
88 exo = as.data.frame( exo_df[i,2:(1+nb_exos)] )
89 exo_hat = as.data.frame( exo_df[i,(1+nb_exos+1):(1+2*nb_exos)] )
90 level = mean(serie, na.rm=TRUE)
91 centered_serie = serie - level
92 data[[i]] = list("time"=time, "centered_serie"=centered_serie, "level"=level,
93 "exo"=exo, "exo_hat"=exo_hat)
94 if (i >= limit)
95 break
96 i = i + 1
97 }
98 start = 1
99 end = length(data)
100 if (length(data[[1]]$centered_serie) < length(data[[2]]$centered_serie))
101 start = 2
102 if (length(data[[length(data)]]$centered_serie) <
103 length(data[[length(data)-1]]$centered_serie))
104 {
105 end = end-1
106 }
107 if (start>1 || end<length(data))
108 data = data[start:end]
109 data
110 }