```{css, echo=FALSE, eval=FALSE} body, .main-container {max-width: 100%; margin-left: auto; margin-right: auto;}

```r
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
options(rmarkdown.html_vignette.check_title = FALSE)
library(tidyverse)
library(wastdr)
library(sparkline)
library(ggplot2)
library(gt)
library(reactable)

# -----------------------------------------------------------------------------#
# Helpers
# 
# Rendered tables: gt
# Interactive tables: reactable
rt <- . %>% 
  reactable::reactable(
    ., 
    filterable = T, 
    sortable = T, 
    searchable = T,
    defaultColDef = reactable::colDef(html = TRUE)
  )

# -----------------------------------------------------------------------------#
# Data export
# 
zipfn <- wastdr::urlize(params$area_name)
fn <- here::here(params$export_dir, zipfn)
if (!fs::file_exists(fn)) {
  "Creating dir {fn}" %>% glue::glue() %>% wastdr::wastdr_msg_info()
  fs::dir_create(fn, recurse = TRUE)
}

# Data labels
pn <- params$area_name
pf <- params$prefix
ld <- fn

# -----------------------------------------------------------------------------#
# WAStD data: load snapshot from ETL
# 
drake::loadd("wastd_data")

x <- wastd_data %>% 
  wastdr::filter_wastd_turtledata(area_name = params$area_name)

x %>% wastdr::export_wastd_turtledata(outdir = fn, filename = zipfn)

# -----------------------------------------------------------------------------#
# WAMTRAM data: load from manual export
# 
species_lookup <- tibble::tribble(
  ~ species_code, ~ species,
  "HK", "eretmochelys-imbricata",
  "FB", "natator-depressus",
  "LB", "dermochelys-coriacea", 
  "GN", "chelonia-mydas"
) 

# WAMTRAM direct database export
w2db <- readRDS(here::here(params$w2_rds_filepath))

#' Convert WAMTRAM `clutch_completed` values into `successful_nesting`
#' 
annotate_w2_nesting_success <- . %>% 
  dplyr::mutate(
    successful_nesting = 
      dplyr::case_when(
        clutch_completed == "Y" ~ "nesting",
        clutch_completed == "N" ~ "non_nesting",
        TRUE ~ "unknown_nesting" # "U", "P", "D", NA
      ))

# WAMTRAM observation summary
# does not contain some important data, e.g. meas CCL_notch, site names
w2_all <- here::here(params$w2_filepath) %>% 
  readr::read_csv() %>% 
  janitor::clean_names() %>% 
  dplyr::left_join(species_lookup, by="species_code") %>% 
  dplyr::filter(is.na(turtle_status) | turtle_status != "E") %>%
  dplyr::mutate(
    o_time = observed_time %>% tidyr::replace_na("00:00:00") %>% as.character(),
    o_datetime = glue::glue("{observation_date} {o_time} +08:00"),
    encounter_datetime_utc = lubridate::parse_date_time(
      o_datetime, 
      orders = c("dmYHMSz", "dmYz"), 
      tz="UTC"
      ) %>% lubridate::with_tz("UTC")
  ) %>% 
  wastdr::add_dates(date_col = "encounter_datetime_utc", parse_date = FALSE) %>% 
  annotate_w2_nesting_success()

# params$w2_initial_rookery_code filters turtles seen anywhere else (could be found outside initial rookery)
# params$w2_place_code filters turtles seen exactly there (could be from other rookeries)
# e.g. "DADI" or "THEE|THNB"
w2 <- w2_all %>% 
  dplyr::filter(grepl(params$w2_place_code, place_code))

w2 %>% 
  readr::write_csv(
    here::here(
      ld, 
      glue::glue("wamtram_{stringr::str_replace_all(params$w2_place_code, '|', '_')}.csv")
    )
  )

# Verify that dates are correct:
# w2 %>% 
#   dplyr::select(
#     observation_date, 
#     observed_time, 
#     o_time,
#     datetime, # same as encounter_datetime_utc
#     encounter_datetime_utc, 
#     calendar_date_awst, 
#     turtle_date) %>% 
#   View()
#   
# R> w2$datetime[[1]]
# [1] "1987-11-25 05:18:00 UTC"
# R> w2$encounter_datetime_utc[[1]] %>% lubridate::with_tz("Australia/Perth")
# [1] "1987-11-25 13:18:00 AWST"

# cols(
#   TURTLE_ID = col_double(),
#   SPECIES_CODE = col_character(),
#   IDENTIFICATION_CONFIDENCE = col_character(),
#   SEX = col_logical(),
#   TURTLE_STATUS = col_character(),
#   OBSERVATION_ID = col_double(),
#   OBSERVATION_STATUS = col_character(),
#   TAG_1 = col_character(),
#   TAG_2 = col_character(),
#   TAG_3 = col_character(),
#   TAG_4 = col_character(),
#   PIT_TAGS = col_character(),
#   OTHER_IDENTIFICATION = col_character(),
#   LOCATION_CODE = col_character(),
#   OBSERVATION_DATE = col_character(),
#   OBSERVED_TIME = col_time(format = ""),
#   OBSERVED_LOCATION_CODE = col_character(),
#   PLACE_CODE = col_character(),
#   ACTIVITY_CODE = col_character(),
#   BEACH_POSITION_CODE = col_character(),
#   NESTING = col_character(),
#   CLUTCH_COMPLETED = col_character(),
#   NUMBER_OF_EGGS = col_double(),
#   EGG_COUNT_METHOD = col_character(),
#   COMMENTS = col_character(),
#   CCL = col_double(),
#   # there are other measurements like CCL_NOTCH not in this CSV export
#   CCW = col_double(),
#   DATUM_CODE = col_character(),
#   LATITUDE = col_double(),
#   LONGITUDE = col_double(),
#   LATITUDE_DEGREES = col_logical(),
#   LATITUDE_MINUTES = col_logical(),
#   LATITUDE_SECONDS = col_logical(),
#   LONGITUDE_DEGREES = col_logical(),
#   LONGITUDE_MINUTES = col_logical(),
#   LONGITUDE_SECONDS = col_logical(),
#   ZONE = col_double(),
#   EASTING = col_double(),
#   NORTHING = col_double(),
#   DAMAGE = col_character()
# )

# -----------------------------------------------------------------------------#
# Survey effort: WAStD
#
# All surveys: useful for programs running over entire season (e.g. C4H)
all_surveys <- x$surveys %>% 
  wastdr::filter_realsurveys() %>% 
  wastdr::survey_season_stats()  

# Distinct reporters of tagging surveys
teams_early_season <- x$surveys %>% 
  wastdr::filter_realsurveys() %>% 
  dplyr::filter(lubridate::month(start_time) > 6) %>% 
  dplyr::group_by(season, reporter) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  dplyr::group_by(season) %>% 
  dplyr::tally(name = "team_size") %>% 
  dplyr::arrange(season)

# Distinct reporters of excav surveys
teams_late_season <- x$surveys %>% 
  wastdr::filter_realsurveys() %>% 
  dplyr::filter(lubridate::month(start_time) < 7) %>% 
  dplyr::group_by(season, reporter) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  dplyr::group_by(season) %>% 
  dplyr::tally(name = "team_size") %>% 
  dplyr::arrange(season)

# Turtle tagging surveys: 4 weeks Nov/Dec
early_season_surveys <- x$surveys %>%
  wastdr::filter_realsurveys() %>%
  dplyr::filter(lubridate::month(start_time) > 10,
                # from 11 Nov onwards
                lubridate::day(start_time) > 10) %>%
  dplyr::group_by(season) %>%
  dplyr::summarise(
    first_day = min(calendar_date_awst),
    last_day = max(calendar_date_awst),
    season_length_days = (as.numeric(
      lubridate::interval(first_day, last_day)
    ) / (3600 * 24)) + 1,
    number_surveys = dplyr::n(),
    number_surveyed_days = dplyr::n_distinct(calendar_date_awst),
    hours_surveyed = round(sum(duration_hours))
  ) %>%
  dplyr::left_join(teams_early_season, by = "season")

