X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FgetData.R;h=d0e69d73daaf374c2e655d9b82df7de5090aaa56;hb=0730374e2981d7b950dced9f667713865b44b390;hp=b095d01ea3a4c7002b79cdfecd4caf98757846c7;hpb=2057c793ad9929ed5bef8663ea28b896c84df0fc;p=talweg.git diff --git a/pkg/R/getData.R b/pkg/R/getData.R index b095d01..d0e69d7 100644 --- a/pkg/R/getData.R +++ b/pkg/R/getData.R @@ -1,19 +1,17 @@ -#' @title Acquire data in a clean format +#' getData #' -#' @description Take in input data frames and/or files containing raw data, and -#' timezones, and output a Data object, roughly corresponding to a list where each cell -#' contains all value for one day (see \code{?Data}). +#' Acquire data as a Data object; see ?Data. +#' +#' Since series are given in columns (database format), this function builds series one +#' by one and incrementally grows a Data object which is finally returned. #' #' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time, -#' second is value) or a CSV file -#' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn is +#' second is value) or a CSV file. +#' @param exo_data Exogenous variables, as a data frame or a CSV file; first column is #' dates, next block are measurements for the day, and final block are exogenous -#' forecasts -#' @param input_tz Timezone in the input files ("GMT" or e.g. "Europe/Paris") +#' forecasts (for the same day). #' @param date_format How date/time are stored (e.g. year/month/day hour:minutes; -#' see \code{strptime}) -#' @param working_tz Timezone to work with ("GMT" or e.g. "Europe/Paris") -#' @param predict_at When does the prediction take place ? Integer, in hours. Default: 0 +#' see ?strptime) #' @param limit Number of days to extract (default: Inf, for "all") #' #' @return An object of class Data @@ -23,14 +21,9 @@ #' exo_data = read.csv(system.file("extdata","meteo_extra_noNAs.csv",package="talweg")) #' data = getData(ts_data, exo_data, limit=120) #' @export -getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%M", - working_tz="GMT", predict_at=0, limit=Inf) +getData = function(ts_data, exo_data, date_format="%d/%m/%Y %H:%M", limit=Inf) { # Sanity checks (not full, but sufficient at this stage) - if (!is.character(input_tz) || !is.character(working_tz)) - stop("Bad timezone (see ?timezone)") - input_tz = input_tz[1] - working_tz = working_tz[1] if ( (!is.data.frame(ts_data) && !is.character(ts_data)) || (!is.data.frame(exo_data) && !is.character(exo_data)) ) stop("Bad time-series / exogenous input (data frame or CSV file)") @@ -38,22 +31,20 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:% ts_data = ts_data[1] if (is.character(exo_data)) exo_data = exo_data[1] - predict_at = as.integer(predict_at)[1] - if (predict_at<0 || predict_at>23) - stop("Bad predict_at (0-23)") if (!is.character(date_format)) stop("Bad date_format (character)") date_format = date_format[1] + if (!is.numeric(limit) || limit < 0) + stop("limit: positive integer") ts_df = if (is.character(ts_data)) read.csv(ts_data) else ts_data - # Convert to the desired timezone (usually "GMT" or "Europe/Paris") - formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz) - ts_df[,1] = format( - as.POSIXct(formatted_dates_POSIXlt, tz=input_tz), tz=working_tz, usetz=TRUE) + # Convert to GMT (pretend it's GMT; no impact) + dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz="GMT") + ts_df[,1] = format(as.POSIXct(dates_POSIXlt, tz="GMT"), tz="GMT", usetz=TRUE) exo_df = if (is.character(exo_data)) @@ -71,34 +62,28 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:% { time = c() serie = c() + level_hat = c() repeat { { time = c(time, ts_df[line,1]) serie = c(serie, ts_df[line,2]) + level_hat = c(level_hat, #in case of data file is incomplete... + ifelse(ncol(ts_df) > 2, ts_df[line,3], mean(serie,na.rm=TRUE))) line = line + 1 }; if (line >= nb_lines + 1 - || as.POSIXlt(ts_df[line-1,1],tz=working_tz)$hour == predict_at) + || as.POSIXlt(ts_df[line-1,1],tz="GMT")$hour == 0) { break } } - hat_exo = as.data.frame( exo_df[i,(1+nb_exos+1):(1+2*nb_exos)] ) - exo = as.data.frame( exo_df[i,2:(1+nb_exos)] ) - data$appendHat(time, hat_exo) - data$append(serie, exo) #in realtime, this call comes hours later + data$append(time=time, value=serie, level_hat=level_hat, + exo=exo_df[i,2:(1+nb_exos)], exo_hat=exo_df[i,(1+nb_exos+1):(1+2*nb_exos)]) if (i >= limit) break i = i + 1 } - if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2))) - data$removeFirst() - if (length(data$getCenteredSerie(data$getSize())) - < length(data$getCenteredSerie(data$getSize()-1))) - { - data$removeLast() - } data }