fixes and improvements
[talweg.git] / R / getData.R
index c2e6842..5139a4d 100644 (file)
 #' @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 ? ab[:cd][:ef] where a,b,c,d,e,f
-#'   in (0,9) and define an hour[minute[second]]; time must be present in the file
+#' @param predict_at When does the prediction take place ? Integer, in hours. Default: 0
 #'
 #' @return An object of class Data
 #'
 #' @export
 getData = function(ts_data, exo_data,
-       input_tz="GMT", date_format="%d/%m/%Y %H:%M", working_tz="GMT", predict_at="00")
+       input_tz="GMT", date_format="%d/%m/%Y %H:%M", working_tz="GMT", predict_at=0)
 {
        # Sanity checks (not full, but sufficient at this stage)
        if (!is.character(input_tz) || !is.character(working_tz))
@@ -31,10 +30,9 @@ getData = function(ts_data, exo_data,
                stop("Bad time-series input (data frame or CSV file)")
        if (is.character(ts_data))
                ts_data = ts_data[1]
-       pattern_index_in_predict_at = grep("^[0-9]{2}(:[0-9]{2}){0,2}$", predict_at)
-       if (!is.character(predict_at) || length(pattern_index_in_predict_at) == 0)
-               stop("Bad predict_at ( ^[0-9]{2}(:[0-9]{2}){0,2}$ )")
-       predict_at = predict_at[ pattern_index_in_predict_at[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]
@@ -55,11 +53,6 @@ getData = function(ts_data, exo_data,
        formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz)
        ts_df[,1] = format(as.POSIXct(formatted_dates_POSIXlt), tz=working_tz, usetz=TRUE)
 
-       if (nchar(predict_at) == 2)
-               predict_at = paste(predict_at,":00",sep="")
-       if (nchar(predict_at) == 5)
-               predict_at = paste(predict_at,":00",sep="")
-
        line = 1 #index in PM10 file (24 lines for 1 cell)
        nb_lines = nrow(ts_df)
        nb_exos = ( ncol(exo_df) - 1 ) / 2
@@ -69,22 +62,20 @@ getData = function(ts_data, exo_data,
        {
                time = c()
                serie = c()
-               repeat {
-               {
-                       time = c(time, ts_df[line,1])
-                       serie = c(serie, ts_df[line,2])
-                       line = line + 1
-               };
-               if (line >= nb_lines + 1
-                       # NOTE: always second part of date/time, because it has been formatted
-                       || strsplit(as.character(ts_df[line-1,1])," ")[[1]][2] == predict_at)
+               repeat
                {
-                       break
-               }}
+                       {
+                               time = c(time, ts_df[line,1])
+                               serie = c(serie, ts_df[line,2])
+                               line = line + 1
+                       };
+                       if (line >= nb_lines + 1 || as.POSIXlt(ts_df[line-1,1])$hour == predict_at)
+                               break
+               }
 
                # NOTE: if predict_at does not cut days at midnight,
                # for the exogenous to be synchronized they need to be shifted
-               if (predict_at != "00:00:00")
+               if (predict_at > 0)
                {
                        exo_hat = as.data.frame(exo_df[max(1,i-1),(1+nb_exos+1):(1+2*nb_exos)])
                        exo_Dm1 = if (i>=3) as.data.frame(exo_df[i-1,2:(1+nb_exos)]) else NA