# Turtle nest excav surveys: 1 week Jan/Feb
late_season_surveys <- x$surveys %>%
  wastdr::filter_realsurveys() %>%
  dplyr::filter(lubridate::month(start_time) < 7) %>%
  dplyr::group_by(season) %>%
  dplyr::summarise(
    first_day = min(calendar_date_awst),
    last_day = max(calendar_date_awst),
    season_length_days = (as.numeric(
      lubridate::interval(first_day, last_day)
    ) / (3600 * 24)) + 1,
    number_surveys = dplyr::n(),
    number_surveyed_days = dplyr::n_distinct(calendar_date_awst),
    hours_surveyed = round(sum(duration_hours))
  ) %>%
  dplyr::left_join(teams_late_season, by = "season")

# Needed to normalise total_turtles_season
season_lenghts <- early_season_surveys %>% 
  dplyr::transmute(season = season, nights = season_length_days)

# -----------------------------------------------------------------------------#
# Emergences: WAStD
# 
# Missed turtles from form "Turtle Track or Nest" filled during tagging
# We don't filter by track_age
# This includes "missed just now" from tagging, 
# "fresh" and "old" from morning track count.
wastd_missed_turtles_single <- 
  x$tracks %>% 
  wastdr::filter_realspecies() %>% 
  dplyr::filter(
    nest_type %in% c(
      "successful-crawl", 
      "false-crawl", 
      "track-unsure", 
      "track-not-assessed")
  ) %>% 
  dplyr::group_by(season, turtle_date, species) %>% 
  dplyr::tally(name = "missed_single") %>% 
  dplyr::ungroup() 

# Missed turtles from "Track Tally"
# This data comes from either saturated beaches (CDO) or backfilled datasheets
wastd_missed_turtles_tally <- 
  x$track_tally %>% 
  wastdr::filter_realspecies() %>% 
  dplyr::filter(
    nest_type %in% c(
      "successful-crawl", 
      "false-crawl", 
      "track-unsure", 
      "track-not-assessed")
  ) %>% 
  dplyr::group_by(season, turtle_date, species) %>% 
  dplyr::summarize(missed_tally = sum(tally)) %>%
  dplyr::ungroup() 

# Combined from forms "Turtle Track or Nest" and "Track Tally"
wastd_missed_turtles <-
  wastd_missed_turtles_single %>% 
  dplyr::full_join(wastd_missed_turtles_tally, 
                   by=c("species", "season", "turtle_date")) %>% 
  tidyr::replace_na(list(missed_single=0, missed_tally=0)) %>% 
  dplyr::mutate(missed = missed_single + missed_tally) %>%
  dplyr::select(-missed_single, -missed_tally)


# B from night tagging
# unique(w2$turtle_status) "T" "Q" "N" "P" "A" "R" 
# processed: "T" "Q" "P" "A" "R" - missed: "N" NA
# new tag applied:
# "A" new tag
# "P" query new tagged - new on a turtle with tag scars and no PIT = not a new turtle
# "Q" query identity - a record flagged for curation, can become R or A
# "T" tagged
# "R" re-entered population - a curated record, was Q
#  
# no new tag applied:
# "N" non tagged
# NA  
# C captive
# E error data not to be used
# s salvage

# -----------------------------------------------------------------------------#
# Emergences: processed + missed
# w2_processed_turtles = w2_tagged_turtles + w2_non_tagged_turtles
w2_processed_turtles <- w2 %>% 
  dplyr::group_by(season, turtle_date, species) %>% 
  dplyr::tally(name = "processed") %>% 
  dplyr::ungroup()

w2_tagged_turtles <- w2 %>% 
  dplyr::filter(turtle_status %in% c("A", "P", "Q", "T", "R")) %>% 
  dplyr::group_by(season, turtle_date, species) %>% 
  dplyr::tally(name = "tagged") %>% 
  dplyr::ungroup()

w2_non_tagged_turtles <- w2 %>% 
  dplyr::filter(turtle_status %in% c("N", NA, "C", "S")) %>% 
  dplyr::group_by(season, turtle_date, species) %>% 
  dplyr::tally(name = "non_tagged") %>% 
  dplyr::ungroup()

# Total emergences = missed (WAStD) + processed (w2)
# Important to replace NA with 0 before adding, as num + NA = NA
total_turtles <- 
  wastd_missed_turtles %>% 
  dplyr::full_join(w2_processed_turtles, by=c("species", "season", "turtle_date")) %>% 
  tidyr::replace_na(list(missed=0, processed=0)) %>%
  dplyr::mutate(
    emergences = processed + missed,
    processed_pct = round(processed * 100 / (emergences), 2)
  ) %>% 
  dplyr::arrange(season, species) %>% 
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
  ) %>% 
  dplyr::select(
    season, turtle_date, species, emergences, processed, missed, processed_pct
  ) %>% 
  janitor::clean_names(case="sentence")

# Season x species tally of wastd_missed_turtles
missed_turtles_season_totals <- wastd_missed_turtles %>% 
  dplyr::group_by(season, species) %>% 
  dplyr::tally(name = "missed", wt = missed) %>% 
  dplyr::ungroup() 

# Season x species tally of w2_processed_turtles
processed_turtles_season_totals <- w2_processed_turtles %>% 
  dplyr::group_by(season, species) %>% 
  dplyr::tally(name = "processed", wt = processed) %>% 
  dplyr::ungroup() 

# Season x species tally of w2_tagged_turtles
tagged_turtles_season_totals <- w2_tagged_turtles %>% 
  dplyr::group_by(season, species) %>% 
  dplyr::tally(name = "tagged", wt = tagged) %>% 
  dplyr::ungroup() 

# Season x species tally of w2_non_tagged_turtles
non_tagged_turtles_season_totals <- w2_non_tagged_turtles %>% 
  dplyr::group_by(season, species) %>% 
  dplyr::tally(name = "non_tagged", wt = non_tagged) %>% 
  dplyr::ungroup() 

# Total turtles
total_turtles_season <- 
  missed_turtles_season_totals %>% 
  dplyr::full_join(processed_turtles_season_totals, by=c("species", "season")) %>% 
  # we skip tagged/non tagged as there is only one non tagged (historic sighting)
  # dplyr::full_join(tagged_turtles_season_totals, by=c("species", "season")) %>% 
  # dplyr::full_join(non_tagged_turtles_season_totals, by=c("species", "season")) %>% 
  tidyr::replace_na(list(missed=0, processed=0)) %>%
  dplyr::left_join(season_lenghts, by="season") %>% 
  dplyr::mutate(
    emergences = processed+missed,
    processed_pct = round(processed * 100 / (emergences), 2),
    total_per_night = round(emergences / nights)) %>% 
  dplyr::arrange(season, species) %>% 
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
  ) %>% 
  dplyr::select(season, species, emergences, nights, total_per_night, 
                processed, missed, processed_pct) %>% 
  janitor::clean_names(case="sentence")


# Export to CSV
total_turtles_season %>% 
  readr::write_csv(here::here(ld, "total_turtles_season.csv"))

# Display helper
total_turtles_gt <- function(data, species){
  ttl <- glue::glue("Total number of {species} turtles")
  data %>% 
      gt::gt(
    groupname_col=NULL,
    caption = ttl
  ) %>% 
  gt::tab_header(
    title=ttl,
    subtitle="Turtle emergences grouped by season and species"
  ) %>% 
  gt::tab_spanner("Total Turtles", columns = c(Emergences, Nights, `Total per night`)) %>% 
  gt::tab_spanner("Emergences", columns = c(Processed, Missed))

}

# -----------------------------------------------------------------------------#
# Nesting success
# 
# 
# We equate clutch_completed with successful nesting.
# w2$clutch_completed %>% unique()
# Y yes, N no, P possible, U uncertain, D did not check
# 
# Nesting success by season and species: season summary 
# From ToN
wastd_nesting_type_by_season_age_species_single <-
  x$tracks %>% 
  wastdr::filter_realspecies() %>% 
  dplyr::group_by(area_name, season, species, nest_type) %>%
    dplyr::tally() %>%
    dplyr::ungroup() %>%
    tidyr::spread(nest_type, n, fill = 0)

