update documention, fix package to compete with 'method Bruno'
[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 13#' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
102bcfda 14#' see ?strptime)
f17665c7 15#' @param limit Number of days to extract (default: Inf, for "all")
3d69ff21 16#'
a66a84b5 17#' @return An object of class Data
3d69ff21 18#'
44a9990b
BA
19#' @examples
20#' ts_data = read.csv(system.file("extdata","pm10_mesures_H_loc.csv",package="talweg"))
21#' exo_data = read.csv(system.file("extdata","meteo_extra_noNAs.csv",package="talweg"))
25b75559 22#' data = getData(ts_data, exo_data, limit=120)
3d69ff21 23#' @export
d2ab47a7 24getData = function(ts_data, exo_data, date_format="%d/%m/%Y %H:%M", limit=Inf)
3d69ff21
BA
25{
26 # Sanity checks (not full, but sufficient at this stage)
6d97bfec
BA
27 if ( (!is.data.frame(ts_data) && !is.character(ts_data)) ||
28 (!is.data.frame(exo_data) && !is.character(exo_data)) )
613a986f 29 stop("Bad time-series / exogenous input (data frame or CSV file)")
3d69ff21
BA
30 if (is.character(ts_data))
31 ts_data = ts_data[1]
6d97bfec
BA
32 if (is.character(exo_data))
33 exo_data = exo_data[1]
3d69ff21
BA
34 if (!is.character(date_format))
35 stop("Bad date_format (character)")
36 date_format = date_format[1]
d2ab47a7
BA
37 if (!is.numeric(limit) || limit < 0)
38 stop("limit: positive integer")
3d69ff21
BA
39
40 ts_df =
613a986f
BA
41 if (is.character(ts_data))
42 read.csv(ts_data)
43 else
3d69ff21 44 ts_data
d2ab47a7
BA
45 # Convert to GMT (pretend it's GMT; no impact)
46 dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz="GMT")
47 ts_df[,1] = format(as.POSIXct(dates_POSIXlt, tz="GMT"), tz="GMT", usetz=TRUE)
7f90df63 48
3d69ff21 49 exo_df =
613a986f 50 if (is.character(exo_data))
3d69ff21 51 read.csv(exo_data)
613a986f 52 else
3d69ff21 53 exo_data
7f90df63 54 # Times in exogenous variables file are ignored: no conversions required
3d69ff21 55
3d69ff21
BA
56 line = 1 #index in PM10 file (24 lines for 1 cell)
57 nb_lines = nrow(ts_df)
58 nb_exos = ( ncol(exo_df) - 1 ) / 2
a66a84b5 59 data = Data$new()
3d69ff21
BA
60 i = 1 #index of a cell in data
61 while (line <= nb_lines)
62 {
63 time = c()
64 serie = c()
09cf9c19 65 repeat
3d69ff21 66 {
09cf9c19
BA
67 {
68 time = c(time, ts_df[line,1])
69 serie = c(serie, ts_df[line,2])
70 line = line + 1
71 };
72b9c501 72 if (line >= nb_lines + 1
d2ab47a7 73 || as.POSIXlt(ts_df[line-1,1],tz="GMT")$hour == 0)
72b9c501 74 {
09cf9c19 75 break
72b9c501 76 }
09cf9c19 77 }
3d69ff21 78
d2ab47a7
BA
79 # TODO: 2 modes, "operational" and "testing"; would need PM10 predictions
80 data$append(time=time, value=serie, level_hat=mean(serie,na.rm=TRUE),
81 exo=exo_df[i,2:(1+nb_exos)], exo_hat=exo_df[i,(1+nb_exos+1):(1+2*nb_exos)])
1e20780e
BA
82 if (i >= limit)
83 break
84 i = i + 1
3d69ff21 85 }
f17665c7 86 data
3d69ff21 87}