1 #' @title Acquire data in a clean format
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}). Current limitation: series (in working_tz) must start at
6 #' right after midnight (to keep in sync with exogenous vars)
8 #' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time,
9 #' second is value) or a CSV file
10 #' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn is dates,
11 #' next block are measurements for the day, and final block are exogenous forecasts
12 #' @param input_tz Timezone in the input files ("GMT" or e.g. "Europe/Paris")
13 #' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
14 #' see \code{strptime})
15 #' @param working_tz Timezone to work with ("GMT" or e.g. "Europe/Paris")
16 #' @param predict_at When does the prediction take place ? ab[:cd][:ef] where a,b,c,d,e,f
17 #' in (0,9) and define an hour[minute[second]]; time must be present in the file
19 #' @return An object of class Data
22 getData = function(ts_data, exo_data,
23 input_tz="GMT", date_format="%d/%m/%Y %H:%M", working_tz="GMT", predict_at="00")
25 # Sanity checks (not full, but sufficient at this stage)
26 if (!is.character(input_tz) || !is.character(working_tz))
27 stop("Bad timezone (see ?timezone)")
28 input_tz = input_tz[1]
29 working_tz = working_tz[1]
30 if (!is.data.frame(ts_data) && !is.character(ts_data))
31 stop("Bad time-series input (data frame or CSV file)")
32 if (is.character(ts_data))
34 pattern_index_in_predict_at = grep("^[0-9]{2}(:[0-9]{2}){0,2}$", predict_at)
35 if (!is.character(predict_at) || length(pattern_index_in_predict_at) == 0)
36 stop("Bad predict_at ( ^[0-9]{2}(:[0-9]{2}){0,2}$ )")
37 predict_at = predict_at[ pattern_index_in_predict_at[1] ]
38 if (!is.character(date_format))
39 stop("Bad date_format (character)")
40 date_format = date_format[1]
43 if (is.character(ts_data)) {
49 if (is.character(exo_data)) {
54 # Convert to the desired timezone (usually "GMT" or "Europe/Paris")
55 formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz)
56 ts_df[,1] = format(as.POSIXct(formatted_dates_POSIXlt), tz=working_tz, usetz=TRUE)
58 if (nchar(predict_at) == 2)
59 predict_at = paste(predict_at,":00",sep="")
60 if (nchar(predict_at) == 5)
61 predict_at = paste(predict_at,":00",sep="")
63 line = 1 #index in PM10 file (24 lines for 1 cell)
64 nb_lines = nrow(ts_df)
65 nb_exos = ( ncol(exo_df) - 1 ) / 2
66 data = list() #new("Data")
67 i = 1 #index of a cell in data
68 while (line <= nb_lines)
74 time = c(time, ts_df[line,1])
75 serie = c(serie, ts_df[line,2])
78 if (line >= nb_lines + 1
79 # NOTE: always second part of date/time, because it has been formatted
80 || strsplit(as.character(ts_df[line-1,1])," ")[[1]][2] == predict_at)
85 # NOTE: if predict_at does not cut days at midnight,
86 # for the exogenous to be synchronized they need to be shifted
87 if (predict_at != "00:00:00")
89 exo_hat = as.data.frame(exo_df[max(1,i-1),(1+nb_exos+1):(1+2*nb_exos)])
90 exo_Dm1 = if (i>=3) as.data.frame(exo_df[i-1,2:(1+nb_exos)]) else NA
93 exo_hat = as.data.frame(exo_df[i,(1+nb_exos+1):(1+2*nb_exos)])
94 exo_Dm1 = if (i>=2) as.data.frame(exo_df[i-1,2:(1+nb_exos)]) else NA
98 level = mean(serie, na.rm=TRUE)
99 centered_serie = serie - level
100 # data$append(time, centered_serie, level, exo_hat, exo_Jm1) #TODO: slow: why ?
101 data[[length(data)+1]] = list("time"=time, "serie"=centered_serie, "level"=level,
102 "exo_hat"=exo_hat, "exo_Dm1"=exo_Dm1)
104 new("Data",data=data)