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