Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Francomano_et_al_2023_PAM_for_Penguins/Camera_Trap_Photo_Selection_Script.R
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
92 lines (79 sloc)
6.19 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###################################################################### | |
# Code from Francomano et al. (IBIS 2023) # | |
###################################################################### | |
# Camera trap photo selection script # | |
###################################################################### | |
# Created by: Dante Francomano (dfrancomano@alumni.purdue.edu) | |
# Date: 20201007 | |
###################################################################### | |
###################################################################### | |
# Load packages | |
library(openxlsx) | |
library(suncalc) | |
library(dplyr) | |
# Read in site coordinates and date range information and condense to pertinent information | |
siteCoordinates<-read.csv("/Users/dantefrancomano/Documents/Purdue/Dissertation/Pingüinos/Site_Coordinates.csv") | |
siteCoordinates[1:7,1]<-sub(" ","_",sub("Isla de los Estados ","",siteCoordinates[1:7,1])) | |
siteCoordinates[12:14,1]<-sub(" ","_",sub("Isla Martillo ","",siteCoordinates[12:14,1])) | |
siteCoordinates[14,1]<-"Zona_Turística" | |
siteCoordinates<-siteCoordinates[-(8:11),] | |
dateRangeInfo<-read.xlsx("/Users/dantefrancomano/Documents/Purdue/Dissertation/Pingüinos/Reproductive_Stage_Data_Coverage_Comparison.xlsx") | |
limitedDateRangeInfo<-dateRangeInfo[which(dateRangeInfo$Use==1),1:2] | |
rownames(limitedDateRangeInfo)<-NULL | |
# Determine sunrise and sunset times for each date-site combination | |
sunriseTimes<-cbind(limitedDateRangeInfo,matrix(NA,dim(limitedDateRangeInfo)[1],dim(siteCoordinates)[1],dimnames=list(NULL,siteCoordinates$Site))) | |
sunsetTimes<-sunriseTimes | |
for(i in 1:dim(sunriseTimes)[1]) for(j in 1:dim(siteCoordinates)[1]) sunriseTimes[i,(j+2)]<-getSunlightTimes(date=as.Date(as.character(sunriseTimes[i,1]),"%Y%m%d"),lat=siteCoordinates[j,2],lon=siteCoordinates[j,3],tz="America/Argentina/Ushuaia",keep=c("sunrise"))$sunrise | |
for(i in 1:dim(sunsetTimes)[1]) for(j in 1:dim(siteCoordinates)[1]) sunsetTimes[i,(j+2)]<-getSunlightTimes(date=as.Date(as.character(sunsetTimes[i,1]),"%Y%m%d"),lat=siteCoordinates[j,2],lon=siteCoordinates[j,3],tz="America/Argentina/Ushuaia",keep=c("sunset"))$sunset | |
# Generate lists of possible on-hour times (limiting sampling to 07:00:00 to 19:00:00 during the first two breeding stages due to limited sampling; | |
# treat sampling of Zona Nueva at 12:00:00 as missing data) | |
possibleOnHourTimes<-list() | |
possibleOnHourTimes[siteCoordinates[,1]]<-list(NULL) | |
for(i in 1:length(siteCoordinates[,1])) possibleOnHourTimes[[i]]<-sapply(as.character(limitedDateRangeInfo[,1]),function(x) NULL) | |
for(i in 1:length(possibleOnHourTimes)) | |
{ | |
for (j in 1:dim(sunriseTimes)[1]) | |
{ | |
posixltSunrise<-as.POSIXlt(sunriseTimes[j,(i+2)],origin="1970-01-01",tz="America/Argentina/Ushuaia") | |
posixltSunset<-as.POSIXlt(sunsetTimes[j,(i+2)],origin="1970-01-01",tz="America/Argentina/Ushuaia") | |
firstPossibleHour<-ifelse(posixltSunrise$min>0|posixltSunrise$sec>0,posixltSunrise$hour+1,posixltSunrise) | |
lastPossibleHour<-posixltSunset$hour | |
if(j<14&firstPossibleHour<7) firstPossibleHour<-7 | |
if(j<14&lastPossibleHour>19) lastPossibleHour<-19 | |
possibleHours<-firstPossibleHour:lastPossibleHour | |
possibleHours<-sapply(possibleHours,function(x) ifelse(nchar(x)==1,paste(0,x,sep=""),x)) | |
possibleOnHourTimes[[i]][[j]]<-possibleHours | |
} | |
} | |
# Randomly sample 4 possible times for each site-day combination and determine proportion of daylight hours at which they occur | |
set.seed(3) | |
sampledTimes<-list() | |
sampledTimes[siteCoordinates[,1]]<-list(NULL) | |
for(i in 1:length(possibleOnHourTimes)) sampledTimes[[i]]<-sapply(possibleOnHourTimes[[i]],sample,4) | |
dayLengthsHours<-sunriseTimes | |
dayLengthsHours[,-(1:2)]<-(sunsetTimes[,-(1:2)]-sunriseTimes[,-(1:2)])/60/60 | |
sampledTimeDaylightProportions<-list() | |
for(i in 1:length(sampledTimes)) sampledTimeDaylightProportions[[siteCoordinates[i,1]]]<-sapply(possibleOnHourTimes[[i]],function(x) rep(NA,4)) | |
for(i in 1:length(sampledTimes)) for(j in 1:dim(sunriseTimes)[1]) sampledTimeDaylightProportions[[i]][,j]<-(as.numeric(sampledTimes[[i]][,j])-difftime(as.POSIXlt(sunriseTimes[j,(i+2)],origin="1970-01-01",tz="America/Argentina/Ushuaia"),trunc(as.POSIXlt(sunriseTimes[j,(i+2)],origin="1970-01-01",tz="America/Argentina/Ushuaia"),"days")))/dayLengthsHours[j,(i+2)] | |
# Generate hypothetical file names for each sample | |
sourcePathRootStem<-"/Users/dantefrancomano/Documents/Purdue/Dissertation/Pingüinos/Datos_de_Cámaras_Trampa/Relevant_Photos_SM_Style_Names" | |
sourcePathStems<-paste(sourcePathRootStem,names(sampledTimes),sep="/") | |
sampleSourcePaths<-list() | |
sampleSourcePaths[siteCoordinates[,1]]<-list(NULL) | |
for(i in 1:length(sampleSourcePaths)) for(j in 1:dim(sunriseTimes)[1]) sampleSourcePaths[[i]][[j]]<-paste(sourcePathStems[i],"/",siteCoordinates[i,1],"_",sunriseTimes[j,1],"_",sampledTimes[[i]][,j],"0000.JPG",sep="") | |
# Check file existence and copy sampled files to new directories | |
fileExistenceCheck<-sampleSourcePaths | |
for(i in 1:length(sampleSourcePaths)) for(j in 1:dim(sunriseTimes)[1]) fileExistenceCheck[[i]][[j]]<-file.exists(sampleSourcePaths[[i]][[j]]) | |
sampleSourcePaths<-unlist(sampleSourcePaths) | |
sampleDestinationPaths<-sub("Relevant_Photos_SM_Style_Names","Stratified_Random_Sample",sampleSourcePaths) | |
for(i in 1:length(sampleSourcePaths)) if(file.exists(sampleSourcePaths[i])) file.copy(sampleSourcePaths[i],sampleDestinationPaths[i]) | |
# Write out data forms to complete | |
fullDataForm<-data.frame(Site=rep(siteCoordinates[,1],each=dim(sunriseTimes)[1]*4),Date=rep(sunriseTimes[,1],each=4,times=length(sampledTimes)),Breeding_Stage=rep(sunriseTimes[,2],each=4,times=length(sampledTimes)),Time=paste(unlist(sampledTimes),"0000",sep=""),Daylight_Proportion=unlist(sampledTimeDaylightProportions),Count_1="",Count_2="",Count_4="") | |
extantDataForm<-fullDataForm[which(unlist(fileExistenceCheck)==TRUE),] | |
extantDataForm<-arrange(extantDataForm,Site,Date,Time) | |
dataFormPPA<-extantDataForm[grep("PPA",extantDataForm$Site),-(7:8)] | |
rownames(dataFormPPA)<-NULL | |
dataFormPM<-extantDataForm[-grep("PPA",extantDataForm$Site),-6] | |
rownames(dataFormPM)<-NULL | |
write.csv(dataFormPPA,"/Users/dantefrancomano/Documents/Purdue/Dissertation/Pingüinos/Datos_de_Cámaras_Trampa/PPA_Data.csv") | |
write.csv(dataFormPM,"/Users/dantefrancomano/Documents/Purdue/Dissertation/Pingüinos/Datos_de_Cámaras_Trampa/PM_Data.csv") |