#' Age at size
#'
#' Calculate expected age at a given size
#'
#' @param A (numeric) Asymptotic body size (can be calculated in \code{calc.growth()})
#' @param k (numeric) Growth rate (can be calculated in \code{calc.growth()})
#' @param size (numeric vector) The size(s) for which you want to know the age (should be in the same units used to calculate A and k)
#' @param size0 (numeric) Initial body size at birth/hatching (should be in the same units used to calculate A and k). This will generally be a species average, rather than being specific per individual, but it can be a vector of specific birth/hatching sizes per individual if known.
#' @param model (character) The model used to calculate A and k. One of "vb", "gompertz","logistic".
#' @return Vector of expected age(s)
#'
#' @details
#' It takes a vector of sizes, initial size for the species, and A and k from \code{calc.growth()} and returns the expected age at a given size based on the specified model.
#'
#' \strong{Note}: Age cannot be calculated for sizes >A, resulting in NaN.
#'
#' @examples
#' #For this example, assume size is in mm and time is in years
#' #Use the gompertz formula to calculate the expected age of a 350 mm
#' #turtle for a species that hatches at an average size of 50 mm
#' age.at.size(A = 396.6738,
#'  k = 0.2029850,
#'  size = 350,
#'  size0 = 50,
#'  model="gompertz")
#'
#' @export
age.at.size <- function(A, k, size, size0,model){
  if(model=="gompertz"){age <- -(1/k) * log( log(size/A) / log(size0/A))}else{
    if(model=="logistic"){age <- -(1/k) * log( (size0 * (A - size)) / (size * (A - size0)) )}else{
      age <- -(1/k) * log( (A - size) / (A - size0) )
    }
  }
  age}



