UtilsRunModel.R 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
#' Create `OutputsModel` for GR non-Cemaneige models
#'
#' @param InputsModel output of [CreateInputsModel]
#' @param RunOptions output of [CreateRunOptions]
#' @param RESULTS outputs of [.Fortran]
#' @param LInputSeries number of time steps of warm-up + run periods
#' @param CemaNeigeLayers outputs of Cemaneige pre-process
#'
9
#' @return OutputsModel object
10
11
#' @noRd
#'
12
.GetOutputsModelGR <- function(InputsModel,
13
14
15
16
17
18
19
20
                                RunOptions,
                                RESULTS,
                                LInputSeries,
                                CemaNeigeLayers = NULL) {

  IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  FortranOutputs <- RunOptions$FortranOutputs$GR

21
  IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
22
23
24

  OutputsModel <- list()

25
  if ("DatesR" %in% RunOptions$Outputs_Sim) {
26
    OutputsModel$DatesR <- InputsModel$DatesR[RunOptions$IndPeriod_Run]
27
28
29
30
31
32
33
34
  }

  seqOutputs <- seq_len(RESULTS$NOutputs)
  names(seqOutputs) <- FortranOutputs[IndOutputs]

  OutputsModel <- c(OutputsModel,
                    lapply(seqOutputs, function(i) RESULTS$Outputs[IndPeriod2, i]))

35
  if (!is.null(CemaNeigeLayers)) {
36
37
38
    OutputsModel$CemaNeigeLayers <- CemaNeigeLayers
  }

39
40
41
42
43
44
45
  if ("WarmUpQsim" %in% RunOptions$Outputs_Sim) {
    OutputsModel$WarmUpQsim <- RESULTS$Outputs[seq_len(length(RunOptions$IndPeriod_WarmUp)),
                                               which(FortranOutputs == "Qsim")]
    class(OutputsModel$WarmUpQsim) <- c("WarmUpOutputsModelItem", class(OutputsModel$WarmUpQsim))
  }

  if ("StateEnd" %in% RunOptions$Outputs_Sim) {
46
47
48
    OutputsModel$StateEnd <- RESULTS$StateEnd
  }

49
50
  class(OutputsModel) <- c("OutputsModel", class(RunOptions)[-1])

51
52
  return(OutputsModel)
}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


#' Check arguments of `RunModel_*GR*` functions
#'
#' @param InputsModel see [CreateInputsModel]
#' @param RunOptions  see [CreateRunOptions]
#' @param Param [numeric] [vector] model calibration parameters
#'
#' @return [NULL]
#' @noRd
#'
.ArgumentsCheckGR <- function(InputsModel, RunOptions, Param) {
  if (!inherits(InputsModel, "InputsModel")) {
    stop("'InputsModel' must be of class 'InputsModel'")
  }
  if (!inherits(InputsModel, RunOptions$FeatFUN_MOD$TimeUnit)) {
    stop("'InputsModel' must be of class '", RunOptions$FeatFUN_MOD$TimeUnit, "'")
  }
  if (!inherits(InputsModel, "GR")) {
    stop("'InputsModel' must be of class 'GR'")
  }
  if (class(RunOptions)[1] != "RunOptions") {
    if (!inherits(RunOptions, "RunOptions")) {
      stop("'RunOptions' must be of class 'RunOptions'")
    } else {
      stop("'RunOptions' class of 'RunOptions' must be in first position")
    }
  }
  if (!inherits(RunOptions, "GR")) {
    stop("'RunOptions' must be of class 'GR'")
  }

  if ("CemaNeige" %in% RunOptions$FeatFUN_MOD$Class) {
    if (!inherits(InputsModel, "CemaNeige")) {
      stop("'InputsModel' must be of class 'CemaNeige'")
    }
    if (!inherits(RunOptions, "CemaNeige")) {
      stop("'RunOptions' must be of class 'CemaNeige'")
    }
  }

  if (!is.vector(Param) | !is.numeric(Param)) {
    stop("'Param' must be a numeric vector")
  }
  if (sum(!is.na(Param)) != RunOptions$FeatFUN_MOD$NbParam) {
    stop(paste("'Param' must be a vector of length", RunOptions$FeatFUN_MOD$NbParam, "and contain no NA"))
  }
}