Utils.R 6.2 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
## =================================================================================
## 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
28
.GetFeatModel <- function(FUN_MOD, DatesR) {
29
  FeatMod <- .FeatModels()
30
  NameFunMod <- ifelse(test = FeatMod$Pkg %in% "airGR",
31
32
33
34
35
36
37
38
39
                       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]
40
41
42
43
44
45
46
47
48
49
    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
54
55
    res$TimeStep <- switch(res$TimeUnit,
                           hourly  =       1,
                           daily   =      24,
                           monthly =   28:31,
                           yearly  = 365:366)
    res$TimeStep <- res$TimeStep * 3600
56

57
58
    res$Class <- c(res$TimeUnit, res$Class)
    if (grepl("CemaNeige", res$NameFunMod)) {
59
60
61
62
63
      res$Class <- unique(c(res$Class, "CemaNeige"))

    }
    if (DiffTimeStep != res$TimeStep) {
      stop("the time step of the model inputs must be ", res$TimeUnit, "\n")
64
65
66
67
68
69
70
    }
    return(res)
  }
}



71
72
73
74
75
## =================================================================================
## function to manage Fortran outputs
## =================================================================================

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

77
78
  outGR <- NULL
  outCN <- NULL
79

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

132
  res <- list(GR = outGR, CN = outCN)
133

134
}
135
136
137
138
139
140
141
142
143
144
145
146



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

## InputsModel

.ExtractInputsModel <- function(x, i) {
  res <- lapply(x, function(x) {
    if (is.matrix(x)) {
147
      res0 <- x[i, , drop = FALSE]
148
149
150
151
152
    }
    if (is.vector(x) | inherits(x, "POSIXt")) {
      res0 <- x[i]
    }
    if (is.list(x) & !inherits(x, "POSIXt")) {
153
154
155
156
157
      if (inherits(x, "OutputsModel")) {
        res0 <- .ExtractOutputsModel(x = x, i = i)
      } else {
        res0 <- .ExtractInputsModel(x = x, i = i)
      }
158
159
160
161
162
163
    }
    return(res0)
  })
  if (!is.null(x$ZLayers)) {
    res$ZLayers <- x$ZLayers
  }
164
165
166
167
  if (inherits(x, "SD")) {
    res$LengthHydro <- x$LengthHydro
    res$BasinAreas  <- x$BasinAreas
  }
168
169
170
171
172
173
174
175
  class(res) <- class(x)
  res
}

'[.InputsModel' <- function(x, i) {
  if (!inherits(x, "InputsModel")) {
    stop("'x' must be of class 'InputsModel'")
  }
176
177
178
179
  if (is.factor(i)) {
    i <- as.character(i)
  }
  if (is.numeric(i)) {
180
    .ExtractInputsModel(x, i)
181
  } else {
182
    NextMethod()
183
  }
184
185
186
}


187
## OutputsModel
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

.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)
  })
205
206
  if (!is.null(x$StateEnd)) {
    res$StateEnd <- x$StateEnd
207
208
209
210
211
  }
  class(res) <- class(x)
  res
}

212
213
214
215
216
217
218
219
220
221
222
223
224
# '[.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()
#   }
# }