# From TT
wastd_nesting_type_by_season_age_species_tally <-
  x$track_tally %>% 
  wastdr::filter_realspecies() %>% 
  dplyr::rename(area_name = encounter_area_name) %>% 
  dplyr::group_by(area_name, season, species, nest_type) %>%
    dplyr::summarise(n=sum(tally)) %>%
    dplyr::ungroup() %>%
    tidyr::spread(nest_type, n, fill = 0)

# Combined
wastd_nesting_type_by_season_age_species <-
  dplyr::bind_rows(
    wastd_nesting_type_by_season_age_species_single %>% tibble::rownames_to_column(), 
    wastd_nesting_type_by_season_age_species_tally %>% tibble::rownames_to_column()
  ) %>% 
  dplyr::group_by(rowname, area_name, season, species) %>% 
  dplyr::summarise_all(sum) %>% 
  tidyr::replace_na(list("body-pit"=0, "track-not-assessed"=0)) %>%
  dplyr::arrange(season) %>% 
  dplyr::select(-rowname, -area_name)

# Nesting success by season and species: season summary (tagging)
w2_nesting_type_by_season_species <- 
  w2 %>% 
  dplyr::group_by(season, species, successful_nesting) %>% 
  dplyr::tally() %>%
  dplyr::ungroup() %>%
  tidyr::spread(successful_nesting, n, fill = 0)

# Nesting success by season and individual turtle: inter-nesting and clutch freq
w2_nesting_type_by_season_turtle <- 
  w2 %>% 
  dplyr::group_by(season, species, turtle_id, successful_nesting) %>% 
  dplyr::tally() %>%
  dplyr::ungroup() %>%
  tidyr::spread(successful_nesting, n, fill = 0) %>% 
  dplyr::arrange(season, turtle_id, -nesting)

total_nesting_by_season_species <-
  dplyr::full_join(
    wastd_nesting_type_by_season_age_species,
    w2_nesting_type_by_season_species,
    by=c("season", "species")
  ) %>% 
  dplyr::arrange(season, species) %>% 
  dplyr::transmute(
    season = season,
    species = stringr::str_to_sentence(species) %>% stringr::str_replace("-"," "),
    nesting = `successful-crawl` + nesting,
    unsure = `track-not-assessed` + `track-unsure` + unknown_nesting,
    non_nesting = `false-crawl` + non_nesting,
    nesting_success_pess = round(nesting * 100 / (nesting+unsure+non_nesting), 2),
    nesting_success_split = round((nesting + (0.5 * unsure)) * 100 / (nesting+unsure+non_nesting), 2),
    nesting_success_opt = round((nesting + unsure) * 100 / (nesting+unsure+non_nesting), 2)
  )

total_nesting_by_season_species %>% 
  readr::write_csv(here::here(ld, "total_nesting_by_season_species.csv"))

w2_nesting_type_by_season_turtle %>% 
  readr::write_csv(here::here(ld, "total_nesting_by_season_turtle.csv"))

# Display helper
total_nesting_gt <- function(data, species){
  data %>% 
  gt::gt(
    groupname_col=NULL,
    caption = glue::glue("{species} nesting by season tallied by ",
                         "nesting success from tagging and track data")
  ) %>% 
  gt::tab_header(
    title=glue::glue("Seasonal nesting totals: {species}"),
    subtitle="Nesting by season tallied by nesting success"
  ) %>%
  gt::cols_label(
    season = "Season",
    species = "Species",
    nesting = "Nesting confirmed",
    unsure = "Nesting unsure",
    non_nesting = "Non nesting",
    nesting_success_pess = "At least",
    nesting_success_opt = "At most",
    nesting_success_split = "Split 50/50",
  ) %>% 
    gt::tab_spanner(
      label = "Nesting Success",
      columns = c(gt::contains("nesting_success"))
    )
}

# -----------------------------------------------------------------------------#
# Connectivity
trt_conn <- 
  w2_all %>% 
  dplyr::group_by(season, species, place_code, location_code) %>% 
  dplyr::rename(resighted_at=place_code) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  tidyr::pivot_wider(
    names_from=location_code, 
    names_sort = TRUE, 
    values_from=n,
    values_fill = 0
  ) %>% 
  dplyr::arrange(season, resighted_at, species)

trt_conn_loc <- 
  w2_all %>% 
  dplyr::group_by(season, species, observed_location_code, location_code) %>% 
  dplyr::rename(resighted_at=observed_location_code) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  tidyr::pivot_wider(
    names_from=location_code, 
    names_sort = TRUE, 
    values_from=n,
    values_fill = 0
  ) %>% 
  dplyr::arrange(season, resighted_at, species)

filter_conn <- function(data, ssn=2020, spc="natator-depressus"){
  data %>% 
    dplyr::filter(season==ssn, species==spc) %>% 
    dplyr::select(-season, -species) %>%
    dplyr::select_if(~!(all(is.na(.)) | all(. == 0))) %>% 
    dplyr::filter_at(dplyr::vars(-resighted_at), dplyr::any_vars(. != 0))
}

gt_conn <- function(data, season=2020, species="Flatback"){
  cpt <- 
    "Connectivity of {species} turtles in {season}-{season+1-2000}" %>% 
    glue::glue()

  data %>% 
    gt::gt(groupname_col=NULL, caption = cpt) %>% 
    gt::tab_header(title=cpt) %>% 
    gt::cols_label(resighted_at = "Resighted at") %>% 
    gt::tab_spanner(label = "Initially observed at",
      columns = c(-gt::contains("resighted_at"))) 
}

trt_conn %>% 
  readr::write_csv(here::here(ld, "turtle_nesting_connectivity.csv"))

# -----------------------------------------------------------------------------#
# Morphometrics
# 
# CCL
# trt_morph_ccl <- w2 %>% 
#   dplyr::filter(!is.na(ccl)) %>% 
#   dplyr::group_by(season, species) %>% 
#   dplyr::summarise(
#     n = dplyr::n(),
#     ccl_mean = mean(ccl) %>% round(0),
#     ccl_sd = sd(ccl) %>% round(2),
#     ccl_min = min(ccl) %>% round(0),
#     ccl_max = max(ccl) %>% round(0),
#     # ccw_mean = mean(ccw) %>% round(0),
#     # ccw_sd = sd(ccw) %>% round(2),
#     # ccw_min = min(ccw) %>% round(0),
#     # ccw_max = max(ccw) %>% round(0),
#     .groups = "keep"
#   ) %>% 
#   dplyr::ungroup() %>% 
#   dplyr::arrange(season, species) %>% 
#   dplyr::mutate(
#     species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
#   ) %>% 
#   janitor::clean_names(case="sentence")

# CCW
# trt_morph_ccw <- w2 %>% 
#   dplyr::filter(!is.na(ccw)) %>% 
#   dplyr::group_by(season, species) %>% 
#   dplyr::summarise(
#     n = dplyr::n(),
#     # ccl_mean = mean(ccl) %>% round(0),
#     # ccl_sd = sd(ccl) %>% round(2),
#     # ccl_min = min(ccl) %>% round(0),
#     # ccl_max = max(ccl) %>% round(0),
#     ccw_mean = mean(ccw) %>% round(0),
#     ccw_sd = sd(ccw) %>% round(2),
#     ccw_min = min(ccw) %>% round(0),
#     ccw_max = max(ccw) %>% round(0),
#     .groups = "keep"
#   ) %>% 
#   dplyr::ungroup() %>% 
#   dplyr::arrange(season, species) %>% 
#   dplyr::mutate(
#     species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
#   ) %>% 
#   janitor::clean_names(case="sentence")

# Export to CSV
# trt_morph_ccl %>% readr::write_csv(here::here(ld, "trt_morph_ccl.csv"))
# trt_morph_ccw %>% readr::write_csv(here::here(ld, "trt_morph_ccw.csv"))
trt_morph <- 
  w2db$obs_measurements %>% 
  dplyr::left_join(w2db$enc, by="observation_id") %>% 
  dplyr::filter(place_code==params$w2_place_code) %>% 
  wastdr::add_dates(date_col="observation_datetime_utc", parse=FALSE)

