CreateRunOptions.R 21.62 KiB
CreateRunOptions <- function(FUN_MOD, InputsModel, IndPeriod_WarmUp = NULL, IndPeriod_Run,
                             IniStates = NULL, IniResLevels = NULL, Imax = NULL,
                             Outputs_Cal = NULL, Outputs_Sim = "all",
                             RunSnowModule, MeanAnSolidPrecip = NULL,
                             IsHyst = FALSE,
                             warnings = TRUE, verbose = TRUE) {
  if (!missing(RunSnowModule)) {
    warning("deprecated 'RunSnowModule' argument: please adapt 'FUN_MOD' instead.", call. = FALSE)
  if (!is.null(Imax)) {
    if (!is.numeric(Imax) | length(Imax) != 1L) {
      stop("'Imax' must be a non negative 'numeric' value of length 1")
    } else {
      if (Imax < 0) {
        stop("'Imax' must be a non negative 'numeric' value of length 1")
    IsIntStore <- TRUE
  } else {
    IsIntStore <- FALSE
  ObjectClass <- NULL
  FUN_MOD <- match.fun(FUN_MOD)
  ##check_FUN_MOD
  BOOL <- FALSE
  if (identical(FUN_MOD, RunModel_GR4H) | identical(FUN_MOD, RunModel_GR5H)) {
    ObjectClass <- c(ObjectClass, "GR", "hourly")
    BOOL <- TRUE
  if (identical(FUN_MOD, RunModel_GR4J) | identical(FUN_MOD, RunModel_GR5J) | identical(FUN_MOD, RunModel_GR6J)) {
    ObjectClass <- c(ObjectClass, "GR", "daily")
    BOOL <- TRUE
  if (identical(FUN_MOD, RunModel_GR2M)) {
    ObjectClass <- c(ObjectClass, "GR", "monthly")
    BOOL <- TRUE
  if (identical(FUN_MOD, RunModel_GR1A)) {
    ObjectClass <- c(ObjectClass, "GR", "yearly")
    BOOL <- TRUE
  if (identical(FUN_MOD, RunModel_CemaNeige)) {
    ObjectClass <- c(ObjectClass, "CemaNeige", "daily")
    BOOL <- TRUE
  if (identical(FUN_MOD, RunModel_CemaNeigeGR4J) | identical(FUN_MOD, RunModel_CemaNeigeGR5J) | identical(FUN_MOD, RunModel_CemaNeigeGR6J)) {
    ObjectClass <- c(ObjectClass, "GR", "CemaNeige", "daily")
    BOOL <- TRUE
  if (identical(FUN_MOD, RunModel_CemaNeigeGR4H)) { # | identical(FUN_MOD, RunModel_CemaNeigeGR5H)
    ObjectClass <- c(ObjectClass, "GR", "CemaNeige", "hourly")
    BOOL <- TRUE
  if (IsIntStore) {
    ObjectClass <- c(ObjectClass, "interception")
  if (IsHyst) {
    ObjectClass <- c(ObjectClass, "hysteresis")
  if (!BOOL) {
    stop("incorrect 'FUN_MOD' for use in 'CreateRunOptions'")
  if (!"CemaNeige" %in% ObjectClass & "hysteresis" %in% ObjectClass) {
    stop("'IsHyst' cannot be TRUE for the chosen 'FUN_MOD'")
  #  if ((!(identical(FUN_MOD, RunModel_GR5H)) | !(identical(FUN_MOD, RunModel_CemaNeigeGR5H))) & "interception" %in% ObjectClass) {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
if (!(identical(FUN_MOD, RunModel_GR5H)) & "interception" %in% ObjectClass) { stop("'IMax' cannot be set for the chosen 'FUN_MOD'") } ##check_InputsModel if (!inherits(InputsModel, "InputsModel")) { stop("'InputsModel' must be of class 'InputsModel'") } if ("GR" %in% ObjectClass & !inherits(InputsModel, "GR")) { stop("'InputsModel' must be of class 'GR'") } if ("CemaNeige" %in% ObjectClass & !inherits(InputsModel, "CemaNeige")) { stop("'InputsModel' must be of class 'CemaNeige'") } if ("hourly" %in% ObjectClass & !inherits(InputsModel, "hourly")) { stop("'InputsModel' must be of class 'hourly'") } if ("daily" %in% ObjectClass & !inherits(InputsModel, "daily")) { stop("'InputsModel' must be of class 'daily'") } if ("monthly" %in% ObjectClass & !inherits(InputsModel, "monthly")) { stop("'InputsModel' must be of class 'monthly'") } if ("yearly" %in% ObjectClass & !inherits(InputsModel, "yearly")) { stop("'InputsModel' must be of class 'yearly'") } ##check_IndPeriod_Run if (!is.vector(IndPeriod_Run)) { stop("'IndPeriod_Run' must be a vector of numeric values") } if (!is.numeric(IndPeriod_Run)) { stop("'IndPeriod_Run' must be a vector of numeric values") } if (!identical(as.integer(IndPeriod_Run), as.integer(seq(from = IndPeriod_Run[1], to = tail(IndPeriod_Run, 1), by = 1)))) { stop("'IndPeriod_Run' must be a continuous sequence of integers") } if (storage.mode(IndPeriod_Run) != "integer") { stop("'IndPeriod_Run' should be of type integer") } ##check_IndPeriod_WarmUp WTxt <- NULL if (is.null(IndPeriod_WarmUp)) { WTxt <- paste(WTxt, "model warm up period not defined: default configuration used", sep = "") ##If_the_run_period_starts_at_the_very_beginning_of_the_time_series if (IndPeriod_Run[1L] == 1L) { IndPeriod_WarmUp <- as.integer(0) WTxt <- paste0(WTxt,"\n no data were found for model warm up!") ##We_look_for_the_longest_period_preceeding_the_run_period_with_a_maximum_of_one_year } else { TmpDateR0 <- InputsModel$DatesR[IndPeriod_Run[1]] TmpDateR <- TmpDateR0 - 365 * 24 * 60 * 60 ### minimal date to start the warmup if (format(TmpDateR, format = "%d") != format(TmpDateR0, format = "%d")) { ### leap year TmpDateR <- TmpDateR - 1 * 24 * 60 * 60 } IndPeriod_WarmUp <- which(InputsModel$DatesR == max(InputsModel$DatesR[1], TmpDateR)):(IndPeriod_Run[1] - 1) if ("hourly" %in% ObjectClass) { TimeStep <- as.integer(60 * 60) } if ("daily" %in% ObjectClass) { TimeStep <- as.integer(24 * 60 * 60)
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
} if ("monthly" %in% ObjectClass) { TimeStep <- as.integer(30.44 * 24 * 60 * 60) } if ("yearly" %in% ObjectClass) { TimeStep <- as.integer(365.25 * 24 * 60 * 60) } if (length(IndPeriod_WarmUp) * TimeStep / (365 * 24 * 60 * 60) >= 1) { WTxt <- paste0(WTxt, "\n the year preceding the run period is used \n") } else { WTxt <- paste0(WTxt, "\n less than a year (without missing values) was found for model warm up:") WTxt <- paste0(WTxt, "\n (", length(IndPeriod_WarmUp), " time-steps are used for initialisation)") } } } if (!is.null(IndPeriod_WarmUp)) { if (!is.vector(IndPeriod_WarmUp)) { stop("'IndPeriod_WarmUp' must be a vector of numeric values") } if (!is.numeric(IndPeriod_WarmUp)) { stop("'IndPeriod_WarmUp' must be a vector of numeric values") } if (storage.mode(IndPeriod_WarmUp) != "integer") { stop("'IndPeriod_WarmUp' should be of type integer") } if (identical(IndPeriod_WarmUp, as.integer(0)) & verbose) { message(paste0(WTxt, " No warm up period is used")) } if ((IndPeriod_Run[1] - 1) != tail(IndPeriod_WarmUp, 1) & !identical(IndPeriod_WarmUp, as.integer(0))) { WTxt <- paste0(WTxt, " Model warm up period is not directly before the model run period") } } if (!is.null(WTxt) & warnings) { warning(WTxt) } ## check IniResLevels if ("GR" %in% ObjectClass & ("monthly" %in% ObjectClass | "daily" %in% ObjectClass | "hourly" %in% ObjectClass)) { if (!is.null(IniResLevels)) { # if (!is.vector(IniResLevels) | !is.numeric(IniResLevels) | any(is.na(IniResLevels))) { if (!is.vector(IniResLevels) | is.character(IniResLevels) | is.factor(IniResLevels) | length(IniResLevels) != 4) { stop("'IniResLevels' must be a vector of 4 numeric values") } # if ((identical(FUN_MOD, RunModel_GR4H) | identical(FUN_MOD, RunModel_CemaNeigeGR4H) | # # (identical(FUN_MOD, RunModel_GR5H) & !IsIntStore) | # identical(FUN_MOD, RunModel_GR5H) | # identical(FUN_MOD, RunModel_GR4J) | identical(FUN_MOD, RunModel_CemaNeigeGR4J) | # identical(FUN_MOD, RunModel_GR5J) | identical(FUN_MOD, RunModel_CemaNeigeGR5J) | # identical(FUN_MOD, RunModel_GR2M)) & # length(IniResLevels) != 2) { # stop("the length of 'IniResLevels' must be 2 for the chosen 'FUN_MOD'") # } if (any(is.na(IniResLevels[1:2]))) { stop("the first 2 values of 'IniResLevels' cannot be missing values for the chosen 'FUN_MOD'") } # if ((identical(FUN_MOD,RunModel_GR6J) | identical(FUN_MOD,RunModel_CemaNeigeGR6J) | # (identical(FUN_MOD, RunModel_GR5H) & IsIntStore)) & # length(IniResLevels) != 3) { # stop("the length of 'IniResLevels' must be 3 for the chosen 'FUN_MOD'") # } if ((identical(FUN_MOD,RunModel_GR6J) | identical(FUN_MOD,RunModel_CemaNeigeGR6J))) { if (is.na(IniResLevels[3L])) { stop("the third value of 'IniResLevels' cannot be a missing value for the chosen 'FUN_MOD'") } } else { if (!is.na(IniResLevels[3L])) { warning("the third value of 'IniResLevels' is set to NA value for the chosen 'FUN_MOD'. Only GR6J presents an exponential store") IniResLevels[3L] <- NA }