R/calc_first.R

Defines functions calc_first

Documented in calc_first

#' Calculate first TRUE
#'
#' @param ... SpatRasters/SpatRaster list - logical - with time attribute
#' @returns SpatRaster - logical - with time attribute
#' @family Calculation
#' @keywords internal
#' @description Takes multiple logical SpatRasters with time attribute and
#' returns SpatRaster with common time and TRUE if any SpatRaster is TRUE.

calc_first <- function(...) {

  li <- list(...)

  if (methods::is(li[[1]], "list")) {

    if (length(li) == 1) li <- li[[1]] else stop("Cant calc_first of multiple lists!")

  }

  if (!methods::is(li[[1]], "SpatRaster")) stop("Input needs to be SpatRaster!")

  # take shared common time
  time <- lapply(li, terra::time)

  common <- Reduce(
    function(x, y) x[x %in% y],
    time
  )

  # subset all SpatRaster to common time
  sr <- lapply(li,
               function(x) terra::subset(x, which(terra::time(x) %in% common))
  )

  # reduce to one SpatRaster
  first <- Reduce(
    function(x, y) terra::ifel(x > y, x, y),
    sr
  )

  return(first)

}

Try the PHENTHAUproc package in your browser

Any scripts or data that you put into this service are public.

PHENTHAUproc documentation built on June 22, 2024, 7:12 p.m.