######################################################################################################

## Graydon McKee
## 2017/18
## Converting Raw GLATOS detections for Black Bay into MARK input format

######################################################################################################

## Add data from GLATOS and rename to BBWAT (GLATOS -> BBWAT Project -> Date range: 2016-05-29 to 2017-11-07)

BBWAT<-BBWAT_detectionsWithLocs_20171201_222434

##Table of fish captures

capture <- unique(BBWAT[,c('animal_id', 'utc_release_date_time', 'release_latitude', 'release_longitude', 'release_location')])

head(capture)

##Table of detections on acoustic receivers

detect <- BBWAT[,c('animal_id', 'detection_timestamp_utc', 'deploy_long', 'deploy_lat','glatos_array')]

head(detect)

##Match capture column names to detection column names

names(capture)[2:5] <- c('detection_timestamp_utc', 'deploy_lat', 'deploy_long', 'glatos_array')

head(capture)

##Combine captue and detection tables (Capture-Mark-Recapture)

CMR<-rbind(detect,capture)

##Retain only detections from receivers used in 2016 and 2017

CMR<-CMR[ ! CMR$glatos_array %in% c('Black Sturgeon River - Above Camp 43','SQB','SAW','Hurkett Cove',
                                       'Pearl Harbour','COP','NOR','HUC','Delany Island','PER','Squaw Bay','CAP'), ]

##Determine the date range of the data
rng <- trunc(range(CMR$detection_timestamp_utc), units='days')

# Create sequence of times at monthly (2592000 seconds)/bi-weekly (1296000 seconds) intervals within the date range of the data.
tSeq <- seq(rng[1], rng[2], 2592000)

# Bin data by time interval and add new column to 'CMR'.
bin <- findInterval(CMR$detection_timestamp_utc, tSeq)
CMR$bin <- tSeq[bin]

## Bin by day

rng <- trunc(range(CMR$detection_timestamp_utc), units='days')

# Create sequence of times at daily intervals.
tSeq.day <- seq(rng[1], rng[2], 86400)

# Bin data by time interval and add new column to 'CMR'.
bin.day <- findInterval(CMR$detection_timestamp_utc, tSeq.day)
CMR$bin.day <- tSeq.day[bin.day]

# Check 'CMR'.
head(CMR)

## Identify individual fish

fish <- unique(CMR$animal_id)

# Create a data frame with all combinations of animal and time bins.
allBins <- expand.grid(animal_id=fish, bin=tSeq)

## Merge with CMR to identify on what gates each fish was detected each day (or if they weren't detected)

CMR <- merge(CMR, allBins, by.x=c('animal_id', 'bin'), all=TRUE)

head(CMR)

##Write to .csv

write.csv(CMR, file = "Monthly_Occasions.csv",row.names=FALSE)

##Remove detection timestamp, lat, snd long columns

CMR$detection_timestamp_utc<-NULL
CMR$deploy_long<-NULL
CMR$deploy_lat<-NULL

head(CMR)

##Remove repeated rows (Remove fish being detected over and over again in the same spot within a tight timeframe)

CMR<-unique(CMR)

head(CMR)

write.csv(CMR, file = "Monthly_Occasions_Bin.csv",row.names=FALSE)




######################################

## Changing names in case there is an error so I don't have to start over

States<-CMR

##Concatenate animal_id, bin, glatos_array

States$Concat<-paste(States$animal_id,States$bin,States$glatos_array,sep="|")

##Number of detections of each fish at each receiver per time bin

X<-table(States$Concat)
head(X)

##Make table of counts a data frame

Y<-data.frame(X)
head(Y)

##Remove duplicate Concats from States

States = States[!duplicated(States$Concat),]
head(States)

##Combine States and Y into binned capture frequency

names(Y)<-c("Concat","Freq")

CapFreq<-merge(States,Y,by="Concat")


##Assign numbers to gates

unique(CapFreq$glatos_array)

index<-c(NA,'BSR','WOR','BEI','GEP','EDI','SIP','BLP')

values<-c(NA,0,0,1,2,3,4,4)

CapFreq$Gate<-values[match(CapFreq$glatos_array,index)]

##Weighted gate value based on gate number and frequency

CapFreq$WGate<-(CapFreq$Freq*CapFreq$Gate)

