SimGR.R 3.13 KB
Newer Older
unknown's avatar
unknown committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
SimGR <- function(ObsGR, CalGR = NULL, Param = NULL, EffCrit = c("NSE", "KGE", "KGE2", "RMSE"),
                  WupPer = NULL, SimPer, transfo = c("", "sqrt", "log", "inv", "sort"), verbose = TRUE) {
 
  if (! any(class(ObsGR) %in% "ObsGR")) {
    stop("Non convenient data for argument \"ObsGR\". Must be of class \"ObsGR\"")
  }
  
  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)) {
    stop("Arguments \"CalGR\" and \"Param\" are missing, with no default. You must fill in one of these two arguments.")
  }
  
  if (is.null(Param)) {
    Param <- CalGR$OutputsCalib$ParamFinalR
  }
  
  WupInd <- NULL
  if (!is.null(WupPer)) {
    WupPer <- as.POSIXct(WupPer, tz = "UTC")
    if (any(is.na(WupPer))) {
      stop("Non convenient date format for the warm-up period \"WupPer\"")
    } else {
      if (! (any(ObsGR$InputsModel$DatesR == WupPer[1]) & any(ObsGR$InputsModel$DatesR == WupPer[1]))) {
        stop("Non convenient date for the warm-up period \"WupPer\"")
      } else {
        WupInd <- which(ObsGR$InputsModel$DatesR == WupPer[1]):which(ObsGR$InputsModel$DatesR == WupPer[2])
      }
    }
  }
 
  SimPer <- as.POSIXct(SimPer, tz = "UTC")
  if (any(is.na(SimPer))) {
    stop("Non convenient date format for the simulation period \"SimPer\"")
  } else {
    if (! (any(ObsGR$InputsModel$DatesR == SimPer[1]) & any(ObsGR$InputsModel$DatesR == SimPer[2]))) {
      stop("Non convenient date for the simulation period \"SimPer\"")
    } else {
      SimInd <- which(ObsGR$InputsModel$DatesR == SimPer[1]):which(ObsGR$InputsModel$DatesR == SimPer[2])
    }
  }
  
  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]
  }  
  
  MOD_opt <- CreateRunOptions(FUN_MOD = get(ObsGR$TypeModel), InputsModel = ObsGR$InputsModel, 
                              IndPeriod_WarmUp = WupInd, IndPeriod_Run = SimInd, verbose = verbose)

  
62
63
  MOD_crt <- CreateInputsCrit(FUN_CRIT = FUN_CRIT, InputsModel = ObsGR$InputsModel, 
                              RunOptions = MOD_opt, Qobs = ObsGR$Qobs[SimInd], transfo = transfo)  
unknown's avatar
unknown committed
64
65
66
67
68
69
70
71
72
  
  
  SIM <- RunModel(InputsModel = ObsGR$InputsModel, RunOptions = MOD_opt, 
                  Param = Param, FUN_MOD =  get(ObsGR$TypeModel))
  
  
  CRT <- ErrorCrit(InputsCrit = MOD_crt, OutputsModel = SIM, FUN_CRIT = FUN_CRIT, verbose = verbose)

  
73
  SimGR <- list(OptionsSimul = MOD_opt, OptionsCrit = MOD_crt, OutputsModel = SIM, Qobs = ObsGR$Qobs[SimInd],
unknown's avatar
unknown committed
74
75
                TypeModel = ObsGR$TypeModel,
                CalCrit = CalGR$CalCrit, EffCrit = CRT,
76
                PeriodModel = list(WarmUp = as.POSIXct(ObsGR$InputsModel$DatesR[range(MOD_opt$IndPeriod_WarmUp)]),
unknown's avatar
unknown committed
77
78
79
80
81
                                   Run    = SimPer))
  class(SimGR) <- c("SimGR", "GR")
  return(SimGR)  
  
}