threshold <- function(data, thrs, var.list, return.values = TRUE){   
    if (!is.null(data$obs)){
        result_obs <- c()
        dim_var <- which(names(dim(data$obs$data)) == 'var')
        for(var in var.list){
            data_tmp <- Subset(data$obs$data, along = 'var', indices = which(var.list == var), drop = FALSE)
            data_threshold <- data_tmp
            data_threshold[which(data_tmp >= thrs[[which(var.list == var)]][[1]] & data_tmp <= thrs[[which(var.list == var)]][[2]])] <- TRUE
            data_threshold[which(data_tmp < thrs[[which(var.list == var)]][[1]] | data_tmp > thrs[[which(var.list == var)]][[2]])] <- FALSE
            data_tmp[which(!data_threshold)] <- NA
            if (return.values){
                result_obs <- abind(result_obs, data_tmp, along = dim_var)
            } else {
                result_obs <- abind(result_obs, data_threshold, along = dim_var)
            }
        }
        names(dim(result_obs)) <- names(dim(data$obs$data))
        data$obs$data <- result_obs
    } 

    if (!is.null(data$hcst)){
        result_hcst <- c()
        dim_var <- which(names(dim(data$hcst$data)) == 'var')
        for(var in var.list){
            data_tmp <- Subset(data$hcst$data, along = 'var', indices = which(var.list == var), drop = FALSE)
            data_threshold <- data_tmp
            data_threshold[which(data_tmp >= thrs[[which(var.list == var)]][[1]] & data_tmp <= thrs[[which(var.list == var)]][[2]])] <- TRUE
            data_threshold[which(data_tmp < thrs[[which(var.list == var)]][[1]] | data_tmp > thrs[[which(var.list == var)]][[2]])] <- FALSE
            data_tmp[which(!data_threshold)] <- NA
            if (return.values){
                result_hcst <- abind(result_hcst, data_tmp, along = dim_var)
            } else {
                result_hcst <- abind(result_hcst, data_threshold, along = dim_var)
            }
        }
        names(dim(result_hcst)) <- names(dim(data$hcst$data))
        data$hcst$data <- result_hcst
    } 

    if (!is.null(data$fcst)){
        result_fcst <- c()
        dim_var <- which(names(dim(data$fcst$data)) == 'var')
        for(var in var.list){
            data_tmp <- Subset(data$fcst$data, along = 'var', indices = which(var.list == var), drop = FALSE)
            data_threshold <- data_tmp
            data_threshold[which(data_tmp >= thrs[[which(var.list == var)]][[1]] & data_tmp <= thrs[[which(var.list == var)]][[2]])] <- TRUE
            data_threshold[which(data_tmp < thrs[[which(var.list == var)]][[1]] | data_tmp > thrs[[which(var.list == var)]][[2]])] <- FALSE
            data_tmp[which(!data_threshold)] <- NA
            if (return.values){
                result_fcst <- abind(result_fcst, data_tmp, along = dim_var)
            } else {
                result_fcst <- abind(result_fcst, data_threshold, along = dim_var)
            }
        }
        names(dim(result_fcst)) <- names(dim(data$fcst$data))
        data$fcst$data <- result_fcst
    } 

    #result <- list(obs = result_obs, hcst = result_hcst, fcst = result_fcst)
    return(data)
}