## to avoid github API hit rate errors, just make sure these are updated outside sbatch calls....
#remotes::install_github("trashbirdecology/bbsAssistant",
#                        force = FALSE,
#                        upgrade = "never") ## "never" for dev purposes... to avoid interactivity
remotes::install_github("trashbirdecology/bbsebird",
                       force = FALSE,
                       upgrade = "never") ## "never" for dev purposes... to avoid interactivity

suppressPackageStartupMessages(library(bbsebird, quietly = TRUE, warn.conflicts = FALSE))
crs.target          = 4326 #target CRS for all created spatial layers
grid.size           = 0.50 # size in decimal degrees (for US/CAN a good est is 1.00dec deg == 111.11km)
#species             = c("house sparrow", "houspa")
species             = c("wood thrush", "woothr")
mmyyyy              = "dec-2021" # the month and year of the eBird data downloads on file
years               = 2008:2019
countries           = c("US") ## string of  countries Call /code{bbsebird::iso.codes} to find relevant
states              = sort(c("us-FL"))
# states              = sort(c("us-ga", "us-fl", "us-al", "us-sc"))
ydays               = 91:245
max.birds.checklist = 55 ## maximum number of birds within a single eBird checklist (removes any over
max.checklists      = 10 # max number checklists per grid cell and year
# set_proj_shorthand: this will make all directories within a new dir in dir.proj. this is useful for iterating over species/time/space and saving all resulting information in those directories.
if (!is.na(pmatch("C:/Users/aroyle", getwd()))){
  setwd("C:/users/aroyle/OneDrive - DOI/AAAA-IntegratedModels")}else{
    if (!is.na(pmatch("C:/Users/jburnett", getwd()))){ # use na because if FALSE does not return as logical..
      if(Sys.info()[1] == "Linux") {dir.proj <- "/home/jburnett/integratedmodels/"}
      if(Sys.info()[4] == "IGSACEESLTAROYB") #JLB's SAS comp
        dir.proj <- "C:/Users/jburnett/DOI/Royle, Andy - AAAA-IntegratedModels/"
      if(Sys.info()[4] == "IGSACEESWSWLIN8") # Bill Link's Comp
        dir.proj <- "C:/Users/jburnett/DOI/Royle, Andy - AAAA-IntegratedModels/"
    }}
dir.orig.data   <-  paste0(dir.proj,"dataorig")  # ebird data + bbs routesdir.orig.data <- paste0(getwd(), "/dataorig")

## subdir.proj is optional but recommended when creating data/models for different species, regions, and/or grid cell sizes...
subdir.proj <-
  set_proj_shorthand(
    species = species,
    countries = countries,
    states = states,
    grid.size = grid.size,
    years = years
  )
## if ebird and/or bbs route files don't exist in dir.orig.data this will throw a warning.
dirs        <-  dir_spec(dir.orig.data = dir.orig.data,
                         dir.proj = dir.proj,
                         subdir.proj = subdir.proj)
grid <-
  make_spatial_grid(
    dir.out = dirs$spatial,
    states = states,
    countries = countries,
    grid.size = grid.size,
    overwrite = FALSE
  )
overlay <- grid$grid.overlay
grid    <- grid$grid
## if the files already exist, don't overwrite unless you've made changes to data specs
if("bbs.rds" %in% list.files(dirs$bbs.out)){
  cat("importing munged bbs data...\n")
  bbs  <- readRDS(list.files(dirs$bbs.out, "bbs.rds", full.names=TRUE))
}else{
  while(!exists("bbs_orig")) bbs_orig <- bbsAssistant::grab_bbs_data(bbs_dir = dirs$bbs.out)
  bbs  <- bbsAssistant::munge_bbs_data(
    bbs_list = bbs_orig,
    states   = states,
    species = species,
    year.range = years) |> as.data.table()
  bbs <- munge_col_names(bbs) # munge column names to mesh with eBird
  saveRDS(bbs, paste0(dirs$bbs.out, "/bbs.rds")) # suggest saving data to file for easy access
}
# Overlay BBS and study area / sampling grid
#fn <- paste0(dirs$bbs.out, "/bbs_spatial.rds")
#if(file.exists(fn)) bbs <- readRDS(fn) else {
bbs <- make_bbs_spatial(
  df = bbs,
  cws.routes.dir = dirs$cws.routes.dir,
  usgs.routes.dir = dirs$usgs.routes.dir,
  crs.target = crs.target,
  grid = grid,
  dir.out = dirs$bbs.out,
  overwrite = FALSE
)
#}
ebird <-
  make_ebird(
    dir.ebird.in = dirs$ebird.in,
    dir.out = dirs$ebird.out,
    mmyyyy = mmyyyy,
    countries = countries,
    species = species,
    max.birds.checklist = max.birds.checklist,
    states = states,
    years = years,
    overwrite = FALSE
  )

ebird <-
  make_ebird_spatial(
    df = ebird,
    dir.out = dirs$ebird.out,
    grid = grid,
    overwrite = FALSE
  )
## visualizing the ebird_spatial data takes a while, do not recommend!
dat.full  <- make_bundle(bbs, ebird, grid, max.ebird = max.birds.checklist)
### make_bunlde provides site-level covariates as both vectors and matrices
### e.g., dat.full$bbs.df$obsfirstyearbbs == dat.full$Xb$obsfirstyearbbs
gam.dat.full  <-
  make_gam(
    coords = dat.full$coords,
    method = "cubic2d")

dat.full <- c(dat.full, gam.dat.full)
## Add Constants/Data Common to Models
model.dat.full      <- make_model_data(data = dat.full)
dir.create(paste0(dirs$project, "/datain"), showWarnings = FALSE)
saveRDS(dat.full, file=paste0(dirs$project, "/datain/bundle.rds"))
saveRDS(model.dat.full, file=paste0(dirs$project, "/datain/model-dat.rds"))
dat.dev   <- make_bundle(bbs, ebird, grid, dev.mode = TRUE, max.ebird = max.birds.checklist) # full data
gam.dat.dev  <-
  make_gam(
    coords = dat.dev$coords,
    method = "cubic2d",
    nd = 4,
    # num.nn = 10,
    nruns = 10,
    print.plot = TRUE,
    plot.main = "utm scaled dev mode"
  )
# stopifnot(dat.dev$G < dat.full$G)
dat.dev  <- c(dat.dev, gam.dat.dev)
model.dat.dev       <- make_model_data(data = dat.dev)
saveRDS(dat.dev,  file=paste0(dirs$project, "/datain/bundle-dev.rds"))
saveRDS(model.dat.dev, file=paste0(dirs$project, "/datain/model-dat-dev.rds"))
dat.dev  <- c(dat.dev, gam.dat.dev)
model.dat.dev       <- make_model_data(data = dat.dev)
saveRDS(dat.dev,  file=paste0(dirs$project, "/datain/bundle-dev.rds"))
saveRDS(model.dat.dev, file=paste0(dirs$project, "/datain/model-dat-dev.rds"))


trashbirdecology/bbsebird documentation built on June 30, 2022, 12:18 a.m.