diff --git a/DESCRIPTION b/DESCRIPTION index 0ee885323d3b81db2f8fb5231a3f2a5c855debe7..b31549027e4266dcbbac7efd4a6937cdbec9079a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: airGR Type: Package Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling -Version: 1.6.3.47 +Version: 1.6.3.48 Date: 2020-11-11 Authors@R: c( person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")), diff --git a/NEWS.md b/NEWS.md index 1beaebb0bfeeda0801c97d44437488eb2910b546..11f32e47a97fe1df297fd3f39555c0272785e788 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ -### 1.6.3.47 Release Notes (2020-11-11) +### 1.6.3.48 Release Notes (2020-11-11) #### New features diff --git a/R/RunModel_GR2M.R b/R/RunModel_GR2M.R index 59c3e9d414e0692c91384e676e3f0671c1cce20b..9b630e947f6e7c8d79473cd6877b38b76c0516af 100644 --- a/R/RunModel_GR2M.R +++ b/R/RunModel_GR2M.R @@ -1,98 +1,98 @@ RunModel_GR2M <- function(InputsModel,RunOptions,Param){ - - NParam <- 2; - FortranOutputs <- .FortranOutputs(GR = "GR2M")$GR - - ##Arguments_check - 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' ") } - if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } - if(inherits(RunOptions,"GR" )==FALSE){ 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_X1X2_threshold <- 1e-2 - if (Param[1L] < Param_X1X2_threshold) { - 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 (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)) - Param[2L] <- Param_X1X2_threshold - } - - ##Input_data_preparation - if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ 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[2]; ### routing store level (mm) - } - - ##Call_fortan - 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] - InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/month] - NParam=as.integer(length(Param)), ### number of model parameter - Param=Param, ### parameter set - 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 - IndOutputs=IndOutputs, ### indices of output series - ##outputs - 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 - ) - RESULTS$Outputs [round(RESULTS$Outputs ,3)==(-999.999)] <- NA; - RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; - if (ExportStateEnd) { - RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR2M, InputsModel = InputsModel, - ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, - UH1 = NULL, UH2 = NULL, - 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_SateEnd_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_SateEnd - 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","monthly","GR"); - return(OutputsModel); - + + NParam <- 2; + FortranOutputs <- .FortranOutputs(GR = "GR2M")$GR + + ##Arguments_check + 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' ") } + if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } + if(inherits(RunOptions,"GR" )==FALSE){ 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_X1X2_threshold <- 1e-2 + if (Param[1L] < Param_X1X2_threshold) { + 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 (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)) + Param[2L] <- Param_X1X2_threshold + } + + ##Input_data_preparation + if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ 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[2]; ### routing store level (mm) + } + + ##Call_fortan + 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] + InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/month] + NParam=as.integer(length(Param)), ### number of model parameter + Param=Param, ### parameter set + 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 + IndOutputs=IndOutputs, ### indices of output series + ##outputs + 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 + ) + RESULTS$Outputs [round(RESULTS$Outputs ,3)==(-999.999)] <- NA; + RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; + if (ExportStateEnd) { + RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR2M, InputsModel = InputsModel, + ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, + UH1 = NULL, UH2 = NULL, + 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_SateEnd_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_SateEnd + 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","monthly","GR"); + return(OutputsModel); + } diff --git a/R/RunModel_GR4H.R b/R/RunModel_GR4H.R index 71ecde1d427433921c2ef3c5ce42d0fca660303c..ebaae2b9e7fc0b39895c72e5e34e36cdefb3e787 100644 --- a/R/RunModel_GR4H.R +++ b/R/RunModel_GR4H.R @@ -1,103 +1,103 @@ RunModel_GR4H <- function(InputsModel,RunOptions,Param){ - - NParam <- 4; - FortranOutputs <- .FortranOutputs(GR = "GR4H")$GR - - ##Arguments_check - 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' ") } - if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } - if(inherits(RunOptions,"GR" )==FALSE){ 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,as.integer(0))){ 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) - } - - ##Call_fortan - RESULTS <- .Fortran("frun_gr4h",PACKAGE="airGR", - ##inputs - LInputs=LInputSeries, ### length of input and output series - InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h] - InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h] - NParam=as.integer(length(Param)), ### number of model parameter - Param=Param, ### parameter set - 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 - IndOutputs=IndOutputs, ### indices of output series - ##outputs - 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 - ) - 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_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)], - 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_SateEnd_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_SateEnd - 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"); - return(OutputsModel); - + + NParam <- 4; + FortranOutputs <- .FortranOutputs(GR = "GR4H")$GR + + ##Arguments_check + 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' ") } + if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } + if(inherits(RunOptions,"GR" )==FALSE){ 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,as.integer(0))){ 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) + } + + ##Call_fortan + RESULTS <- .Fortran("frun_gr4h",PACKAGE="airGR", + ##inputs + LInputs=LInputSeries, ### length of input and output series + InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h] + InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h] + NParam=as.integer(length(Param)), ### number of model parameter + Param=Param, ### parameter set + 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 + IndOutputs=IndOutputs, ### indices of output series + ##outputs + 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 + ) + 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_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)], + 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_SateEnd_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_SateEnd + 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"); + return(OutputsModel); + } diff --git a/R/RunModel_GR4J.R b/R/RunModel_GR4J.R index 8d383a18a17e16f0ee3aac77e952ebc96727b47f..da0f38024b06b63144166194bb8271da12246908 100644 --- a/R/RunModel_GR4J.R +++ b/R/RunModel_GR4J.R @@ -1,102 +1,102 @@ RunModel_GR4J <- function(InputsModel,RunOptions,Param){ - - NParam <- 4; - FortranOutputs <- .FortranOutputs(GR = "GR4J")$GR - - ##Arguments_check - 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' ") } - if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } - if(inherits(RunOptions,"GR" )==FALSE){ 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 [d]) < %.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,as.integer(0))){ 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); } - ##Input_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) - } - - ##Call_fortan - RESULTS <- .Fortran("frun_gr4j",PACKAGE="airGR", - ##inputs - LInputs=LInputSeries, ### length of input and output series - InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] - InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] - NParam=as.integer(length(Param)), ### number of model parameter - Param=Param, ### parameter set - 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 - IndOutputs=IndOutputs, ### indices of output series - ##outputs - 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 - ) - 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_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)], - 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","daily","GR"); - return(OutputsModel); - + + NParam <- 4; + FortranOutputs <- .FortranOutputs(GR = "GR4J")$GR + + ##Arguments_check + 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' ") } + if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } + if(inherits(RunOptions,"GR" )==FALSE){ 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 [d]) < %.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,as.integer(0))){ 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); } + ##Input_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) + } + + ##Call_fortan + RESULTS <- .Fortran("frun_gr4j",PACKAGE="airGR", + ##inputs + LInputs=LInputSeries, ### length of input and output series + InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] + InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] + NParam=as.integer(length(Param)), ### number of model parameter + Param=Param, ### parameter set + 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 + IndOutputs=IndOutputs, ### indices of output series + ##outputs + 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 + ) + 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_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)], + 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","daily","GR"); + return(OutputsModel); + } diff --git a/R/RunModel_GR5H.R b/R/RunModel_GR5H.R index a36613d6aad5038fe134199a4a36fe85df3e382f..a078b1321bdafd891aa841157f8b34abe0ae03a1 100644 --- a/R/RunModel_GR5H.R +++ b/R/RunModel_GR5H.R @@ -1,117 +1,117 @@ RunModel_GR5H <- function(InputsModel,RunOptions,Param){ - - NParam <- 5; - FortranOutputs <- .FortranOutputs(GR = "GR5H")$GR - IsIntStore <- inherits(RunOptions, "interception") + + NParam <- 5; + FortranOutputs <- .FortranOutputs(GR = "GR5H")$GR + IsIntStore <- inherits(RunOptions, "interception") + if(IsIntStore) { + Imax <- RunOptions$Imax + } else { + Imax <- -99 + } + + ##Arguments_check + 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' ") } + if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } + if(inherits(RunOptions,"GR" )==FALSE){ 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,as.integer(0))){ 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) { - Imax <- RunOptions$Imax - } else { - Imax <- -99 + 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'") } - if(inherits(InputsModel,"hourly" )==FALSE){ stop("'InputsModel' must be of class 'hourly' ") } - if(inherits(InputsModel,"GR" )==FALSE){ stop("'InputsModel' must be of class 'GR' ") } - if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } - if(inherits(RunOptions,"GR" )==FALSE){ 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,as.integer(0))){ 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) - } - } - - ##Call_fortan - RESULTS <- .Fortran("frun_gr5h",PACKAGE="airGR", - ##inputs - LInputs=LInputSeries, ### length of input and output series - InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h] - InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h] - NParam=as.integer(length(Param)), ### number of model parameter - Param=Param, ### parameter set - 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 - NOutputs=as.integer(length(IndOutputs)), ### number of output series - IndOutputs=IndOutputs, ### indices of output series - ##outputs - Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm or mm/h] - 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$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); - + } + + ##Call_fortan + RESULTS <- .Fortran("frun_gr5h",PACKAGE="airGR", + ##inputs + LInputs=LInputSeries, ### length of input and output series + InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/h] + InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/h] + NParam=as.integer(length(Param)), ### number of model parameter + Param=Param, ### parameter set + 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 + NOutputs=as.integer(length(IndOutputs)), ### number of output series + IndOutputs=IndOutputs, ### indices of output series + ##outputs + Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm or mm/h] + 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$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); + } diff --git a/R/RunModel_GR5J.R b/R/RunModel_GR5J.R index 56e750ae73aef1affd9762460837d445f3dc1b5a..c4ee72bc99afc6ac9d9541c7d0cd16bfa3347022 100644 --- a/R/RunModel_GR5J.R +++ b/R/RunModel_GR5J.R @@ -1,103 +1,103 @@ RunModel_GR5J <- function(InputsModel,RunOptions,Param){ - - NParam <- 5; - FortranOutputs <- .FortranOutputs(GR = "GR5J")$GR - - ##Arguments_check - 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' ") } - if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } - if(inherits(RunOptions,"GR" )==FALSE){ 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 [d]) < %.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,as.integer(0))){ 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) - } - - ##Call_fortan - RESULTS <- .Fortran("frun_gr5j",PACKAGE="airGR", - ##inputs - LInputs=LInputSeries, ### length of input and output series - InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] - InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] - NParam=as.integer(length(Param)), ### number of model parameter - Param=Param, ### parameter set - 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 - IndOutputs=IndOutputs, ### indices of output series - ##outputs - 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 - ) - 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_GR5J, InputsModel = InputsModel, - ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, - UH1 = NULL, UH2 = RESULTS$StateEnd[(1:40)+(7+20)], - 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_SateEnd_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_SateEnd - 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","daily","GR"); - return(OutputsModel); - + + NParam <- 5; + FortranOutputs <- .FortranOutputs(GR = "GR5J")$GR + + ##Arguments_check + 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' ") } + if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } + if(inherits(RunOptions,"GR" )==FALSE){ 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 [d]) < %.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,as.integer(0))){ 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) + } + + ##Call_fortan + RESULTS <- .Fortran("frun_gr5j",PACKAGE="airGR", + ##inputs + LInputs=LInputSeries, ### length of input and output series + InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] + InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] + NParam=as.integer(length(Param)), ### number of model parameter + Param=Param, ### parameter set + 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 + IndOutputs=IndOutputs, ### indices of output series + ##outputs + 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 + ) + 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_GR5J, InputsModel = InputsModel, + ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, + UH1 = NULL, UH2 = RESULTS$StateEnd[(1:40)+(7+20)], + 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_SateEnd_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_SateEnd + 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","daily","GR"); + return(OutputsModel); + } diff --git a/R/RunModel_GR6J.R b/R/RunModel_GR6J.R index 15bd28423f7efa46e2962b9aa47dea9fddce0813..d9c8e4dd50ebb9f430ad5ecf6e55e833279e032a 100644 --- a/R/RunModel_GR6J.R +++ b/R/RunModel_GR6J.R @@ -1,109 +1,108 @@ RunModel_GR6J <- function(InputsModel,RunOptions,Param){ - - NParam <- 6; - FortranOutputs <- .FortranOutputs(GR = "GR6J")$GR - - ##Arguments_check - 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' ") } - if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } - if(inherits(RunOptions,"GR" )==FALSE){ 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_X1X3X6_threshold <- 1e-2 - Param_X4_threshold <- 0.5 - 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)) - Param[1L] <- Param_X1X3X6_threshold - } - if (Param[3L] < Param_X1X3X6_threshold) { - 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 - } - if (Param[4L] < Param_X4_threshold) { - 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 (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[6L] <- Param_X1X3X6_threshold - } - - ##Input_data_preparation - if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ 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) - RunOptions$IniStates[3] <- RunOptions$IniResLevels[3] ### exponential store level (mm) - } - - ##Call_fortan - RESULTS <- .Fortran("frun_gr6j",PACKAGE="airGR", - ##inputs - LInputs=LInputSeries, ### length of input and output series - InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] - InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] - NParam=as.integer(length(Param)), ### number of model parameter - Param=Param, ### parameter set - 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 - IndOutputs=IndOutputs, ### indices of output series - ##outputs - 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 - ) - 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_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)], - 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_SateEnd_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_SateEnd - 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","daily","GR"); - return(OutputsModel); - + + NParam <- 6; + FortranOutputs <- .FortranOutputs(GR = "GR6J")$GR + + ##Arguments_check + 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' ") } + if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("'RunOptions' must be of class 'RunOptions' ") } + if(inherits(RunOptions,"GR" )==FALSE){ 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_X1X3X6_threshold <- 1e-2 + Param_X4_threshold <- 0.5 + 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)) + Param[1L] <- Param_X1X3X6_threshold + } + if (Param[3L] < Param_X1X3X6_threshold) { + 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 + } + if (Param[4L] < Param_X4_threshold) { + 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 (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[6L] <- Param_X1X3X6_threshold + } + + ##Input_data_preparation + if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ 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) + RunOptions$IniStates[3] <- RunOptions$IniResLevels[3] ### exponential store level (mm) + } + + ##Call_fortan + RESULTS <- .Fortran("frun_gr6j",PACKAGE="airGR", + ##inputs + LInputs=LInputSeries, ### length of input and output series + InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] + InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] + NParam=as.integer(length(Param)), ### number of model parameter + Param=Param, ### parameter set + 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 + IndOutputs=IndOutputs, ### indices of output series + ##outputs + 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 + ) + 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_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)], + 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_SateEnd_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_SateEnd + 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","daily","GR"); + return(OutputsModel); + } - \ No newline at end of file