# unique(trt_morph$measurement_type_code)
# "CCL" "CCL NOTCH" "CCW" "NOTCH" "Track Wmax" "WEIGHT K"
# 
morph_stats <- trt_morph %>% 
  dplyr::left_join(species_lookup, by="species_code") %>% 
  dplyr::rename(metric = measurement_type_code) %>% 
  dplyr::filter(measurement_value>500) %>% # QA: exclude decimal typos
  dplyr::group_by(species, season, metric) %>% 
  dplyr::summarize(
    n = dplyr::n(),
    mean = mean(measurement_value) %>% round(0),
    sd = sd(measurement_value) %>% round(2),
    min = min(measurement_value) %>% round(0),
    max = max(measurement_value) %>% round(0)
  ) %>% 
  dplyr::ungroup()%>% 
  dplyr::arrange(species, season)

morph_stats %>%  readr::write_csv(here::here(ld, "trt_morph.csv"))

morph_stats_gt <- morph_stats %>%
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
  ) %>% 
  janitor::clean_names(case="sentence") 

# Morphometrics QA table
morph_qa <- trt_morph %>% 
  dplyr::left_join(species_lookup, by="species_code") %>% 
  dplyr::group_by(species, measurement_type_code) %>%
  dplyr::summarise(
    chart = sparkline::spk_chr(measurement_value, type="line")
  ) %>% 
  dplyr::ungroup()

morph_qa_html <- morph_qa %>%
  gt::gt() %>%
  gt::fmt_markdown(columns = c(chart)) %>% 
  gt:::as.tags.gt_tbl() %>% 
  htmltools::attachDependencies(htmlwidgets::getDependency("sparkline"))

# Display helper
morph_gt <- function(data, species, metric){
  ttl <- glue::glue(
    "Morphometrics by season and species: {species} turtles {metric}")

  data %>% 
    gt::gt(groupname_col = NULL, caption = ttl) %>% 
    gt::tab_header(
      title= ttl,
      subtitle="Values in millimetres"
    ) %>% 
    gt::tab_spanner(
      label = "Group",
      columns = c(Season, Species, N)
    ) %>% 
    gt::tab_spanner(
      label = "Curved Carapace Length (mm)",
      columns = c(gt::contains("ccl"))
    ) %>% gt::tab_spanner(
      label = "Curved Carapace Width (mm)",
      columns = c(gt::contains("ccw"))
    )
}

# -----------------------------------------------------------------------------#
# Damages and injuries
damage_codes <- tibble::tribble(
  ~"damage_type", ~"damage",
  "0", "None significant",
  "1", "Tip off - Flipper",
  "2", "Lost from Nail - Flipper",
  "3", "Lost half - Flipper",
  "4", "Lost whole - Flipper",
  "5", "Minor Wounds or cuts",
  "6", "Major Wounds or cuts",
  "7", "Deformity"
)

# w2$damage split by comma, tally unique injuries
# 
trt_numbers <- w2 %>% 
  dplyr::group_by(season, species) %>% 
  dplyr::tally(name = "total_turtles") %>% 
  dplyr::ungroup() %>% 
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
  )

trt_dmg <- w2 %>% 
  dplyr::select(season, turtle_date, species, damage) %>% 
  tidyr::separate_rows(damage, sep = ", ") %>% 
  tidyr::drop_na(damage) %>% 
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," "),
    damage = damage %>% 
      stringr::str_replace("mid-carapace", "mid carapace") %>%
      stringr::str_replace(" - entire", " entire") %>% 
      trimws()
  ) %>% 
  tidyr::separate(damage, c("body_part", "damage_type"), sep = " - ") %>% 
  dplyr::left_join(damage_codes, by = c("damage_type")) %>% 
  dplyr::select(-damage_type) %>% 
  dplyr::arrange(season) %>% 
  dplyr::left_join(trt_numbers, by=c("season", "species"))

trt_dmg %>% 
  readr::write_csv(here::here(ld, "trt_dmg.csv"))

trt_dmg_by_species_season <- trt_dmg %>% 
  dplyr::group_by(season, species, damage, total_turtles) %>% 
  dplyr::tally(name = "damaged_turtles") %>% 
  dplyr::mutate(damage_pct = round(100* damaged_turtles / total_turtles, 2)) %>% 
  dplyr::ungroup() %>% 
  dplyr::arrange(species, season, -damage_pct) %>% 
  janitor::clean_names(case="sentence")

trt_dmg_by_species_season %>% 
  readr::write_csv(here::here(ld, "trt_dmg_by_species_season.csv"))

trt_dmg_type_pivot <- trt_dmg %>% 
  dplyr::group_by(season, species, damage, total_turtles) %>% 
  dplyr::tally(name = "damaged_turtles") %>% 
  dplyr::mutate(damage_pct = round(100* damaged_turtles / total_turtles, 2)) %>% 
  dplyr::ungroup() %>% 
  janitor::clean_names(case="sentence") %>% 
  tidyr::pivot_wider(
    names_from = Season, 
    values_from = `Damage pct`, 
    names_sort = TRUE, 
    values_fill = 0
  ) %>% 
  dplyr::select(-`Total turtles`, -`Damaged turtles`) 

trt_dmg_type_pivot %>% 
  readr::write_csv(here::here(ld, "trt_dmg_type_pivot.csv"))

trt_dmg_bodypart_pivot <- trt_dmg %>% 
  dplyr::group_by(season, species, body_part, total_turtles) %>% 
  dplyr::tally(name = "damaged_turtles") %>% 
  dplyr::mutate(damage_pct = round(100* damaged_turtles / total_turtles, 2)) %>% 
  dplyr::ungroup() %>% 
  janitor::clean_names(case="sentence") %>% 
  tidyr::pivot_wider(
    names_from = Season, values_from = `Damage pct`, names_sort = TRUE, values_fill = 0
  ) %>% 
  dplyr::select(-`Total turtles`, -`Damaged turtles`) 

trt_dmg_bodypart_pivot %>% 
  readr::write_csv(here::here(ld, "trt_dmg_bodypart_pivot.csv"))

Data

In this report, we present combined Turtle Monitoring data from WAStD (Turtle Nesting Census, "tracks") and the tagging database WAMTRAM2 (Turtle Tagging, "tagging") and filter the data to the location r params$area_name.

The WAStD data was downloaded on r wastd_data$downloaded_on and reflects all QA operations on WAStD data up to this date.

The WAMTRAM2 data was exported on r params$w2_exported_on and reflects all QA operations on WAMTRAM2 tagging data up to this date.

Survey effort

Survey effort as shown here is calculated from surveys captured in WAStD. Test and training surveys are excluded. Dates are calendar dates.

The season length spans the dates of the first to the last survey of the season.

The estimate of hours spent surveying is inflated in past seasons (2019 and earlier) by incorrectly merged surveys which span either two night tagging shifts, or a night tagging shift and a morning track count. The estimate of hours will be more accurate after these merged surveys have been un-merged.

On days of night tagging and morning track counts there should be two surveys per date.

In the 2017-18 season, the new digital data capture system was introduced and partially used in the third and fourth week of field work. The figures and numbers below reflect the absence of data captured only on paper, pending backfilling into WAStD. Once backfilled, the season length and survey effort will be similar to the effort shown in later seasons.

In the 2020-21 season, monitoring effort was disrupted by thunderstorms and cyclone activity in weeks 3 and 4. On 2020-12-09, the field team was evacuated from Delambre Island.

The team size is approximated through the number of distinct names in the digital data capture system. In 2017-18, when the digital data capture was introduced but inconsitently applied, the backfilled data default to the team leader and therefore under-represent the actual team size (closer to other seasons).

# This makes more sense for places with continuous monitoring, e.g. Pt Hedland
all_surveys %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Seasonal overview of survey effort"
  ) %>% 
  # gt::fmt_date(c("First day", "Last day")) %>% # Friday, October 20, 2017
  gt::tab_header(
    title="Seasonal overview of survey effort",
    subtitle="Survey stats grouped by rookery and season, dates are calendar dates"
  )

Turtle tagging surveys

