First commit
[talweg.git] / pkg / R / getData.R
CommitLineData
3a38473a
BA
1#' getData
2#'
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.
7#'
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 column is
11#' dates, next block are measurements for the day, and final block are exogenous
12#' forecasts (for the same day).
13#' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
14#' see ?strptime)
15#' @param limit Number of days to extract (default: Inf, for "all")
16#'
17#' @return An object of class Data
18#'
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"))
22#' data = getData(ts_data, exo_data, limit=120)
23#' @export
24getData = function(ts_data, exo_data, date_format="%d/%m/%Y %H:%M", limit=Inf)
25{
26 # Sanity checks (not full, but sufficient at this stage)
27 if ( (!is.data.frame(ts_data) && !is.character(ts_data)) ||
28 (!is.data.frame(exo_data) && !is.character(exo_data)) )
29 stop("Bad time-series / exogenous input (data frame or CSV file)")
30 if (is.character(ts_data))
31 ts_data = ts_data[1]
32 if (is.character(exo_data))
33 exo_data = exo_data[1]
34 if (!is.character(date_format))
35 stop("Bad date_format (character)")
36 date_format = date_format[1]
37 if (!is.numeric(limit) || limit < 0)
38 stop("limit: positive integer")
39
40 ts_df =
41 if (is.character(ts_data))
42 read.csv(ts_data)
43 else
44 ts_data
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)
48
49 exo_df =
50 if (is.character(exo_data))
51 read.csv(exo_data)
52 else
53 exo_data
54 # Times in exogenous variables file are ignored: no conversions required
55
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
59 data = Data$new()
60 i = 1 #index of a cell in data
61 while (line <= nb_lines)
62 {
63 time = c()
64 serie = c()
65 level_hat = c()
66 repeat
67 {
68 {
69 time = c(time, ts_df[line,1])
70 serie = c(serie, ts_df[line,2])
71 level_hat = c(level_hat, #if data file is incomplete...
72 ifelse(ncol(ts_df) > 2, ts_df[line,3], mean(serie,na.rm=TRUE)))
73 line = line + 1
74 };
75 if (line >= nb_lines + 1
76 || as.POSIXlt(ts_df[line-1,1],tz="GMT")$hour == 0)
77 {
78 break
79 }
80 }
81
82 data$append(time=time, value=serie, level_hat=level_hat,
83 exo=exo_df[i,2:(1+nb_exos)], exo_hat=exo_df[i,(1+nb_exos+1):(1+2*nb_exos)])
84 if (i >= limit)
85 break
86 i = i + 1
87 }
88 data
89}