RMSSS <- function(var_exp, var_obs, posloop = 1, posRMS = 2, pval = TRUE,
                  parallel = FALSE, ncores = NULL) {
  #
  #  Enlarge var_exp & var_obs & clim to 10 dim + move posloop & posRMS  
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  dimsvar <- dim(var_exp)
  dim_names <- names(dim(var_exp))
  for (iind in 1:length(dimsvar)) {
    if (iind !=posloop & dim(var_obs)[iind] != dimsvar[iind]) { 
      stop("var_exp & var_obs must have same dimensions except along posloop")
    }
  }
  if (dimsvar[posRMS] < 2 ) {
    stop("At least 2 values required to compute RMSE")
  }

  if (is.null(dim(var_exp))) {
    var_exp <- Insert1Dim(var_exp, 1, 1)
    var_obs <- Insert1Dim(var_obs, 1, 1)
  }
  
  #
  #  RMSSS & its p-value computation
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  var_exp <- aperm(var_exp, c(posloop, posRMS, c(1:length(dim(var_exp)))[-c(posloop, posRMS)]))
  var_obs <- aperm(var_obs, c(posloop, posRMS, c(1 :length(dim(var_obs)))[-c(posloop, posRMS)]))
  margins <- list(c(1 : length(dim(var_exp)))[-2], c(1 : length(dim(var_obs)))[-2])   
  RMSSS <- Apply(list(var_exp, var_obs), margins = margins, AtomicFun = ".RMSSS", 
               pval = pval, parallel = parallel, ncores = ncores)
  #  
  
  #
  #  Output
  # ~~~~~~~~
  #
  names(dim(RMSSS))[2 : length(dim(RMSSS))] <- dim_names[-posRMS]
  RMSSS
}

.RMSSS <- function(exp, obs, pval = pval) {
  dif2 <- obs
  p_val <- c()
  if (is.matrix(exp)) {
    dif1 <- rowMeans(exp) - obs
  } else {
    dif1 <- exp - obs
  }
  rms1 <- mean(dif1 ** 2, na.rm = TRUE) ** 0.5
  rms2 <- mean(dif2 ** 2, na.rm = TRUE) ** 0.5
  rms2[abs(rms2) <= abs(rms2) / 1000] <- abs(rms2) / 1000
  rmsss <- c(1 - (rms1 / rms2))
  if (pval == TRUE) {
    eno1 <- Eno(dif1, 1)
    eno2 <- Eno(dif2, 1)
    F.stat <- (eno2 * (rms2) ** 2 / (eno2 - 1)) / (eno1 * (rms1) ** 2 / (eno1 - 1))
    if (is.na(eno1) == FALSE && is.na(eno2) == FALSE && eno1 > 2 && eno2 > 2) {
      p_val <- 1 - pf(F.stat, eno1 - 1, eno2 - 1)
    }
  }

  #
  #  Output
  # ~~~~~~~~
  #
  list(rmsss = rmsss, p_val = p_val)
}
