'update'
[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()
0730374e 65 level_hat = c()
09cf9c19 66 repeat
3d69ff21 67 {
09cf9c19
BA
68 {
69 time = c(time, ts_df[line,1])
70 serie = c(serie, ts_df[line,2])
a3344f75 71 level_hat = c(level_hat, #if data file is incomplete...
0730374e 72 ifelse(ncol(ts_df) > 2, ts_df[line,3], mean(serie,na.rm=TRUE)))
09cf9c19
BA
73 line = line + 1
74 };
72b9c501 75 if (line >= nb_lines + 1
d2ab47a7 76 || as.POSIXlt(ts_df[line-1,1],tz="GMT")$hour == 0)
72b9c501 77 {
09cf9c19 78 break
72b9c501 79 }
09cf9c19 80 }
3d69ff21 81
0730374e 82 data$append(time=time, value=serie, level_hat=level_hat,
d2ab47a7 83 exo=exo_df[i,2:(1+nb_exos)], exo_hat=exo_df[i,(1+nb_exos+1):(1+2*nb_exos)])
1e20780e
BA
84 if (i >= limit)
85 break
86 i = i + 1
3d69ff21 87 }
f17665c7 88 data
3d69ff21 89}