From 31f8e94e6377b3c23ec337639998966111bea93d Mon Sep 17 00:00:00 2001
From: David <david.dorchies@inrae.fr>
Date: Mon, 12 Sep 2022 08:42:40 +0200
Subject: [PATCH] refactor: change name of routing functions to Munier and QLR

- RunModel_LLR handle state length smaller than IndPeriod_Run length

Refs HYCAR-Hydro/airgr#153
---
 NAMESPACE                                     |  7 ++--
 R/CreateCalibOptions.R                        | 10 ++---
 R/RunModel_LLR.R                              |  6 ++-
 R/{RunModel_LBLR.R => RunModel_Munier_LR.R}   |  2 +-
 R/{RunModel_LRQ.R => RunModel_QLR.R}          |  4 +-
 ...oParam_LBLR.R => TransfoParam_Munier_LR.R} |  2 +-
 R/{TransfoParam_LRQ.R => TransfoParam_QLR.R}  |  4 +-
 R/Utils.R                                     |  6 +--
 R/UtilsCalibOptions.R                         |  9 ++---
 airGR.Rproj                                   | 40 +++++++++----------
 inst/modelsFeatures/FeatModelsGR.csv          |  4 +-
 11 files changed, 49 insertions(+), 45 deletions(-)
 rename R/{RunModel_LBLR.R => RunModel_Munier_LR.R} (96%)
 rename R/{RunModel_LRQ.R => RunModel_QLR.R} (96%)
 rename R/{TransfoParam_LBLR.R => TransfoParam_Munier_LR.R} (91%)
 rename R/{TransfoParam_LRQ.R => TransfoParam_QLR.R} (86%)

diff --git a/NAMESPACE b/NAMESPACE
index e98e8424..86ea5952 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -55,8 +55,8 @@ export(RunModel_GR5J)
 export(RunModel_GR6J)
 export(RunModel_Lag)
 export(RunModel_LLR)
-export(RunModel_LBLR)
-export(RunModel_LRQ)
+export(RunModel_Munier_LR)
+export(RunModel_QLR)
 export(SeriesAggreg)
 export(TransfoParam)
 export(TransfoParam_CemaNeige)
@@ -70,7 +70,8 @@ export(TransfoParam_GR5J)
 export(TransfoParam_GR6J)
 export(TransfoParam_Lag)
 export(TransfoParam_LLR)
-export(TransfoParam_LBLR)
+export(TransfoParam_Munier_LR)
+export(TransfoParam_QLR)
 export(.ErrorCrit)
 export(.FeatModels)
 
diff --git a/R/CreateCalibOptions.R b/R/CreateCalibOptions.R
index 8a974aa0..691903fa 100644
--- a/R/CreateCalibOptions.R
+++ b/R/CreateCalibOptions.R
@@ -208,17 +208,17 @@ CreateCalibOptions <- function(FUN_MOD,
         ParamT <- cbind(ParamTSD, ParamT)
       }
       else if (identical(RunModel_LLR, FUN_SD)){
-        ParamTSD <- matrix(c(-9.00, -5,
-                             -7.00, 0,
-                             -5.00, +5), ncol = 2, byrow = TRUE)
+        ParamTSD <- matrix(c(-9.9, -9.8,
+                             -7.00, -1,
+                             -5.00, +7.8), ncol = 2, byrow = TRUE)
         ParamT <- cbind(ParamTSD, ParamT)
       }
