Improve documentation
[talweg.git] / pkg / R / getData.R
CommitLineData
102bcfda 1#' getData
3d69ff21 2#'
102bcfda
BA
3#' Acquire data as a Data object; see ?Data.
4#'
5#' Since series are given in columns (database format), this function builds series one
6#' by one and incrementally grows a Data object which is finally returned.
3d69ff21
BA
7#'
8#' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time,
102bcfda
BA
9#' second is value) or a CSV file.
10#' @param exo_data Exogenous variables, as a data frame or a CSV file; first column is
72b9c501 11#' dates, next block are measurements for the day, and final block are exogenous
102bcfda 12#' forecasts (for the same day).
3d69ff21
BA
13#' @param input_tz Timezone in the input files ("GMT" or e.g. "Europe/Paris")
14#' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
102bcfda 15#' see ?strptime)
3d69ff21 16#' @param working_tz Timezone to work with ("GMT" or e.g. "Europe/Paris")
102bcfda 17#' @param predict_at When does the prediction take place? Integer, in hours. Default: 0
f17665c7 18#' @param limit Number of days to extract (default: Inf, for "all")
3d69ff21 19#'
a66a84b5 20#' @return An object of class Data
3d69ff21 21#'
44a9990b
BA
22#' @examples
23#' ts_data = read.csv(system.file("extdata","pm10_mesures_H_loc.csv",package="talweg"))
24#' exo_data = read.csv(system.file("extdata","meteo_extra_noNAs.csv",package="talweg"))
25b75559 25#' data = getData(ts_data, exo_data, limit=120)
3d69ff21 26#' @export
1e20780e
BA
27getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%M",
28 working_tz="GMT", predict_at=0, limit=Inf)
3d69ff21
BA
29{
30 # Sanity checks (not full, but sufficient at this stage)
31 if (!is.character(input_tz) || !is.character(working_tz))
32 stop("Bad timezone (see ?timezone)")
33 input_tz = input_tz[1]
34 working_tz = working_tz[1]
6d97bfec
BA
35 if ( (!is.data.frame(ts_data) && !is.character(ts_data)) ||
36 (!is.data.frame(exo_data) && !is.character(exo_data)) )
613a986f 37 stop("Bad time-series / exogenous input (data frame or CSV file)")
3d69ff21
BA
38 if (is.character(ts_data))
39 ts_data = ts_data[1]
6d97bfec
BA
40 if (is.character(exo_data))
41 exo_data = exo_data[1]
09cf9c19
BA
42 predict_at = as.integer(predict_at)[1]
43 if (predict_at<0 || predict_at>23)
44 stop("Bad predict_at (0-23)")
3d69ff21
BA
45 if (!is.character(date_format))
46 stop("Bad date_format (character)")
47 date_format = date_format[1]
48
49 ts_df =
613a986f
BA
50 if (is.character(ts_data))
51 read.csv(ts_data)
52 else
3d69ff21 53 ts_data
7f90df63
BA
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)
72b9c501
BA
56 ts_df[,1] = format(
57 as.POSIXct(formatted_dates_POSIXlt, tz=input_tz), tz=working_tz, usetz=TRUE)
7f90df63 58
3d69ff21 59 exo_df =
613a986f 60 if (is.character(exo_data))
3d69ff21 61 read.csv(exo_data)
613a986f 62 else
3d69ff21 63 exo_data
7f90df63 64 # Times in exogenous variables file are ignored: no conversions required
3d69ff21 65
3d69ff21
BA
66 line = 1 #index in PM10 file (24 lines for 1 cell)
67 nb_lines = nrow(ts_df)
68 nb_exos = ( ncol(exo_df) - 1 ) / 2
a66a84b5 69 data = Data$new()
3d69ff21
BA
70 i = 1 #index of a cell in data
71 while (line <= nb_lines)
72 {
73 time = c()
74 serie = c()
09cf9c19 75 repeat
3d69ff21 76 {
09cf9c19
BA
77 {
78 time = c(time, ts_df[line,1])
79 serie = c(serie, ts_df[line,2])
80 line = line + 1
81 };
72b9c501
BA
82 if (line >= nb_lines + 1
83 || as.POSIXlt(ts_df[line-1,1],tz=working_tz)$hour == predict_at)
84 {
09cf9c19 85 break
72b9c501 86 }
09cf9c19 87 }
3d69ff21 88
f17665c7 89 exo = as.data.frame( exo_df[i,2:(1+nb_exos)] )
4f5204f0
BA
90 exo_hat =
91 if (i < nrow(exo_df))
92 as.data.frame( exo_df[i+1,(1+nb_exos+1):(1+2*nb_exos)] )
93 else
94 NA #exogenous prediction for next day are useless on last day
c1be9898 95 data$append(time, serie, exo, exo_hat)
1e20780e
BA
96 if (i >= limit)
97 break
98 i = i + 1
3d69ff21 99 }
a66a84b5
BA
100 if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2)))
101 data$removeFirst()
102 if (length(data$getCenteredSerie(data$getSize()))
103 < length(data$getCenteredSerie(data$getSize()-1)))
f17665c7 104 {
a66a84b5 105 data$removeLast()
f17665c7 106 }
f17665c7 107 data
3d69ff21 108}