R/dir2nest.R

Defines functions dir2nest

Documented in dir2nest

#' Dir2Nest
#'
#' @param resights resights
#' @param ring_db ring_db
#' @param repro_fledge_db repro_fledge_db
#' @return distance to nest in Km
#' @import magrittr
#' @export
#'
dir2nest <- function(ring_db = NULL, resights = NULL, repro_fledge_db = NULL) {

  ring_db <- ring_db[,c("Ring", "Brood_ID")] %>%
    unique.data.frame() %>%
    na.omit()

  ## if not unique, remove both cases ;-(
  case <- ring_db$Ring[(duplicated(ring_db$Ring))]
  if (length(case) > 1) ring_db <- dplyr::filter(ring_db, Ring != case)

  ## add nests to resights
  x <- dplyr::left_join(resights, ring_db, by = "Ring") %>%
    dplyr::left_join(., repro_fledge_db[, c("Brood_ID", "N", "E")],
                     by = "Brood_ID")

  out <- geosphere::bearing(p1 = x[,c("E","N")],
                            p2 = x[,c("long", "lat")])

  return(out)

}
mottensmann/DBChecks documentation built on Feb. 3, 2022, 9:21 p.m.