Commit | Line | Data |
---|---|---|
3d69ff21 BA |
1 | #' @title Acquire data in a clean format |
2 | #' | |
72b9c501 BA |
3 | #' @description Take in input data frames and/or files containing raw data, and |
4 | #' timezones, and output a Data object, roughly corresponding to a list where each cell | |
5 | #' contains all value for one day (see \code{?Data}). | |
3d69ff21 BA |
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 | |
72b9c501 BA |
9 | #' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn is |
10 | #' dates, next block are measurements for the day, and final block are exogenous | |
11 | #' forecasts | |
3d69ff21 BA |
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") | |
09cf9c19 | 16 | #' @param predict_at When does the prediction take place ? Integer, in hours. Default: 0 |
f17665c7 | 17 | #' @param limit Number of days to extract (default: Inf, for "all") |
3d69ff21 | 18 | #' |
a66a84b5 | 19 | #' @return An object of class Data |
3d69ff21 | 20 | #' |
44a9990b BA |
21 | #' @examples |
22 | #' ts_data = read.csv(system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")) | |
23 | #' exo_data = read.csv(system.file("extdata","meteo_extra_noNAs.csv",package="talweg")) | |
25b75559 | 24 | #' data = getData(ts_data, exo_data, limit=120) |
3d69ff21 | 25 | #' @export |
1e20780e BA |
26 | getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%M", |
27 | working_tz="GMT", predict_at=0, limit=Inf) | |
3d69ff21 BA |
28 | { |
29 | # Sanity checks (not full, but sufficient at this stage) | |
30 | if (!is.character(input_tz) || !is.character(working_tz)) | |
31 | stop("Bad timezone (see ?timezone)") | |
32 | input_tz = input_tz[1] | |
33 | working_tz = working_tz[1] | |
6d97bfec BA |
34 | if ( (!is.data.frame(ts_data) && !is.character(ts_data)) || |
35 | (!is.data.frame(exo_data) && !is.character(exo_data)) ) | |
613a986f | 36 | stop("Bad time-series / exogenous input (data frame or CSV file)") |
3d69ff21 BA |
37 | if (is.character(ts_data)) |
38 | ts_data = ts_data[1] | |
6d97bfec BA |
39 | if (is.character(exo_data)) |
40 | exo_data = exo_data[1] | |
09cf9c19 BA |
41 | predict_at = as.integer(predict_at)[1] |
42 | if (predict_at<0 || predict_at>23) | |
43 | stop("Bad predict_at (0-23)") | |
3d69ff21 BA |
44 | if (!is.character(date_format)) |
45 | stop("Bad date_format (character)") | |
46 | date_format = date_format[1] | |
47 | ||
48 | ts_df = | |
613a986f BA |
49 | if (is.character(ts_data)) |
50 | read.csv(ts_data) | |
51 | else | |
3d69ff21 | 52 | ts_data |
7f90df63 BA |
53 | # Convert to the desired timezone (usually "GMT" or "Europe/Paris") |
54 | formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz) | |
72b9c501 BA |
55 | ts_df[,1] = format( |
56 | as.POSIXct(formatted_dates_POSIXlt, tz=input_tz), tz=working_tz, usetz=TRUE) | |
7f90df63 | 57 | |
3d69ff21 | 58 | exo_df = |
613a986f | 59 | if (is.character(exo_data)) |
3d69ff21 | 60 | read.csv(exo_data) |
613a986f | 61 | else |
3d69ff21 | 62 | exo_data |
7f90df63 | 63 | # Times in exogenous variables file are ignored: no conversions required |
3d69ff21 | 64 | |
3d69ff21 BA |
65 | line = 1 #index in PM10 file (24 lines for 1 cell) |
66 | nb_lines = nrow(ts_df) | |
67 | nb_exos = ( ncol(exo_df) - 1 ) / 2 | |
a66a84b5 | 68 | data = Data$new() |
3d69ff21 BA |
69 | i = 1 #index of a cell in data |
70 | while (line <= nb_lines) | |
71 | { | |
72 | time = c() | |
73 | serie = c() | |
09cf9c19 | 74 | repeat |
3d69ff21 | 75 | { |
09cf9c19 BA |
76 | { |
77 | time = c(time, ts_df[line,1]) | |
78 | serie = c(serie, ts_df[line,2]) | |
79 | line = line + 1 | |
80 | }; | |
72b9c501 BA |
81 | if (line >= nb_lines + 1 |
82 | || as.POSIXlt(ts_df[line-1,1],tz=working_tz)$hour == predict_at) | |
83 | { | |
09cf9c19 | 84 | break |
72b9c501 | 85 | } |
09cf9c19 | 86 | } |
3d69ff21 | 87 | |
72b9c501 | 88 | hat_exo = as.data.frame( exo_df[i,(1+nb_exos+1):(1+2*nb_exos)] ) |
f17665c7 | 89 | exo = as.data.frame( exo_df[i,2:(1+nb_exos)] ) |
2057c793 | 90 | data$appendHat(time, hat_exo) |
72b9c501 | 91 | data$append(serie, exo) #in realtime, this call comes hours later |
1e20780e BA |
92 | if (i >= limit) |
93 | break | |
94 | i = i + 1 | |
3d69ff21 | 95 | } |
a66a84b5 BA |
96 | if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2))) |
97 | data$removeFirst() | |
98 | if (length(data$getCenteredSerie(data$getSize())) | |
99 | < length(data$getCenteredSerie(data$getSize()-1))) | |
f17665c7 | 100 | { |
a66a84b5 | 101 | data$removeLast() |
f17665c7 | 102 | } |
f17665c7 | 103 | data |
3d69ff21 | 104 | } |