RunModel_GR5H.R 6.62 KB
Newer Older
1
RunModel_GR5H <- function(InputsModel, RunOptions, Param) {
2
3


4
  ## Initialization of variables
5
  NParam <- 5
6
7
  FortranOutputs <- .FortranOutputs(GR = "GR5H")$GR
  IsIntStore <- inherits(RunOptions, "interception")
8
  if (IsIntStore) {
9
10
11
12
    Imax <- RunOptions$Imax
  } else {
    Imax <- -99
  }
13
14


15
  ## Arguments check
16
  if (!inherits(InputsModel, "InputsModel")) {
17
    stop("'InputsModel' must be of class 'InputsModel'")
18
  }
19
  if (!inherits(InputsModel, "hourly"     )) {
20
    stop("'InputsModel' must be of class 'hourly'     ")
21
  }
22
  if (!inherits(InputsModel, "GR"         )) {
23
    stop("'InputsModel' must be of class 'GR'         ")
24
  }
25
  if (!inherits(RunOptions, "RunOptions"  )) {
26
    stop("'RunOptions' must be of class 'RunOptions'  ")
27
  }
28
  if (!inherits(RunOptions, "GR"          )) {
29
    stop("'RunOptions' must be of class 'GR'          ")
30
  }
31
32
33
34
35
36
  if (!is.vector(Param) | !is.numeric(Param)) {
    stop("'Param' must be a numeric vector")
  }
  if (sum(!is.na(Param)) != NParam) {
    stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
  }
37
  Param <- as.double(Param)
38

39
40
41
42
43
44
45
46
47
48
49
50
51
  Param_X1X3_threshold <- 1e-2
  Param_X4_threshold   <- 0.5
  if (Param[1L] < Param_X1X3_threshold) {
    warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
    Param[1L] <- Param_X1X3_threshold
  }
  if (Param[3L] < Param_X1X3_threshold) {
    warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
    Param[3L] <- Param_X1X3_threshold
  }
  if (Param[4L] < Param_X4_threshold) {
    warning(sprintf("Param[4] (X4: unit hydrograph time constant [h]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
    Param[4L] <- Param_X4_threshold
52
53
  }

54
  ## Input data preparation
55
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
56
57
    RunOptions$IndPeriod_WarmUp <- NULL
  }
58
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
59
  LInputSeries <- as.integer(length(IndPeriod1))
60
61
  if ("all" %in% RunOptions$Outputs_Sim) {
    IndOutputs <- as.integer(1:length(FortranOutputs))
62
63
64
  } else {
    IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
  }
65

66
  ## Output data preparation
67
68
69
  IndPeriod2     <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  ExportDatesR   <- "DatesR"   %in% RunOptions$Outputs_Sim
  ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
70

71
  ## Use of IniResLevels
72
  if (!is.null(RunOptions$IniResLevels)) {
73
74
    RunOptions$IniStates[1] <- RunOptions$IniResLevels[1] * Param[1] ### production store level (mm)
    RunOptions$IniStates[2] <- RunOptions$IniResLevels[2] * Param[3] ### routing store level (mm)
75
    if (IsIntStore) {
76
      RunOptions$IniStates[4] <- RunOptions$IniResLevels[4] * Imax ### interception store level (mm)
77
    }
78
  }
79

80
  ## Call GR model Fortan
81
  RESULTS <- .Fortran("frun_gr5h", PACKAGE = "airGR",
82
                      ## inputs
83
84
85
86
87
88
89
90
91
92
                      LInputs = LInputSeries,                             ### length of input and output series
                      InputsPrecip = InputsModel$Precip[IndPeriod1],      ### input series of total precipitation [mm/h]
                      InputsPE = InputsModel$PotEvap[IndPeriod1],         ### input series potential evapotranspiration [mm/h]
                      NParam = as.integer(length(Param)),                 ### number of model parameter
                      Param = Param,                                      ### parameter set
                      NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
                      StateStart = RunOptions$IniStates,                  ### state variables used when the model run starts
                      Imax = Imax,                                        ### maximal capacity of interception store
                      NOutputs = as.integer(length(IndOutputs)),          ### number of output series
                      IndOutputs = IndOutputs,                            ### indices of output series
93
                      ## outputs
94
95
                      Outputs = matrix(as.double(-99e9), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm or mm/h]
                      StateEnd = rep(as.double(-99e9), length(RunOptions$IniStates))                      ### state variables at the end of the model run
96
  )
97
98
  RESULTS$Outputs[RESULTS$Outputs   <= -99e8] <- NA
  RESULTS$StateEnd[RESULTS$StateEnd <= -99e8] <- NA
99
100
  if (ExportStateEnd) {
    RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
101
102
103
    RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5H, InputsModel = InputsModel,
                                        ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
                                        IntStore = RESULTS$StateEnd[4L],
104
                                        UH1 = NULL, UH2 = RESULTS$StateEnd[(1:(40*24)) + (7+20*24)],
105
                                        GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
106
107
                                        verbose = FALSE)
  }
108

109
110
  ## Output data preparation
  ## OutputsModel only
111
  if (!ExportDatesR & !ExportStateEnd) {
112
    OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
113
114
    names(OutputsModel) <- FortranOutputs[IndOutputs]
  }
115
  ## DatesR and OutputsModel only
116
  if (ExportDatesR & !ExportStateEnd) {
117
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
118
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
119
120
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
  }
121
  ## OutputsModel and StateEnd only
122
  if (!ExportDatesR & ExportStateEnd) {
123
    OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
124
                      list(RESULTS$StateEnd))
125
126
    names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
  }
127
  ## DatesR and OutputsModel and StateEnd
128
  if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
129
130
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
131
                      list(RESULTS$StateEnd))
132
133
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
  }
134

135
  ## End
136
  rm(RESULTS)
137
  class(OutputsModel) <- c("OutputsModel", "hourly", "GR")
138
  if (IsIntStore) {
139
140
    class(OutputsModel) <- c(class(OutputsModel), "interception")
  }
141
  return(OutputsModel)
142

143
}