# Code to read and combine segment count files from the
# Waterfowl Breeding Population & Habitat Survey

# 31-Aug-2022 
# Author: emily_silverman@fws.gov

# Segment data are found at: https://ecos.fws.gov/ServCat/Reference/Profile/142670

library(tidyverse)

# Set Path for accessing CSV files from your computer:

Path <- "X:\\MyDataLocation\\"

inFile1 <- "wbphs_segment_counts_forDistribution.csv"
inFile2 <- "wbphs_segment_effort_forDistribution.csv"
inFile3 <- "wbphs_segment_counts_1955to1999_forDistribution.csv"

# Step 1: Read in files: ####

segCounts_recent <- read.csv(paste0(Path,inFile1), as.is = T, na.strings = c("NULL"))
segEffort <- read.csv(paste0(Path,inFile2), as.is = T, na.strings = c("NULL"))
segCounts_historical <- read.csv(paste0(Path,inFile3), as.is = T, na.strings = c("NULL"))

# Step 2: Expand effort x species to add 0 segments: ####

# NOTE: the resulting frame includes survey_species in stratum
# where they are never observed
# User may subset surveySPP to species of interest.

# Unique survey species, less ponds
surveySPP <- setdiff(unique(segCounts_recent$survey_species), "POND")

# segment location and version information is not retained,
# not present in segCounts_historical
segCounts <- segEffort %>% 
  expand(., nesting(survey_year, survey_month, survey_day,stratum, transect, segment), 
         survey_species = surveySPP)

# Add ponds if desired:
segCounts <- bind_rows(segCounts,
              segEffort %>% 
                select(survey_year, survey_month, survey_day,
                       stratum, transect, segment) %>%
                filter(stratum %in% c(26:49,75,76)) %>% 
                mutate(survey_species = "POND"))

# Step 3: Merge with counts, replace NA with 0: ####

# large_group dropped for merging with segCounts_historical
# fill in 0s, NOTE: AMCO, SWAN, POND are coded NA for drake and pair fields
segCounts <- left_join(segCounts, segCounts_recent %>% 
                         select(-large_group)) %>%
              mutate(drake = ifelse(!(survey_species %in% c("AMCO","SWAN", "POND"))
                                    & is.na(drake),
                                    0, drake),
                     pair = ifelse(!(survey_species %in% c("AMCO","SWAN", "POND"))
                                    & is.na(pair),
                                    0, pair),
                     group = ifelse(is.na(group), 0, group)
              )

# Step 4: Join with historical (pre-2000) segment counts: ####

segCounts <- bind_rows(segCounts_historical, segCounts)