The 2020 season was cut short by a week through a cyclone, data capture in the third and last week was already impacted by inclement weather.

Early season surveys are counted as surveys between 15 November to the end the December. The main field trip begins after 15 Movember, but some years (e.g. 2017) show some additional, unrelated surveys.

early_season_surveys %>%
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Seasonal overview of early season survey effort"
  ) %>% 
  # gt::fmt_date(c("First day", "Last day")) %>% # Friday, October 20, 2017
  gt::tab_header(
    title="Seasonal overview of early season survey effort",
    subtitle="Survey stats grouped by rookery and season"
  )

Turtle nest excavation surveys

The team size is impacted through incorrect data capture - if observers forget to update the forms with their name, the previously filled in name is recorded. This impacted at least the 2020 late season (team size was > 1).

late_season_surveys %>%
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Seasonal overview of late season survey effort"
  ) %>% 
  # gt::fmt_date(c("First day", "Last day")) %>% # Friday, October 20, 2017
  gt::tab_header(
    title="Seasonal overview of late season survey effort",
    subtitle="Survey stats grouped by rookery and season"
  )

The following figure is saved as DEL_survey_count_delambre-island.png.

x$surveys %>% 
  wastdr::filter_realsurveys() %>% 
  plot_survey_count(local_dir = fn, placename = pn, prefix = pf, export = TRUE)

The following figure is saved as DEL_survey_effort_delambre-island.png.

x$surveys %>% 
  wastdr::filter_realsurveys() %>% 
  plot_survey_effort(local_dir = ld, placename = pn, prefix = pf, export = TRUE)

Nesting

Season overview

Nesting census data (WAStD tracks) are shown for the last season (2020-21). Training records are excluded.

Turtle nests

Confirmed nests from tagging and track census are shown as a heatmap. Tagging coordinates outside of the island indicate a need for QA.

Caveat: The heatmap clustering is scale dependent and will change on zoom. Patterns visible at a larger scale can be artefacts of clustering.

library(leaflet.extras)

tag_nests <- w2 %>% 
  dplyr::filter(successful_nesting=="nesting", species=="natator-depressus") %>% 
  dplyr::select(species, latitude, longitude, season, calendar_date_awst) %>% 
  dplyr::filter(!is.na(latitude) & !is.na(longitude))

track_nests <- x$tracks %>% 
  dplyr::filter(nest_type=="successful-crawl", species=="natator-depressus") %>% 
  dplyr::select(species, latitude, longitude, season, calendar_date_awst)

nests <- dplyr::bind_rows(tag_nests, track_nests)

nests %>% 
  wastdr::filter_wastd_season(2020) %>% 
  leaflet() %>%
  leaflet::addProviderTiles("Esri.WorldImagery", group = "Aerial") %>%
  # leaflet::addProviderTiles("OpenStreetMap.Mapnik", group = "Place names") %>%
  addHeatmap(
    lng = ~longitude, lat = ~latitude, #intensity = ~mag,
    blur = 20, max = 0.05, radius = 15
  )

Tracks are shown individually here. The interactive map provides details on individual tracks through labels (on mouse hover) and popups (click on markers). Individual species can be toggled from the layer legend.

x$tracks %>% 
  wastdr::filter_realspecies() %>% 
  wastdr::filter_wastd_season(2020) %>% 
  wastdr::map_tracks(sites = x$sites, cluster = FALSE)

Turtle emergences and processing success

We calculate the total number of turtle emergences per night as the sum of emergences from the following observations:

We exclude hatched nests, incubating nests without a track (each are older than a day), and body pits (which can be recorded, but are of little value).

We assume each record in WAMTRAM is a turtle emergence, be it tagged or not, and nesting or not. We assume no record in WAMTRAM is duplicated in WAStD, i.e. turtles are either captured on paper (WAMTRAM) or in a tablet (WAStD), but never on both media.

We bin the numbers for tallying by the "turtle date", which is the calendar date of the tagging night and extends to encounters past midnight and on the next morning. Turtle date rolls over at noon and lags behind calendar date by 12 hours. Turtle date bins all recorded events from one nesting night under one date.

Caveats

The same turtle can emerge multiple times per night. E.g., a turtle could emerge one or several times for an unsuccessful nesting attempt before being processed after a successful one. Another turtle could emerge one or several times and evade tagging altogether. In either scenario, one turtle would contribute several emergences to a night's tally. Therefore, the numbers presented here are an optimistic estimate of total turtle numbers.

Total emergences (processed and missed) turtles per season

The emergences are the sum of processed and missed turtles. The percentage of processed turtles is shown as the fraction of processed turtles over total emergences (processed and missed).

All processed turtles but one historic sighting of a Leatherback turtle are tagged. We therefore exclude the further split of processed turtles into tagged and non-tagged turtles.

The rounded "Total per night" are the number of total emergences divided by 27 nesting nights of the 28 day field trips.

Later in this report, we split the total emergences into nesting and non nesting.

This summary is exported as total_turtles_season.csv.

The low numbers in the 2020-21 season (even when corrected for only 21 nights) can be explained with thunderstorms and cyclone activity slowing down field work in week 3 and the evacuation of the island before week 4 of the 4 week turtle monitoring expedition.

Density

The length of surveyed coastline at Delambre is 2000m. The beach width is about 20m. This results in a nesting area of about 40000m2.

With an emergence count of 1187 turtles (2020-21) to 3158 turtles (2017-18, all species) and assuming homogeneous nesting density, we can estimate a nest density of 0.03 to 0.08 nests per square metre.

The spatial footprint of foreign objects, such as a 10ft shipping container (3.05m by 2.44m = 7.442 square metres), can be estimated to directly cover a mean value of 0.22 to 0.59 nests.

Considering the worst case scenario of the foreign objects covering a contiguous array of turtle nests (1 square metre each), we can estimate that e.g. a 10ft shipping container (7.442 square metres) can cover a maximum of about 7 to 8 nests.

Flatback turtles

total_turtles_season %>% 
  dplyr::filter(Species == "Natator depressus"
                # , Season > 2016
                ) %>% 
  total_turtles_gt("Flatback")

Hawksbill turtles

total_turtles_season %>% 
  dplyr::filter(Species == "Eretmochelys imbricata"
                # , Season > 2016
                ) %>% 
  total_turtles_gt("Hawksbill")

Green turtles

total_turtles_season %>% 
  dplyr::filter(Species == "Chelonia mydas"
                # , Season > 2016
                ) %>% 
  total_turtles_gt("Green")

Total, processed, and missed turtles per night

Data:

The tally of processed (tagged) and missed turtles is shown first as an interactive table (tallied by season and date, respectively), then as a time series plots in the following sections.

We show an interactive table, as the printed version is too long. This summary is exported as total_turtles.csv.

total_turtles %>% 
  dplyr::arrange(Season, `Turtle date`) %>% 
  rt()

Recent seasons split by species

This figure is exported as turtles_proc_mis_by_species.png.

plt_total_turtles_season <- 
  total_turtles_season %>%
  tidyr::pivot_longer(
    cols=c(Missed, Processed), names_to="Type", values_to="N"
  ) %>%
  dplyr::filter(Season > 2007) %>% # , species == "natator-depressus"
  ggplot2::ggplot() +
  ggplot2::geom_col(ggplot2::aes(x=Season, y=N, colour=Type, fill=Type)) +
  ggplot2::facet_grid(facets = ggplot2::vars(Species), scales="free_x") +
  ggplot2::ggtitle(
    label="Processed and missed turtles", 
    subtitle="Numbers of processed and missed turtles over seasons by species") + 
  ggplot2::labs(x="Year", y="Turtles") +
  ggplot2::theme_bw() +
  ggplot2::theme(legend.position="bottom") +
  NULL

ggplot2::ggsave(
  plot = plt_total_turtles_season,
  filename = "turtles_proc_mis_by_species.png",
  path = fn,
  width = 10,
  height = 8
)

plt_total_turtles_season

All species, all seasons

This figure is exported as turtles_proc_mis_by_season.png.

Missed turtles have only been captured since the (incomplete) 2017 season, and complete numbers of missed turtles are shown from the 2018 season onwards.

