UltimateBrier <- function(exp_ano, obs_ano, posmemb = 2, posdates = 3, quantile = TRUE, thr = c(5/100, 95/100), type = 'BS', decomposition = TRUE) {
  library(SpecsVerification)
                 
  if (!(type %in% c('BS', 'FairEnsembleBS', 'FairStartDatesBS', 'BSS', 'FairEnsembleBSS', 'FairStartDatesBSS'))) {
    stop("Error: 'type' not supported")
  }

  if (dim(obs_ano)[posmemb] != 1) {
    stop("Error: only observations with one member are supported. dim(obs_ano)[posmemb] shoul be 1.")
  }

  if (type == 'FairEnsembleBSS') {
    bss <- array(dim = c(dim(exp_ano)[1], dim(obs_ano)[1], length(thr) + 1, dim(exp_ano)[-c(1, posmemb, posdates)]))
    # Does it make sense to take into account posmemb != 2 and posdates != 3? Always in same position from Ano(). Besides, parameterizing this and not posexp and posltimes is nonsense.
    size_ens_ref <- prod(dim(obs_ano[c(posmemb, posdates)]))
    for (n_obs in 1:dim(obs_ano)[1]) {
      for (n_ltime in 1:dim(exp_ano)[4]) {
        for (n_lat in 1:dim(exp_ano)[5]) {
          for (n_lon in 1:dim(exp_ano)[6]) {
            # Calculate reference climatologies of each lead-time, longitude and latitude before calculating BSS.
            ens_ref <- matrix(obs_ano[n_obs, , , n_ltime, n_lat, n_lon], size_ens_ref, size_ens_ref, byrow = TRUE)
            for (n_exp in 1:dim(exp_ano)[1]) {
              n_tau <- 1
              for (tau in c(thr, 1)) {
                bss[n_exp, n_obs, n_tau, n_ltime, n_lat, n_lon] <- FairBrierSs(t(exp_ano[n_exp, , , n_ltime, n_lat, n_lon]), ens_ref, obs_ano[n_obs, 1, , n_ltime, n_lat, n_lon], tau)$bss
                n_tau <- n_tau + 1
              }
            }
          }
        }
      }
    }
    bss
  } else {
    if (type == 'FairEnsembleBS') {
      #       ens<-exp_ano
      #       obs<-obs_ano
      #       tau<-thr
      FairBrier
    } else {
      # Calculate probabilities of data with ProbBins and average over members
      exp_probs <- Mean1Dim(ProbBins(exp_ano, 1:dim(exp_ano)[posdates], thr, quantile, posdates, posmemb), 3)
      obs_probs <- ProbBins(obs_ano, 1:dim(obs_ano)[posdates], thr, quantile, posdates, posmemb)

      if (type == 'BS') {
        rel <- array(dim = c(dim(exp_ano)[1], dim(obs_ano)[1], length(thr) + 1, dim(exp_ano)[-c(1, posmemb, posdates)]))
        res <- array(dim = c(dim(exp_ano)[1], dim(obs_ano)[1], length(thr) + 1, dim(exp_ano)[-c(1, posmemb, posdates)]))
        unc <- array(dim = c(dim(exp_ano)[1], dim(obs_ano)[1], length(thr) + 1, dim(exp_ano)[-c(1, posmemb, posdates)]))
        for (n_obs in 1:dim(obs_ano)[1]) {
          for (n_ltime in 1:dim(exp_ano)[4]) {
            for (n_lat in 1:dim(exp_ano)[5]) {
              for (n_lon in 1:dim(exp_ano)[6]) {
                for (n_exp in 1:dim(exp_ano)[1]) {
                  n_tau <- 1
                  for (tau in c(thr, 1)) {
                    bsd <- BrierScoreDecomposition(exp_probs[n_tau, , n_exp, n_ltime, n_lat, n_lon], obs_probs[n_tau, , 1, n_ltime, n_lat, n_lon])
                    rel[n_exp, n_obs, n_tau, n_ltime, n_lat, n_lon] <- bsd$REL
                    res[n_exp, n_obs, n_tau, n_ltime, n_lat, n_lon] <- bsd$RES
                    unc[n_exp, n_obs, n_tau, n_ltime, n_lat, n_lon] <- bsd$UNC
                    n_tau <- n_tau + 1
                  }
                }
              }
            }
          }
        }
        bs <- rel - res + unc
      } else if (type == 'FairStartDatesBS') {
#       # This will be introduced for each thr+1 event
#       obs<-obs_probs 
#       pred<-exp_probs 
        CFU_BS
      } else if (type == 'BSS') {
        BrierScore( ... CFU_BS? Ask Omar
      # I don't know where this function is

      } else if (type == 'FairStartDatesBSS') {
#       # This will be introduced for each thr+1 event
#       obs<-obs_probs 
#       pred<-exp_probs 
        CFU_BS[S]
      }
    }

    if (type %in% c('BSS', 'FairStartDatesBSS')) {
      bss
    } else if (decomposition && type != 'FairEnsembleBS') {
      list(BS = bs, REL = rel, UNC = unc, RES = res)
    } else {
      bs
    }
  }
}
