From c77f56468ec8a8cd8d5db5edca3b2a2ee8049f95 Mon Sep 17 00:00:00 2001
From: David <david.dorchies@inrae.fr>
Date: Mon, 27 May 2024 15:33:15 +0200
Subject: [PATCH] feat(CreateInputsCrit): allow sibling nodes as AprioriIds

Refs #156
---
 R/CreateInputsCrit.GRiwrmInputsModel.R |  8 +++++---
 tests/testthat/test-CreateInputsCrit.R | 21 ++++++++++++++++++---
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/R/CreateInputsCrit.GRiwrmInputsModel.R b/R/CreateInputsCrit.GRiwrmInputsModel.R
index 69a97d5..bc234ed 100644
--- a/R/CreateInputsCrit.GRiwrmInputsModel.R
+++ b/R/CreateInputsCrit.GRiwrmInputsModel.R
@@ -53,14 +53,16 @@ CreateInputsCrit.GRiwrmInputsModel <- function(InputsModel,
         stop("'Each item of AprioriIds must be an id of a modelled node:",
              " the id \"", AprioriIds[id] ,"\" is not in the list of the modelled nodes")
       }
-      if (! isNodeDownstream(InputsModel, AprioriIds[id], id)) {
+      if (!AprioriIds[id] %in% names(InputsModel)[1:which(id == names(InputsModel))]) {
         stop("'AprioriIds': the node \"", AprioriIds[id],
-             "\" is not upstream the node \"", id,"\"")
+             "\" is not calibrated before the node \"", id,"\".",
+             "\nIf possible, set this apriori id as the donor of the node \"",
+             id,"\" to force the calibration sequence order")
       }
       if (InputsModel[[AprioriIds[id]]]$isUngauged &
           InputsModel[[AprioriIds[id]]]$gaugedId == id) {
         stop("'AprioriIds': the node \"", AprioriIds[id],
-             "\" is an ungauged upstream node of the node \"", id,"\"")
+             "\" is ungauged, use a gauged node instead")
       }
       if (!identical(InputsModel[[id]]$FUN_MOD, InputsModel[[AprioriIds[id]]]$FUN_MOD)) {
         stop("'AprioriIds': the node \"", AprioriIds[id],
diff --git a/tests/testthat/test-CreateInputsCrit.R b/tests/testthat/test-CreateInputsCrit.R
index 3b419ae..8c3ad5a 100644
--- a/tests/testthat/test-CreateInputsCrit.R
+++ b/tests/testthat/test-CreateInputsCrit.R
@@ -103,10 +103,25 @@ test_that("Lavenne criterion: wrong sub-catchment order should throw error", {
     CreateInputsCrit(InputsModel = InputsModel,
                      RunOptions = RunOptions,
                      Obs = Qobs[IndPeriod_Run,],
-                     AprioriIds = c("54057" = "54032", "54032" = "54001", "54001" = "54029"),
+                     AprioriIds = c("54057" = "54032", "54032" = "54001", "54095" = "54029"),
                      transfo = "sqrt"),
-    regexp = "is not upstream the node"
+    regexp = "is not calibrated before the node"
+  )
+})
+
+test_that("Lavenne criterion: not upstream a priori nodes are allow if processed before #156", {
+  IC156 <- CreateInputsCrit(
+    InputsModel = InputsModel,
+    RunOptions = RunOptions,
+    Obs = Qobs[IndPeriod_Run, ],
+    AprioriIds = c(
+      "54057" = "54032",
+      "54032" = "54001",
+      "54029" = "54095"
+    ),
+    transfo = "sqrt"
   )
+  expect_equal(attr(IC156$`54029`, "AprioriId"), c("54029" = "54095"))
 })
 
 test_that("Lavenne criterion: current node and a priori node must use the same model", {
@@ -131,6 +146,6 @@ test_that("Ungauged node as Apriori node should throw an error", {
                      Obs = Qobs[IndPeriod_Run,],
                      AprioriIds = c("54057" = "54032", "54032" = "54001", "54001" = "54029"),
                      transfo = "sqrt"),
-    regexp = "\"54001\" is an ungauged upstream node of the node \"54032\""
+    regexp = "\"54001\" is ungauged"
   )
 })
-- 
GitLab