SimGR.R 3.6 KB
Newer Older
1
SimGR <- function(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2", "RMSE"),
unknown's avatar
unknown committed
2
3
                  WupPer = NULL, SimPer, transfo = c("", "sqrt", "log", "inv", "sort"), verbose = TRUE) {
 
4
5
  if (! any(class(PrepGR) %in% "PrepGR")) {
    stop("Non convenient data for argument \"PrepGR\". Must be of class \"PrepGR\"")
unknown's avatar
unknown committed
6
7
  }
  
8
9
10
11
12
13
14
15
  if (!missing(CalGR)) {
    warning("Deprecated \"CalGR\" argument. Use \"Param\" instead")
  }
  
  ### to remove when the CalGR will be removed
  if (missing(Param)) {
    Param <- NULL
  }
unknown's avatar
unknown committed
16
17
18
19
  if (! any(class(CalGR) %in% "CalGR") & !is.null(CalGR)) {
    stop("Non convenient data  for argument \"CalGR\". Must be of class \"CalGR\"")
  }
  if (is.null(CalGR) & is.null(Param)) {
20
    stop("Arguments \"CalGR\" and \"Param\" are missing, with no default. You must fill in one of these two arguments")
unknown's avatar
unknown committed
21
  }
22

unknown's avatar
unknown committed
23
24
25
  if (is.null(Param)) {
    Param <- CalGR$OutputsCalib$ParamFinalR
  }
26
27
28
29
30
  ###
  
  if (inherits(Param, "CalGR")) {
    Param <- Param$OutputsCalib$ParamFinalR
  }
unknown's avatar
unknown committed
31
32
33
34
  
  WupInd <- NULL
  if (!is.null(WupPer)) {
    WupPer <- as.POSIXct(WupPer, tz = "UTC")
35
36
37
    if (length(WupPer) != 2) {
      stop("Warm-up period \"WupPer\" must be of length 2")
    }
unknown's avatar
unknown committed
38
39
40
    if (any(is.na(WupPer))) {
      stop("Non convenient date format for the warm-up period \"WupPer\"")
    } else {
41
      if (!any(PrepGR$InputsModel$DatesR == WupPer[1]) | !any(PrepGR$InputsModel$DatesR == WupPer[2])) {
unknown's avatar
unknown committed
42
43
        stop("Non convenient date for the warm-up period \"WupPer\"")
      } else {
44
        WupInd <- which(PrepGR$InputsModel$DatesR == WupPer[1]):which(PrepGR$InputsModel$DatesR == WupPer[2])
unknown's avatar
unknown committed
45
46
47
48
49
      }
    }
  }
 
  SimPer <- as.POSIXct(SimPer, tz = "UTC")
50
51
52
  if (length(SimPer) != 2) {
    stop("Simulation period \"SimPer\" must be of length 2")
  }
unknown's avatar
unknown committed
53
54
55
  if (any(is.na(SimPer))) {
    stop("Non convenient date format for the simulation period \"SimPer\"")
  } else {
56
    if (!any(PrepGR$InputsModel$DatesR == SimPer[1]) | !any(PrepGR$InputsModel$DatesR == SimPer[2])) {
unknown's avatar
unknown committed
57
58
      stop("Non convenient date for the simulation period \"SimPer\"")
    } else {
59
      SimInd <- which(PrepGR$InputsModel$DatesR == SimPer[1]):which(PrepGR$InputsModel$DatesR == SimPer[2])
unknown's avatar
unknown committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    }
  }
  
  if (! any(EffCrit %in% c("NSE", "KGE", "KGE2", "RMSE"))) {
    stop("Non convenient efficiency criteria \"EffCrit\"")
  } else {
    EffCrit <- EffCrit[1L]
    EffCrit <- sprintf("ErrorCrit_%s", EffCrit)
    FUN_CRIT <- get(EffCrit)
  }
  
  if (! any(transfo %in% c("", "sqrt", "log", "inv", "sort"))) {
    stop("Non convenient transformation \"transfo\"")
  } else {
    transfo <- transfo[1L]
  }  
  
77
  MOD_opt <- CreateRunOptions(FUN_MOD = get(PrepGR$TypeModel), InputsModel = PrepGR$InputsModel, 
unknown's avatar
unknown committed
78
79
80
                              IndPeriod_WarmUp = WupInd, IndPeriod_Run = SimInd, verbose = verbose)

  
81
  MOD_crt <- CreateInputsCrit(FUN_CRIT = FUN_CRIT, InputsModel = PrepGR$InputsModel, 
82
                              RunOptions = MOD_opt, Obs = PrepGR$Qobs[SimInd], transfo = transfo)  
unknown's avatar
unknown committed
83
84
  
  
85
  SIM <- RunModel(InputsModel = PrepGR$InputsModel, RunOptions = MOD_opt, 
86
                  Param = Param, FUN_MOD = get(PrepGR$TypeModel))
unknown's avatar
unknown committed
87
88
  
  
89
  CRT <- ErrorCrit(InputsCrit = MOD_crt, OutputsModel = SIM, verbose = verbose)
unknown's avatar
unknown committed
90
91

  
92
93
  SimGR <- list(OptionsSimul = MOD_opt, OptionsCrit = MOD_crt, OutputsModel = SIM, Qobs = PrepGR$Qobs[SimInd],
                TypeModel = PrepGR$TypeModel,
unknown's avatar
unknown committed
94
                CalCrit = CalGR$CalCrit, EffCrit = CRT,
95
                PeriodModel = list(WarmUp = as.POSIXct(PrepGR$InputsModel$DatesR[range(MOD_opt$IndPeriod_WarmUp)], tz = "UTC"),
unknown's avatar
unknown committed
96
                                   Run    = SimPer))
97
  class(SimGR) <- c("SimGR", "GR", "airGRt")
unknown's avatar
unknown committed
98
99
100
  return(SimGR)  
  
}