\name{Apply}
\alias{Apply}

\title{
Function For Applying Atomic Functions To Arrays
}
\description{
Function for applying atomic functions (e.g. cor(x,y)) to two arrays.
}
\usage{
Apply <- function(data, margins = NULL, AtomicFun, 
                          parallel = FALSE, ncores = NULL ) {
}
\arguments{
  \item{data}{
List of input arrays. 
  }
  \item{margins}{
List of vectors containing the margins for the function to be applied over for each object in data. 
  }
  \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]].
}
\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, \email{alasdair.hunter@bsc.es})  
}
\keyword{datagen}
