#'Anomalies relative to a climatology along sdates with or without cross-validation
#'
#'@description This function computes the anomalies relative to a climatology computed along the 
#'starting dates dimension allowing the application or not of crossvalidated climatologies. The 
#'computation is carried out independently for experimental and observational data products.
#'
#'@param var_exp A multidimensional array related to experimental simulations with the specific 
#'dimensions required by Clim(): i.e. \code{dim(var_exp)} goes from c(nmod/nexp, nmemb/nparam, nsdates, nltime) 
#'up to c(nmod/nexp, nmemb/nparam, nsdates, nltime, nlevel, nlat, nlon)
#'
#'@param var_obs A multidimensional array related to observations with the specific dimensions required 
#'by Clim(): i.e. \code{dim(var_exp)} goes from c(nmod/nexp, nmemb/nparam, nsdates, nltime) up to 
#' c(nmod/nexp, nmemb/nparam, nsdates, nltime, nlevel, nlat, nlon)
#'
#'@param cross A logical value indicating whether cross-validation should be applied or not. Default = FALSE.
#'
#'@param memb A logical value indicating whether Clim() computes one climatology for each experimental data 
#'product member(TRUE) or it computes one sole climatology for all members (FALSE). Default = TRUE.
#'
#'
#'@return A list of length 2:
#'\itemize{
#'\item\code{$var_exp}{ Array of anomalies for \code{var_exp} with same dimensions input.}
#'\item\code{$var_obs}{ Array of anomalies for \code{var_obs} with same dimensions input.}}
#'
#'
#'@examples
#'
#'# Example 1:
#'Two arrays to compute anomalies: \code{var_exp} and \code{var_obs}
#'
#'dim_exp <- c(mod = 1, member = 3, sdates = 2, ftime = 100)
#'dim_obs <- c(mod = 1, member = 1, sdates = 2, ftime = 100)
#'
#'var_exp <- array(rnorm(prod(dim_exp)), dim = dim_exp)
#'var_obs <- array(rnorm(prod(dim_obs)), dim = dim_obs)
#'
#'Computation of anomalies without cross-validation
#'anoM <- AnoMultiOptions(var_exp = var_exp, var_obs = var_obs, 
#'                        cross = FALSE, memb = TRUE)
#'ano_exp <- anoM$var_exp
#'ano_obs <- anoM$var_obs
#'
#'
#'# Example 2:
#'One array to compute anomalie: \code{var_exp}
#'
#'dim_exp <- c(mod = 1, member = 3, sdates = 2, ftime = 100, nlon = 100, nlat = 50)
#'
#'var_exp <- array(rnorm(prod(dim_exp)), dim = dim_exp)
#'
#'Computation of anomalies with cross-validation using kharin and NDV methods
#'anoM <- AnoMultiOptions(var_exp , 
#'                        cross = TRUE, memb = TRUE)
#'ano_exp <- anoM$var_exp
#'
#'@seealso 
#'\code{\link{Ano_CrossValid, }} 
#'\code{\link{Clim.}}
#'
#'
#'@export

AnoMultiOptions <- function(var_exp = NULL, var_obs = NULL, cross = FALSE, memb = TRUE) {
  
  dimexp <- dim(var_exp)
  dimobs <- dim(var_obs)
  
  if (is.null(var_exp) & is.null(var_obs)) {
    stop("Not-NULL array var_exp or var_obs or both should be provided.")
  }
  if (is.null(var_exp)) {
    var_exp <- var_obs
    dimexp <- dim(var_exp)
    warning('No experimental data (var_exp) provided.')
  } 
  if (is.null(var_obs)) {
    var_obs <- var_exp
    dimobs <- dim(var_obs)
    warning('No observational data provided (var_obs).')
  }
  if (length(dimexp) < 4 | length(dimobs) < 4) {
    stop("Invalid input dimensions: At least 4 dimensions needed for var_exp or var_obs, 
         c(nexp/nobs, nmemb, nsdates, nltime).")
  }
  if (length(dimexp) != length(dimobs)) {
    stop('Invalid input dimensions: var_exp and var_obs should have same number of dimensions.')
  }
  for (jn in 3:max(length(dimexp), length(dimobs))) {
    if (dimexp[jn] != dimobs[jn]) {
      stop("Invalid input dimensions: var_exp and var_obs dimensions should have same length
           for nsdates and nltime (and for nlevel,nlon and nlat if present).")
    }
  }
  if (dimexp[3] == 1 | dimobs[3] == 1) {
    stop("Invalid input dimensions: sdate dimension length should be greater than 1")
  }
  if (!is.logical(cross) | !is.logical(memb) ) {
    stop("Invalid flags used: Flags 'cross' and 'memb' should be all logical.")
  }
  if (length(cross) > 1 | length(memb) > 1 ) {
    warning("Invalid flags used: Flags 'cross' and 'memb' should all have length 1.")
  }
  
  
  # Computating anomalies
  #---------------------
  
  # With cross-validation
  if (cross) {
    ano <- Ano_CrossValid(var_exp, var_obs, memb = memb)
   
    
  #  Without cross-validation 
  } else {
    tmp <- Clim(var_exp , var_obs , memb )
    
    if (memb) { 
      clim_exp <- tmp$clim_exp
      clim_obs <- tmp$clim_obs
    } else {
      clim_exp <- InsertDim(tmp$clim_exp, 2, dimexp[2]) 
      clim_obs <- InsertDim(tmp$clim_obs, 2, dimobs[2]) 
    }
    
    clim_exp <- InsertDim(clim_exp, 3, dimexp[3]) 
    clim_obs <- InsertDim(clim_obs, 3, dimobs[3]) 
    
    ano_exp <- var_exp - clim_exp
    ano_obs <- var_obs - clim_obs 

    ano <- list(ano_exp = ano_exp, ano_obs = ano_obs)
  }

  #  Outputs
  # ~~~~~~~~~
  invisible(ano)
}

  