turtles_proc_mis_by_season <- 
  ggplot2::ggplot(
  data = total_turtles,
  mapping = ggplot2::aes(x = tdate_as_fdate(`Turtle date`))
) +
  ggplot2::geom_bar(ggplot2::aes(y = Missed),
                    stat = "identity",
                    color = "black",
                    fill = "black"
  ) +
    ggplot2::geom_bar(ggplot2::aes(y = Processed),
                    stat = "identity",
                    color = "grey",
                    fill = "grey"
  ) +
  ggplot2::facet_grid(facets = ggplot2::vars(Season), scales = "free_x") +
  ggplot2::scale_x_continuous(labels = function(x) {fdate_as_tdate(x)}) +
  ggplot2::ggtitle(label="Total turtle emergences", 
                   subtitle="Tagged (grey) vs missed (black) by season") + 
  ggplot2::labs(x="Date", y="Turtles") +
  ggplot2::theme_bw() +
  ggplot2::theme(legend.position="bottom") +
  NULL

ggplot2::ggsave(
  plot = turtles_proc_mis_by_season,
  filename = "turtles_proc_mis_by_season.png",
  path = fn,
  width = 10,
  height = 12
)

turtles_proc_mis_by_season

Flatback turtles, all seasons

ggplot2::ggplot(
  data = total_turtles %>% dplyr::filter(Species == "Natator depressus"),
  mapping = ggplot2::aes(x = tdate_as_fdate(`Turtle date`))
) +
  ggplot2::geom_bar(ggplot2::aes(y = Missed),
                    stat = "identity",
                    color = "black",
                    fill = "black"
  ) +
    ggplot2::geom_bar(ggplot2::aes(y = Processed),
                    stat = "identity",
                    color = "grey",
                    fill = "grey"
  ) +
  ggplot2::facet_grid(facets = ggplot2::vars(Season), scales = "free_x") +
  ggplot2::scale_x_continuous(labels = function(x) {fdate_as_tdate(x)}) +
  ggplot2::ggtitle(label="Total Flatback turtle emergences", 
                    subtitle="Tagged (grey) vs missed (black) by season") + 
  ggplot2::labs(x="Date", y="Turtles") +
  ggplot2::theme_bw() +
  ggplot2::theme(legend.position="bottom") +
  NULL

Primary tagged, resighted, remigrant turtles

<!-- Per species:

Season New tagged ISR# inter season returns (turtle recaptured from previous season) Total individuals Total processed Missed turtles Total turtles recorded Mean nightly turtles

Definitions:

Within one season, the first recorded processing of a turtle should be either an "Initial Nesting" or a "Remigrant".

Example encounter history of a remigrant turtle in 2019:

-->

remigrant_case_history <- function(data, ssn, case_no){ 
  remigrants <- data %>% 
    dplyr::filter(season==ssn, observation_status=="Remigrant")
  data %>% 
    dplyr::filter(turtle_id==remigrants$turtle_id[case_no])
}

remi <- remigrant_case_history(w2_all, 2019, 2)
remi %>% rt()
ggplot(
  remi, 
  aes(x=lubridate::isoweek(calendar_date_awst), 
      y=1, 
      colour=observation_status,
      shape=observed_location_code)
  ) +
  geom_point(cex=5) +
  labs(title="Case history of one turtle", x="ISO Week") +
  facet_wrap("season", ncol=1)

WAMTRAM tagging records are grouped by season, species, and observation status ("Primary tagging", "Within Season Recapture", and "Inter Season Recapture") and tallied.

The only record of an "Initial sighting" is a historic sighting of a Leatherback turtle in 1995, so we omit this category altogether here.

We see a dip in numbers in the 2020 season due to the cyclone cutting short field work.

This summary is exported as turtles_new_rem_rec.csv and shown in tables below split by species.

turtles_mcr <- w2 %>% 
  # dplyr::filter(species_code == "FB") %>% 
  dplyr::group_by(season, species, observation_status) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  dplyr::arrange(season, species) %>% 
  tidyr::pivot_wider(names_from = observation_status, values_from = n) %>% 
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
  ) %>% 
  janitor::clean_names(case="sentence") 

Flatback turtles

turtles_mcr %>% 
  dplyr::filter(Species == "Natator depressus") %>% 
  dplyr::select(-"Initial sighting") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Recapture status by season of Flatback turtles"
  ) %>% 
  gt::tab_header(
    title="Recapture status by season of Flatback turtles"
    # subtitle="Initial nesting vs Remigrant vs Resighting vs Initial sighting"
  ) %>% 
  gt::cols_label(
    "Initial nesting" = "Primary Tagging",
    "Resighting" = "Within Season Recapture",
    "Remigrant" = "Inter Season Recapture")

Hawksbill turtles

turtles_mcr %>% 
  dplyr::filter(Species == "Eretmochelys imbricata") %>%  
  dplyr::select(-"Initial sighting") %>%
  gt::gt(
    groupname_col=NULL,
    caption = "Recapture status by season of Hawksbill turtles"
  ) %>% 
  gt::tab_header(
    title="Recapture status by season of Hawksbill turtles"
    # subtitle="Initial nesting vs Remigrant vs Resighting vs Initial sighting"
  ) %>% 
  gt::cols_label(
    "Initial nesting" = "Primary Tagging",
    "Resighting" = "Within Season Recapture",
    "Remigrant" = "Inter Season Recapture")

Green turtles

turtles_mcr %>% 
  dplyr::filter(Species == "Chelonia mydas") %>%  
  dplyr::select(-"Initial sighting") %>%
  gt::gt(
    groupname_col=NULL,
    caption = "Recapture status by season of Green turtles"
  ) %>% 
  gt::tab_header(
    title="Recapture status by season of Green turtles"
    # subtitle="Initial nesting vs Remigrant vs Resighting vs Initial sighting"
  ) %>% 
  gt::cols_label(
    "Initial nesting" = "Primary Tagging",
    "Resighting" = "Within Season Recapture",
    "Remigrant" = "Inter Season Recapture")

Inter-nesting interval

The recorded nesting success of identified turtles is exported as total_nesting_by_season_turtle.csv.

We show summary statistics per season split by species in the following three tables.

trt_internesting <- 
  w2_nesting_type_by_season_turtle %>% 
  dplyr::group_by(season, species) %>% 
  dplyr::summarise(
    n = dplyr::n(),

    nesting_mean = mean(nesting) %>% round(2),
    nesting_sd = sd(nesting) %>% round(2),
    nesting_max = max(nesting),
    nesting_min = min(nesting),

    non_nesting_mean = mean(non_nesting) %>% round(2),
    non_nesting_sd = sd(non_nesting) %>% round(2),
    non_nesting_max = max(non_nesting),
    non_nesting_min = min(non_nesting),

    unknown_nesting_mean = mean(unknown_nesting) %>% round(2),
    unknown_nesting_sd = sd(unknown_nesting) %>% round(2),
    unknown_nesting_max = max(unknown_nesting),
    unknown_nesting_min = min(unknown_nesting)
    ) %>% 
  dplyr::ungroup() %>% 
  dplyr::arrange(season, species)

Flatback turtles

trt_internesting %>% 
  dplyr::filter(species == "natator-depressus") %>% 
  dplyr::select(-species) %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Inter-nesting by season of Flatback turtles"
  ) %>% 
  gt::tab_header(
    title="Inter-nesting by season of Flatback turtles",
    subtitle="Counts of successful, unsuccessful, and unknown clutches per season"
  ) %>% 
  gt::tab_spanner(
    "Successful nesting", 
    columns = dplyr::contains("Nesting", ignore.case = FALSE)
  ) %>% 
  gt::tab_spanner(
    "Unsucessful nesting", 
    columns = dplyr::contains("Non nesting", ignore.case = FALSE)
  ) %>%  
  gt::tab_spanner(
    "Unknown nesting", 
    columns = dplyr::contains("Unknown nesting", ignore.case = FALSE)
  )

Hawksbill turtles

