Newer
Older
Delaigue Olivier
committed
RunModel_CemaNeigeGR4J <- function(InputsModel,RunOptions,Param){
Delaigue Olivier
committed
Delaigue Olivier
committed
## Initialization of variables
IsHyst <- inherits(RunOptions, "hysteresis")
NParam <- ifelse(test = IsHyst, yes = 8L, no = 6L)
Delaigue Olivier
committed
NParamCN <- NParam - 4L
Delaigue Olivier
committed
NStates <- 4L
Delaigue Olivier
committed
FortranOutputs <- .FortranOutputs(GR = "GR4J", isCN = TRUE)
unknown
committed
Delaigue Olivier
committed
if(!inherits(InputsModel,"InputsModel")){ stop("'InputsModel' must be of class 'InputsModel'") }
if(!inherits(InputsModel,"daily" )){ stop("'InputsModel' must be of class 'daily' ") }
if(!inherits(InputsModel,"GR" )){ stop("'InputsModel' must be of class 'GR' ") }
if(!inherits(InputsModel,"CemaNeige" )){ stop("'InputsModel' must be of class 'CemaNeige' ") }
if(!inherits(RunOptions,"RunOptions" )){ stop("'RunOptions' must be of class 'RunOptions' ") }
if(!inherits(RunOptions,"GR" )){ stop("'RunOptions' must be of class 'GR' ") }
if(!inherits(RunOptions,"CemaNeige" )){ stop("'RunOptions' must be of class 'CemaNeige' ") }
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_X1X3_threshold <- 1e-2
unknown
committed
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
}
unknown
committed
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))
IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries;
Delaigue Olivier
committed
ParamCemaNeige <- Param[(length(Param)-1-2*as.integer(IsHyst)):length(Param)];
NParamMod <- as.integer(length(Param)-(2+2*as.integer(IsHyst)));
ParamMod <- Param[1:NParamMod];
NLayers <- length(InputsModel$LayerPrecip);
Delaigue Olivier
committed
NStatesMod <- as.integer(length(RunOptions$IniStates)-NStates*NLayers);
ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim;
ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim;
unknown
committed
##SNOW_MODULE________________________________________________________________________________##
if(inherits(RunOptions,"CemaNeige")){
Delaigue Olivier
committed
if("all" %in% RunOptions$Outputs_Sim){ IndOutputsCemaNeige <- as.integer(1:length(FortranOutputs$CN));
} else { IndOutputsCemaNeige <- which(FortranOutputs$CN %in% RunOptions$Outputs_Sim); }
CemaNeigeLayers <- list(); CemaNeigeStateEnd <- NULL; NameCemaNeigeLayers <- "CemaNeigeLayers";
##Call_DLL_CemaNeige_________________________
for(iLayer in 1:NLayers){
Delaigue Olivier
committed
if (!IsHyst) {
StateStartCemaNeige <- RunOptions$IniStates[(7 + 20 + 40) + c(iLayer, iLayer+NLayers)]
} else {
StateStartCemaNeige <- RunOptions$IniStates[(7 + 20 + 40) + c(iLayer, iLayer+NLayers, iLayer+2*NLayers, iLayer+3*NLayers)]
}
Delaigue Olivier
committed
RESULTS <- .Fortran("frun_cemaneige",PACKAGE="airGR",
Delaigue Olivier
committed
LInputs=LInputSeries, ### length of input and output series
InputsPrecip=InputsModel$LayerPrecip[[iLayer]][IndPeriod1], ### input series of total precipitation [mm/d]
InputsFracSolidPrecip=InputsModel$LayerFracSolidPrecip[[iLayer]][IndPeriod1], ### input series of fraction of solid precipitation [0-1]
InputsTemp=InputsModel$LayerTemp[[iLayer]][IndPeriod1], ### input series of air mean temperature [degC]
MeanAnSolidPrecip=RunOptions$MeanAnSolidPrecip[iLayer], ### value of annual mean solid precip [mm/y]
Delaigue Olivier
committed
NParam=as.integer(NParamCN), ### number of model parameters = 2 or 4
Delaigue Olivier
committed
Param=as.double(ParamCemaNeige), ### parameter set
Delaigue Olivier
committed
NStates=as.integer(NStates), ### number of state variables used for model initialising = 4
Delaigue Olivier
committed
StateStart=StateStartCemaNeige, ### state variables used when the model run starts
IsHyst = as.integer(IsHyst), ### use of hysteresis
NOutputs=as.integer(length(IndOutputsCemaNeige)), ### number of output series
IndOutputs=IndOutputsCemaNeige, ### indices of output series
Delaigue Olivier
committed
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputsCemaNeige)), ### output series [mm]
Delaigue Olivier
committed
StateEnd=rep(as.double(-999.999),as.integer(NStates)) ### 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;
##Data_storage
CemaNeigeLayers[[iLayer]] <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]);
Delaigue Olivier
committed
names(CemaNeigeLayers[[iLayer]]) <- FortranOutputs$CN[IndOutputsCemaNeige];
IndPliqAndMelt <- which(names(CemaNeigeLayers[[iLayer]]) == "PliqAndMelt");
if(iLayer==1){ CatchMeltAndPliq <- RESULTS$Outputs[,IndPliqAndMelt]/NLayers; }
if(iLayer >1){ CatchMeltAndPliq <- CatchMeltAndPliq + RESULTS$Outputs[,IndPliqAndMelt]/NLayers; }
if(ExportStateEnd){ CemaNeigeStateEnd <- c(CemaNeigeStateEnd,RESULTS$StateEnd); }
rm(RESULTS);
} ###ENDFOR_iLayer
names(CemaNeigeLayers) <- sprintf("Layer%02i", seq_len(NLayers))
if(!inherits(RunOptions,"CemaNeige")){
CemaNeigeLayers <- list(); CemaNeigeStateEnd <- NULL; NameCemaNeigeLayers <- NULL;
CatchMeltAndPliq <- InputsModel$Precip[IndPeriod1]; }
##MODEL______________________________________________________________________________________##
Delaigue Olivier
committed
if("all" %in% RunOptions$Outputs_Sim){ IndOutputsMod <- as.integer(1:length(FortranOutputs$GR));
} else { IndOutputsMod <- which(FortranOutputs$GR %in% RunOptions$Outputs_Sim); }
unknown
committed
if(!is.null(RunOptions$IniResLevels)){
Delaigue Olivier
committed
RunOptions$IniStates[1] <- RunOptions$IniResLevels[1]*ParamMod[1]; ### production store level (mm)
RunOptions$IniStates[2] <- RunOptions$IniResLevels[2]*ParamMod[3]; ### routing store level (mm)
Delaigue Olivier
committed
RESULTS <- .Fortran("frun_gr4j",PACKAGE="airGR",
Delaigue Olivier
committed
LInputs=LInputSeries, ### length of input and output series
InputsPrecip=CatchMeltAndPliq, ### input series of total precipitation [mm/d]
InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d]
NParam=NParamMod, ### number of model parameter
Param=ParamMod, ### parameter set
NStates=NStatesMod, ### number of state variables used for model initialising
StateStart=RunOptions$IniStates[1:NStatesMod], ### state variables used when the model run starts
Delaigue Olivier
committed
NOutputs=as.integer(length(IndOutputsMod)), ### number of output series
IndOutputs=IndOutputsMod, ### indices of output series
Delaigue Olivier
committed
Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputsMod)), ### output series [mm]
StateEnd=rep(as.double(-999.999),NStatesMod) ### 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;
Delaigue Olivier
committed
if (ExportStateEnd) {
Delaigue Olivier
committed
RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
Delaigue Olivier
committed
idNStates <- seq_len(NStates*NLayers) %% NStates
Delaigue Olivier
committed
RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_CemaNeigeGR4J, InputsModel = InputsModel, IsHyst = IsHyst,
unknown
committed
ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
UH1 = RESULTS$StateEnd[(1:20)+7], UH2 = RESULTS$StateEnd[(1:40)+(7+20)],
Delaigue Olivier
committed
GCemaNeigeLayers = CemaNeigeStateEnd[seq_len(NStates*NLayers)[idNStates == 1]],
Delaigue Olivier
committed
eTGCemaNeigeLayers = CemaNeigeStateEnd[seq_len(NStates*NLayers)[idNStates == 2]],
Delaigue Olivier
committed
GthrCemaNeigeLayers = CemaNeigeStateEnd[seq_len(NStates*NLayers)[idNStates == 3]],
Delaigue Olivier
committed
GlocmaxCemaNeigeLayers = CemaNeigeStateEnd[seq_len(NStates*NLayers)[idNStates == 0]],
unknown
committed
verbose = FALSE)
}
unknown
committed
if(inherits(RunOptions,"CemaNeige") & "Precip" %in% RunOptions$Outputs_Sim){ RESULTS$Outputs[,which(FortranOutputs$GR[IndOutputsMod]=="Precip")] <- InputsModel$Precip[IndPeriod1]; }
##Output_data_preparation
##OutputsModel_only
if(!ExportDatesR & !ExportStateEnd){
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]),
list(CemaNeigeLayers) );
Delaigue Olivier
committed
names(OutputsModel) <- c(FortranOutputs$GR[IndOutputsMod],NameCemaNeigeLayers); }
if( ExportDatesR & !ExportStateEnd){
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]),
list(CemaNeigeLayers) );
Delaigue Olivier
committed
names(OutputsModel) <- c("DatesR",FortranOutputs$GR[IndOutputsMod],NameCemaNeigeLayers); }
if(!ExportDatesR & ExportStateEnd){
OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]),
list(CemaNeigeLayers),
unknown
committed
list(RESULTS$StateEnd) );
Delaigue Olivier
committed
names(OutputsModel) <- c(FortranOutputs$GR[IndOutputsMod],NameCemaNeigeLayers,"StateEnd"); }
if( ExportDatesR & ExportStateEnd){
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]),
lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]),
list(CemaNeigeLayers),
unknown
committed
list(RESULTS$StateEnd) );
Delaigue Olivier
committed
names(OutputsModel) <- c("DatesR",FortranOutputs$GR[IndOutputsMod],NameCemaNeigeLayers,"StateEnd"); }
Delaigue Olivier
committed
class(OutputsModel) <- c("OutputsModel","daily","GR","CemaNeige");
if(IsHyst) {
class(OutputsModel) <- c(class(OutputsModel), "hysteresis")
}