-      else if (identical(RunModel_LBLR, FUN_SD)){
+      else if (identical(RunModel_Munier_LR, FUN_SD)){
         ParamTSD <- matrix(c(-9.00, -5,
                              -7.00, 0,
                              -5.00, +5), ncol = 2, byrow = TRUE)
         ParamT <- cbind(ParamTSD, ParamT)
-      } else if (identical(RunModel_LRQ, FUN_SD)){
+      } else if (identical(RunModel_QLR, FUN_SD)){
         ParamTSD <- matrix(c(-9.00, -5,
                             -7.00, 0,
                             -5.00, +5), ncol = 2, byrow = TRUE)
diff --git a/R/RunModel_LLR.R b/R/RunModel_LLR.R
index 8bc65198..aa31e502 100644
--- a/R/RunModel_LLR.R
+++ b/R/RunModel_LLR.R
@@ -176,7 +176,11 @@ RunModel_LLR <- function(InputsModel, RunOptions, Param, QcontribDown) {
   if ("StateEnd" %in% RunOptions$Outputs_Sim) {
     SD <- lapply(seq(NbUpBasins), function(x) {
       lastTS <- RunOptions$IndPeriod_Run[length(RunOptions$IndPeriod_Run)]
-      InputsModel$Qupstream[(lastTS - floor(PT[x])):lastTS, x]
+      if (lastTS - floor(PT[x]) < 0) {
+        InputsModel$Qupstream[1 :lastTS, x]
+      } else {
+        InputsModel$Qupstream[(lastTS - floor(PT[x])):lastTS, x]
+      }
     })
     if (is.null(OutputsModel$StateEnd)) {
       OutputsModel$StateEnd <- CreateIniStates(RunModel_LLR, InputsModel, SD = SD)
diff --git a/R/RunModel_LBLR.R b/R/RunModel_Munier_LR.R
similarity index 96%
rename from R/RunModel_LBLR.R
rename to R/RunModel_Munier_LR.R
index c8591eab..a3ea21a2 100644
--- a/R/RunModel_LBLR.R
+++ b/R/RunModel_Munier_LR.R
@@ -1,4 +1,4 @@
-RunModel_LBLR <- function(InputsModel, RunOptions, Param, QcontribDown) {
+RunModel_Munier_LR <- function(InputsModel, RunOptions, Param, QcontribDown) {
   NParam <- 2
   ##RunModel_LBLR
   ## argument check
diff --git a/R/RunModel_LRQ.R b/R/RunModel_QLR.R
similarity index 96%
rename from R/RunModel_LRQ.R
rename to R/RunModel_QLR.R
index 8bf801b5..01c43f6a 100644
--- a/R/RunModel_LRQ.R
+++ b/R/RunModel_QLR.R
@@ -1,6 +1,6 @@
-RunModel_LRQ <- function(InputsModel, RunOptions, Param, QcontribDown) {
+RunModel_QLR <- function(InputsModel, RunOptions, Param, QcontribDown) {
   NParam <- 2
-  #RunModel_LRQ
+
   ## argument check
   if (!inherits(InputsModel, "InputsModel")) {
     stop("'InputsModel' must be of class 'InputsModel'")
diff --git a/R/TransfoParam_LBLR.R b/R/TransfoParam_Munier_LR.R
similarity index 91%
rename from R/TransfoParam_LBLR.R
rename to R/TransfoParam_Munier_LR.R
index dbcd52d5..c5e62594 100644
--- a/R/TransfoParam_LBLR.R
+++ b/R/TransfoParam_Munier_LR.R
@@ -1,4 +1,4 @@
-TransfoParam_LBLR <- function(ParamIn, Direction) {
+TransfoParam_Munier_LR <- function(ParamIn, Direction) {
 
   ## number of model parameters
   NParam <- 2L
diff --git a/R/TransfoParam_LRQ.R b/R/TransfoParam_QLR.R
similarity index 86%
rename from R/TransfoParam_LRQ.R
rename to R/TransfoParam_QLR.R
index 5400ee69..bcb8795b 100644
--- a/R/TransfoParam_LRQ.R
+++ b/R/TransfoParam_QLR.R
@@ -1,4 +1,4 @@
-TransfoParam_LRQ <- function(ParamIn, Direction) {
+TransfoParam_QLR <- function(ParamIn, Direction) {
 
   ## number of model parameters
   NParam <- 2L
@@ -22,7 +22,7 @@ TransfoParam_LRQ <- function(ParamIn, Direction) {
   ## transformation
   if (Direction == "TR") {
     ParamOut <- ParamIn
-    ParamOut[, 1] <- 20 * (ParamIn[, 1] + 10) / 20.0
+    ParamOut[, 1] <- 20 * (ParamIn[,1] + 10) / 20
     ParamOut[, 2] <- 500000 * (ParamIn[, 2] + 10) / 20
   }
   if (Direction == "RT") {
diff --git a/R/Utils.R b/R/Utils.R
index e75d51d4..f28a9c09 100644
--- a/R/Utils.R
+++ b/R/Utils.R
@@ -80,12 +80,12 @@
       }else if(identical(RunModel_LLR, FUN_SD)){
         res$NParamSD <- FeatMod$NbParam[15]
         res$FUN_SD <- "LLR"
-      }else if(identical(RunModel_LBLR, FUN_SD)){
+      }else if(identical(RunModel_Munier_LR, FUN_SD)){
         res$NParamSD <- FeatMod$NbParam[16]
         res$FUN_SD <- "LBLR"
-      }else if (identical(RunModel_LRQ, FUN_SD)){
+      }else if (identical(RunModel_QLR, FUN_SD)){
         res$NParamSD <- FeatMod$NbParam[17]
-        res$FUN_SD <- "LRQ"
+        res$FUN_SD <- "QLR"
       }
     }
     return(res)
diff --git a/R/UtilsCalibOptions.R b/R/UtilsCalibOptions.R
index 90f14eef..67501d6c 100644
--- a/R/UtilsCalibOptions.R
+++ b/R/UtilsCalibOptions.R
@@ -31,12 +31,11 @@
       FUN_ROUT <- TransfoParam_Lag
     } else if (identical(RunModel_LLR, FUN_SD)){
       FUN_ROUT <- TransfoParam_LLR
-    } else if (identical(RunModel_LBLR, FUN_SD)){
-      FUN_ROUT <- TransfoParam_LBLR
-    } else if (identical(RunModel_LRQ, FUN_SD)){
-      FUN_ROUT <- TransfoParam_LRQ
+    } else if (identical(RunModel_Munier_LR, FUN_SD)){
+      FUN_ROUT <- TransfoParam_Munier_LR
+    } else if (identical(RunModel_QLR, FUN_SD)){
+      FUN_ROUT <- TransfoParam_QLR
     }
-
   }
 
     ## set FUN_TRANSFO
diff --git a/airGR.Rproj b/airGR.Rproj
index 398aa143..5caa49b4 100644
--- a/airGR.Rproj
+++ b/airGR.Rproj
@@ -1,20 +1,20 @@
-Version: 1.0
-
-RestoreWorkspace: Default
-SaveWorkspace: Default
-AlwaysSaveHistory: Default
-
-EnableCodeIndexing: Yes
-UseSpacesForTab: Yes
-NumSpacesForTab: 2
-Encoding: UTF-8
-
-RnwWeave: knitr
-LaTeX: pdfLaTeX
-
-AutoAppendNewline: Yes
-StripTrailingWhitespace: Yes
-
-BuildType: Package
-PackageUseDevtools: Yes
-PackageInstallArgs: --no-multiarch --with-keep.source
+Version: 1.0
+
+RestoreWorkspace: Default
+SaveWorkspace: Default
+AlwaysSaveHistory: Default
+
+EnableCodeIndexing: Yes
+UseSpacesForTab: Yes
+NumSpacesForTab: 2
+Encoding: UTF-8
+
+RnwWeave: knitr
+LaTeX: pdfLaTeX
+
+AutoAppendNewline: Yes
+StripTrailingWhitespace: Yes
+
+BuildType: Package
+PackageUseDevtools: Yes
+PackageInstallArgs: --no-multiarch --with-keep.source
diff --git a/inst/modelsFeatures/FeatModelsGR.csv b/inst/modelsFeatures/FeatModelsGR.csv
index b8738de4..730bdbdf 100644
--- a/inst/modelsFeatures/FeatModelsGR.csv
+++ b/inst/modelsFeatures/FeatModelsGR.csv
@@ -14,5 +14,5 @@ CemaNeigeGR4H;CemaNeigeGR4H;6;hourly;NA;GR;airGR
 CemaNeigeGR5H;CemaNeigeGR5H;7;hourly;NA;GR;airGR
 Lag;Lag;1;NA;NA;SD;airGR
 LLR;LLR;2;NA;NA;SD;airGR
-LBLR;LBLR;2;NA;NA;SD;airGR
-LRQ;LRQ;2;NA;NA;SD;airGR
+Munier_LR;Munier_LR;2;NA;NA;SD;airGR
+QLR;QLR;2;NA;NA;SD;airGR
-- 
GitLab