MCA <- function(vary, varx, siglev = 0.98) {
  # Checking vary
  if (!is.numeric(vary) || !is.array(vary)) {
    stop("Parameter 'vary' must be a numeric array.")
  }
  if (length(dim(vary)) != 3) {
    stop("'vary' must have dimensions c(n. start dates/forecast time steps/time samples, n. latitudes, n. longitudes).")
  }

  # Checking varx
  if (!is.numeric(varx) || !is.array(varx)) {
    stop("Parameter 'varx' must be a numeric array.")
  }
  if (length(dim(varx)) != 3) {
    stop("'varx' must have dimensions c(n. start dates/forecast time steps/time samples, n. latitudes, n. longitudes).")
  }

  # Checking siglev
  if (!is.numeric(siglev)) {
    stop("'siglev' must be numeric.")
  }
  if (siglev < 0 || siglev > 1) {
    stop("'siglev' must be in the range [0, 1].")
  }

  dx <- dim(varx)
  #if (length(dx) == 3) {
    nlonx <- dx[1]
    nlatx <- dx[2]
    nsx <- nlonx * nlatx
    nt <- dx[3]
  #} else {
  #  nlonx <- 1
  #  nlatx <- dx[1]
  #  nsx <- nlonx * nlatx
  #  nt <- dx[2]
  #}
  
  X <- array(dim = c(nsx, nt))
  for (i in 1:nlatx) {
    X[(nlonx * (i - 1) + 1):(nlonx * (i - 1) + nlonx), ] <- varx[, 
      i, ]
  }
  not <- is.na(X[, 1])
  yesx <- array(dim = c(nsx))
  counterx <- 0
  for (i in 1:nsx) {
    if (not[i] == "FALSE") {
      counterx <- counterx + 1
      yesx[counterx] <- i
    }
  }
  yesx <- yesx[1:counterx]
  Xyes <- X[yesx, ]  #(nsx_with_data,nt)
  
  dy <- dim(vary)
  #if (length(dy) == 3) {
    nlony <- dy[1]
    nlaty <- dy[2]
    nsy <- nlony * nlaty
    nt <- dy[3]
  #} else {
  #  nlony <- 1
  #  nlaty <- dy[1]
  #  nsy <- nlony * nlaty
  #  nt <- dy[2]
  #}
  
  Y <- array(dim = c(nsy, nt))
  for (i in 1:nlaty) {
    Y[(nlony * (i - 1) + 1):(nlony * (i - 1) + nlony), ] <- vary[, 
      i, ]
  }
  not <- is.na(Y[, 1])
  yesy <- array(dim = c(nsy))
  countery <- 0
  for (i in 1:nsy) {
    if (not[i] == "FALSE") {
      countery <- countery + 1
      yesy[countery] <- i
    }
  }
  yesy <- yesy[1:countery]
  Yyes <- Y[yesy, ]  #(nsy_with_data,nt)
  
  # covariance matrix
  
  covXY <- (Xyes %*% t(Yyes))/(nt - 1)
  
  # MCA analysis
  
  MCA <- svd(covXY)
  
  # outputs
  
  nm <- 10  #number of modes to be retained
  
  SV <- MCA$d  #singular values
  SV2 <- SV * SV
  S <- sum(SV2)
  SCF <- SV2/S  #squared covariance fraction
  SCF <- SCF[1:nm] * 100
  
  Ux <- MCA$u
  Vy <- MCA$v
  Un <- Ux[, 1:nm]
  Vn <- Vy[, 1:nm]
  ECu <- t(Xyes) %*% Un
  ECv <- t(Yyes) %*% Vn  #(nt,nm)
  ECu_std <- array(dim = c(nt, nm))
  ECv_std <- array(dim = c(nt, nm))
  for (i in 1:nm) {
    Su <- sd(ECu[, i])
    ECu_std[, i] <- ECu[, i]/Su
    Sv <- sd(ECv[, i])
    ECv_std[, i] <- ECv[, i]/Sv
  }
  
  RUV <- array(dim = c(1, nm))
  for (i in 1:nm) {
    RUV[i] <- cor(ECu_std[, i], ECv_std[, i])
  }
  
  Uyes <- (Xyes %*% ECu_std)/nt  #(ns_with_data,nm)
  Vyes <- (Yyes %*% ECv_std)/nt
  mcaU <- array(dim = c(nsx, nm))
  mcaU[yesx, ] <- Uyes
  mcaV <- array(dim = c(nsy, nm))
  mcaV[yesy, ] <- Vyes
  MCAU <- array(dim = c(nlonx, nlatx, nm))
  MCAV <- array(dim = c(nlony, nlaty, nm))
  for (i in 1:nlatx) {
    MCAU[, i, ] <- mcaU[(nlonx * (i - 1) + 1):(nlonx * (i - 
      1) + nlonx), ]
  }
  for (i in 1:nlaty) {
    MCAV[, i, ] <- mcaV[(nlony * (i - 1) + 1):(nlony * (i - 
      1) + nlony), ]
  }

  # Array of corr. maps associated with predictor field (c(dim), nm).
  #CORRUyes <- array(dim = c(counterx, nm))
  # Array of corr. maps associated with predictand field (c(dim), nm).
  #CORRVyes <- array(dim = c(countery, nm))
  #for (i in 1:nm) {
  #  for (j in 1:counterx) {
  #    CORRUyes[j, i] <- cor(Xyes[j, ], ECu_std[, i])
  #  }
  #  for (j in 1:countery) {
  #    CORRVyes[j, i] <- cor(Yyes[j, ], ECv_std[, i])
  #  }
  #}
  #corrU <- array(dim = c(nsx, nm))
  #corrU[yesx, ] <- CORRUyes
  #corrV <- array(dim = c(nsy, nm))
  #corrV[yesy, ] <- CORRVyes
  #CORRU <- array(dim = c(nlonx, nlatx, nm))
  #CORRV <- array(dim = c(nlony, nlaty, nm))
  #for (i in 1:nlatx) {
  #  CORRU[, i, ] <- corrU[(nlonx * (i - 1) + 1):(nlonx * 
  #    (i - 1) + nlonx), ]
  #}
  #for (i in 1:nlaty) {
  #  CORRV[, i, ] <- corrV[(nlony * (i - 1) + 1):(nlony * 
  #    (i - 1) + nlony), ]
  #}
  
  # t-Test
  
  cl <- cl
  cl2 <- (1 - cl)/2
  t <- qt(cl + cl2, nt - 2)
  R <- sqrt((t * t)/((t * t) + nt - 2))
  
  # if(ofiles) { save(SCF, ascii=TRUE,
  # file='$plotsdir/SCFs_of_data.dat') save(RUV, ascii=TRUE,
  # file='$plotsdir/RUVs_of_data.dat') save(ECu_std,
  # ascii=TRUE, file='$plotsdir/ECsU_of_data.dat')
  # save(ECv_std, ascii=TRUE,
  # file='$plotsdir/ECsV_of_data.dat') }
  
  invisible(list(SCFs = SCF, RUVs = RUV, ECs_U = ECu_std, MCAs_U = MCAU, 
                 ECs_V = ECv_std, MCAs_V = MCAV, Rtest = R)) 
  #               CORRs_U = CORRU, CORRs_V = CORRV))
}
