Utils.R 4.28 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
## =================================================================================
## function to manage Fortran outputs
## =================================================================================

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

22
23
  outGR <- NULL
  outCN <- NULL
24

25
26
27
28
29
30
31
  if (is.null(GR)) {
    GR <- ""
  }
  if (GR == "GR1A") {
    outGR <- c("PotEvap", "Precip",
               "Qsim")
  } else if (GR == "GR2M") {
32
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
33
34
35
36
               "AE",
               "Perc", "PR",
               "Rout", "Exch",
               "Qsim")
37
38
  } else if (GR == "GR5H") {
    outGR <- c("PotEvap", "Precip", "Interc", "Prod", "Pn", "Ps",
39
               "AE", "EI", "ES",
40
41
               "Perc", "PR",
               "Q9", "Q1",
42
               "Rout", "Exch",
43
44
               "AExch1", "AExch2",
               "AExch", "QR",
45
46
               "QD",
               "Qsim")
47
  } else if (GR %in% c("GR4J", "GR5J", "GR4H")) {
48
49
50
51
    outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
               "AE",
               "Perc", "PR",
               "Q9", "Q1",
52
               "Rout", "Exch",
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
               "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) {
70
71
72
    outCN <- c("Pliq", "Psol",
               "SnowPack", "ThermalState", "Gratio",
               "PotMelt", "Melt", "PliqAndMelt", "Temp",
73
74
               "Gthreshold", "Glocalmax")
  }
75

76
  res <- list(GR = outGR, CN = outCN)
77

78
}
79
80
81
82
83
84
85
86
87
88
89
90



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

## InputsModel

.ExtractInputsModel <- function(x, i) {
  res <- lapply(x, function(x) {
    if (is.matrix(x)) {
91
      res0 <- x[i, , drop = FALSE]
92
93
94
95
96
    }
    if (is.vector(x) | inherits(x, "POSIXt")) {
      res0 <- x[i]
    }
    if (is.list(x) & !inherits(x, "POSIXt")) {
97
98
99
100
101
      if (inherits(x, "OutputsModel")) {
        res0 <- .ExtractOutputsModel(x = x, i = i)
      } else {
        res0 <- .ExtractInputsModel(x = x, i = i)
      }
102
103
104
105
106
107
    }
    return(res0)
  })
  if (!is.null(x$ZLayers)) {
    res$ZLayers <- x$ZLayers
  }
108
109
110
111
  if (inherits(x, "SD")) {
    res$LengthHydro <- x$LengthHydro
    res$BasinAreas  <- x$BasinAreas
  }
112
113
114
115
116
117
118
119
  class(res) <- class(x)
  res
}

'[.InputsModel' <- function(x, i) {
  if (!inherits(x, "InputsModel")) {
    stop("'x' must be of class 'InputsModel'")
  }
120
121
122
123
  if (is.factor(i)) {
    i <- as.character(i)
  }
  if (is.numeric(i)) {
124
    .ExtractInputsModel(x, i)
125
  } else {
126
    NextMethod()
127
  }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}


## InputsModel

.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)
  })
149
150
  if (!is.null(x$StateEnd)) {
    res$StateEnd <- x$StateEnd
151
152
153
154
155
156
157
158
159
  }
  class(res) <- class(x)
  res
}

'[.OutputsModel' <- function(x, i) {
  if (!inherits(x, "OutputsModel")) {
    stop("'x' must be of class 'OutputsModel'")
  }
160
161
162
163
  if (is.factor(i)) {
    i <- as.character(i)
  }
  if (is.numeric(i)) {
164
    .ExtractOutputsModel(x, i)
165
  } else {
166
    NextMethod()
167
  }
168
}