#' Compute the centered version of spatial functional data
#'
#' This function is used to center spatial functional data.
#' @param X The functional spatial data.
#' @param margins The margins for which the centering should be done separately
#'   in every entry, in order to remove a trend.
#' @param na.rm Whether or not missing values should be ignored.
#' @return The centered data.
#' @seealso \code{\link{mean.fsd.fd}}
#' @keywords fsd
#' @export
#' @examples
#' data("temp")
#'
#' T.centered = center.fsd.fd(temp)
#' plot(mean(T.centered, margins = 1:2))
#'
#' T.centered2 = center.fsd.fd(temp, margins = 1:2)
#' plot(mean(T.centered2, margins = 1:2))
#' plot(mean(temp, margins = 1:2))

center.fsd.fd = function (X, margins = NULL, na.rm = FALSE)
{
  if (!inherits(X, "fd"))
    stop("X needs to be functional data")
  if (is.null(margins))
    return(fsd.fd(X$coefs - as.vector(mean.fsd.fd(X, na.rm = na.rm)$coefs),
                  basisobj = X$basis, fdnames = X$fdnames))
  else {
    if (any(margins > length(dim(X$coefs))-1) || any(margins < 1))
      stop("Please provide valid margins")

    r = length(dim(X$coefs)) - 1
    permut = c(1, margins +1, (1:r)[-margins] +1)
    Y = aperm(X$coefs, permut)
    Y.mean = apply(Y, seq(1+length(margins)), mean, na.rm = na.rm)
    Y = aperm(Y-as.vector(Y.mean), order(permut))

    return(fsd.fd(coef = Y,
                  basisobj = X$basis, fdnames = X$fdnames))
  }
}
