Newer
Older
Delaigue Olivier
committed
## =================================================================================
## 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.")
# }
# }
# }
Delaigue Olivier
committed
Delaigue Olivier
committed
## =================================================================================
## function to manage Fortran outputs
## =================================================================================
.FortranOutputs <- function(GR = NULL, isCN = FALSE) {
Dorchies David
committed
Delaigue Olivier
committed
outGR <- NULL
outCN <- NULL
Dorchies David
committed
Delaigue Olivier
committed
if (is.null(GR)) {
GR <- ""
}
if (GR == "GR1A") {
outGR <- c("PotEvap", "Precip",
"Qsim")
} else if (GR == "GR2M") {
Delaigue Olivier
committed
outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
Delaigue Olivier
committed
"AE",
"Perc", "PR",
"Rout", "Exch",
"Qsim")
} else if (GR == "GR5H") {
outGR <- c("PotEvap", "Precip", "Interc", "Prod", "Pn", "Ps",
Delaigue Olivier
committed
"AE", "EI", "ES",
"Perc", "PR",
"Q9", "Q1",
Dorchies David
committed
"Rout", "Exch",
"AExch1", "AExch2",
"AExch", "QR",
Delaigue Olivier
committed
"QD",
"Qsim")
Delaigue Olivier
committed
} else if (GR %in% c("GR4J", "GR5J", "GR4H")) {
Delaigue Olivier
committed
outGR <- c("PotEvap", "Precip", "Prod", "Pn", "Ps",
"AE",
"Perc", "PR",
"Q9", "Q1",
Dorchies David
committed
"Rout", "Exch",
Delaigue Olivier
committed
"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) {
Dorchies David
committed
outCN <- c("Pliq", "Psol",
"SnowPack", "ThermalState", "Gratio",
"PotMelt", "Melt", "PliqAndMelt", "Temp",
Delaigue Olivier
committed
"Gthreshold", "Glocalmax")
}
Dorchies David
committed
Delaigue Olivier
committed
res <- list(GR = outGR, CN = outCN)
Dorchies David
committed
Delaigue Olivier
committed
}
Delaigue Olivier
committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
## =================================================================================
## functions to extract parts of InputsModel or OutputsModel objects
## =================================================================================
## InputsModel
.ExtractInputsModel <- function(x, i) {
res <- lapply(x, function(x) {
if (is.matrix(x)) {
res0 <- x[i, ]
}
if (is.vector(x) | inherits(x, "POSIXt")) {
res0 <- x[i]
}
if (is.list(x) & !inherits(x, "POSIXt")) {
res0 <- .ExtractInputsModel(x = x, i = i)
}
return(res0)
})
if (!is.null(x$ZLayers)) {
res$ZLayers <- x$ZLayers
}
class(res) <- class(x)
res
}
'[.InputsModel' <- function(x, i) {
if (!inherits(x, "InputsModel")) {
stop("'x' must be of class 'InputsModel'")
}
.ExtractInputsModel(x, i)
}
## InputsModel
.ExtractOutputsModel <- function(x, i) {
IsStateEnd <- !is.null(x$StateEnd)
if (IsStateEnd) {
IsStateEnd <- TRUE
StateEnd <- x$StateEnd
x$StateEnd <- NULL
}
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)
})
if (IsStateEnd) {
res$StateEnd <- StateEnd
}
class(res) <- class(x)
res
}
'[.OutputsModel' <- function(x, i) {
if (!inherits(x, "OutputsModel")) {
stop("'x' must be of class 'OutputsModel'")
}
.ExtractOutputsModel(x, i)
}