RunModel_GR2M.R 5.73 KB
Newer Older
1
RunModel_GR2M <- function(InputsModel, RunOptions, Param) {
2
  
3
4
  
  ## Initialization of variables
5
  NParam <- 2
6
7
  FortranOutputs <- .FortranOutputs(GR = "GR2M")$GR
  
8
  
9
  ## Arguments check
10
  if (!inherits(InputsModel, "InputsModel")) {
11
12
    stop("'InputsModel' must be of class 'InputsModel'")
  }  
13
  if (!inherits(InputsModel, "monthly"    )) {
14
15
    stop("'InputsModel' must be of class 'monthly'    ")
  }  
16
  if (!inherits(InputsModel, "GR"         )) {
17
18
    stop("'InputsModel' must be of class 'GR'         ")
  }  
19
  if (!inherits(RunOptions, "RunOptions"  )) {
20
21
    stop("'RunOptions' must be of class 'RunOptions'  ")
  }  
22
  if (!inherits(RunOptions, "GR"          )) {
23
24
25
26
27
28
29
30
    stop("'RunOptions' must be of class 'GR'          ")
  }  
  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 = ""))
  }
31
  Param <- as.double(Param)
32
33
34
35
36
37
38
39
40
41
42
  
  Param_X1X2_threshold <- 1e-2
  if (Param[1L] < Param_X1X2_threshold) {
    warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X2_threshold, Param_X1X2_threshold))
    Param[1L] <- Param_X1X2_threshold
  }
  if (Param[2L] < Param_X1X2_threshold) {
    warning(sprintf("Param[2] (X2: routing store capacity [mm]) < %.2f\n X2 set to %.2f", Param_X1X2_threshold, Param_X1X2_threshold))
    Param[2L] <- Param_X1X2_threshold
  }
  
43
  ## Input data preparation
44
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
45
46
    RunOptions$IndPeriod_WarmUp <- NULL
  }
47
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
48
  LInputSeries <- as.integer(length(IndPeriod1))
49
50
51
52
53
  if ("all" %in% RunOptions$Outputs_Sim) {
    IndOutputs <- as.integer(1:length(FortranOutputs)) 
  } else {
    IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
  }
54
  
55
  ## Output data preparation
56
57
58
  IndPeriod2     <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  ExportDatesR   <- "DatesR"   %in% RunOptions$Outputs_Sim
  ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
59
  
60
  ## Use of IniResLevels
61
  if (!is.null(RunOptions$IniResLevels)) {
62
63
    RunOptions$IniStates[1] <- RunOptions$IniResLevels[1] * Param[1] ### production store level (mm)
    RunOptions$IniStates[2] <- RunOptions$IniResLevels[2] * Param[2] ### routing store level (mm)
64
65
  }
  
66
  ## Call GR model Fortan
67
  RESULTS <- .Fortran("frun_gr2M", PACKAGE = "airGR", 
68
                      ## inputs
69
70
71
72
73
74
75
76
77
                      LInputs = LInputSeries,                             ### length of input and output series
                      InputsPrecip = InputsModel$Precip[IndPeriod1],      ### input series of total precipitation [mm/month]
                      InputsPE = InputsModel$PotEvap[IndPeriod1],         ### input series potential evapotranspiration [mm/month]
                      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
                      NOutputs = as.integer(length(IndOutputs)),          ### number of output series
                      IndOutputs = IndOutputs,                            ### indices of output series
78
                      ## outputs
79
80
                      Outputs = matrix(as.double(-99e9), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm or mm/month]
                      StateEnd = rep(as.double(-99e9), length(RunOptions$IniStates))                      ### state variables at the end of the model run
81
  )
82
83
  RESULTS$Outputs[RESULTS$Outputs   <= -99e8] <- NA
  RESULTS$StateEnd[RESULTS$StateEnd <= -99e8] <- NA
84
  if (ExportStateEnd) { 
85
86
87
88
    RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR2M, InputsModel = InputsModel, 
                                        ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, 
                                        UH1 = NULL, UH2 = NULL, 
                                        GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, 
89
90
91
92
                                        verbose = FALSE)
  }
  
  
93
94
  ## Output data preparation
  ## OutputsModel only
95
  if (!ExportDatesR & !ExportStateEnd) {
96
    OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
97
98
    names(OutputsModel) <- FortranOutputs[IndOutputs]
  }
99
  ## DatesR and OutputsModel only
100
  if (ExportDatesR & !ExportStateEnd) {
101
102
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), 
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
103
104
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
  }
105
  ## OutputsModel and SateEnd only
106
  if (!ExportDatesR & ExportStateEnd) {
107
108
    OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), 
                      list(RESULTS$StateEnd))
109
110
    names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
  }
111
  ## DatesR and OutputsModel and SateEnd
112
  if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
113
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), 
114
                       lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), 
115
                       list(RESULTS$StateEnd))
116
117
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
  }
118
  
119
  ## End
120
121
122
  rm(RESULTS) 
  class(OutputsModel) <- c("OutputsModel", "monthly", "GR")
  return(OutputsModel)
123
  
Delaigue Olivier's avatar
Delaigue Olivier committed
124
}