From 4d9e5134dfca663398df9bad0962c112b6da2b39 Mon Sep 17 00:00:00 2001
From: Delaigue Olivier <olivier.delaigue@irstea.fr>
Date: Tue, 22 Dec 2020 13:33:41 +0100
Subject: [PATCH] v1.6.8.28 style: indent code in SerriesAggreg.data.frame and
 SerriesAggreg.list

---
 DESCRIPTION                 |  4 +--
 NEWS.md                     |  4 +--
 R/SeriesAggreg.data.frame.R | 52 +++++++++++++---------------------
 R/SeriesAggreg.list.R       | 56 +++++++++++++++----------------------
 4 files changed, 45 insertions(+), 71 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index eca376c2..be8bb30e 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,8 +1,8 @@
 Package: airGR
 Type: Package
 Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling
-Version: 1.6.8.27
-Date: 2020-12-03
+Version: 1.6.8.28
+Date: 2020-12-22
 Authors@R: c(
   person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")),
   person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"),
diff --git a/NEWS.md b/NEWS.md
index 84a2383d..5bf06c6e 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,7 +2,7 @@
 
 
 
-### 1.6.8.20 Release Notes (2020-12-01)
+### 1.6.8.28 Release Notes (2020-12-22)
 
 #### New features
 
@@ -12,7 +12,7 @@
 
 #### Bug fixes
 
-- <code>TimeLag</code> of the <code>SeriesAggreg()</code> function now runs when <code>TimeLag >= 3600</code>.
+- <code>TimeLag</code> of the <code>SeriesAggreg()</code> function now runs when <code>TimeLag >= 3600</code>. ([#41](https://gitlab.irstea.fr/HYCAR-Hydro/airgr/-/issues/41))
 
 ____________________________________________________________________________________
 
diff --git a/R/SeriesAggreg.data.frame.R b/R/SeriesAggreg.data.frame.R
index 6b58ba0c..3320e8f1 100644
--- a/R/SeriesAggreg.data.frame.R
+++ b/R/SeriesAggreg.data.frame.R
@@ -77,8 +77,7 @@ SeriesAggreg.data.frame <- function(x,
     warning("'YearFirstMonth' is ignored because  Format != '%Y'")
   }
   ## check TimeLag
-  msgTimeLag <-
-    "'TimeLag' should be a single vector of a positive numeric value"
+  msgTimeLag <- "'TimeLag' should be a single vector of a positive numeric value"
   if (!is.vector(TimeLag)) {
     stop(msgTimeLag)
   }
@@ -96,30 +95,24 @@ SeriesAggreg.data.frame <- function(x,
   TabSeries2 <- TabSeries0
 
   if (!Format %in% c("%d", "%m")) {
-    start <-
-      sprintf("%i-01-01 00:00:00", as.numeric(format(TabSeries2$DatesR[1L], format = "%Y")) -
-                1)
-    stop  <-
-      sprintf("%i-12-31 00:00:00", as.numeric(format(TabSeries2$DatesR[nrow(TabSeries2)], format = "%Y")) +
-                1)
+    start <- sprintf("%i-01-01 00:00:00",
+                     as.numeric(format(TabSeries2$DatesR[1L], format = "%Y")) - 1)
+    stop  <- sprintf("%i-12-31 00:00:00",
+                     as.numeric(format(TabSeries2$DatesR[nrow(TabSeries2)], format = "%Y")) + 1)
     Ts <- format(diff(x[1:2, 1]))
-    if(gsub("[0-9]+ ", "", Ts) == "hours") {
+    if (gsub("[0-9]+ ", "", Ts) == "hours") {
       by <- "hours"
     } else {
-      if(gsub(" days$", "", Ts) == "1") {
+      if (gsub(" days$", "", Ts) == "1") {
         by <- "days"
       } else {
         by <- "months"
       }
     }
-    fakeTs <-
-      data.frame(DatesR = seq(
-        from = as.POSIXct(start, tz = "UTC"),
-        to   = as.POSIXct(stop , tz = "UTC"),
-        by   = by
-      ) + TimeLag)
-    TabSeries2 <-
-      merge(fakeTs, TabSeries2, by = "DatesR", all.x = TRUE)
+    fakeTs <- data.frame(DatesR = seq(from = as.POSIXct(start, tz = "UTC"),
+                                      to   = as.POSIXct(stop , tz = "UTC"),
+                                      by   = by) + TimeLag)
+    TabSeries2 <- merge(fakeTs, TabSeries2, by = "DatesR", all.x = TRUE)
   }
   TabSeries2$DatesRini <- TabSeries2$DatesR %in% TabSeries0$DatesR
 
@@ -130,9 +123,7 @@ SeriesAggreg.data.frame <- function(x,
     # Compute aggregation
     TabSeries2$Selec <- !duplicated(TabSeries2$Selec2)
     if (all(TabSeries2$Selec)) {
-      warning(
-        "the requested time 'Format' is the same as the one in 'x'. No time-step conversion was performed"
-      )
+      warning("the requested time 'Format' is the same as the one in 'x'. No time-step conversion was performed")
       return(x)
     }
     if (Format == "%Y") {
@@ -141,8 +132,7 @@ SeriesAggreg.data.frame <- function(x,
       spF2 <- "%Y-%m"
       TabSeries2$Selec1 <- format(TabSeries2$DatesR, spF1)
       TabSeries2$Selec2 <- format(TabSeries2$DatesR, spF2)
-      TabSeries2$Selec <-
-        !duplicated(TabSeries2$Selec2) & TabSeries2$Selec1 == yfm
+      TabSeries2$Selec <- !duplicated(TabSeries2$Selec2) & TabSeries2$Selec1 == yfm
     }
     TabSeries2$Fac2 <- cumsum(TabSeries2$Selec)
   } else {
@@ -158,8 +148,7 @@ SeriesAggreg.data.frame <- function(x,
 
   listTsAggreg <- lapply(names(listConvertFun), function(y) {
     if (any(ConvertFun == y)) {
-      colTsAggreg <-
-        c("Fac2", colnames(x)[-1L][ConvertFun == y])
+      colTsAggreg <- c("Fac2", colnames(x)[-1L][ConvertFun == y])
       aggregate(. ~ Fac2,
                 data = TabSeries2[, colTsAggreg],
                 FUN = listConvertFun[[y]],
@@ -171,14 +160,11 @@ SeriesAggreg.data.frame <- function(x,
   listTsAggreg <- listTsAggreg[!sapply(listTsAggreg, is.null)]
   tsAggreg <- do.call(cbind, listTsAggreg)
   tsAggreg <- tsAggreg[, !duplicated(colnames(tsAggreg))]
-  tsAggreg <-
-    merge(
-      tsAggreg,
-      TabSeries2[, c("Fac2", "DatesR", "DatesRini", "Selec")],
-      by = "Fac2",
-      all.x = TRUE,
-      all.y = FALSE
-    )
+  tsAggreg <- merge(tsAggreg,
+                    TabSeries2[, c("Fac2", "DatesR", "DatesRini", "Selec")],
+                    by = "Fac2",
+                    all.x = TRUE,
+                    all.y = FALSE)
   tsAggreg <- tsAggreg[tsAggreg$Selec & tsAggreg$DatesRini, ]
   tsAggreg <- tsAggreg[, colnames(TabSeries0)]
   return(tsAggreg)
diff --git a/R/SeriesAggreg.list.R b/R/SeriesAggreg.list.R
index 5152f4d2..939745a7 100644
--- a/R/SeriesAggreg.list.R
+++ b/R/SeriesAggreg.list.R
@@ -28,11 +28,9 @@ SeriesAggreg.list <- function(x,
     if (is.na(itemPOSIXt)) {
       stop("At least one item of argument 'x' should be of class 'POSIXt'")
     }
-    warning(
-      "Item 'DatesR' not found in 'x' argument: the item ",
-      names(x)[itemPOSIXt],
-      " has been automatically chosen"
-    )
+    warning("Item 'DatesR' not found in 'x' argument: the item ",
+            names(x)[itemPOSIXt],
+            " has been automatically chosen")
     DatesR <- x[[names(x)[itemPOSIXt]]]
   }
 
@@ -53,13 +51,11 @@ SeriesAggreg.list <- function(x,
     sErr <- paste0(names(lengthCols)[lengthCols != length(DatesR)],
                    " (", lengthCols[lengthCols != length(DatesR)], ")",
                    collapse = ", ")
-    warning(
-      "The length of the following `numeric` items in 'x' ",
-      "is different than the length of 'DatesR (",
-      length(DatesR),
-      ")': they will be ignored in the aggregation: ",
-      sErr
-    )
+    warning("The length of the following `numeric` items in 'x' ",
+            "is different than the length of 'DatesR (",
+            length(DatesR),
+            ")': they will be ignored in the aggregation: ",
+            sErr)
     cols <- cols[lengthCols == length(DatesR)]
   }
   dfOut <- NULL
@@ -87,7 +83,7 @@ SeriesAggreg.list <- function(x,
     # Convert aggregated data.frame into list
     if (!is.null(dfOut)) {
       res <- as.list(dfOut)
-      ## to be consistent with InputsModel class and because plot.OutputsModel use the POSIXlt class
+      ## To be consistent with InputsModel class and because plot.OutputsModel use the POSIXlt class
       res$DatesR <- as.POSIXlt(res$DatesR)
     }
 
@@ -95,26 +91,21 @@ SeriesAggreg.list <- function(x,
     if (is.null(recursive) || recursive) {
       listCols <- x[sapply(x, inherits, "list")]
       dfCols <- x[sapply(x, inherits, "data.frame")]
-      dfCols <-
-        c(dfCols, x[sapply(x, inherits, "matrix")])
+      dfCols <- c(dfCols, x[sapply(x, inherits, "matrix")])
       listCols <- listCols[setdiff(names(listCols), names(dfCols))]
       if (length(listCols) > 0) {
         # Check for predefined ConvertFun for all sub-elements
         ConvertFun <- getAggregConvertFun(names(listCols))
         # Run SeriesAggreg for each embedded list
-        listRes <-
-          lapply(names(listCols),
-                 function(x) {
-                   listCols[[x]]$DatesR <- DatesR
-                   SeriesAggreg(
-                     listCols[[x]],
-                     Format = Format,
-                     except = except,
-                     ConvertFun = ConvertFun[x],
-                     recursive = NULL,
-                     ...
-                   )
-                 })
+        listRes <- lapply(names(listCols), function(x) {
+          listCols[[x]]$DatesR <- DatesR
+          SeriesAggreg(listCols[[x]],
+                       Format = Format,
+                       except = except,
+                       ConvertFun = ConvertFun[x],
+                       recursive = NULL,
+                       ...)
+        })
         names(listRes) <- names(listCols)
         if (is.null(res$DatesR)) {
           # Copy DatesR in top level list
@@ -151,12 +142,9 @@ SeriesAggreg.list <- function(x,
     # Store elements that are not aggregated
     res <- c(res, x[setdiff(names(x), names(res))])
 
-    class(res) <-
-      gsub(
-        "hourly|daily|monthly|yearly",
-        getSeriesAggregClass(Format),
-        class(x)
-      )
+    class(res) <- gsub("hourly|daily|monthly|yearly",
+                       getSeriesAggregClass(Format),
+                       class(x))
 
     return(res)
 
-- 
GitLab