trt_internesting %>% 
  dplyr::filter(species == "eretmochelys-imbricata") %>% 
  dplyr::select(-species) %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Inter-nesting by season of Hawksbill turtles"
  ) %>% 
  gt::tab_header(
    title="Inter-nesting by season of Hawksbill turtles",
    subtitle="Counts of successful, unsuccessful, and unknown clutches per season"
  ) %>% 
  gt::tab_spanner(
    "Successful nesting", 
    columns = dplyr::contains("Nesting", ignore.case = FALSE)
  ) %>% 
  gt::tab_spanner(
    "Unsucessful nesting", 
    columns = dplyr::contains("Non nesting", ignore.case = FALSE)
  ) %>%  
  gt::tab_spanner(
    "Unknown nesting", 
    columns = dplyr::contains("Unknown nesting", ignore.case = FALSE)
  )

Green turtles

trt_internesting %>% 
  dplyr::filter(species == "chelonia-mydas") %>% 
  dplyr::select(-species) %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Inter-nesting by season of Green turtles"
  ) %>% 
  gt::tab_header(
    title="Inter-nesting by season of Green turtles",
    subtitle="Counts of successful, unsuccessful, and unknown clutches per season"
  ) %>% 
  gt::tab_spanner(
    "Successful nesting", 
    columns = dplyr::contains("Nesting", ignore.case = FALSE)
  ) %>% 
  gt::tab_spanner(
    "Unsucessful nesting", 
    columns = dplyr::contains("Non nesting", ignore.case = FALSE)
  ) %>%  
  gt::tab_spanner(
    "Unknown nesting", 
    columns = dplyr::contains("Unknown nesting", ignore.case = FALSE)
  )

Clutch frequency

The clutch frequency is calculated from the individual turtles' nesting frequencies (w2_nesting_type_by_season_turtle) as a tally of the frequency of the confirmed successful nesting events. This summary ignores unsuccessful and unknown nesting events.

We show the clutch frequencies per season split by species in the following three tables.

clutch_freq <- 
  w2_nesting_type_by_season_turtle %>% 
  dplyr::group_by(season, species, nesting) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  dplyr::arrange(season, species, -nesting) %>%
  dplyr::rename(confirmed_clutches=nesting, Season=season) %>% 
  tidyr::pivot_wider(
    names_from=confirmed_clutches, values_from=n, names_sort=TRUE
  )

Flatback turtles

clutch_freq %>% 
  dplyr::filter(species == "natator-depressus") %>% 
  dplyr::select(-species) %>% 
  # janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Clutch frequency of Flatback turtles"
  ) %>% 
  gt::tab_header(
    title="Clutch frequency of Flatback turtles"
    # subtitle=""
  ) %>% 
  gt::tab_spanner(
    "Number of successful clutches", 
    columns = !dplyr::contains("Season")
  )

Hawksbill turtles

clutch_freq %>% 
  dplyr::filter(species == "eretmochelys-imbricata") %>% 
  dplyr::select(-species) %>% 
  # janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Clutch frequency of Hawksbill turtles"
  ) %>% 
  gt::tab_header(
    title="Clutch frequency of Hawksbill turtles"
    # subtitle=""
  ) %>% 
  gt::tab_spanner(
    "Number of successful clutches", 
    columns = !dplyr::contains("Season")
  )

Green turtles

clutch_freq %>% 
  dplyr::filter(species == "chelonia-mydas") %>% 
  dplyr::select(-species) %>% 
  # janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Clutch frequency of Green turtles"
  ) %>% 
  gt::tab_header(
    title="Clutch frequency of Green turtles"
    # subtitle=""
  ) %>% 
  gt::tab_spanner(
    "Number of successful clutches", 
    columns = !dplyr::contains("Season")

      )

Morphometrics

Tagging data contains morphometric measurements "curved carapace length" (w2$ccl) and "curved carapace width" (w2$ccw). In error, some ccl measurements were entered as CCL NOTCH instead of CCL. Therefore, we use the raw database export here.

Separately for each metric (ccl, ccl notch, ccw), records with existing measurements are grouped by season and species, then summarised into number of records, mean, sd, min, and max. The SD is rounded to two decimals, mean, min, max are rounded to 0 decimals. Values are in millimetres.

The two summaries are exported as trt_morph_ccl/ccw.csv but shown below split by species and metric (ccl or ccw).

It is notable that the tagging database reports a very small number of CCL measurements for Flatbacks in the past three seasons (3-5 per seasons), whereas the far less often measured metric CCW is reported at numbers (175-318) comparable to previous seasons.

QA: Suspiciously small turtles

The following table lists measurements of CCL, CCL NOTCH, or CCW below 500mm. Some of these records could be missing a digit (e.g. trailing zero). We exclude measurements below 500 mm (50 cm) from the summary.

# morph_qa_html
trt_morph %>% 
  dplyr::left_join(species_lookup, by="species_code") %>% 
  dplyr::filter(measurement_type_code %in% c("CCL NOTCH", "CCL", "CCW"), 
                measurement_value < 500) %>% 
  dplyr::select(
    turtle_id, observation_id, measurement_type_code, measurement_value
  ) %>% 
  rt()

Flatback turtles

morph_stats_gt %>% 
  dplyr::filter(Species == "Natator depressus") %>% 
  gt::gt()

Hawksbill turtles

morph_stats_gt %>% 
  dplyr::filter(Species == "Eretmochelys imbricata") %>% 
  gt::gt()

Green turtles

morph_stats_gt %>% 
  dplyr::filter(Species == "Chelonia mydas") %>% 
  gt::gt()

Nesting success

We calculate the nesting success as the fraction of confirmed nests (turtles observed laying during tagging plus "successful crawl" turtle tracks with what is believed by the observer to be a nest) over the total number of nesting attempts (emergences).

As nesting success are counted:

As unknowns are counted:

As non nesting are counted:

Caveats

When calculating the nesting success as fraction of successful over total nesting, we calculate three versions of nesting success.

Uncertain track count records include cases where the presence of a nest is unsure or not checked for. The tagging records cater for a range of unknowns, which again could well be successfully completed clutches.

The data are biased (both ways) by the observers' individual ability to recognize a nest, and the opportunity to observe the clutch up to its successful end.

Nesting success measured during night tagging can be assumed to be lowered by the disruption through monitoring disturbance.

We see again the dip in numbers in the shortened 2020 survey.

This summary is exported as total_nesting_by_season_species.csv.

Flatback turtles

total_nesting_by_season_species %>%
  dplyr::filter(season>2016, species == "Natator depressus") %>% 
  total_nesting_gt("Flatback")

Hawksbill turtles

total_nesting_by_season_species %>%
  dplyr::filter(season>2016, species == "Eretmochelys imbricata") %>% 
  total_nesting_gt("Hawksbill")

Green turtles

total_nesting_by_season_species %>%
  dplyr::filter(season>2016, species == "Chelonia mydas") %>% 
  total_nesting_gt("Green")
sp <- "natator-depressus"
track_success(x$tracks) %>% ggplot_track_success_by_date(sp)
track_success(x$tracks) %>% ggplot_track_successrate_by_date(sp)
sp <- "chelonia-mydas"
track_success(x$tracks) %>% ggplot_track_success_by_date(sp)
track_success(x$tracks) %>% ggplot_track_successrate_by_date(sp)
sp <- "eretmochelys-imbricata"
track_success(x$tracks) %>% ggplot_track_success_by_date(sp)
track_success(x$tracks) %>% ggplot_track_successrate_by_date(sp)

Hatching and emergence success

WAStD nest excavation data on clutch size, hatching and emergence success are grouped by season and species, then summarised into mean, sd, min, max, and rounded to two decimals.

Nest excavations are exported as nest_excavations.csv. One record therein refers to one excavated nest.

x$nest_excavations %>% 
  dplyr::filter(emergence_success > 0, hatching_success > 0) %>% 
  wastdr::hatching_emergence_success() %>% 
  dplyr::arrange(season) %>% 
  dplyr::mutate(
    species=stringr::str_to_sentence(species) %>% stringr::str_replace("-"," ")
  ) %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Hatching and emergence success by season and species"
  ) %>% 
  gt::tab_header(
    title="Hatching and emergence success by season and species",
    subtitle="Values in percent, rounded to two decimals"
  ) %>% 
  gt::tab_spanner(
    label = "Clutch size",
    columns = c(gt::contains("clutch"))
  ) %>% 
  gt::tab_spanner(
    label = "Hatching success",
    columns = c(gt::contains("hatching"))
  ) %>% 
  gt::tab_spanner(
    label = "Emergence success",
    columns = c(gt::contains("emergence"))
  )

