PrepGR.R 2.22 KB
Newer Older
1
PrepGR <- function(ObsDF = NULL, DatesR = NULL, Precip = NULL, PotEvap = NULL, Qobs = NULL, TempMean = NULL, 
2
3
                   ZInputs = NULL, HypsoData = NULL, NLayers = 5,
                   HydroModel, CemaNeige = FALSE) {
unknown's avatar
unknown committed
4
5
  
  
6
  if (is.null(ObsDF) && (is.null(DatesR) | is.null(Precip) | is.null(PotEvap))) {
unknown's avatar
unknown committed
7
8
9
    stop("Missing input data")
  }

10
11
12
  if (!is.null(ObsDF)) {
    if (ncol(ObsDF) >= 5) {
      TempMean <- ObsDF[, 5L]
13
    }
unknown's avatar
unknown committed
14
  }
15
  
16
17
18
19
20
21
  if (!is.null(Qobs)) {
    Qobs <- Qobs
  } else {
    Qobs <- NA
  }
  
unknown's avatar
unknown committed
22
23
24
25
26
27
  if (!is.null(TempMean)) {
    TempMean <- TempMean
  } else {
    TempMean <- NA
  }
  
28
29
  if (is.null(ObsDF)) {
    ObsDF <- data.frame(DatesR   = DatesR,
unknown's avatar
unknown committed
30
31
32
33
34
35
                        Precip   = Precip,
                        PotEvap  = PotEvap,
                        Qobs     = Qobs,
                        TempMean = TempMean)
  }
  
36
37
38
39
40
  if (!is.null(ObsDF)) {
    ObsDF <- data.frame(DatesR   = ObsDF[, 1L],
                        Precip   = ObsDF[, 2L],
                        PotEvap  = ObsDF[, 3L],
                        Qobs     = ObsDF[, 4L],
41
42
43
                        TempMean = TempMean)
  }
  
44
  if (! any(attributes(ObsDF$DatesR[1])$tzone %in%  "UTC")) {
45
    stop("Non convenient date format. Time zone must be defined as \"UTC\"")
unknown's avatar
unknown committed
46
47
  }
  
48
  SuiteGR <- paste0("GR", c("1A", "2M", "4J", "5J", "6J", "4H", "5H"))
unknown's avatar
unknown committed
49
  
50
  if (! any(HydroModel %in% SuiteGR)) {
unknown's avatar
unknown committed
51
52
53
    stop("Non convenient model")
  } else {
    if (! CemaNeige) {
54
      TypeModel <- sprintf("RunModel_%s", HydroModel)
unknown's avatar
unknown committed
55
    }
56
    if (CemaNeige && grepl("J|H", HydroModel)) {
57
      TypeModel <- sprintf("RunModel_CemaNeige%s", HydroModel)
unknown's avatar
unknown committed
58
    }
59
    if (CemaNeige && !grepl("J|H", HydroModel)) {
60
61
      warning("CemaNeige can not be used with ", HydroModel)
      TypeModel <- sprintf("RunModel_%s", HydroModel)
unknown's avatar
unknown committed
62
63
64
65
66
    }
    FUN_MOD <- get(TypeModel)
  }


67
68
  MOD_obs <- CreateInputsModel(FUN_MOD = FUN_MOD, DatesR = ObsDF$DatesR, 
                      Precip = ObsDF$Precip, PotEvap = ObsDF$PotEvap, TempMean = ObsDF$TempMean, 
unknown's avatar
unknown committed
69
70
71
                      ZInputs = ZInputs,  HypsoData = HypsoData, NLayers = NLayers, verbose = FALSE)
  
  
72
  PrepGR <- list(InputsModel = MOD_obs, Qobs = ObsDF$Qobs, TypeModel = TypeModel)
73
  class(PrepGR) <- c("PrepGR", "GR", "airGRt")
74
  return(PrepGR)
unknown's avatar
unknown committed
75
76
 
}