Utils.R 7.6 KB
Newer Older
1

2
3
4
5
## =================================================================================
## function to check
## =================================================================================

6
# .onLoad <- function(libname, pkgname) {
7
8
9
10
11
12
13
#   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
## =================================================================================
## function to extract model features
## =================================================================================

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


## function to extract model features
28
.GetFeatModel <- function(FUN_MOD, DatesR = NULL) {
29
  FeatMod <- .FeatModels()
30
  NameFunMod <- ifelse(test = FeatMod$Pkg %in% "airGR",
31
32
                       yes  = paste("RunModel", FeatMod$NameMod, sep = "_"),
                       no   = FeatMod$NameMod)
33
  FunMod <- lapply(NameFunMod, FUN = match.fun)
34
35
36
37
38
39
  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]
40
41
42
43
44
45
46
47
48
49
    if (!is.null(DatesR)) {
      DiffTimeStep <- as.numeric(difftime(DatesR[length(DatesR)],
                                          DatesR[length(DatesR)-1],
                                          units = "secs"))
      if (is.na(res$TimeUnit)) {
        if (any(DiffTimeStep %in% 3600:3601)) { # 3601: leap second
          res$TimeUnit <- "hourly"
        } else {
          res$TimeUnit <- "daily"
        }
50
51
      }
    }
52
53
    res$TimeStep <- switch(res$TimeUnit,
                           hourly  =       1,
54
55
56
                           daily   =       1 * 24,
                           monthly =   28:31 * 24,
                           yearly  = 365:366 * 24)
57
    res$TimeStepMean <- switch(res$TimeUnit,
58
59
60
61
                               hourly  =           1,
                               daily   =           1 * 24,
                               monthly = 365.25 / 12 * 24,
                               yearly  =      365.25 * 24)
62
    res$TimeStep     <- res$TimeStep * 3600
63
    res$TimeStepMean <- as.integer(res$TimeStepMean * 3600)
64
    res$Class <- c(res$TimeUnit, res$Class)
65
    res$CodeModHydro <- res$CodeMod
66
    if (grepl("CemaNeige", res$NameFunMod)) {
67
      res$Class <- c(res$Class, "CemaNeige")
68
      res$CodeModHydro <- gsub("CemaNeige", "", res$CodeMod)
69
    }
70
    res$Class <- res$Class[!is.na(res$Class)]
71
72
    if (!is.null(DatesR)) {
      if (all(DiffTimeStep != res$TimeStep)) {
73
        stop("the time step of the model inputs must be ", res$TimeUnit)
74
      }
75
76
77
78
79
80
81
    }
    return(res)
  }
}



82
83
84
85
86
## =================================================================================
## function to manage Fortran outputs
## =================================================================================

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

88
89
  outGR <- NULL
  outCN <- NULL
90

91
92
93
94
95
96
97
  if (is.null(GR)) {
    GR <- ""
  }
  if (GR == "GR1A") {
    outGR <- c("PotEvap", "Precip",
               "Qsim")
  } else if (GR == "GR2M") {
98
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
99
100
               "AE",
               "Perc", "PR",
101
102
               "Rout",
               "AExch",
103
               "Qsim")
104
105
  } else if (GR == "GR5H") {
    outGR <- c("PotEvap", "Precip", "Interc", "Prod", "Pn", "Ps",
106
               "AE", "EI", "ES",
107
108
               "Perc", "PR",
               "Q9", "Q1",
109
               "Rout", "Exch",
110
111
               "AExch1", "AExch2",
               "AExch", "QR",
112
113
               "QD",
               "Qsim")
114
  } else if (GR %in% c("GR4J", "GR5J", "GR4H")) {
115
116
117
118
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
               "AE",
               "Perc", "PR",
               "Q9", "Q1",
119
               "Rout", "Exch",
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
               "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) {
137
138
139
    outCN <- c("Pliq", "Psol",
               "SnowPack", "ThermalState", "Gratio",
               "PotMelt", "Melt", "PliqAndMelt", "Temp",
140
141
               "Gthreshold", "Glocalmax")
  }
142

143
  res <- list(GR = outGR, CN = outCN)
144

145
}
146
147
148
149
150
151
152
153
154
155
156
157



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

## InputsModel

.ExtractInputsModel <- function(x, i) {
  res <- lapply(x, function(x) {
    if (is.matrix(x)) {
158
      res0 <- x[i, , drop = FALSE]
159
160
161
162
163
    }
    if (is.vector(x) | inherits(x, "POSIXt")) {
      res0 <- x[i]
    }
    if (is.list(x) & !inherits(x, "POSIXt")) {
164
165
166
167
168
      if (inherits(x, "OutputsModel")) {
        res0 <- .ExtractOutputsModel(x = x, i = i)
      } else {
        res0 <- .ExtractInputsModel(x = x, i = i)
      }
169
170
171
172
173
174
    }
    return(res0)
  })
  if (!is.null(x$ZLayers)) {
    res$ZLayers <- x$ZLayers
  }
175
176
177
178
  if (inherits(x, "SD")) {
    res$LengthHydro <- x$LengthHydro
    res$BasinAreas  <- x$BasinAreas
  }
179
180
181
182
183
184
185
186
  class(res) <- class(x)
  res
}

'[.InputsModel' <- function(x, i) {
  if (!inherits(x, "InputsModel")) {
    stop("'x' must be of class 'InputsModel'")
  }
187
188
189
190
  if (is.factor(i)) {
    i <- as.character(i)
  }
  if (is.numeric(i)) {
191
    .ExtractInputsModel(x, i)
192
  } else {
193
    NextMethod()
194
  }
195
196
197
}


198
## OutputsModel
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

.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)
  })
216
217
218
  if (!is.null(x$RunOptions)) {
    res$RunOptions <- x$RunOptions
  }
219
220
  if (!is.null(x$StateEnd)) {
    res$StateEnd <- x$StateEnd
221
222
223
224
225
  }
  class(res) <- class(x)
  res
}

226
.IndexOutputsModel <- function(x, i) {
227
  # '[.OutputsModel' <- function(x, i) {
228
229
230
231
232
233
234
235
236
237
238
239
  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()
  }
}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263



## =================================================================================
## function to try to set local time in English
## =================================================================================

.TrySetLcTimeEN <- function() {
  locale <- list("English_United Kingdom",
                 "en_US",
                 "en_US.UTF-8",
                 "en_US.utf8",
                 "en")
  dateTest <- as.POSIXct("2000-02-15", tz = "UTC", format = "%Y-%m-%d")
  monthTestTarget <- "February"
  monthTest <- function() {
    format(dateTest, format = "%B")
  }
  lapply(locale, function(x) {
    if (monthTest() != monthTestTarget) {
      Sys.setlocale(category = "LC_TIME", locale = x)
    }
  })
}