TPDRichness(TPDc=NULL, TPDs=NULL)

Compute functional richness (FRIC) from community or species TPD objects.

functional diversity
Args:TPDc — community TPDTPDs — species TPD
TPDRichness <- function(TPDc = NULL, TPDs = NULL) {
  if (is.null(TPDc) & is.null(TPDs)) {
    stop("At least one of 'TPDc' or 'TPDs' must be supplied")
  }
  if (!is.null(TPDc) & class(TPDc) != "TPDcomm") {
    stop("Class mismatch: please specify if your object is a TPDc or a TPDs (TPDcomm expected).")
  }
  if (!is.null(TPDs) & class(TPDs) != "TPDsp") {
    stop("Class mismatch: please specify if your object is a TPDc or a TPDs (TPDsp expected).")
  }

  results <- list()

  Calc_FRich <- function(x) {
    results_FR <- numeric()
    if (class(x) == "TPDcomm") {
      TPD <- x$TPDc$TPDc
      names_aux <- names(x$TPDc$TPDc)
      cell_volume <- x$data$cell_volume
    }
    if (class(x) == "TPDsp") {
      TPD <- x$TPDs
      names_aux <- names(x$TPDs)
      cell_volume <- x$data$cell_volume
    }
    for (i in 1:length(TPD)) {
      TPD_aux <- TPD[[i]]
      TPD_aux[TPD_aux > 0] <- cell_volume
      results_FR[i] <- sum(TPD_aux)
    }
    names(results_FR) <- names_aux
    return(results_FR)
  }

  if (!is.null(TPDc)) {
    results$communities <- list()
    results$communities$FRichness <- Calc_FRich(TPDc)
  }
  if (!is.null(TPDs)) {
    if (TPDs$data$type == "One population_One species" |
        TPDs$data$type == "One population_Multiple species") {
      results$species <- list()
      results$species$FRichness <- Calc_FRich(TPDs)
    } else {
      results$populations <- list()
      results$populations$FRichness <- Calc_FRich(TPDs)
    }
    if (TPDs$data$method == "mean") {
      message("WARNING: When TPDs are calculated using the TPDsMean function, Evenness and Divergence are meaningless!!")
    }
  }
  return(results)
}