R/sync_na.R

Defines functions sync_na

Documented in sync_na

#' Synchronise NA cells across layers in a RasterStack.
#'
#' If any layer within a rasterstack contains a missing value (NA), all layers will be set to NA for that cell across the entire rasterstack.
#' This function is useful when combining data products from different sources which may have slight mismatches in available data along coastlines, for example.
#'
#' @param stack RasterStack object to be processed
#' @return Returns a rasterstack object.
#' @export
#' @examples Coming soon.

sync_na <- function(stack)

{
  if(canProcessInMemory(stack, n = 2))
  {
    val <- getValues(stack)
    NA.pos <- unique(which(is.na(val), arr.ind = T)[, 1])
    val[NA.pos, ] <- NA
    x <- setValues(stack, val)
    return(x)
  } else
  {
    x <- mask(stack, calc(stack, fun = sum))
    return(x)
  }
}
simon-tarr/island documentation built on May 6, 2019, 8:05 p.m.