3 #' Acquire data as a Data object; see ?Data.
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.
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;
15 #' @param limit Number of days to extract (default: Inf, for "all")
17 #' @return An object of class Data
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)
24 getData = function(ts_data, exo_data, date_format="%d/%m/%Y %H:%M", limit=Inf)
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))
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")
41 if (is.character(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)
50 if (is.character(exo_data))
54 # Times in exogenous variables file are ignored: no conversions required
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
60 i = 1 #index of a cell in data
61 while (line <= nb_lines)
69 time = c(time, ts_df[line,1])
70 serie = c(serie, ts_df[line,2])
71 level_hat = c(level_hat, #in case of data file is incomplete...
72 ifelse(ncol(ts_df) > 2, ts_df[line,3], mean(serie,na.rm=TRUE)))
75 if (line >= nb_lines + 1
76 || as.POSIXlt(ts_df[line-1,1],tz="GMT")$hour == 0)
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)])