R/beetle_targets.R

Defines functions beetle_targets

Documented in beetle_targets

##' @title Generate targets from NEON data used by the beetle challenge
##' @return None
##' @param targets_dir, directory where target csv is located
##' @export
##'
##' @author Carl Boettiger
##' @author Anna Spiers
##'
##'

beetle_targets <- function(targets_dir){

  ## Load data from raw files
  sorting <- neonstore::neon_read("bet_sorting", altrep = FALSE)
  para <- neonstore::neon_read("bet_parataxonomistID", altrep = FALSE)
  expert <- neonstore::neon_read("bet_expertTaxonomistIDProcessed", altrep = FALSE)
  field <- neonstore::neon_read("bet_fielddata", altrep = FALSE)

  #### Generate derived richness table  ####################

  beetles <- NEONeco4cast::resolve_taxonomy(sorting, para, expert) %>%
    dplyr::mutate(month = lubridate::month(collectDate, label=TRUE),
           year =  lubridate::year(collectDate))

  richness <- beetles %>%
    dplyr::select(taxonID, siteID, collectDate, month, year) %>%
    dplyr::distinct() %>%
    dplyr::count(siteID, collectDate, month, year)

  readr::write_csv(richness, paste0(targets_dir,"/beetle-richness.csv"))

  #### Generate derived abundance table ####################

  effort <- field %>%
    dplyr::group_by(siteID, collectDate) %>%
    dplyr::summarize(trapnights = as.integer(sum(collectDate - setDate)))

  counts <- sorting %>%
    dplyr::mutate(month = lubridate::month(collectDate, label=TRUE),
           year =  lubridate::year(collectDate)) %>%
    dplyr::group_by(collectDate, siteID, year, month) %>%
    dplyr::summarize(count = sum(individualCount, na.rm = TRUE))

  abund <- counts %>%
    dplyr::left_join(effort) %>%
    dplyr::mutate(abund = count / trapnights) %>% ungroup()

  readr::write_csv(abund, paste0(targets_dir,"/beetle-abund.csv"))

}
eco4cast/NEONeco4cast documentation built on Aug. 30, 2020, 12:03 a.m.