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

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

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



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

## InputsModel

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

'[.InputsModel' <- function(x, i) {
  if (!inherits(x, "InputsModel")) {
    stop("'x' must be of class 'InputsModel'")
  }
121
122
123
124
  if (is.factor(i)) {
    i <- as.character(i)
  }
  if (is.numeric(i)) {
125
    .ExtractInputsModel(x, i)
126
  } else {
127
    NextMethod()
128
  }
129
130
131
}


132
## OutputsModel
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

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

157
158
159
160
161
162
163
164
165
166
167
168
169
# '[.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()
#   }
# }