##Change CapFreq to new object in case of error

Cap1<-CapFreq

##Concatenate animal_id and bin

Cap1$AnBin<-paste(Cap1$animal_id,Cap1$bin,sep="|")

##Add WeightAvg column with NA

Cap1["WeightAvg"]<-NA

##Sum gates and frequency for each fish in each bin

WGate.split <- split(Cap1$WGate, Cap1$AnBin)
head(WGate.split)

SumGate <- sapply(WGate.split, sum)
head(SumGate)

Freq.split<-split(Cap1$Freq,Cap1$AnBin)
head(Freq.split)

SumFreq<-sapply(Freq.split,sum)
head(SumFreq)

##Create new table with sum gates (sum of gate*frequency detected at gate) and sum frequency (sum of frequency detected within bin)

Cap2<-data.frame(SumFreq,SumGate)
head(Cap2)

##Calculate weighted average

Cap2$W.Avg<-Cap2$SumGate/Cap2$SumFreq

## Change name in case of error

Cap3<-Cap2

##Add animal_id and bin concat

Cap3$FishBin<-rownames(Cap3)

##Split animal_id and bin 

FishBin.Split<-strsplit(Cap3$FishBin, "|", fixed = TRUE)
head(FishBin.Split)

str(FishBin.Split)

##subset lists 

Fish<-sapply(FishBin.Split, function(x){x[[1]]})
Bin<-sapply(FishBin.Split, function(x){x[[2]]})

##Add to Cap3

Cap3$Fish.No<-Fish
Cap3$Bin<-Bin

##Turn weighted averages for states in lettered states (0 for NA)

W.Avg<-Cap3$W.Avg

head(W.Avg)

T<-ifelse(W.Avg<=1,"A",
          ifelse(W.Avg>1 & W.Avg<=2,"B",
                 ifelse(W.Avg>2 & W.Avg<=3,"C",
                        ifelse(W.Avg>3 & W.Avg<=4,"D",
                               ifelse(is.na(W.Avg),"0","9")))))
head(T)

T[is.na(T)] <- 0

##Add lettered states to table

Cap3$States<-T

## Change name in case of error

MARK1<-Cap3

##Write to .csv 

write.csv(MARK1,file="MARK1.csv")

## Isolate for Fish, Bin, and State columns

Mark2<-data.frame(MARK1$Fish.No,MARK1$Bin,MARK1$States)

head(Mark2)

names(Mark2)<-c("Fish","Bin","State")

head(Mark2)

Mark3<-reshape(Mark2, idvar="Fish", timevar="Bin", direction="wide")

## Create encounter history coloumn

## Monthly Bin

Mark3$History<-paste(Mark3[,2],Mark3[,3],Mark3[,4],Mark3[,5],Mark3[,6],Mark3[,7],Mark3[,8],Mark3[,9],Mark3[,10],
                     Mark3[,11],Mark3[,12],Mark3[,13],Mark3[,14],Mark3[,15],Mark3[,16],Mark3[,17],Mark3[,18],
                     Mark3[,19])

## Bi-weekly bin

###Mark3$History<-paste(Mark3[,2],Mark3[,3],Mark3[,4],Mark3[,5],Mark3[,6],Mark3[,7],Mark3[,8],Mark3[,9],Mark3[,10],
#                     Mark3[,11],Mark3[,12],Mark3[,13],Mark3[,14],Mark3[,15],Mark3[,16],Mark3[,17],Mark3[,18],
 #                    Mark3[,19],Mark3[,20],Mark3[,21],Mark3[,22],Mark3[,23],Mark3[,24],Mark3[,25],Mark3[,26],Mark3[,27]
  #                   ,Mark3[,28],Mark3[,29],Mark3[,30],Mark3[,31],Mark3[,32],Mark3[,33],Mark3[,34],Mark3[,35],Mark3[,36]
   #                  ,Mark3[,37],Mark3[,38],Mark3[,39])
                    

##Encounter History table

EncHist<-data.frame(Mark3$Fish,Mark3$History)

names(EncHist)<-c("Fish","History")

##Write to .csv for mark

write.csv(EncHist,file="Walleye_Encounter_History_Bi-weekly.csv")


#################################

