Commit dffc8b9d authored by Delaigue Olivier's avatar Delaigue Olivier
Browse files

Merge branch 'formatRunModel' into 'dev'

Resolve "Clean the RunModel functions"

Closes #14

See merge request HYCAR-Hydro/airgr!17
Showing with 1836 additions and 1450 deletions
+1836 -1450
Package: airGR Package: airGR
Type: Package Type: Package
Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling
Version: 1.6.3.42 Version: 1.6.3.65
Date: 2020-11-09 Date: 2020-11-17
Authors@R: c( Authors@R: c(
person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")), person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")),
person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"), person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"),
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
### 1.6.3.42 Release Notes (2020-11-09) ### 1.6.3.65 Release Notes (2020-11-17)
#### New features #### New features
......
...@@ -8,7 +8,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -8,7 +8,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
FortranOutputsCemaNeige <- .FortranOutputs(GR = NULL, isCN = TRUE)$CN FortranOutputsCemaNeige <- .FortranOutputs(GR = NULL, isCN = TRUE)$CN
## Arguments_check ## Arguments check
if (!inherits(InputsModel, "InputsModel")) { if (!inherits(InputsModel, "InputsModel")) {
stop("'InputsModel' must be of class 'InputsModel'") stop("'InputsModel' must be of class 'InputsModel'")
} }
...@@ -39,7 +39,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -39,7 +39,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
time_mult <- 24 time_mult <- 24
} }
## Input_data_preparation ## Input data preparation
if (identical(RunOptions$IndPeriod_WarmUp, as.integer(0))) { if (identical(RunOptions$IndPeriod_WarmUp, as.integer(0))) {
RunOptions$IndPeriod_WarmUp <- NULL RunOptions$IndPeriod_WarmUp <- NULL
} }
...@@ -52,7 +52,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -52,7 +52,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
## SNOW_MODULE________________________________________________________________________________ ## CemaNeige________________________________________________________________________________
ParamCemaNeige <- Param ParamCemaNeige <- Param
NLayers <- length(InputsModel$LayerPrecip) NLayers <- length(InputsModel$LayerPrecip)
...@@ -71,7 +71,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -71,7 +71,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
NameCemaNeigeLayers <- "CemaNeigeLayers" NameCemaNeigeLayers <- "CemaNeigeLayers"
## Call_DLL_CemaNeige_________________________ ## Call CemaNeige Fortran_________________________
for (iLayer in 1:NLayers) { for (iLayer in 1:NLayers) {
if (!IsHyst) { if (!IsHyst) {
...@@ -105,7 +105,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -105,7 +105,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
## Data_storage ## Data storage
CemaNeigeLayers[[iLayer]] <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]) CemaNeigeLayers[[iLayer]] <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
names(CemaNeigeLayers[[iLayer]]) <- FortranOutputsCemaNeige[IndOutputsCemaNeige] names(CemaNeigeLayers[[iLayer]]) <- FortranOutputsCemaNeige[IndOutputsCemaNeige]
if (ExportStateEnd) { if (ExportStateEnd) {
...@@ -113,7 +113,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -113,7 +113,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
} }
rm(RESULTS) rm(RESULTS)
} ### ENDFOR_iLayer } ### ENDFOR iLayer
names(CemaNeigeLayers) <- sprintf("Layer%02i", seq_len(NLayers)) names(CemaNeigeLayers) <- sprintf("Layer%02i", seq_len(NLayers))
...@@ -129,7 +129,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) { ...@@ -129,7 +129,7 @@ RunModel_CemaNeige <- function(InputsModel, RunOptions, Param) {
verbose = FALSE) verbose = FALSE)
} }
## Output_data_preparation ## Output data preparation
if (!ExportDatesR & !ExportStateEnd) { if (!ExportDatesR & !ExportStateEnd) {
OutputsModel <- list(CemaNeigeLayers) OutputsModel <- list(CemaNeigeLayers)
names(OutputsModel) <- NameCemaNeigeLayers names(OutputsModel) <- NameCemaNeigeLayers
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
RunModel_GR1A <- function(InputsModel, RunOptions, Param) { RunModel_GR1A <- function(InputsModel, RunOptions, Param) {
## Initialization of variables
NParam <- 1 NParam <- 1
FortranOutputs <- .FortranOutputs(GR = "GR1A")$GR FortranOutputs <- .FortranOutputs(GR = "GR1A")$GR
## Arguments_check ## Arguments check
if (!inherits(InputsModel, "InputsModel")) { if (!inherits(InputsModel, "InputsModel")) {
stop("'InputsModel' must be of class 'InputsModel'") stop("'InputsModel' must be of class 'InputsModel'")
} }
...@@ -48,8 +50,8 @@ RunModel_GR1A <- function(InputsModel, RunOptions, Param) { ...@@ -48,8 +50,8 @@ RunModel_GR1A <- function(InputsModel, RunOptions, Param) {
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
## Call_fortan ## Call GR model Fortan
RESULTS <- .Fortran("frun_gr1a", PACKAGE = "airGR", RESULTS <- .Fortran("frun_gr1a", PACKAGE = "airGR",
## inputs ## inputs
LInputs = LInputSeries, ### length of input and output series LInputs = LInputSeries, ### length of input and output series
InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/y] InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/y]
...@@ -61,11 +63,11 @@ RunModel_GR1A <- function(InputsModel, RunOptions, Param) { ...@@ -61,11 +63,11 @@ RunModel_GR1A <- function(InputsModel, RunOptions, Param) {
NOutputs = as.integer(length(IndOutputs)), ### number of output series NOutputs = as.integer(length(IndOutputs)), ### number of output series
IndOutputs = IndOutputs, ### indices of output series IndOutputs = IndOutputs, ### indices of output series
## outputs ## outputs
Outputs = matrix(as.double(-999.999), nrow = LInputSeries,ncol=length(IndOutputs)), ### output series [mm] Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol=length(IndOutputs)), ### output series [mm]
StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
) )
RESULTS$Outputs[ round(RESULTS$Outputs , 3) == (-999.999)] <- NA RESULTS$Outputs[ round(RESULTS$Outputs , 3) == -999.999] <- NA
RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == (-999.999)] <- NA RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
## Output data preparation ## Output data preparation
...@@ -76,20 +78,20 @@ RunModel_GR1A <- function(InputsModel, RunOptions, Param) { ...@@ -76,20 +78,20 @@ RunModel_GR1A <- function(InputsModel, RunOptions, Param) {
} }
## DatesR and OutputsModel only ## DatesR and OutputsModel only
if (ExportDatesR & !ExportStateEnd) { if (ExportDatesR & !ExportStateEnd) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])) lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs]) names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
} }
## OutputsModel and SateEnd only ## OutputsModel and SateEnd only
if (!ExportDatesR & ExportStateEnd) { if (!ExportDatesR & ExportStateEnd) {
OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd)) list(RESULTS$StateEnd))
names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd") names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
} }
## DatesR and OutputsModel and SateEnd ## DatesR and OutputsModel and SateEnd
if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) { if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd)) list(RESULTS$StateEnd))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd") names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
} }
......
RunModel_GR2M <- function(InputsModel,RunOptions,Param){ RunModel_GR2M <- function(InputsModel, RunOptions, Param) {
NParam <- 2;
FortranOutputs <- .FortranOutputs(GR = "GR2M")$GR ## Initialization of variables
NParam <- 2
##Arguments_check FortranOutputs <- .FortranOutputs(GR = "GR2M")$GR
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("'InputsModel' must be of class 'InputsModel'") }
if(inherits(InputsModel,"monthly" )==FALSE){ stop("'InputsModel' must be of class 'monthly' ") }
if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } ## Arguments check
if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } if (!inherits(InputsModel, "InputsModel")) {
if(inherits(RunOptions,"GR" )==FALSE){ stop("'RunOptions' must be of class 'GR' ") } stop("'InputsModel' must be of class 'InputsModel'")
if(!is.vector(Param) | !is.numeric(Param)){ stop("'Param' must be a numeric vector") } }
if(sum(!is.na(Param))!=NParam){ stop(paste("'Param' must be a vector of length ",NParam," and contain no NA",sep="")) } if (!inherits(InputsModel, "monthly" )) {
Param <- as.double(Param); stop("'InputsModel' must be of class 'monthly' ")
}
Param_X1X2_threshold <- 1e-2 if (!inherits(InputsModel, "GR" )) {
if (Param[1L] < Param_X1X2_threshold) { stop("'InputsModel' must be of class 'GR' ")
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X2_threshold, Param_X1X2_threshold)) }
Param[1L] <- Param_X1X2_threshold if (!inherits(RunOptions, "RunOptions" )) {
} stop("'RunOptions' must be of class 'RunOptions' ")
if (Param[2L] < Param_X1X2_threshold) { }
warning(sprintf("Param[2] (X2: routing store capacity [mm]) < %.2f\n X2 set to %.2f", Param_X1X2_threshold, Param_X1X2_threshold)) if (!inherits(RunOptions, "GR" )) {
Param[2L] <- Param_X1X2_threshold stop("'RunOptions' must be of class 'GR' ")
} }
if (!is.vector(Param) | !is.numeric(Param)) {
##Input_data_preparation stop("'Param' must be a numeric vector")
if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; } }
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); if (sum(!is.na(Param)) != NParam) {
LInputSeries <- as.integer(length(IndPeriod1)) stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs)); }
} else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } Param <- as.double(Param)
##Output_data_preparation Param_X1X2_threshold <- 1e-2
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; if (Param[1L] < Param_X1X2_threshold) {
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X2_threshold, Param_X1X2_threshold))
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; Param[1L] <- Param_X1X2_threshold
}
##Use_of_IniResLevels if (Param[2L] < Param_X1X2_threshold) {
if(!is.null(RunOptions$IniResLevels)){ warning(sprintf("Param[2] (X2: routing store capacity [mm]) < %.2f\n X2 set to %.2f", Param_X1X2_threshold, Param_X1X2_threshold))
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1]; ### production store level (mm) Param[2L] <- Param_X1X2_threshold
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[2]; ### routing store level (mm) }
}
## Input data preparation
##Call_fortan if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
RESULTS <- .Fortran("frun_gr2M",PACKAGE="airGR", RunOptions$IndPeriod_WarmUp <- NULL
##inputs }
LInputs=LInputSeries, ### length of input and output series IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/month] LInputSeries <- as.integer(length(IndPeriod1))
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/month] if ("all" %in% RunOptions$Outputs_Sim) {
NParam=as.integer(length(Param)), ### number of model parameter IndOutputs <- as.integer(1:length(FortranOutputs))
Param=Param, ### parameter set } else {
NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
StateStart=RunOptions$IniStates, ### state variables used when the model run starts }
NOutputs=as.integer(length(IndOutputs)), ### number of output series
IndOutputs=IndOutputs, ### indices of output series ## Output data preparation
##outputs IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim
StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
)
RESULTS$Outputs [round(RESULTS$Outputs ,3)==(-999.999)] <- NA; ## Use of IniResLevels
RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; if (!is.null(RunOptions$IniResLevels)) {
if (ExportStateEnd) { RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1] ### production store level (mm)
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR2M, InputsModel = InputsModel, RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[2] ### routing store level (mm)
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, }
UH1 = NULL, UH2 = NULL,
GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, ## Call GR model Fortan
verbose = FALSE) RESULTS <- .Fortran("frun_gr2M", PACKAGE = "airGR",
} ## inputs
LInputs = LInputSeries, ### length of input and output series
InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/month]
##Output_data_preparation InputsPE = InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/month]
##OutputsModel_only NParam = as.integer(length(Param)), ### number of model parameter
if(ExportDatesR==FALSE & ExportStateEnd==FALSE){ Param = Param, ### parameter set
OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]); NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
names(OutputsModel) <- FortranOutputs[IndOutputs]; } StateStart = RunOptions$IniStates, ### state variables used when the model run starts
##DatesR_and_OutputsModel_only NOutputs = as.integer(length(IndOutputs)), ### number of output series
if(ExportDatesR==TRUE & ExportStateEnd==FALSE){ IndOutputs = IndOutputs, ### indices of output series
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), ## outputs
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) ); Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm]
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); } StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
##OutputsModel_and_SateEnd_only )
if(ExportDatesR==FALSE & ExportStateEnd==TRUE){ RESULTS$Outputs [round(RESULTS$Outputs , 3) == -999.999] <- NA
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
list(RESULTS$StateEnd) ); if (ExportStateEnd) {
names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); } RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR2M, InputsModel = InputsModel,
##DatesR_and_OutputsModel_and_SateEnd ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){ UH1 = NULL, UH2 = NULL,
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), verbose = FALSE)
list(RESULTS$StateEnd) ); }
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); }
##End ## Output data preparation
rm(RESULTS); ## OutputsModel only
class(OutputsModel) <- c("OutputsModel","monthly","GR"); if (!ExportDatesR & !ExportStateEnd) {
return(OutputsModel); OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
names(OutputsModel) <- FortranOutputs[IndOutputs]
}
## DatesR and OutputsModel only
if (ExportDatesR & !ExportStateEnd) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
}
## OutputsModel and SateEnd only
if (!ExportDatesR & ExportStateEnd) {
OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
}
## DatesR and OutputsModel and SateEnd
if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
}
## End
rm(RESULTS)
class(OutputsModel) <- c("OutputsModel", "monthly", "GR")
return(OutputsModel)
} }
RunModel_GR4H <- function(InputsModel,RunOptions,Param){ RunModel_GR4H <- function(InputsModel, RunOptions, Param) {
NParam <- 4;
FortranOutputs <- .FortranOutputs(GR = "GR4H")$GR ## Initialization of variables
NParam <- 4
##Arguments_check FortranOutputs <- .FortranOutputs(GR = "GR4H")$GR
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("'InputsModel' must be of class 'InputsModel'") }
if(inherits(InputsModel,"hourly" )==FALSE){ stop("'InputsModel' must be of class 'hourly' ") }
if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } ## Arguments check
if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } if (!inherits(InputsModel, "InputsModel")) {
if(inherits(RunOptions,"GR" )==FALSE){ stop("'RunOptions' must be of class 'GR' ") } stop("'InputsModel' must be of class 'InputsModel'")
if(!is.vector(Param) | !is.numeric(Param)){ stop("'Param' must be a numeric vector") } }
if(sum(!is.na(Param))!=NParam){ stop(paste("'Param' must be a vector of length ",NParam," and contain no NA",sep="")) } if (!inherits(InputsModel, "hourly" )) {
Param <- as.double(Param); stop("'InputsModel' must be of class 'hourly' ")
}
Param_X1X3_threshold <- 1e-2 if (!inherits(InputsModel, "GR" )) {
Param_X4_threshold <- 0.5 stop("'InputsModel' must be of class 'GR' ")
if (Param[1L] < Param_X1X3_threshold) { }
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) if (!inherits(RunOptions, "RunOptions" )) {
Param[1L] <- Param_X1X3_threshold stop("'RunOptions' must be of class 'RunOptions' ")
} }
if (Param[3L] < Param_X1X3_threshold) { if (!inherits(RunOptions, "GR" )) {
warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) stop("'RunOptions' must be of class 'GR' ")
Param[3L] <- Param_X1X3_threshold }
} if (!is.vector(Param) | !is.numeric(Param)) {
if (Param[4L] < Param_X4_threshold) { stop("'Param' must be a numeric vector")
warning(sprintf("Param[4] (X4: unit hydrograph time constant [h]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold)) }
Param[4L] <- Param_X4_threshold if (sum(!is.na(Param)) != NParam) {
} stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
}
##Input_data_preparation Param <- as.double(Param)
if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; }
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); Param_X1X3_threshold <- 1e-2
LInputSeries <- as.integer(length(IndPeriod1)) Param_X4_threshold <- 0.5
if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs)); if (Param[1L] < Param_X1X3_threshold) {
} else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
Param[1L] <- Param_X1X3_threshold
##Output_data_preparation }
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; if (Param[3L] < Param_X1X3_threshold) {
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; Param[3L] <- Param_X1X3_threshold
}
##Use_of_IniResLevels if (Param[4L] < Param_X4_threshold) {
if(!is.null(RunOptions$IniResLevels)){ warning(sprintf("Param[4] (X4: unit hydrograph time constant [h]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1]; ### production store level (mm) Param[4L] <- Param_X4_threshold
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3]; ### routing store level (mm) }
}
## Input data preparation
##Call_fortan if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
RESULTS <- .Fortran("frun_gr4h",PACKAGE="airGR", RunOptions$IndPeriod_WarmUp <- NULL
##inputs }
LInputs=LInputSeries, ### length of input and output series IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h] LInputSeries <- as.integer(length(IndPeriod1))
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h] if ("all" %in% RunOptions$Outputs_Sim) {
NParam=as.integer(length(Param)), ### number of model parameter IndOutputs <- as.integer(1:length(FortranOutputs))
Param=Param, ### parameter set } else {
NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
StateStart=RunOptions$IniStates, ### state variables used when the model run starts }
NOutputs=as.integer(length(IndOutputs)), ### number of output series
IndOutputs=IndOutputs, ### indices of output series ## Output data preparation
##outputs IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim
StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
)
RESULTS$Outputs[ round(RESULTS$Outputs ,3)==(-999.999)] <- NA; ## Use of IniResLevels
RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; if (!is.null(RunOptions$IniResLevels)) {
if (ExportStateEnd) { RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1] ### production store level (mm)
RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3] ### routing store level (mm)
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR4H, InputsModel = InputsModel, }
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
UH1 = RESULTS$StateEnd[(1:(20*24))+7], UH2 = RESULTS$StateEnd[(1:(40*24))+(7+20*24)], ## Call GR model Fortan
GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, RESULTS <- .Fortran("frun_gr4h", PACKAGE = "airGR",
verbose = FALSE) ## inputs
} LInputs = LInputSeries, ### length of input and output series
InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h]
##Output_data_preparation InputsPE = InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h]
##OutputsModel_only NParam = as.integer(length(Param)), ### number of model parameter
if(ExportDatesR==FALSE & ExportStateEnd==FALSE){ Param = Param, ### parameter set
OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]); NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
names(OutputsModel) <- FortranOutputs[IndOutputs]; } StateStart = RunOptions$IniStates, ### state variables used when the model run starts
##DatesR_and_OutputsModel_only NOutputs = as.integer(length(IndOutputs)), ### number of output series
if(ExportDatesR==TRUE & ExportStateEnd==FALSE){ IndOutputs = IndOutputs, ### indices of output series
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), ## outputs
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) ); Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm]
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); } StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
##OutputsModel_and_SateEnd_only )
if(ExportDatesR==FALSE & ExportStateEnd==TRUE){ RESULTS$Outputs[ round(RESULTS$Outputs , 3) == -999.999] <- NA
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
list(RESULTS$StateEnd) ); if (ExportStateEnd) {
names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); } RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
##DatesR_and_OutputsModel_and_SateEnd RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR4H, InputsModel = InputsModel,
if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){ ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), UH1 = RESULTS$StateEnd[(1:(20*24))+7], UH2 = RESULTS$StateEnd[(1:(40*24))+(7+20*24)],
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
list(RESULTS$StateEnd) ); verbose = FALSE)
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); } }
##End ## Output data preparation
rm(RESULTS); ## OutputsModel only
class(OutputsModel) <- c("OutputsModel","hourly","GR"); if (!ExportDatesR & !ExportStateEnd) {
return(OutputsModel); OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
names(OutputsModel) <- FortranOutputs[IndOutputs]
}
## DatesR and OutputsModel only
if (ExportDatesR & !ExportStateEnd) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
}
## OutputsModel and SateEnd only
if (!ExportDatesR & ExportStateEnd) {
OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
}
## DatesR and OutputsModel and SateEnd
if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
}
## End
rm(RESULTS)
class(OutputsModel) <- c("OutputsModel", "hourly", "GR")
return(OutputsModel)
} }
RunModel_GR4J <- function(InputsModel,RunOptions,Param){ RunModel_GR4J <- function(InputsModel, RunOptions, Param) {
NParam <- 4;
FortranOutputs <- .FortranOutputs(GR = "GR4J")$GR ## Initialization of variables
NParam <- 4
##Arguments_check FortranOutputs <- .FortranOutputs(GR = "GR4J")$GR
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("'InputsModel' must be of class 'InputsModel'") }
if(inherits(InputsModel,"daily" )==FALSE){ stop("'InputsModel' must be of class 'daily' ") }
if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } ## Arguments check
if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } if (!inherits(InputsModel, "InputsModel")) {
if(inherits(RunOptions,"GR" )==FALSE){ stop("'RunOptions' must be of class 'GR' ") } stop("'InputsModel' must be of class 'InputsModel'")
if(!is.vector(Param) | !is.numeric(Param)){ stop("'Param' must be a numeric vector") } }
if(sum(!is.na(Param))!=NParam){ stop(paste("'Param' must be a vector of length ",NParam," and contain no NA",sep="")) } if (!inherits(InputsModel, "daily" )) {
Param <- as.double(Param); stop("'InputsModel' must be of class 'daily' ")
}
Param_X1X3_threshold <- 1e-2 if (!inherits(InputsModel, "GR" )) {
Param_X4_threshold <- 0.5 stop("'InputsModel' must be of class 'GR' ")
if (Param[1L] < Param_X1X3_threshold) { }
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) if (!inherits(RunOptions, "RunOptions" )) {
Param[1L] <- Param_X1X3_threshold stop("'RunOptions' must be of class 'RunOptions' ")
} }
if (Param[3L] < Param_X1X3_threshold) { if (!inherits(RunOptions, "GR" )) {
warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) stop("'RunOptions' must be of class 'GR' ")
Param[3L] <- Param_X1X3_threshold }
} if (!is.vector(Param) | !is.numeric(Param)) {
if (Param[4L] < Param_X4_threshold) { stop("'Param' must be a numeric vector")
warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold)) }
Param[4L] <- Param_X4_threshold if (sum(!is.na(Param)) != NParam) {
} stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
}
##Input_data_preparation Param <- as.double(Param)
if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; }
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); Param_X1X3_threshold <- 1e-2
LInputSeries <- as.integer(length(IndPeriod1)) Param_X4_threshold <- 0.5
if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs)); if (Param[1L] < Param_X1X3_threshold) {
} else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
##Input_data_preparation Param[1L] <- Param_X1X3_threshold
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; }
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; if (Param[3L] < Param_X1X3_threshold) {
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
Param[3L] <- Param_X1X3_threshold
##Use_of_IniResLevels }
if(!is.null(RunOptions$IniResLevels)){ if (Param[4L] < Param_X4_threshold) {
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1]; ### production store level (mm) warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3]; ### routing store level (mm) Param[4L] <- Param_X4_threshold
} }
##Call_fortan ## Input data preparation
RESULTS <- .Fortran("frun_gr4j",PACKAGE="airGR", if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
##inputs RunOptions$IndPeriod_WarmUp <- NULL
LInputs=LInputSeries, ### length of input and output series }
InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] LInputSeries <- as.integer(length(IndPeriod1))
NParam=as.integer(length(Param)), ### number of model parameter if ("all" %in% RunOptions$Outputs_Sim) {
Param=Param, ### parameter set IndOutputs <- as.integer(1:length(FortranOutputs))
NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising } else {
StateStart=RunOptions$IniStates, ### state variables used when the model run starts IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
NOutputs=as.integer(length(IndOutputs)), ### number of output series }
IndOutputs=IndOutputs, ### indices of output series ## Input data preparation
##outputs IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim
StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
)
RESULTS$Outputs[ round(RESULTS$Outputs ,3)==(-999.999)] <- NA; ## Use of IniResLevels
RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; if (!is.null(RunOptions$IniResLevels)) {
if (ExportStateEnd) { RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1] ### production store level (mm)
RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3] ### routing store level (mm)
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR4J, InputsModel = InputsModel, }
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
UH1 = RESULTS$StateEnd[(1:20)+7], UH2 = RESULTS$StateEnd[(1:40)+(7+20)], ## Call GR model Fortan
GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, RESULTS <- .Fortran("frun_gr4j", PACKAGE = "airGR",
verbose = FALSE) ## inputs
} LInputs = LInputSeries, ### length of input and output series
InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d]
##Output_data_preparation InputsPE = InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d]
##OutputsModel_only NParam = as.integer(length(Param)), ### number of model parameter
if(ExportDatesR==FALSE & ExportStateEnd==FALSE){ Param = Param, ### parameter set
OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]); NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
names(OutputsModel) <- FortranOutputs[IndOutputs]; } StateStart = RunOptions$IniStates, ### state variables used when the model run starts
##DatesR_and_OutputsModel_only NOutputs = as.integer(length(IndOutputs)), ### number of output series
if(ExportDatesR==TRUE & ExportStateEnd==FALSE){ IndOutputs = IndOutputs, ### indices of output series
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), ## outputs
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) ); Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm]
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); } StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
##OutputsModel_and_StateEnd_only )
if(ExportDatesR==FALSE & ExportStateEnd==TRUE){ RESULTS$Outputs[ round(RESULTS$Outputs , 3) == -999.999] <- NA
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
list(RESULTS$StateEnd) ); if (ExportStateEnd) {
names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); } RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
##DatesR_and_OutputsModel_and_StateEnd RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR4J, InputsModel = InputsModel,
if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){ ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), UH1 = RESULTS$StateEnd[(1:20)+7], UH2 = RESULTS$StateEnd[(1:40)+(7+20)],
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
list(RESULTS$StateEnd) ); verbose = FALSE)
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); } }
##End ## Output data preparation
rm(RESULTS); ## OutputsModel only
class(OutputsModel) <- c("OutputsModel","daily","GR"); if (!ExportDatesR & !ExportStateEnd) {
return(OutputsModel); OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
names(OutputsModel) <- FortranOutputs[IndOutputs]
}
## DatesR and OutputsModel only
if (ExportDatesR & !ExportStateEnd) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
}
## OutputsModel and StateEnd only
if (!ExportDatesR & ExportStateEnd) {
OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
}
## DatesR and OutputsModel and StateEnd
if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
}
## End
rm(RESULTS)
class(OutputsModel) <- c("OutputsModel", "daily", "GR")
return(OutputsModel)
} }
RunModel_GR5H <- function(InputsModel,RunOptions,Param){ RunModel_GR5H <- function(InputsModel, RunOptions, Param) {
NParam <- 5;
FortranOutputs <- .FortranOutputs(GR = "GR5H")$GR ## Initialization of variables
IsIntStore <- inherits(RunOptions, "interception") NParam <- 5
if(IsIntStore) { FortranOutputs <- .FortranOutputs(GR = "GR5H")$GR
Imax <- RunOptions$Imax IsIntStore <- inherits(RunOptions, "interception")
} else { if (IsIntStore) {
Imax <- -99 Imax <- RunOptions$Imax
} else {
Imax <- -99
}
## Arguments check
if (!inherits(InputsModel, "InputsModel")) {
stop("'InputsModel' must be of class 'InputsModel'")
}
if (!inherits(InputsModel, "hourly" )) {
stop("'InputsModel' must be of class 'hourly' ")
}
if (!inherits(InputsModel, "GR" )) {
stop("'InputsModel' must be of class 'GR' ")
}
if (!inherits(RunOptions, "RunOptions" )) {
stop("'RunOptions' must be of class 'RunOptions' ")
}
if (!inherits(RunOptions, "GR" )) {
stop("'RunOptions' must be of class 'GR' ")
}
if (!is.vector(Param) | !is.numeric(Param)) {
stop("'Param' must be a numeric vector")
}
if (sum(!is.na(Param)) != NParam) {
stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
}
Param <- as.double(Param)
Param_X1X3_threshold <- 1e-2
Param_X4_threshold <- 0.5
if (Param[1L] < Param_X1X3_threshold) {
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
Param[1L] <- Param_X1X3_threshold
}
if (Param[3L] < Param_X1X3_threshold) {
warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
Param[3L] <- Param_X1X3_threshold
}
if (Param[4L] < Param_X4_threshold) {
warning(sprintf("Param[4] (X4: unit hydrograph time constant [h]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
Param[4L] <- Param_X4_threshold
}
## Input data preparation
if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
RunOptions$IndPeriod_WarmUp <- NULL
}
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
LInputSeries <- as.integer(length(IndPeriod1))
if ("all" %in% RunOptions$Outputs_Sim) { IndOutputs <- as.integer(1:length(FortranOutputs))
} else {
IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
}
## Output data preparation
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
## Use of IniResLevels
if (!is.null(RunOptions$IniResLevels)) {
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1] ### production store level (mm)
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3] ### routing store level (mm)
if (IsIntStore) {
RunOptions$IniStates[4] <- RunOptions$IniResLevels[4] * Imax ### interception store level (mm)
} }
}
##Arguments_check
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("'InputsModel' must be of class 'InputsModel'") } ## Call GR model Fortan
if(inherits(InputsModel,"hourly" )==FALSE){ stop("'InputsModel' must be of class 'hourly' ") } RESULTS <- .Fortran("frun_gr5h", PACKAGE = "airGR",
if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } ## inputs
if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } LInputs = LInputSeries, ### length of input and output series
if(inherits(RunOptions,"GR" )==FALSE){ stop("'RunOptions' must be of class 'GR' ") } InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h]
if(!is.vector(Param) | !is.numeric(Param)){ stop("'Param' must be a numeric vector") } InputsPE = InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h]
if(sum(!is.na(Param))!=NParam){ stop(paste("'Param' must be a vector of length ",NParam," and contain no NA",sep="")) } NParam = as.integer(length(Param)), ### number of model parameter
Param <- as.double(Param); Param = Param, ### parameter set
NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
Param_X1X3_threshold <- 1e-2 StateStart = RunOptions$IniStates, ### state variables used when the model run starts
Param_X4_threshold <- 0.5 Imax = Imax, ### maximal capacity of interception store
if (Param[1L] < Param_X1X3_threshold) { NOutputs = as.integer(length(IndOutputs)), ### number of output series
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) IndOutputs = IndOutputs, ### indices of output series
Param[1L] <- Param_X1X3_threshold ## outputs
} Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm or mm/h]
if (Param[3L] < Param_X1X3_threshold) { StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) )
Param[3L] <- Param_X1X3_threshold RESULTS$Outputs[ round(RESULTS$Outputs , 3) == -999.999] <- NA
} RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
if (Param[4L] < Param_X4_threshold) { if (ExportStateEnd) {
warning(sprintf("Param[4] (X4: unit hydrograph time constant [h]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold)) RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
Param[4L] <- Param_X4_threshold RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5H, InputsModel = InputsModel,
} ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
IntStore = RESULTS$StateEnd[4L],
##Input_data_preparation UH1 = NULL, UH2 = RESULTS$StateEnd[(1:(40*24))+(7+20*24)],
if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; } GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); verbose = FALSE)
LInputSeries <- as.integer(length(IndPeriod1)) }
if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs));
} else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } ## Output data preparation
## OutputsModel only
##Output_data_preparation if (!ExportDatesR & !ExportStateEnd) {
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; names(OutputsModel) <- FortranOutputs[IndOutputs]
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; }
## DatesR and OutputsModel only
##Use_of_IniResLevels if (ExportDatesR & !ExportStateEnd) {
if(!is.null(RunOptions$IniResLevels)){ OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1]; ### production store level (mm) lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3]; ### routing store level (mm) names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
if(IsIntStore) { }
RunOptions$IniStates[4] <- RunOptions$IniResLevels[4] * Imax; ### interception store level (mm) ## OutputsModel and StateEnd only
} if (!ExportDatesR & ExportStateEnd) {
} OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
##Call_fortan names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
RESULTS <- .Fortran("frun_gr5h",PACKAGE="airGR", }
##inputs ## DatesR and OutputsModel and StateEnd
LInputs=LInputSeries, ### length of input and output series if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h] OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h] lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
NParam=as.integer(length(Param)), ### number of model parameter list(RESULTS$StateEnd))
Param=Param, ### parameter set names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising }
StateStart=RunOptions$IniStates, ### state variables used when the model run starts
Imax=Imax, ### maximal capacity of interception store ## End
NOutputs=as.integer(length(IndOutputs)), ### number of output series rm(RESULTS)
IndOutputs=IndOutputs, ### indices of output series class(OutputsModel) <- c("OutputsModel", "hourly", "GR")
##outputs if (IsIntStore) {
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm or mm/h] class(OutputsModel) <- c(class(OutputsModel), "interception")
StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run }
) return(OutputsModel)
RESULTS$Outputs[ round(RESULTS$Outputs ,3)==(-999.999)] <- NA;
RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA;
if (ExportStateEnd) {
RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5H, InputsModel = InputsModel,
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
IntStore = RESULTS$StateEnd[4L],
UH1 = NULL, UH2 = RESULTS$StateEnd[(1:(40*24))+(7+20*24)],
GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
verbose = FALSE)
}
##Output_data_preparation
##OutputsModel_only
if(ExportDatesR==FALSE & ExportStateEnd==FALSE){
OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]);
names(OutputsModel) <- FortranOutputs[IndOutputs]; }
##DatesR_and_OutputsModel_only
if(ExportDatesR==TRUE & ExportStateEnd==FALSE){
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) );
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); }
##OutputsModel_and_StateEnd_only
if(ExportDatesR==FALSE & ExportStateEnd==TRUE){
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]),
list(RESULTS$StateEnd) );
names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); }
##DatesR_and_OutputsModel_and_StateEnd
if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]),
list(RESULTS$StateEnd) );
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); }
##End
rm(RESULTS);
class(OutputsModel) <- c("OutputsModel","hourly","GR");
if(IsIntStore) {
class(OutputsModel) <- c(class(OutputsModel), "interception")
}
return(OutputsModel);
} }
RunModel_GR5J <- function(InputsModel,RunOptions,Param){ RunModel_GR5J <- function(InputsModel, RunOptions, Param) {
NParam <- 5;
FortranOutputs <- .FortranOutputs(GR = "GR5J")$GR ## Initialization of variables
NParam <- 5
##Arguments_check FortranOutputs <- .FortranOutputs(GR = "GR5J")$GR
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("'InputsModel' must be of class 'InputsModel'") }
if(inherits(InputsModel,"daily" )==FALSE){ stop("'InputsModel' must be of class 'daily' ") }
if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } ## Arguments check
if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } if (!inherits(InputsModel, "InputsModel")) {
if(inherits(RunOptions,"GR" )==FALSE){ stop("'RunOptions' must be of class 'GR' ") } stop("'InputsModel' must be of class 'InputsModel'")
if(!is.vector(Param) | !is.numeric(Param)){ stop("'Param' must be a numeric vector") } }
if(sum(!is.na(Param))!=NParam){ stop(paste("'Param' must be a vector of length ",NParam," and contain no NA",sep="")) } if (!inherits(InputsModel, "daily" )) {
Param <- as.double(Param); stop("'InputsModel' must be of class 'daily' ")
}
Param_X1X3_threshold <- 1e-2 if (!inherits(InputsModel, "GR" )) {
Param_X4_threshold <- 0.5 stop("'InputsModel' must be of class 'GR' ")
if (Param[1L] < Param_X1X3_threshold) { }
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) if (!inherits(RunOptions, "RunOptions" )) {
Param[1L] <- Param_X1X3_threshold stop("'RunOptions' must be of class 'RunOptions' ")
} }
if (Param[3L] < Param_X1X3_threshold) { if (!inherits(RunOptions, "GR" )) {
warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold)) stop("'RunOptions' must be of class 'GR' ")
Param[3L] <- Param_X1X3_threshold }
} if (!is.vector(Param) | !is.numeric(Param)) {
if (Param[4L] < Param_X4_threshold) { stop("'Param' must be a numeric vector")
warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold)) }
Param[4L] <- Param_X4_threshold if (sum(!is.na(Param)) != NParam) {
} stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
}
##Input_data_preparation Param <- as.double(Param)
if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; }
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); Param_X1X3_threshold <- 1e-2
LInputSeries <- as.integer(length(IndPeriod1)) Param_X4_threshold <- 0.5
if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs)); if (Param[1L] < Param_X1X3_threshold) {
} else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
Param[1L] <- Param_X1X3_threshold
##Output_data_preparation }
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; if (Param[3L] < Param_X1X3_threshold) {
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; Param[3L] <- Param_X1X3_threshold
}
##Use_of_IniResLevels if (Param[4L] < Param_X4_threshold) {
if(!is.null(RunOptions$IniResLevels)){ warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1]; ### production store level (mm) Param[4L] <- Param_X4_threshold
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3]; ### routing store level (mm) }
}
## Input data preparation
##Call_fortan if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
RESULTS <- .Fortran("frun_gr5j",PACKAGE="airGR", RunOptions$IndPeriod_WarmUp <- NULL
##inputs }
LInputs=LInputSeries, ### length of input and output series IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] LInputSeries <- as.integer(length(IndPeriod1))
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] if ("all" %in% RunOptions$Outputs_Sim) {
NParam=as.integer(length(Param)), ### number of model parameter IndOutputs <- as.integer(1:length(FortranOutputs))
Param=Param, ### parameter set } else {
NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
StateStart=RunOptions$IniStates, ### state variables used when the model run starts }
NOutputs=as.integer(length(IndOutputs)), ### number of output series
IndOutputs=IndOutputs, ### indices of output series ## Output data preparation
##outputs IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim
StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
)
RESULTS$Outputs[ round(RESULTS$Outputs ,3)==(-999.999)] <- NA; ## Use of IniResLevels
RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; if (!is.null(RunOptions$IniResLevels)) {
if (ExportStateEnd) { RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1] ### production store level (mm)
RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3] ### routing store level (mm)
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5J, InputsModel = InputsModel, }
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
UH1 = NULL, UH2 = RESULTS$StateEnd[(1:40)+(7+20)], ## Call GR model Fortan
GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, RESULTS <- .Fortran("frun_gr5j", PACKAGE = "airGR",
verbose = FALSE) ## inputs
} LInputs = LInputSeries, ### length of input and output series
InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d]
##Output_data_preparation InputsPE = InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d]
##OutputsModel_only NParam = as.integer(length(Param)), ### number of model parameter
if(ExportDatesR==FALSE & ExportStateEnd==FALSE){ Param = Param, ### parameter set
OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]); NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
names(OutputsModel) <- FortranOutputs[IndOutputs]; } StateStart = RunOptions$IniStates, ### state variables used when the model run starts
##DatesR_and_OutputsModel_only NOutputs = as.integer(length(IndOutputs)), ### number of output series
if(ExportDatesR==TRUE & ExportStateEnd==FALSE){ IndOutputs = IndOutputs, ### indices of output series
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), ## outputs
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) ); Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm]
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); } StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
##OutputsModel_and_SateEnd_only )
if(ExportDatesR==FALSE & ExportStateEnd==TRUE){ RESULTS$Outputs[ round(RESULTS$Outputs , 3) == -999.999] <- NA
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
list(RESULTS$StateEnd) ); if (ExportStateEnd) {
names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); } RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
##DatesR_and_OutputsModel_and_SateEnd RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5J, InputsModel = InputsModel,
if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){ ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), UH1 = NULL, UH2 = RESULTS$StateEnd[(1:40)+(7+20)],
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
list(RESULTS$StateEnd) ); verbose = FALSE)
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); } }
##End ## Output data preparation
rm(RESULTS); ## OutputsModel only
class(OutputsModel) <- c("OutputsModel","daily","GR"); if (!ExportDatesR & !ExportStateEnd) {
return(OutputsModel); OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
names(OutputsModel) <- FortranOutputs[IndOutputs]
}
## DatesR and OutputsModel only
if (ExportDatesR & !ExportStateEnd) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
}
## OutputsModel and SateEnd only
if (!ExportDatesR & ExportStateEnd) {
OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
}
## DatesR and OutputsModel and SateEnd
if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
}
## End
rm(RESULTS)
class(OutputsModel) <- c("OutputsModel", "daily", "GR")
return(OutputsModel)
} }
RunModel_GR6J <- function(InputsModel,RunOptions,Param){ RunModel_GR6J <- function(InputsModel, RunOptions, Param) {
NParam <- 6;
FortranOutputs <- .FortranOutputs(GR = "GR6J")$GR ## Initialization of variables
NParam <- 6
##Arguments_check FortranOutputs <- .FortranOutputs(GR = "GR6J")$GR
if(inherits(InputsModel,"InputsModel")==FALSE){ stop("'InputsModel' must be of class 'InputsModel'") }
if(inherits(InputsModel,"daily" )==FALSE){ stop("'InputsModel' must be of class 'daily' ") }
if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } ## Arguments check
if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } if (!inherits(InputsModel, "InputsModel")) {
if(inherits(RunOptions,"GR" )==FALSE){ stop("'RunOptions' must be of class 'GR' ") } stop("'InputsModel' must be of class 'InputsModel'")
if(!is.vector(Param) | !is.numeric(Param)){ stop("'Param' must be a numeric vector") } }
if(sum(!is.na(Param))!=NParam){ stop(paste("'Param' must be a vector of length ",NParam," and contain no NA",sep="")) } if (!inherits(InputsModel, "daily" )) {
Param <- as.double(Param); stop("'InputsModel' must be of class 'daily' ")
}
Param_X1X3X6_threshold <- 1e-2 if (!inherits(InputsModel, "GR" )) {
Param_X4_threshold <- 0.5 stop("'InputsModel' must be of class 'GR' ")
if (Param[1L] < Param_X1X3X6_threshold) { }
warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold)) if (!inherits(RunOptions, "RunOptions" )) {
Param[1L] <- Param_X1X3X6_threshold stop("'RunOptions' must be of class 'RunOptions' ")
} }
if (Param[3L] < Param_X1X3X6_threshold) { if (!inherits(RunOptions, "GR" )) {
warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold)) stop("'RunOptions' must be of class 'GR' ")
Param[3L] <- Param_X1X3X6_threshold }
} if (!is.vector(Param) | !is.numeric(Param)) {
if (Param[4L] < Param_X4_threshold) { stop("'Param' must be a numeric vector")
warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold)) }
Param[4L] <- Param_X4_threshold if (sum(!is.na(Param)) != NParam) {
} stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
if (Param[6L] < Param_X1X3X6_threshold) { }
warning(sprintf("Param[6] (X6: coefficient for emptying exponential store [mm]) < %.2f\n X6 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold)) Param <- as.double(Param)
Param[6L] <- Param_X1X3X6_threshold
} Param_X1X3X6_threshold <- 1e-2
Param_X4_threshold <- 0.5
##Input_data_preparation if (Param[1L] < Param_X1X3X6_threshold) {
if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; } warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold))
IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); Param[1L] <- Param_X1X3X6_threshold
LInputSeries <- as.integer(length(IndPeriod1)) }
if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs)); if (Param[3L] < Param_X1X3X6_threshold) {
} else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold))
Param[3L] <- Param_X1X3X6_threshold
##Output_data_preparation }
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; if (Param[4L] < Param_X4_threshold) {
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; Param[4L] <- Param_X4_threshold
}
##Use_of_IniResLevels if (Param[6L] < Param_X1X3X6_threshold) {
if(!is.null(RunOptions$IniResLevels)){ warning(sprintf("Param[6] (X6: coefficient for emptying exponential store [mm]) < %.2f\n X6 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold))
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1]; ### production store level (mm) Param[6L] <- Param_X1X3X6_threshold
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3]; ### routing store level (mm) }
RunOptions$IniStates[3] <- RunOptions$IniResLevels[3] ### exponential store level (mm)
} ## Input data preparation
if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
##Call_fortan RunOptions$IndPeriod_WarmUp <- NULL
RESULTS <- .Fortran("frun_gr6j",PACKAGE="airGR", }
##inputs IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
LInputs=LInputSeries, ### length of input and output series LInputSeries <- as.integer(length(IndPeriod1))
InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] if ("all" %in% RunOptions$Outputs_Sim) {
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] IndOutputs <- as.integer(1:length(FortranOutputs))
NParam=as.integer(length(Param)), ### number of model parameter } else {
Param=Param, ### parameter set IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising }
StateStart=RunOptions$IniStates, ### state variables used when the model run starts
NOutputs=as.integer(length(IndOutputs)), ### number of output series ## Output data preparation
IndOutputs=IndOutputs, ### indices of output series IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
##outputs ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run
) ## Use of IniResLevels
RESULTS$Outputs[ round(RESULTS$Outputs ,3)==(-999.999)] <- NA; if (!is.null(RunOptions$IniResLevels)) {
RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*Param[1] ### production store level (mm)
if (ExportStateEnd) { RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*Param[3] ### routing store level (mm)
RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location RunOptions$IniStates[3] <- RunOptions$IniResLevels[3] ### exponential store level (mm)
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR6J, InputsModel = InputsModel, }
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = RESULTS$StateEnd[3L],
UH1 = RESULTS$StateEnd[(1:20)+7], UH2 = RESULTS$StateEnd[(1:40)+(7+20)], ## Call GR model Fortan
GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, RESULTS <- .Fortran("frun_gr6j", PACKAGE = "airGR",
verbose = FALSE) ## inputs
} LInputs = LInputSeries, ### length of input and output series
InputsPrecip = InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d]
##Output_data_preparation InputsPE = InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d]
##OutputsModel_only NParam = as.integer(length(Param)), ### number of model parameter
if(ExportDatesR==FALSE & ExportStateEnd==FALSE){ Param = Param, ### parameter set
OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]); NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
names(OutputsModel) <- FortranOutputs[IndOutputs]; } StateStart = RunOptions$IniStates, ### state variables used when the model run starts
##DatesR_and_OutputsModel_only NOutputs = as.integer(length(IndOutputs)), ### number of output series
if(ExportDatesR==TRUE & ExportStateEnd==FALSE){ IndOutputs = IndOutputs, ### indices of output series
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), ## outputs
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) ); Outputs = matrix(as.double(-999.999), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm]
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); } StateEnd = rep(as.double(-999.999), length(RunOptions$IniStates)) ### state variables at the end of the model run
##OutputsModel_and_SateEnd_only )
if(ExportDatesR==FALSE & ExportStateEnd==TRUE){ RESULTS$Outputs[ round(RESULTS$Outputs , 3) == -999.999] <- NA
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), RESULTS$StateEnd[round(RESULTS$StateEnd, 3) == -999.999] <- NA
list(RESULTS$StateEnd) ); if (ExportStateEnd) {
names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); } RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
##DatesR_and_OutputsModel_and_SateEnd RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR6J, InputsModel = InputsModel,
if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){ ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = RESULTS$StateEnd[3L],
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), UH1 = RESULTS$StateEnd[(1:20)+7], UH2 = RESULTS$StateEnd[(1:40)+(7+20)],
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
list(RESULTS$StateEnd) ); verbose = FALSE)
names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); } }
##End ## Output data preparation
rm(RESULTS); ## OutputsModel only
class(OutputsModel) <- c("OutputsModel","daily","GR"); if (!ExportDatesR & !ExportStateEnd) {
return(OutputsModel); OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
names(OutputsModel) <- FortranOutputs[IndOutputs]
}
## DatesR and OutputsModel only
if (ExportDatesR & !ExportStateEnd) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
}
## OutputsModel and SateEnd only
if (!ExportDatesR & ExportStateEnd) {
OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
}
## DatesR and OutputsModel and SateEnd
if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]),
list(RESULTS$StateEnd))
names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
}
## End
rm(RESULTS)
class(OutputsModel) <- c("OutputsModel", "daily", "GR")
return(OutputsModel)
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment