Utils.R 5.73 KB
Newer Older
1

2
3
4
5
6
7
8
9
10
11
12
13
## =================================================================================
## function to check
## =================================================================================

# .onLoad <- function(libname, pkgname){
#   if (requireNamespace("airGRteaching", quietly = TRUE)) {
#     if (packageVersion("airGRteaching") %in% package_version(c("0.2.0.9", "0.2.2.2", "0.2.3.2"))) {
#       packageStartupMessage("In order to be compatible with the present version of 'airGR', please update your version of the 'airGRteaching' package.")
#     }
#   }
# }

14
15


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## =================================================================================
## function to extract model features
## =================================================================================

## table of feature models
.FeatModels <- function() {
  path <- system.file("modelsFeatures/FeatModelsGR.csv", package = "airGR")
  read.table(path, header = TRUE, sep = ";")
}


## function to extract model features
.GetFeatModel <- function(FUN_MOD) {
  FeatMod <- .FeatModels()
  FUN_MOD <- match.fun(FUN_MOD)
  NameFunMod <- ifelse(test = FeatMod$Class %in% "GR",
                       yes  = paste("RunModel", FeatMod$NameMod, sep = "_"),
                       no   = FeatMod$NameMod)
  FunMod <- lapply(NameFunMod, FUN = get)
  IdMod <- which(sapply(FunMod, FUN = function(x) identical(FUN_MOD, x)))
  if (length(IdMod) < 1) {
    stop("'FUN_MOD' must be one of ", paste(NameFunMod, collapse = ", "))
  } else {
    res <- as.list(FeatMod[IdMod, ])
    res$NameFunMod <- NameFunMod[IdMod]
    res$TimeStep <- switch(res$TimeUnit,
                           hourly  =       1,
                           daily   =      24,
                           monthly =   28:31,
                           yearly  = 365:366)
    res$TimeStep <- res$TimeStep * 3600
    res$Class <- c(res$TimeUnit, res$Class)
    if (grepl("CemaNeige", res$NameFunMod)) {
      res$Class <- c(res$Class, "CemaNeige")
    }
    return(res)
  }
}



57
58
59
60
61
## =================================================================================
## function to manage Fortran outputs
## =================================================================================

.FortranOutputs <- function(GR = NULL, isCN = FALSE) {
62

63
64
  outGR <- NULL
  outCN <- NULL
65

66
67
68
69
70
71
72
  if (is.null(GR)) {
    GR <- ""
  }
  if (GR == "GR1A") {
    outGR <- c("PotEvap", "Precip",
               "Qsim")
  } else if (GR == "GR2M") {
73
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
74
75
               "AE",
               "Perc", "PR",
76
77
               "Rout",
               "AExch",
78
               "Qsim")
79
80
  } else if (GR == "GR5H") {
    outGR <- c("PotEvap", "Precip", "Interc", "Prod", "Pn", "Ps",
81
               "AE", "EI", "ES",
82
83
               "Perc", "PR",
               "Q9", "Q1",
84
               "Rout", "Exch",
85
86
               "AExch1", "AExch2",
               "AExch", "QR",
87
88
               "QD",
               "Qsim")
89
  } else if (GR %in% c("GR4J", "GR5J", "GR4H")) {
90
91
92
93
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
               "AE",
               "Perc", "PR",
               "Q9", "Q1",
94
               "Rout", "Exch",
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
               "AExch1", "AExch2",
               "AExch", "QR",
               "QD",
               "Qsim")
  } else if (GR == "GR6J") {
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
               "AE",
               "Perc", "PR",
               "Q9", "Q1",
               "Rout", "Exch",
               "AExch1", "AExch2",
               "AExch", "QR",
               "QRExp", "Exp",
               "QD",
               "Qsim")
  }
  if (isCN) {
112
113
114
    outCN <- c("Pliq", "Psol",
               "SnowPack", "ThermalState", "Gratio",
               "PotMelt", "Melt", "PliqAndMelt", "Temp",
115
116
               "Gthreshold", "Glocalmax")
  }
117

118
  res <- list(GR = outGR, CN = outCN)
119

120
}
121
122
123
124
125
126
127
128
129
130
131
132



## =================================================================================
## functions to extract parts of InputsModel or OutputsModel objects
## =================================================================================

## InputsModel

.ExtractInputsModel <- function(x, i) {
  res <- lapply(x, function(x) {
    if (is.matrix(x)) {
133
      res0 <- x[i, , drop = FALSE]
134
135
136
137
138
    }
    if (is.vector(x) | inherits(x, "POSIXt")) {
      res0 <- x[i]
    }
    if (is.list(x) & !inherits(x, "POSIXt")) {
139
140
141
142
143
      if (inherits(x, "OutputsModel")) {
        res0 <- .ExtractOutputsModel(x = x, i = i)
      } else {
        res0 <- .ExtractInputsModel(x = x, i = i)
      }
144
145
146
147
148
149
    }
    return(res0)
  })
  if (!is.null(x$ZLayers)) {
    res$ZLayers <- x$ZLayers
  }
150
151
152
153
  if (inherits(x, "SD")) {
    res$LengthHydro <- x$LengthHydro
    res$BasinAreas  <- x$BasinAreas
  }
154
155
156
157
158
159
160
161
  class(res) <- class(x)
  res
}

'[.InputsModel' <- function(x, i) {
  if (!inherits(x, "InputsModel")) {
    stop("'x' must be of class 'InputsModel'")
  }
162
163
164
165
  if (is.factor(i)) {
    i <- as.character(i)
  }
  if (is.numeric(i)) {
166
    .ExtractInputsModel(x, i)
167
  } else {
168
    NextMethod()
169
  }
170
171
172
}


173
## OutputsModel
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

.ExtractOutputsModel <- function(x, i) {
  res <- lapply(x, function(x) {
    if (is.matrix(x)  && length(dim(x)) == 2L) {
      res0 <- x[i, ]
    }
    if (is.array(x) && length(dim(x)) == 3L) {
      res0 <- x[i, , ]
    }
    if (is.vector(x) | inherits(x, "POSIXt")) {
      res0 <- x[i]
    }
    if (is.list(x) & !inherits(x, "POSIXt")) {
      res0 <- .ExtractOutputsModel(x = x, i = i)
    }
    return(res0)
  })
191
192
  if (!is.null(x$StateEnd)) {
    res$StateEnd <- x$StateEnd
193
194
195
196
197
  }
  class(res) <- class(x)
  res
}

198
199
200
201
202
203
204
205
206
207
208
209
210
# '[.OutputsModel' <- function(x, i) {
#   if (!inherits(x, "OutputsModel")) {
#     stop("'x' must be of class 'OutputsModel'")
#   }
#   if (is.factor(i)) {
#     i <- as.character(i)
#   }
#   if (is.numeric(i)) {
#     .ExtractOutputsModel(x, i)
#   } else {
#     NextMethod()
#   }
# }