Forked from HYCAR-Hydro / airGR
Source project has a limited visibility.
CreateInputsModel.R 10.92 KiB
CreateInputsModel <- function(FUN_MOD,
                              DatesR,
                              Precip, PrecipScale = TRUE,
                              PotEvap = NULL,
                              TempMean = NULL, TempMin = NULL, TempMax = NULL,
                              ZInputs = NULL, HypsoData = NULL, NLayers = 5,
                              verbose = TRUE) {
    ObjectClass <- NULL
    ##check_FUN_MOD
    BOOL <- FALSE
    if (identical(FUN_MOD, RunModel_GR4H)) {
      ObjectClass <- c(ObjectClass, "hourly", "GR")
      TimeStep <- as.integer(60 * 60)
      BOOL <- TRUE
    if (identical(FUN_MOD, RunModel_GR4J) |
        identical(FUN_MOD, RunModel_GR5J) |
        identical(FUN_MOD, RunModel_GR6J)) {
      ObjectClass <- c(ObjectClass, "daily", "GR")
      TimeStep <- as.integer(24 * 60 * 60)
      BOOL <- TRUE
    if (identical(FUN_MOD, RunModel_GR2M)) {
      ObjectClass <- c(ObjectClass, "GR", "monthly")
      TimeStep <- as.integer(c(28, 29, 30, 31) * 24 * 60 * 60)
      BOOL <- TRUE
    if (identical(FUN_MOD, RunModel_GR1A)) {
      ObjectClass <- c(ObjectClass, "GR", "yearly")
      TimeStep <- as.integer(c(365, 366) * 24 * 60 * 60)
      BOOL <- TRUE
    if (identical(FUN_MOD, RunModel_CemaNeige)) {
      ObjectClass <- c(ObjectClass, "daily", "CemaNeige")
      TimeStep <- as.integer(24 * 60 * 60)
      BOOL <- TRUE
    if (identical(FUN_MOD, RunModel_CemaNeigeGR4J) |
        identical(FUN_MOD, RunModel_CemaNeigeGR5J) |
        identical(FUN_MOD, RunModel_CemaNeigeGR6J)) {
      ObjectClass <- c(ObjectClass, "daily", "GR", "CemaNeige")
      TimeStep <- as.integer(24 * 60 * 60)
      BOOL <- TRUE
    if (!BOOL) {
      stop("Incorrect FUN_MOD for use in CreateInputsModel \n")
      return(NULL)
    ##check_arguments
    if ("GR" %in% ObjectClass | "CemaNeige" %in% ObjectClass) {
      if (is.null(DatesR)) {
        stop("DatesR is missing \n")
        return(NULL)
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
if ("POSIXlt" %in% class(DatesR) == FALSE & "POSIXct" %in% class(DatesR) == FALSE) { stop("DatesR must be defined as POSIXlt or POSIXct \n") return(NULL) } if ("POSIXlt" %in% class(DatesR) == FALSE) { DatesR <- as.POSIXlt(DatesR) } if (difftime(tail(DatesR, 1), tail(DatesR, 2), units = "secs")[[1]] %in% TimeStep == FALSE) { TimeStepName <- grep("hourly|daily|monthly|yearly", ObjectClass, value = TRUE) stop(paste0("The time step of the model inputs must be ", TimeStepName, "\n")) return(NULL) } if (any(duplicated(DatesR))) { stop("DatesR must not include duplicated values \n") return(NULL) } LLL <- length(DatesR) } if ("GR" %in% ObjectClass) { if (is.null(Precip)) { stop("Precip is missing \n") return(NULL) } if (is.null(PotEvap)) { stop("PotEvap is missing \n") return(NULL) } if (!is.vector(Precip) | !is.vector(PotEvap)) { stop("Precip and PotEvap must be vectors of numeric values \n") return(NULL) } if (!is.numeric(Precip) | !is.numeric(PotEvap)) { stop("Precip and PotEvap must be vectors of numeric values \n") return(NULL) } if (length(Precip) != LLL | length(PotEvap) != LLL) { stop("Precip, PotEvap and DatesR must have the same length \n") return(NULL) } } if ("CemaNeige" %in% ObjectClass) { if (is.null(Precip)) { stop("Precip is missing \n") return(NULL) } if (is.null(TempMean)) { stop("TempMean is missing \n") return(NULL) } if (!is.vector(Precip) | !is.vector(TempMean)) { stop("Precip and TempMean must be vectors of numeric values \n") return(NULL) } if (!is.numeric(Precip) | !is.numeric(TempMean)) { stop("Precip and TempMean must be vectors of numeric values \n") return(NULL) } if (length(Precip) != LLL | length(TempMean) != LLL) { stop("Precip, TempMean and DatesR must have the same length \n") return(NULL) } if (is.null(TempMin) != is.null(TempMax)) { stop("TempMin and TempMax must be both defined if not null \n") return(NULL) } if (!is.null(TempMin) & !is.null(TempMax)) { if (!is.vector(TempMin) | !is.vector(TempMax)) { stop("TempMin and TempMax must be vectors of numeric values \n") return(NULL) }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
if (!is.numeric(TempMin) | !is.numeric(TempMax)) { stop("TempMin and TempMax must be vectors of numeric values \n") return(NULL) } if (length(TempMin) != LLL | length(TempMax) != LLL) { stop("TempMin, TempMax and DatesR must have the same length \n") return(NULL) } } if (!is.null(HypsoData)) { if (!is.vector(HypsoData)) { stop("HypsoData must be a vector of numeric values if not null \n") return(NULL) } if (!is.numeric(HypsoData)) { stop("HypsoData must be a vector of numeric values if not null \n") return(NULL) } if (length(HypsoData) != 101) { stop("HypsoData must be of length 101 if not null \n") return(NULL) } if (sum(is.na(HypsoData)) != 0 & sum(is.na(HypsoData)) != 101) { stop("HypsoData must not contain any NA if not null \n") return(NULL) } } if (!is.null(ZInputs)) { if (length(ZInputs) != 1) { stop("\t ZInputs must be a single numeric value if not null \n") return(NULL) } if (is.na(ZInputs) | !is.numeric(ZInputs)) { stop("\t ZInputs must be a single numeric value if not null \n") return(NULL) } } if (is.null(HypsoData)) { if (verbose) { warning("\t HypsoData is missing => a single layer is used and no extrapolation is made \n") } HypsoData <- as.numeric(rep(NA, 101)) ZInputs <- as.numeric(NA) NLayers <- as.integer(1) } if (is.null(ZInputs)) { if (verbose & !identical(HypsoData, as.numeric(rep(NA, 101)))) { warning("\t ZInputs is missing => HypsoData[51] is used \n") } ZInputs <- HypsoData[51L] } if (NLayers <= 0) { stop("NLayers must be a positive integer value \n") return(NULL) } if (NLayers != as.integer(NLayers)) { warning("Coerce NLayers to be of integer type (", NLayers, " => ", as.integer(NLayers), ")") NLayers <- as.integer(NLayers) } } ##check_NA_values BOOL_NA <- rep(FALSE, length(DatesR)) if ("GR" %in% ObjectClass) { BOOL_NA_TMP <- (Precip < 0) | is.na(Precip) if (sum(BOOL_NA_TMP) != 0) { BOOL_NA <- BOOL_NA | BOOL_NA_TMP