R/MESSMask.R

#' @title Process module: MESSMask
#'   
#' @description Calculate the multivariate environmental similarity surface 
#'   and mask areas with values outside the range of variability in the 
#'   training region.
#'   
#' @param .data \strong{Internal parameter, do not use in the workflow
#'   function}. \code{.data} is a list of a data frame and a raster object
#'   returned from occurrence modules and covariate modules respectively.
#'   \code{.data} is passed automatically in workflow from the occurrence and
#'   covariate modules to the process module(s) and should not be passed by the
#'   user.
#'   
#' @seealso \code{\link{dismo::mess}}
#' 
#' @author ZOON Developers, \email{zoonproject@@gmail.com}
#' @section Version: 1.0
#' @section Date submitted: 2016-03-23 
#' @section Data type: presence-only, presence/absence, abundance, proportion
#'   
#' @name MESSMask
#' @family process

MESSMask <- function (.data) {
  
  zoon:::GetPackage('dismo')
 
  # Dont allow attributes to be dropped
  org_attrs <- attributes(.data$ras)
  
  # Ignore warnings because they're just a bunch of NA issues
  suppressWarnings(
    mess <- dismo::mess(.data$ras, v = .data$df[, attr(.data$df, 'covCols')], full = FALSE)
  )

  # Extreme values become NA
  values(mess)[values(mess) < 0] <- NA

  .data$ras <- mask(.data$ras, mess)

  # replace any attributes lost during processing
  end_attrs <- attributes(.data$ras)
  which_missing <- which(!names(org_attrs) %in% names(end_attrs))
  attributes(.data$ras) <- c(end_attrs, org_attrs[which_missing])  

  return(.data)
  
}
zoonproject/modules documentation built on May 4, 2019, 11:25 p.m.