\name{Apply}
\alias{Apply}

\title{
Wrapper For Applying Atomic Functions To Arrays.
}
\description{
A wrapper for applying a function across one or more arrays. The user can input one or more arrays, and specify the dimensions of each the arrays over which the function should be looped. This is a extension of the apply paradigm to the case where the data being considered is distributed across multiple objects. 
}
\usage{
Apply <- function(data, margins = NULL, AtomicFun, 
                          parallel = FALSE, ncores = NULL ) {
}
\arguments{
  \item{data}{
Either a single object (vector, matrix or array) or a list of objects. 
  }
  \item{margins}{
List of vectors containing the margins for the function to be applied over for each object in the data. Or, if there is a single vector of margins specified and a list of objects in data, then the single set of margins is applied over all objects. 
  }
  \item{AtomicFun} {
Function to be applied to the arrays.
  }
   \item{parallel} {
Logical, should the function be applied in parallel.
  }
   \item{ncores} {
The number of cores to use for parallel computation.
  }
  \item{...} {
Additional arguments to be used in the AtomicFun.
  } 
}
\value{
Array with dimensions equal to margins[[1]], and any additional dimensions resulting from AtomicFun.
}
\examples{
# Load sample data as in Load() example: 
example(Load) 
clim <- Clim(sampleData$mod, sampleData$obs) 
ano_exp <- Ano(sampleData$mod, clim$clim_exp) 
ano_obs <- Ano(sampleData$obs, clim$clim_obs) 
runmean_months <- 12 
dim_to_smooth <- 4  
# Smooth along lead-times
smooth_ano_exp <- Smoothing(ano_exp, runmean_months, dim_to_smooth)
smooth_ano_obs <- Smoothing(ano_obs, runmean_months, dim_to_smooth)

#Calculate the correlation between two arrays
data = list(Mean1Dim(smooth_ano_exp, 2), Mean1Dim(smooth_ano_obs, 2))
margins = list(c(1, 3), c(1, 3))
corr <- Apply(data, margins, AtomicFun = "cor")


}
\references{
Wickham, H (2011), The Split-Apply-Combine Strategy for Data Analysis, Journal of Statistical Software.
}
\author{
0.1  -  2017-03  (A. Hunter and N. Manubens, \email{alasdair.hunter@bsc.es})  
}
\keyword{datagen}