Connectivity

This section shows a confusion matrix of turtle numbers between places they were observed. These places include rookeries and foraging grounds.

As origin ("Initially observed at") we consider the place where a previously unmarked turtle was tagged for the first time. In the original data, this is coded as column location_code.

As destination ("Observed at") we consider the place a turtle was observed. In the original data, this is coded as column observed_location_code.

The data is saved as turtle_nesting_connectivity.csv.

Site register

To understand the site codes in the connectivity matrix, we show here the list of known sites.

The Code describes nesting beaches; e.g. DADI is all of Delambre Island. The Prefix describes contiguous localities (e.g. DA Dampier Archipelago), which can be turtles rookeries, and can contain one or several nesting beaches.

w2db$sites %>% 
  janitor::clean_names(case="sentence") %>% 
  rt()
w2db$sites %>%
  dplyr::rename(
    latitude = site_latitude,
    longitude = site_longitude
  ) %>% 
  leaflet::leaflet(width = 800, height = 600) %>%
    leaflet::addProviderTiles("Esri.WorldImagery", group = "Aerial") %>%
    leaflet::addProviderTiles("OpenStreetMap.Mapnik", group = "Place names") %>%
  leaflet::addMarkers(
     group = "Sites",
    label = ~ glue::glue("[{code}] {label}"), 
    popup = ~ glue::glue(
      "<h3>[{code}] {label}</h3>Rookery: {is_rookery}<br/>{description}"
    )
  ) %>%
    leaflet::addLayersControl(
      baseGroups = c("Aerial", "Place names"),
      overlayGroups = c("Sites"),
      options = leaflet::layersControlOptions(collapsed = FALSE)
    )

Flatback turtles

2020-21

trt_conn_loc %>% 
  filter_conn(2020, "natator-depressus") %>%
  gt_conn(2020, "Flatback")
trt_conn %>% 
  filter_conn(2020, "natator-depressus") %>% 
  gt_conn(2020, "Flatback")

2019-20

trt_conn %>% 
  filter_conn(2019, "natator-depressus") %>% 
  gt_conn(2019, "Flatback")

Hawksbill turtles

2020-21

trt_conn %>% 
  filter_conn(2020, "eretmochelys-imbricata") %>% 
  gt_conn(2020, "Hawksbill")

2019-20

trt_conn %>% 
  filter_conn(2019, "eretmochelys-imbricata") %>% 
  gt_conn(2019, "Hawksbill")

Green turtles

2020-21

trt_conn %>% 
  filter_conn(2020, "chelonia-mydas") %>% 
  gt_conn(2020, "Green")

2019-20

trt_conn %>% 
  filter_conn(2019, "chelonia-mydas") %>% 
  gt_conn(2019, "Green")

Pressures and Threats

Injuries of tagged turtles

Injuries are reported as part of the tagging process. The full list of injuries is exported as turtle_injuries.csv. Summaries by damage type and damaged body part are shown below.

Caveats

Injured turtles with flipper amputations are more likely to be encountered, as they nest slower.

The numbers shown as "Damaged turtles" are absolute numbers, the percentages shown as "Damage pct" are normalised over the number of encountered turtles per respective season ("Total turtles").

The tagging database holds no records of injuries seen in the 2009-10 season.

This summary is exported as trt_dmg_by_species_season.csv.

Flatback turtles

trt_dmg_by_species_season %>% 
  dplyr::filter(Species == "Natator depressus") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Most frequent injuries by season and species: Flatback turtles"
  ) %>% 
  gt::tab_header(
    title="Most frequent injuries by season and species: Flatback turtles",
    subtitle=""
  ) 

Hawksbill turtles

trt_dmg_by_species_season %>% 
  dplyr::filter(Species == "Eretmochelys imbricata") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Most frequent injuries by season and species: Hawksbill turtles"
  ) %>% 
  gt::tab_header(
    title="Most frequent injuries by season and species: Hawksbill turtles",
    subtitle=""
  ) 

Green turtles

trt_dmg_by_species_season %>% 
  dplyr::filter(Species == "Chelonia mydas") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Most frequent injuries by season and species: Green turtles"
  ) %>% 
  gt::tab_header(
    title="Most frequent injuries by season and species: Green turtles",
    subtitle=""
  ) 

Injury type over time

To inspect the relative abundance of each injury type per species, we show injury abundances (as fraction of turtles seen with that damage over total number of processed turtles in that season) over time.

This summary is exported as trt_dmg_type_pivot.csv.

trt_dmg_type_pivot %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Injuries by species over time"
  ) %>% 
  gt::tab_header(
    title="Injuries by species over time",
    subtitle="Percent of processed turtles with given damage"
  ) 
library(ggplot2)
library(scales)
plt <- ggplot(
  data=trt_dmg_by_species_season,
  mapping=aes(x=Season, y=`Damage pct`, colour=Damage)) +
  geom_line() +
  scale_x_continuous(breaks=pretty_breaks()) +
  labs(
    title="Damage over time",
    subtitle = "Fraction of processed turtles with given damage") +
  theme_minimal() +
  facet_wrap("Species", ncol=1)

ggplot2::ggsave(
      plot = plt,
      filename = "dmg_timeseries.png",
      path = fn,
      width = 10,
      height = 8
    )
plt
# TODO make relative numbers - use total_turtles of all times
trt_dmg %>% 
  dplyr::group_by(species, body_part) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(n>1) %>%
  dplyr::arrange(species, -n) %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Most frequent injured body parts by species (all seasons)"
  ) %>% 
  gt::tab_header(
    title="Most frequent injured body parts by species (all seasons)",
    subtitle="Injuries occurring more than once"
  ) 

Injured body parts over time

To inspect the relative abundance of each injured body part per species, we show injury abundances (as fraction of turtles seen with damage to that body part over total number of processed turtles in that season) over time.

This summary is exported as trt_dmg_bodypart_pivot.csv.

trt_dmg_bodypart_pivot %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Injuries to body parts by species over time"
  ) %>% 
  gt::tab_header(
    title="Injuries to body parts by species over time",
    subtitle="Percent of processed turtles with given damaged body part"
  )  
# TODO normalise
trt_dmg %>% 
  dplyr::group_by(species, body_part, damage) %>% 
  dplyr::tally() %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(n>2) %>%
  dplyr::arrange(species, -n) %>% 
  janitor::clean_names(case="sentence") %>% 
  gt::gt(
    groupname_col=NULL,
    caption = "Most frequent injuries by species (all seasons)"
  ) %>% 
  gt::tab_header(
    title="Most frequent injuries by species (all seasons)",
    subtitle="Injuries occurring more than twice"
  ) 

Predation and Disturbance

Predation and disturbance is recorded through WAStD as either general disturbance and stand-alone signs of predator presence, or as disturbed or predated nests.

Each distinct cause of predation or disturbance is recorded separately.

Disturbance and nest predation

Nest and general disturbances are exported as nest_dist.csv. One record therein refers to one distinct disturbance or predation of one nest or one distinct sign of general disturbance or predator presence.

x$nest_dist %>% 
  wastdr::disturbance_by_season() %>% 
  dplyr::arrange(season, encounter_type, -n) %>% 
  janitor::clean_names(case = "sentence") %>% 
  rt()

General disturbance

Data is shown for all seasons.

x$nest_dist %>% 
  dplyr::filter(encounter_encounter_type == "other") %>% 
  wastdr::map_dist()

Disturbed nests

Data is shown for all seasons.

x$nest_dist %>% 
  dplyr::filter(encounter_encounter_type == "nest") %>% 
  wastdr::map_dist()


dbca-wa/etlTurtleNesting documentation built on Nov. 18, 2022, 8:03 a.m.