add realtime option, slightly refactor data acquisition
[talweg.git] / pkg / R / getData.R
index e13cf86..b944dfb 100644 (file)
@@ -1,13 +1,14 @@
 #' @title Acquire data in a clean format
 #'
-#' @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}).
+#' @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}).
 #'
 #' @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 dates,
-#'   next block are measurements for the day, and final block are exogenous forecasts
+#' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn 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")
 #' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
 #'   see \code{strptime})
@@ -51,7 +52,8 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%
                        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)
+       ts_df[,1] = format(
+               as.POSIXct(formatted_dates_POSIXlt, tz=input_tz), tz=working_tz, usetz=TRUE)
 
        exo_df =
                if (is.character(exo_data))
@@ -69,22 +71,26 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%
        {
                time = c()
                serie = c()
+               hat_serie = c()
                repeat
                {
                        {
                                time = c(time, ts_df[line,1])
+                               hat_serie = c(serie, ts_df[line,3])
                                serie = c(serie, ts_df[line,2])
                                line = line + 1
                        };
-                       if (line >= nb_lines + 1 || as.POSIXlt(ts_df[line-1,1],tz=working_tz)$hour == predict_at)
+                       if (line >= nb_lines + 1
+                               || as.POSIXlt(ts_df[line-1,1],tz=working_tz)$hour == predict_at)
+                       {
                                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)] )
-               exo_hat = as.data.frame( exo_df[i,(1+nb_exos+1):(1+2*nb_exos)] )
-               level = mean(serie, na.rm=TRUE)
-               centered_serie = serie - level
-               data$append(time, centered_serie, level, exo, exo_hat)
+               data$appendHat(time, hat_serie, hat_exo)
+               data$append(serie, exo) #in realtime, this call comes hours later
                if (i >= limit)
                        break
                i = i + 1