CalGR.R 3.2 KB
Newer Older
1
CalGR <- function(PrepGR, CalCrit = c("NSE", "KGE", "KGE2", "RMSE"), 
unknown's avatar
unknown committed
2
3
                  WupPer = NULL, CalPer, 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
  }
  
  WupInd <- NULL
  if (!is.null(WupPer)) {
    WupPer <- as.POSIXct(WupPer, tz = "UTC")
11
12
13
    if (length(WupPer) != 2) {
      stop("Warm-up period \"WupPer\" must be of length 2")
    }
unknown's avatar
unknown committed
14
15
16
    if (any(is.na(WupPer))) {
      stop("Non convenient date format for the warm-up period \"WupPer\"")
    } else {
17
      if (!any(PrepGR$InputsModel$DatesR == WupPer[1]) | !any(PrepGR$InputsModel$DatesR == WupPer[2])) {
unknown's avatar
unknown committed
18
19
        stop("Non convenient date for the warm-up period \"WupPer\"")
      } else {
20
        WupInd <- which(PrepGR$InputsModel$DatesR == WupPer[1]):which(PrepGR$InputsModel$DatesR == WupPer[2])
unknown's avatar
unknown committed
21
22
23
24
25
      }
    }
  }
  
  CalPer <- as.POSIXct(CalPer, tz = "UTC")
26
27
28
  if (length(CalPer) != 2) {
    stop("Calibration period \"CalPer\" must be of length 2")
  }
unknown's avatar
unknown committed
29
30
31
  if (any(is.na(CalPer))) {
    stop("Non convenient date format for the calibration period \"CalPer\"")
  } else {
32
    if (!any(PrepGR$InputsModel$DatesR == CalPer[1]) | !any(PrepGR$InputsModel$DatesR == CalPer[2])) {
unknown's avatar
unknown committed
33
34
      stop("Non convenient date for the calibration period \"CalPer\"")
    } else {
35
      CalInd <- which(PrepGR$InputsModel$DatesR == CalPer[1]):which(PrepGR$InputsModel$DatesR == CalPer[2])
unknown's avatar
unknown committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    }
  }
  
  if (! any(CalCrit %in% c("NSE", "KGE", "KGE2", "RMSE"))) {
    stop("Non convenient efficiency criteria \"EffCrit\"")
  } else {
    CalCrit <- CalCrit[1L]
    CalCrit <- sprintf("ErrorCrit_%s", CalCrit)
    FUN_CRIT <- get(CalCrit)
  }
  
  if (! any(transfo %in% c("", "sqrt", "log", "inv", "sort"))) {
    stop("Non convenient transformation \"transfo\"")
  } else {
    transfo <- transfo[1L]
  }
  
53
  MOD_opt <- CreateRunOptions(FUN_MOD = get(PrepGR$TypeModel), InputsModel = PrepGR$InputsModel, 
unknown's avatar
unknown committed
54
55
56
                              IndPeriod_WarmUp = WupInd, IndPeriod_Run = CalInd, verbose = FALSE) 
  
  
57
  MOD_crt <- CreateInputsCrit(FUN_CRIT = FUN_CRIT, InputsModel = PrepGR$InputsModel, 
58
                              RunOptions = MOD_opt, Obs = PrepGR$Qobs[CalInd], transfo = transfo)
unknown's avatar
unknown committed
59
60
  
  
61
  CAL_opt <- CreateCalibOptions(FUN_MOD = get(PrepGR$TypeModel), FUN_CALIB = Calibration_Michel)
unknown's avatar
unknown committed
62
63
  
  
64
  CAL <- Calibration(InputsModel = PrepGR$InputsModel, RunOptions = MOD_opt,
unknown's avatar
unknown committed
65
                     InputsCrit = MOD_crt,  CalibOptions = CAL_opt,
66
                     FUN_MOD = get(PrepGR$TypeModel), FUN_CRIT = FUN_CRIT,
67
                     FUN_CALIB = Calibration_Michel, verbose = verbose)
unknown's avatar
unknown committed
68
69
  
  
70
71
  SIM <- RunModel(InputsModel = PrepGR$InputsModel, RunOptions = MOD_opt,
                  Param = CAL$ParamFinalR, FUN_MOD = get(PrepGR$TypeModel))
unknown's avatar
unknown committed
72
73
  
  
74
  CalGR <- list(OptionsCalib = MOD_opt, Qobs = PrepGR$Qobs[CalInd],
unknown's avatar
unknown committed
75
                OutputsCalib = CAL, OutputsModel = SIM,
76
77
                TypeModel = PrepGR$TypeModel, CalCrit = CalCrit,
                PeriodModel = list(WarmUp = as.POSIXct(PrepGR$InputsModel$DatesR[range(MOD_opt$IndPeriod_WarmUp)]),
unknown's avatar
unknown committed
78
79
80
81
82
                                   Run    = CalPer))
  class(CalGR) <- c("CalGR", "GR")
  return(CalGR)  
  
}