###############################################################################################################

## Graydon McKee
## 2018
## TOHA

###############################################################################################################

rm(list = ls())

###############################################################################################################

## Optical Data

###############################################################################################################

## Rename

Light.Data<-Light_2017

Light.Data$'Date-Time (mm/dd/yyyy HH:MM:SS)'<-paste(Light.Data$Date,Light.Data$Time,sep=' ')

## Deleat empty rows if blanks exist

## Light.Data<-Light.Data[-(seq(1, length(Light.Data$`Date-Time (mm/dd/yyyy HH:MM:SS)`), 2)),]

## Time ***time zone doesn't matter as long as the light matches the correct time

class(Light.Data$`Date-Time (mm/dd/yyyy HH:MM:SS)`)

Light.Data$`Date-Time (mm/dd/yyyy HH:MM:SS)`<-as.POSIXct(Light.Data$`Date-Time (mm/dd/yyyy HH:MM:SS)`,tz='GMT', 
                                                         format='%m/%d/%Y %H:%M:%S')

## Remove columns

Light.Data$Date<-NULL
Light.Data$Time<-NULL
Light.Data$`Solar Rad.Slr_KJ_Tot@min60`<-NULL
Light.Data$`Solar Rad.Slr_KJ_Tot@min60_1`<-NULL
Light.Data$`Solar Rad.Slr_KJ_Tot@min60_2`<-NULL
Light.Data$`Solar Rad.Slr_W_Avg@min60`<-NULL
Light.Data$`Solar Rad.Slr_W_Avg@min60_1`<-NULL
Light.Data$`Solar Rad.Slr_W_Avg@min60_2`<-NULL

## Rename columns

names(Light.Data)<-c('Avg.Rad.KJ/m2','Avg.Rad.W/m2','Date-Time')

## Remove winter months (Jan-May 15, Oct 15-Dec)

Date<-as.POSIXct('2017-05-15',tz='GMT',format='%Y-%m-%d')

as.numeric(Date)

Date2<-as.POSIXct('2017-10-15',tz='GMT',format='%Y-%m-%d')

as.numeric(Date2)

Light.Data$Season<-ifelse(as.numeric(Light.Data$`Date-Time`)>=1494806400&
                            as.numeric(Light.Data$`Date-Time`)<=1508025600,'S','W')

Light.Data<-subset(Light.Data,Season=='S')

## Remove Season column

Light.Data$Season<-NULL

## Watts/m2 to lux (for 555nm as green and blue penetrate deepest-555nm is green)

Light.Data$Avg.Rad.lux<-(Light.Data$`Avg.Rad.W/m2`)/(1.46412884333821*10^-3)

## Remove night hours

Light.Data$DT<-as.character(Light.Data$`Date-Time`)
class(Light.Data$DT)

T_split<-strsplit(Light.Data$DT, ' ')
head(T_split)

Time<-sapply(T_split, function(x){x[[2]]})

Light.Data$Time<-Time

Time_split<-strsplit(Light.Data$Time,':')
head(Time_split)

Hour<-sapply(Time_split, function(x){x[[1]]})
head(Hour)

Light.Data$Hour<-as.numeric(Hour)             

Light.Data$Day<-ifelse(Light.Data$Hour>=5&Light.Data$Hour<=22, 'Y','N')

Light.Data<-subset(Light.Data,Day=='Y')

## Plot light level vs hour with every month showing a different colour

plot(Light.Data$Avg.Rad.lux~Light.Data$Hour)

## Remove useless columns

Light.Data$DT<-NULL
Light.Data$Time<-NULL

## Month column (half way through one month to half way through the next to match time bins from mark recapture)

MJ<-as.POSIXct('2017-10-15',tz='GMT',format='%Y-%m-%d')

as.numeric(MJ)

Light.Data$Month<-ifelse(as.numeric(Light.Data$`Date-Time`)<=1497484800, 'MJ',
                         ifelse(as.numeric(Light.Data$`Date-Time`)>1497484800 & as.numeric(Light.Data$`Date-Time`)<=1500076800, 'JJ',
                                ifelse(as.numeric(Light.Data$`Date-Time`)>1500076800 & as.numeric(Light.Data$`Date-Time`)<=1502755200, 'JA',
                                       ifelse(as.numeric(Light.Data$`Date-Time`)>1502755200 & as.numeric(Light.Data$`Date-Time`)<=1505433600, 'AS',
                                              ifelse(as.numeric(Light.Data$`Date-Time`)>1505433600 & as.numeric(Light.Data$`Date-Time`)<=1508025600, 'SO','9')))))

MJ<-subset(Light.Data,Month=='MJ')
JJ<-subset(Light.Data,Month=='JJ')
JA<-subset(Light.Data,Month=='JA')
AS<-subset(Light.Data,Month=='AS')
SO<-subset(Light.Data,Month=='SO')

plot(MJ$Avg.Rad.lux~MJ$Hour,col=1)
points(JJ$Avg.Rad.lux~JJ$Hour,col=2)
points(JA$Avg.Rad.lux~JA$Hour,col=3)
points(AS$Avg.Rad.lux~AS$Hour,col=4)
points(SO$Avg.Rad.lux~SO$Hour,col=5)

par(mfrow=c(2,3))

plot(MJ$Avg.Rad.lux~MJ$Hour,col=1,ylim=c(0,600000))
plot(JJ$Avg.Rad.lux~JJ$Hour,col=2,ylim=c(0,600000))
plot(JA$Avg.Rad.lux~JA$Hour,col=3,ylim=c(0,600000))
plot(AS$Avg.Rad.lux~AS$Hour,col=4,ylim=c(0,600000))
plot(SO$Avg.Rad.lux~SO$Hour,col=5,ylim=c(0,600000))

## Monthly daylight hours

MJ$Day<-ifelse(MJ$Hour>=5&MJ$Hour<=21, 'Y','N')

MJ<-subset(MJ,Day=='Y')

JJ$Day<-ifelse(JJ$Hour>=5&JJ$Hour<=21, 'Y','N')

JJ<-subset(JJ,Day=='Y')

JA$Day<-ifelse(JA$Hour>=6&JA$Hour<=21, 'Y','N')

JA<-subset(JA,Day=='Y')

AS$Day<-ifelse(AS$Hour>=6&AS$Hour<=20, 'Y','N')

AS<-subset(AS,Day=='Y')

SO$Day<-ifelse(SO$Hour>=7&SO$Hour<=19, 'Y','N')

SO<-subset(SO,Day=='Y')

## Bi-weekly periods around secchi measurments

## MJ

##Date<-as.POSIXct('2016-06-08',tz='GMT',format='%Y-%m-%d')

##as.numeric(Date)

##MJ$Period<-ifelse(as.numeric(MJ$`Date-Time`)>=1464134400&
                            ##as.numeric(MJ$`Date-Time`)<=1465344000,'Y','N')

##MJ<-subset(MJ,Period=='Y')

## JJ

##Date<-as.POSIXct('2016-07-08',tz='GMT',format='%Y-%m-%d')

##as.numeric(Date)

##JJ$Period<-ifelse(as.numeric(JJ$`Date-Time`)>=1466726400&
                    ##as.numeric(JJ$`Date-Time`)<=1467936000,'Y','N')

##JJ<-subset(JJ,Period=='Y')

##JA

##Date<-as.POSIXct('2016-08-08',tz='GMT',format='%Y-%m-%d')

##as.numeric(Date)

##JA$Period<-ifelse(as.numeric(JA$`Date-Time`)>=1469404800&
                    ##as.numeric(JA$`Date-Time`)<=1470614400,'Y','N')

##JA<-subset(JA,Period=='Y')

## AS

##Date<-as.POSIXct('2016-09-07',tz='GMT',format='%Y-%m-%d')

##as.numeric(Date)

##AS$Period<-ifelse(as.numeric(AS$`Date-Time`)>=1472083200&
                    ##as.numeric(AS$`Date-Time`)<=1473206400,'Y','N')

##AS<-subset(AS,Period=='Y')

## SO

##Date<-as.POSIXct('2016-10-08',tz='GMT',format='%Y-%m-%d')

##as.numeric(Date)

##SO$Period<-ifelse(as.numeric(SO$`Date-Time`)>=1474675200&
                    ##as.numeric(SO$`Date-Time`)<=1475884800,'Y','N')

##SO<-subset(SO,Period=='Y')

## Calculate average surface illuminance by hour and add month indentifier

## MJ

Io.MJ.Hr<-tapply(MJ$Avg.Rad.lux,MJ$Hour,mean)
Io.MJ.Hr

Io.MJ.Hr<-data.frame(Hour=names(Io.MJ.Hr), Io=Io.MJ.Hr)
Io.MJ.Hr

Io.MJ.Hr$Period<-rep('MJ', each=17)

## JJ

Io.JJ.Hr<-tapply(JJ$Avg.Rad.lux,JJ$Hour,mean)
Io.JJ.Hr

Io.JJ.Hr<-data.frame(Hour=names(Io.JJ.Hr), Io=Io.JJ.Hr)
Io.JJ.Hr

Io.JJ.Hr$Period<-rep('JJ', each=17)

## JA

Io.JA.Hr<-tapply(JA$Avg.Rad.lux,JA$Hour,mean)
Io.JA.Hr

Io.JA.Hr<-data.frame(Hour=names(Io.JA.Hr), Io=Io.JA.Hr)
Io.JA.Hr

Io.JA.Hr$Period<-rep('JA', each=16)

## AS

Io.AS.Hr<-tapply(AS$Avg.Rad.lux,AS$Hour,mean)
Io.AS.Hr

Io.AS.Hr<-data.frame(Hour=names(Io.AS.Hr), Io=Io.AS.Hr)
Io.AS.Hr

Io.AS.Hr$Period<-rep('AS', each=15)

## SO

Io.SO.Hr<-tapply(SO$Avg.Rad.lux,SO$Hour,mean)
Io.JJ.Hr

Io.SO.Hr<-data.frame(Hour=names(Io.SO.Hr), Io=Io.SO.Hr)
Io.SO.Hr

Io.SO.Hr$Period<-rep('SO', each=13)

## Recombine tables

OHD<-rbind(Io.MJ.Hr,Io.JJ.Hr,Io.JA.Hr,Io.AS.Hr,Io.SO.Hr)

##Rename Columns

names(OHD)<-c('Hour','Io','Month')

## Combine OHD and SecchiR

OHD<-merge(OHD,SecchiR, by='Month')

## Shallow and deep optimum optical habitat for each hour (k=2.1)

OHD$zdeep<-(-(OHD$`Secchi depth (m)`)/2.1)*log(8/OHD$Io)

OHD$zshallow<-(-(OHD$`Secchi depth (m)`)/2.1)*log(68/OHD$Io)

## Use Raster datasets to calculate hourly OHA for each section of Black Bay

## Combine Rasters (import raster tables)

Raster<-rbind(North_Raster,GI_Raster,Pearl_Raster,Squaw_Raster,EI_Raster)

## Rename columns

names(Raster)<-c('Location','Depth','Cells')

## Convert depth and cells to numeric

Raster$Depth<-as.numeric(Raster$Depth)

Raster$Cells<-as.numeric(Raster$Cells)

## Combine raster and OHD

OHA<-merge(OHD,Raster, by='Location')

####################################################################################################

## Find optimal thermal habitat depths here and determine Y or N if depths fall in both optimal optical and temperature zones

## Thermal Data

####################################################################################################

## Import all temp data files - There are some formatting errors out of Hoboware in each file that require a correction below

####################################################################################################

## Temp string 5 (state D)

####################################################################################################

## Fill Logger and Depth columns for each temp logger on the string

X5_50$Logger<-rep(5,each=length(X5_50$Log))

X5_50$Depth<-rep(50,each=length(X5_50$Log))

## Remove useless columns for each temp table

X5_50[,6:10]<- list(NULL)

names(X5_7)<-c('Logger','Depth','Log','Date Time','Temp')

## Combine Temp tables

Temp5<-rbind(X5_2,X5_3,X5_4,X5_5,X5_6,X5_7,X5_8,X5_9,X5_10,X5_11,X5_12,X5_13,X5_14,X5_15,X5_16,X5_17,X5_18,X5_19,
             X5_20,X5_25,X5_30,X5_35,X5_40,X5_50)

## Fix messed up dates

## Separate good and bad formatting

Temp5.time<- Temp5[grep("/", Temp5$`Date Time`), ]

Temp5.format<- Temp5[grep("-", Temp5$`Date Time`), ]

## Split bad formatting for corrections

DT.Split<-strsplit(Temp5.format$`Date Time`,'-')
head(DT.Split)

DT.table<-data.frame(Month=(sapply(DT.Split,function(x){x[[1]]})),Day=(sapply(DT.Split,function(x){x[[2]]})),
                            Year.Time=(sapply(DT.Split,function(x){x[[3]]})))

## correct month

unique(DT.table$Month)

DT.table$Month<-ifelse(DT.table$Month==2006,'06',
                       ifelse(DT.table$Month==2007,'07',
                        ifelse(DT.table$Month==2008,'08',
                         ifelse(DT.table$Month==2009,'09',
                          ifelse(DT.table$Month==2010,'10',
                           ifelse(DT.table$Month==2011,'11','N'))))))
## Recombine columns

Date<-paste(DT.table$Month,DT.table$Day,DT.table$Year.Time,sep="/")
head(Date)

## Convert Date object to time

Date<-as.POSIXct(Date,tz='GMT', format='%m/%d/%y %H:%M')
head(as.numeric(Date))

Temp5.format$Date<-Date

## Convert Date Time in Temp5.time to time

Temp5.time$Date<-as.POSIXct(Temp5.time$`Date Time`,tz='GMT', format='%m/%d/%y %I:%M:%S %p')

## Combine tables with corrected Date/Time formats

Temp5.cor<-rbind(Temp5.time,Temp5.format)

## Pull data within dates needed for analysis

as.numeric(as.POSIXct('2017-05-15',tz='GMT',format='%Y-%m-%d'))

Temp5.A<-subset(Temp5.cor, as.numeric(Date)>=1496923200 & as.numeric(Date)<=1497484800 | 
                  as.numeric(Date)>1497484800 & as.numeric(Date)<=1500076800 |
                  as.numeric(Date)>1500076800 & as.numeric(Date)<=1502755200 |
                  as.numeric(Date)>1502755200 & as.numeric(Date)<=1505433600 |
                  as.numeric(Date)>1505433600 & as.numeric(Date)<=1508025600)

## determine the average temp for each hour of each day at each depth

## Group by hour

Temp5.A$`Date Time`<-as.character(Temp5.A$Date)

DT.Split<-strsplit(Temp5.A$`Date Time`,' ')
head(DT.Split)

DT.table<-data.frame(Date=(sapply(DT.Split,function(x){x[[1]]})),Time=(sapply(DT.Split,function(x){x[[2]]})))
head(DT.table)

Time_split<-strsplit(as.character(DT.table$Time),':')
head(Time_split)

DT.table$Hour<-as.numeric(sapply(Time_split,function(x){x[[1]]}))

DT.table$'Date Time'<-paste(DT.table$Date,DT.table$Time, by=' ')

Temp5.A$Hour<-DT.table$Hour

## Assign months
Temp5.A$Month<-ifelse(as.numeric(Temp5.A$Date)<=1497484800, 'MJ',
                         ifelse(as.numeric(Temp5.A$Date)>1497484800 & as.numeric(Temp5.A$Date)<=1500076800, 'JJ',
                                ifelse(as.numeric(Temp5.A$Date)>1500076800 & as.numeric(Temp5.A$Date)<=1502755200, 'JA',
                                       ifelse(as.numeric(Temp5.A$Date)>1502755200 & as.numeric(Temp5.A$Date)<=1505433600, 'AS',
                                              ifelse(as.numeric(Temp5.A$Date)>1505433600 & as.numeric(Temp5.A$Date)<=1508025600, 'SO','9')))))

unique(Temp5.A$Month)

## calculate average hourly temp for each depth in each month

Temp5.A$concat<-paste(Temp5.A$Depth,Temp5.A$Month,Temp5.A$Hour, by=' ')

Temp5.D.M.Hr.Avg<-tapply(Temp5.A$Temp,Temp5.A$concat,mean)

head(Temp5.D.M.Hr.Avg)

Avg.Hr.Temp<-data.frame(Depth.Month.Hr=names(Temp5.D.M.Hr.Avg), Temp=Temp5.D.M.Hr.Avg)

## Split depth from concat and determine max and min depth within optimal temp range for each depth in each month

Date.split<-strsplit(as.character(Avg.Hr.Temp$Depth.Month.Hr),' ')
head(Date.split)

Avg.Hr.Temp$Depth<-as.numeric(sapply(Date.split, function(x){x[[1]]}))

Avg.Hr.Temp$Month.Hr<-paste(sapply(Date.split, function(x){x[[2]]}),sapply(Date.split, function(x){x[[3]]}),by=' ')

Avg.Hr.Temp$Op.temp<-ifelse(Avg.Hr.Temp$Temp>=11 & Avg.Hr.Temp$Temp<=25, 'Y', 'N')

## Determine if depths are in optimal values

Avg.Hr.Temp<-subset(Avg.Hr.Temp, Avg.Hr.Temp$Temp>=11 & Avg.Hr.Temp$Temp<=25)

## Find max depths for optimal temp values (min depths do not exist and are covered by OHA)

Dmax.temp<-tapply(Avg.Hr.Temp$Depth,Avg.Hr.Temp$Month.Hr,max)
head(Dmax.temp)

Dmax.temp.table<-data.frame(Month.Hr=names(Dmax.temp),Max.Depth=Dmax.temp)

Dmax.split<-strsplit(as.character(Dmax.temp.table$Month.Hr),split=' ')

Dmax.temp.table$Month<-sapply(Dmax.split, function(x){x[[1]]})
Dmax.temp.table$Hour<-as.numeric(sapply(Dmax.split, function(x){x[[2]]}))

###################################################################################################################

## Temp String 2 (State C)

###################################################################################################################

X2_7$Logger<-rep(2,each=length(X2_7$Log))

X2_14$Depth<-rep(14,each=length(X2_14$Log))

## Remove useless columns for each temp table

X2_14[,6:10]<- list(NULL)

names(X2_14)<-c('Logger','Depth','Log','Date Time','Temp')

## Combine Temp tables

Temp2<-rbind(X2_2,X2_3,X2_4,X2_5,X2_7,X2_9,X2_10,X2_11,X2_12,X2_13,X2_14)

## Fix messed up dates

## Separate good and bad formatting

Temp2.time<- Temp2[grep("/", Temp2$`Date Time`), ]

Temp2.format<- Temp2[grep("-", Temp2$`Date Time`), ]

## Split bad formatting for corrections

DT.Split<-strsplit(Temp2.format$`Date Time`,'-')
head(DT.Split)

DT.table<-data.frame(Month=(sapply(DT.Split,function(x){x[[1]]})),Day=(sapply(DT.Split,function(x){x[[2]]})),
                     Year.Time=(sapply(DT.Split,function(x){x[[3]]})))

## correct month

unique(DT.table$Month)

DT.table$Month<-ifelse(DT.table$Month==2001,'01',
                       ifelse(DT.table$Month==2002,'02',
                              ifelse(DT.table$Month==2003,'03',
                                     ifelse(DT.table$Month==2004,'04',
                                            ifelse(DT.table$Month==2005,'05',
                                                   ifelse(DT.table$Month==2006,'06', 
                                                          ifelse(DT.table$Month==2007,'07',
                                                                 ifelse(DT.table$Month==2008,'08',
                                                                        ifelse(DT.table$Month==2009,'09',
                                                                               ifelse(DT.table$Month==2010,'10',
                                                                                      ifelse(DT.table$Month==2011,'11',
                                                                                             ifelse(DT.table$Month==2012,'12','N'))))))))))))

## Recombine columns

Date<-paste(DT.table$Month,DT.table$Day,DT.table$Year.Time,sep="/")
head(Date)

## Convert Date object to time

Date<-as.POSIXct(Date,tz='GMT', format='%m/%d/%y %H:%M')
head(as.numeric(Date))

Temp2.format$Date<-Date

## Convert Date Time in Temp2.time to time

Temp2.time$Date<-as.POSIXct(Temp2.time$`Date Time`,tz='GMT', format='%m/%d/%y %I:%M:%S %p')

## Combine tables with corrected Date/Time formats

Temp2.cor<-rbind(Temp2.time,Temp2.format)

## Pull data within dates needed for analysis

as.numeric(as.POSIXct('2017-10-15',tz='GMT',format='%Y-%m-%d'))

Temp2.A<-subset(Temp2.cor, as.numeric(Date)>=1496923200 & as.numeric(Date)<=1497484800 | 
                  as.numeric(Date)>1497484800 & as.numeric(Date)<=1500076800 |
                  as.numeric(Date)>1500076800 & as.numeric(Date)<=1502755200 |
                  as.numeric(Date)>1502755200 & as.numeric(Date)<=1505433600 |
                  as.numeric(Date)>1505433600 & as.numeric(Date)<=1508025600)

## determine the average temp for each hour of each day at each depth

## Group by hour

Temp2.A$`Date Time`<-as.character(Temp2.A$Date)

DT.Split<-strsplit(Temp2.A$`Date Time`,' ')
head(DT.Split)

DT.table<-data.frame(Date=(sapply(DT.Split,function(x){x[[1]]})),Time=(sapply(DT.Split,function(x){x[[2]]})))
head(DT.table)

Time_split<-strsplit(as.character(DT.table$Time),':')
head(Time_split)

DT.table$Hour<-as.numeric(sapply(Time_split,function(x){x[[1]]}))

DT.table$'Date Time'<-paste(DT.table$Date,DT.table$Time, by=' ')

Temp2.A$Hour<-DT.table$Hour

## Assign months
Temp2.A$Month<-ifelse(as.numeric(Temp2.A$Date)<=1497484800, 'MJ',
                      ifelse(as.numeric(Temp2.A$Date)>1497484800 & as.numeric(Temp2.A$Date)<=1500076800, 'JJ',
                             ifelse(as.numeric(Temp2.A$Date)>1500076800 & as.numeric(Temp2.A$Date)<=1502755200, 'JA',
                                    ifelse(as.numeric(Temp2.A$Date)>1502755200 & as.numeric(Temp2.A$Date)<=1505433600, 'AS',
                                           ifelse(as.numeric(Temp2.A$Date)>1505433600 & as.numeric(Temp2.A$Date)<=1508025600, 'SO','9')))))

unique(Temp2.A$Month)

## calculate average hourly temp for each depth in each month

Temp2.A$concat<-paste(Temp2.A$Depth,Temp2.A$Month,Temp2.A$Hour, by=' ')

Temp2.D.M.Hr.Avg<-tapply(Temp2.A$Temp,Temp2.A$concat,mean)

head(Temp2.D.M.Hr.Avg)

Avg.Hr.Temp<-data.frame(Depth.Month.Hr=names(Temp2.D.M.Hr.Avg), Temp=Temp2.D.M.Hr.Avg)

## Split depth from concat and determine max and min depth within optimal temp range for each depth in each month

Date.split<-strsplit(as.character(Avg.Hr.Temp$Depth.Month.Hr),' ')
head(Date.split)

Avg.Hr.Temp$Depth<-as.numeric(sapply(Date.split, function(x){x[[1]]}))

Avg.Hr.Temp$Month.Hr<-paste(sapply(Date.split, function(x){x[[2]]}),sapply(Date.split, function(x){x[[3]]}),by=' ')

Avg.Hr.Temp$Op.temp<-ifelse(Avg.Hr.Temp$Temp>=11 & Avg.Hr.Temp$Temp<=25, 'Y', 'N')

## Determine if depths are in optimal values

Avg.Hr.Temp<-subset(Avg.Hr.Temp, Avg.Hr.Temp$Temp>=11 & Avg.Hr.Temp$Temp<=25)

## Find max depths for optimal temp values (min depths do not exist and are covered by OHA)

Dmax.temp<-tapply(Avg.Hr.Temp$Depth,Avg.Hr.Temp$Month.Hr,max)
head(Dmax.temp)

Dmax.temp.table_2<-data.frame(Month.Hr=names(Dmax.temp),Max.Depth=Dmax.temp)

Dmax.split<-strsplit(as.character(Dmax.temp.table_2$Month.Hr),split=' ')

Dmax.temp.table_2$Month<-sapply(Dmax.split, function(x){x[[1]]})
Dmax.temp.table_2$Hour<-as.numeric(sapply(Dmax.split, function(x){x[[2]]}))


###################################################################################################################

###################################################################################################################

## Temp String 3 (State A)

###################################################################################################################

X3_19$Logger<-rep(3,each=length(X3_19$Log))

X3_19$Depth<-rep(19,each=length(X3_19$Log))

## Remove useless columns for each temp table

X3_19[,7:10]<- list(NULL)

names(X3_19)<-c('Logger','Depth','Log','Date','Time','Temp')

## Combine Date and Time as in other tables

X3_19$'Date Time'<-paste(X3_19$Date,X3_19$Time,sep=" ")

## Combine Temp tables

Temp3<-rbind(X3_2,X3_6,X3_8,X3_9,X3_10,X3_11,X3_12,X3_13,X3_14,X3_15,X3_16,X3_17,X3_18,X3_19)

## Fix messed up dates

## Separate good and bad formatting

Temp3.time<- Temp3[grep("/", Temp3$`Date Time`), ]

Temp3.format<- Temp3[grep("-", Temp3$`Date Time`), ]

## Split bad formatting for corrections

DT.Split<-strsplit(Temp3.format$`Date Time`,'-')
head(DT.Split)

DT.table<-data.frame(Month=(sapply(DT.Split,function(x){x[[1]]})),Day=(sapply(DT.Split,function(x){x[[2]]})),
                     Year.Time=(sapply(DT.Split,function(x){x[[3]]})))

## correct month

unique(DT.table$Month)

DT.table$Month<-ifelse(DT.table$Month==2001,'01',
                       ifelse(DT.table$Month==2002,'02',
                              ifelse(DT.table$Month==2003,'03',
                                     ifelse(DT.table$Month==2004,'04',
                                            ifelse(DT.table$Month==2005,'05',
                                                   ifelse(DT.table$Month==2006,'06', 
                                                          ifelse(DT.table$Month==2007,'07',
                                                                 ifelse(DT.table$Month==2008,'08',
                                                                        ifelse(DT.table$Month==2009,'09',
                                                                               ifelse(DT.table$Month==2010,'10',
                                                                                      ifelse(DT.table$Month==2011,'11',
                                                                                             ifelse(DT.table$Month==2012,'12',as.character(DT.table$Month)))))))))))))

## Recombine columns

Date<-paste(DT.table$Month,DT.table$Day,DT.table$Year.Time,sep="/")
head(Date)

## Convert Date object to time

Date<-as.POSIXct(Date,tz='GMT', format='%m/%d/%y %H:%M')
head(as.numeric(Date))

Temp3.format$Date<-Date

## Convert Date Time in Temp3.time to time

Temp3.time$Date<-as.POSIXct(Temp3.time$`Date Time`,tz='GMT', format='%m/%d/%y %I:%M:%S %p')

## Combine tables with corrected Date/Time formats

Temp3.cor<-rbind(Temp3.time,Temp3.format)

## Pull data within dates needed for analysis

as.numeric(as.POSIXct('2017-10-15',tz='GMT',format='%Y-%m-%d'))

Temp3.A<-subset(Temp3.cor, as.numeric(Date)>=1496923200 & as.numeric(Date)<=1497484800 | 
                  as.numeric(Date)>1497484800 & as.numeric(Date)<=1500076800 |
                  as.numeric(Date)>1500076800 & as.numeric(Date)<=1502755200 |
                  as.numeric(Date)>1502755200 & as.numeric(Date)<=1505433600 |
                  as.numeric(Date)>1505433600 & as.numeric(Date)<=1508025600)

## determine the average temp for each hour of each day at each depth

## Group by hour

Temp3.A$`Date Time`<-as.character(Temp3.A$Date)

DT.Split<-strsplit(Temp3.A$`Date Time`,' ')
head(DT.Split)

DT.table<-data.frame(Date=(sapply(DT.Split,function(x){x[[1]]})),Time=(sapply(DT.Split,function(x){x[[2]]})))
head(DT.table)

Time_split<-strsplit(as.character(DT.table$Time),':')
head(Time_split)

DT.table$Hour<-as.numeric(sapply(Time_split,function(x){x[[1]]}))

DT.table$'Date Time'<-paste(DT.table$Date,DT.table$Time, by=' ')

Temp3.A$Hour<-DT.table$Hour

## Assign months
Temp3.A$Month<-ifelse(as.numeric(Temp3.A$Date)<=1497484800, 'MJ',
                      ifelse(as.numeric(Temp3.A$Date)>1497484800 & as.numeric(Temp3.A$Date)<=1500076800, 'JJ',
                             ifelse(as.numeric(Temp3.A$Date)>1500076800 & as.numeric(Temp3.A$Date)<=1502755200, 'JA',
                                    ifelse(as.numeric(Temp3.A$Date)>1502755200 & as.numeric(Temp3.A$Date)<=1505433600, 'AS',
                                           ifelse(as.numeric(Temp3.A$Date)>1505433600 & as.numeric(Temp3.A$Date)<=1508025600, 'SO','9')))))

unique(Temp3.A$Month)

## calculate average hourly temp for each depth in each month

Temp3.A$concat<-paste(Temp3.A$Depth,Temp3.A$Month,Temp3.A$Hour, by=' ')

Temp3.D.M.Hr.Avg<-tapply(Temp3.A$Temp,Temp3.A$concat,mean)

head(Temp3.D.M.Hr.Avg)

Avg.Hr.Temp<-data.frame(Depth.Month.Hr=names(Temp3.D.M.Hr.Avg), Temp=Temp3.D.M.Hr.Avg)

## Split depth from concat and determine max and min depth within optimal temp range for each depth in each month

Date.split<-strsplit(as.character(Avg.Hr.Temp$Depth.Month.Hr),' ')
head(Date.split)

Avg.Hr.Temp$Depth<-as.numeric(sapply(Date.split, function(x){x[[1]]}))

Avg.Hr.Temp$Month.Hr<-paste(sapply(Date.split, function(x){x[[2]]}),sapply(Date.split, function(x){x[[3]]}),by=' ')

Avg.Hr.Temp$Op.temp<-ifelse(Avg.Hr.Temp$Temp>=11 & Avg.Hr.Temp$Temp<=25, 'Y', 'N')

## Determine if depths are in optimal values

Avg.Hr.Temp<-subset(Avg.Hr.Temp, Avg.Hr.Temp$Temp>=11 & Avg.Hr.Temp$Temp<=25)

## Find max depths for optimal temp values (min depths do not exist and are covered by OHA)

Dmax.temp<-tapply(Avg.Hr.Temp$Depth,Avg.Hr.Temp$Month.Hr,max)
head(Dmax.temp)

Dmax.temp.table_3<-data.frame(Month.Hr=names(Dmax.temp),Max.Depth=Dmax.temp)

Dmax.split<-strsplit(as.character(Dmax.temp.table_3$Month.Hr),split=' ')

Dmax.temp.table_3$Month<-sapply(Dmax.split, function(x){x[[1]]})
Dmax.temp.table_3$Hour<-as.numeric(sapply(Dmax.split, function(x){x[[2]]}))


###################################################################################################################


## Dmax temp table for each section of Black Bay

## 1-1

Dmax.1<-Dmax.temp.table_2
Dmax.1$Location<-rep('1-1',each=length(Dmax.1$Max.Depth))
Dmax.1<-subset(Dmax.1,Month=='MJ' & Hour>=5 & Hour<=21 |
                 Month=='JJ' & Hour>=5 & Hour<=21 |
                 Month=='JA' & Hour>=6 & Hour<=21 |
                 Month=='AS' & Hour>=6 & Hour<=20 |
                 Month=='SO' & Hour>=7 & Hour<=19)

## 2-1

Dmax.2<-Dmax.temp.table_2
Dmax.2$Location<-rep('2-1',each=length(Dmax.2$Max.Depth))
Dmax.2<-subset(Dmax.2,Month=='MJ' & Hour>=5 & Hour<=21 |
                 Month=='JJ' & Hour>=5 & Hour<=21 |
                 Month=='JA' & Hour>=6 & Hour<=21 |
                 Month=='AS' & Hour>=6 & Hour<=20 |
                 Month=='SO' & Hour>=7 & Hour<=19)

## 3-1

Dmax.3<-Dmax.temp.table_3
Dmax.3$Location<-rep('3-1',each=length(Dmax.3$Max.Depth))
Dmax.3<-subset(Dmax.3,Month=='MJ' & Hour>=5 & Hour<=21 |
                 Month=='JJ' & Hour>=5 & Hour<=21 |
                 Month=='JA' & Hour>=6 & Hour<=21 |
                 Month=='AS' & Hour>=6 & Hour<=20 |
                 Month=='SO' & Hour>=7 & Hour<=19)

## 4-1

Dmax.4<-Dmax.temp.table
Dmax.4$Location<-rep('4-1',each=length(Dmax.4$Max.Depth))
Dmax.4<-subset(Dmax.4,Month=='MJ' & Hour>=5 & Hour<=21 |
                 Month=='JJ' & Hour>=5 & Hour<=21 |
                 Month=='JA' & Hour>=6 & Hour<=21 |
                 Month=='AS' & Hour>=6 & Hour<=20 |
                 Month=='SO' & Hour>=7 & Hour<=19)

## 5-1

Dmax.5<-Dmax.temp.table
Dmax.5$Location<-rep('5-1',each=length(Dmax.5$Max.Depth))
Dmax.5<-subset(Dmax.5,Month=='MJ' & Hour>=5 & Hour<=21 |
                 Month=='JJ' & Hour>=5 & Hour<=21 |
                 Month=='JA' & Hour>=6 & Hour<=21 |
                 Month=='AS' & Hour>=6 & Hour<=20 |
                 Month=='SO' & Hour>=7 & Hour<=19)

## Combine max depth by temp tables

Dmax<-rbind(Dmax.1,Dmax.2,Dmax.3,Dmax.4,Dmax.5)

Dmax$concat<-paste(Dmax$Location,Dmax$Month,Dmax$Hour)

OHA$concat<-paste(OHA$Location,OHA$Month,OHA$Hour)

TOHD<-merge(Dmax,OHA, by='concat')

TOHD$Month.Hr<-NULL
TOHD$concat<-NULL
TOHD$Location.y<-NULL
TOHD$Month.y<-NULL
TOHD$Hour.y<-NULL

colnames(TOHD)[2:4]<-c('Month','Hour','Location')

####################################################################################################

## Determine which depths fall within optimal habtitat depths

TOHD$Op.Depth<-ifelse(TOHD$Depth>=TOHD$zshallow & TOHD$Depth<=TOHD$zdeep & TOHD$Depth<=TOHD$Max.Depth, 'Y','N')


## Subset for only depths within optimal range

TOHA<-subset(TOHD, Op.Depth=='Y')

## Concatenate Month and Location

TOHA$concat<-paste(TOHA$Location,TOHA$Month,sep="|")

## Sum optimal area by concat

HA<-tapply(TOHA$Cells,TOHA$concat,sum)

## Create data table with sum of cells, Region, Period, Hours of daylight in period (average day length*14 days), average OHA

HA.table<-data.frame(Concat=names(HA), Cells.sum=HA)
HA.table

class(HA.table$Concat)
HA.table$Concat<-as.character(HA.table$Concat)

Concat.split<-strsplit(HA.table$Concat, split='\\|')

Region<-sapply(Concat.split, function(x){x[[1]]})
head(Region)

HA.table$Region<-Region

Period<-sapply(Concat.split, function(x){x[[2]]})
head(Period)

HA.table$Period<-Period

## Hours Daylight

HA.table$Daylight<-ifelse(HA.table$Period=='MJ',21,
                          ifelse(HA.table$Period=='JJ',21,
                                 ifelse(HA.table$Period=='JA',21,
                                        ifelse(HA.table$Period=='AS',20,
                                               ifelse(HA.table$Period=='SO',19,0)))))

## Average hourly optical habitat area (daylight hours)

HA.table$'THermal Optical Habitat Area (m2)'<-(HA.table$Cells.sum*30*30)/HA.table$Daylight
                          
write.csv(HA.table,file="TOHA.csv")

#################################################################################################################

## Fish occupancy with vs TOHA

Mark<-MARK1

## Dates for summer 2017

Mark$Bin<-as.POSIXct(Mark$Bin)

Mark<-subset(Mark,as.numeric(Mark$Bin)>=as.numeric(as.POSIXct('2017-05-15',tz='GMT', format='%Y-%m-%d')) &
                as.numeric(Mark$Bin)<=as.numeric(as.POSIXct('2017-10-16',tz='GMT', format='%Y-%m-%d')))

Mark$SB<-paste(Mark$Bin,Mark$States,sep=' ')

tapply(Mark$Fish.No, Mark$SB, length) 

A.Occupy<-c(101,73,63,55,61)
D.Occupy<-c(0,0,2,10,3)

TOHA.12<-merge(TOHA.1,TOHA.2, by='Period')
TOHA.12[,2:5]<-NULL
TOHA.12[,3:6]<-NULL
TOHA.12$Area<-(TOHA.12$`THermal Optical Habitat Area (m2).x`)+(TOHA.12$`THermal Optical Habitat Area (m2).y`)

TOHA.A<-c(35654614,33777686,34093971,39651165,26131358)
TOHA.D<-c(0,0,1263300,3538305,1352700)

plot(A.Occupy~TOHA.A)
plot(D.Occupy~TOHA.D)

D.Occ.lm<-lm(D.Occupy~TOHA.D)
summary(D.Occ.lm)

A.Occ.lm<-lm(A.Occupy~TOHA.A)
summary(A.Occ.lm)

## Plot south end TOHA Regression

par(mfrow=c(1,1))

par(mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0),fig=c(0,1,0,1), las=0, bg='white')

plot(D.Occupy~TOHA.D,bty='l', xlab= (expression(paste("Optimal Thermal Optical Habitat Area (m"^"2"*")"))),
     ylab="Number of Walleye", cex=1, cex.lab=1, cex.axis=1, pch=19,xlim=c(0,5500000), ylim=c(0,15) )

clip(-73000,3700000, -100, 100)

abline(D.Occ.lm$coefficients[[1]], D.Occ.lm$coefficients[[2]])

text(0,2,"MJ",cex=1)
text(0,1,"JJ",cex=1)
text(1263300,1,"JA",cex=1)
text(1352700,4,"AS",cex=1)
text(3538305,9,"SO",cex=1)

## Plot north end TOHA regression

par(fig=c(.7, 1, .55, 1), mar=c(3,0,1,0), mgp=c(0, 1, 0), bg='white', new=TRUE)

plot(D.Occupy~TOHA.D,bty='l', xlab="", ylab="", cex=1, cex.lab=1, cex.axis=1, pch=17, col=4)
abline(D.Occ.lm$coefficients[[1]], D.Occ.lm$coefficients[[2]])


text(35654614,96,"MJ",cex=1)
text(33777685,78,"JJ",cex=1)
text(34093971,68,"JA",cex=1)
text(39651165,60,"AS",cex=1)
text(26131357,66,"SO",cex=1)








## All TOHA together

TOHA.tot<-c(35654614,33777686,34093971,39651165,26131358,0,0,1263300,3538305,1352700)
Occupy.tot<-c(101,73,63,55,61,0,0,2,10,3)

TOHA.dat<-data.frame(Area=TOHA.tot,Occupancy=Occupy.tot)
TOHA.dat$Region<-c('N','N','N','N','N','S','S','S','S','S')
TOHA.dat$col<-c(1,1,1,1,1,4,4,4,4,4)
TOHA.dat$pch<-c(19,19,19,19,19,17,17,17,17,17)
TOHA.dat$log.Area<-log(TOHA.dat$Area)
TOHA.dat$log.Occupancy<-log(TOHA.dat$Occupancy)

TOHA.lm<-lm(TOHA.dat$Occupancy~TOHA.dat$Area)
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1))

plot((TOHA.dat$Area/1000000), TOHA.dat$Occupancy, xlab= (expression(paste("Optimal Thermal Optical Habitat Area (km"^"2"*")"))),
     ylab="Number of Walleye", cex=1.5, cex.lab=1.5, cex.axis=1.5, pch=TOHA.dat$pch, col=TOHA.dat$col, bty="l" )
abline(TOHA.lm$coefficients[[1]], (TOHA.lm$coefficients[[2]]*1000000), lty=2, lwd=2)
abline(D.Occ.lm$coefficients[[1]],(D.Occ.lm$coefficients[[2]]*1000000), lwd=2)

text(35.654614,97,"MJ",cex=1)
text(33.777685,77,"JJ",cex=1)
text(34.093971,67,"JA",cex=1)
text(39.651165,60,"AS",cex=1)
text(26.131357,65,"SO",cex=1)

rect(-2.000000,-4.2,5.000000,15)

par(fig=c(.15, .4, .5, .9), mar=c(3,0,1,0), mgp=c(0, 1, 0), bg='white', new=TRUE)

plot((TOHA.D/1000000), D.Occupy, bty='l', xlab="", ylab="", cex=1.5, cex.lab=1.5, cex.axis=1.5, pch=17, col=4)
abline(D.Occ.lm$coefficients[[1]], (D.Occ.lm$coefficients[[2]]*1000000), lwd=2)
abline(TOHA.lm$coefficients[[1]], (TOHA.lm$coefficients[[2]]*1000000), lty=2, lwd=2)

text(0.070000,4,"MJ",cex=1)
text(0.060000,3,"JJ",cex=1)
text(1.263300,1,"JA",cex=1)
text(1.352700,5,"AS",cex=1)
text(3.400000,7,"SO",cex=1)

par(mfrow=c(1,1))

par(mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0),fig=c(0,1,0,1), las=0, bg='white')









## Log transformed no zeros

TOHA.tot<-c(35654614,33777686,34093971,39651165,26131358,1263300,3538305,1352700)
Occupy.tot<-c(101,73,63,55,61,2,10,3)

TOHA.lm<-lm(log(Occupy.tot)~log(TOHA.tot))
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1))

plot(TOHA.dat$log.Occupancy~TOHA.dat$log.Area,xlab= (expression(paste("Optimal Thermal Optical Habitat Area (m"^"2"*")"))),
     ylab="Number of Walleye", cex=1, cex.lab=1, cex.axis=1, pch=19, col=TOHA.dat$col )
abline(TOHA.lm$coefficients[[1]], TOHA.lm$coefficients[[2]])

##Log zeros are small

TOHA.tot<-c(35654614,33777686,34093971,39651165,26131358,1263300,3538305,1352700,0.00001,0.00001)
Occupy.tot<-c(101,73,63,55,61,2,10,3,0.00001,0.00001)

TOHA.lm<-lm(log(Occupy.tot)~log(TOHA.tot))
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1))

TOHA.dat$log.Occupancy<-log(Occupy.tot)
TOHA.dat$log.Area<-log(TOHA.tot)

plot(TOHA.dat$log.Occupancy~TOHA.dat$log.Area,xlab= (expression(paste("Optimal Thermal Optical Habitat Area (m"^"2"*")"))),
     ylab="Number of Walleye", cex=1, cex.lab=1, cex.axis=1, pch=19, col=TOHA.dat$col )
abline(TOHA.lm$coefficients[[1]], TOHA.lm$coefficients[[2]])

## Mixed effects

TOHA.lm<-lm(TOHA.dat$log.Occupancy~TOHA.dat$log.Area+TOHA.dat$Region+TOHA.dat$log.Area*TOHA.dat$Region)
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1))


##################################################################################################################

##################################################################################################################

## Adding TOHA for section 3

#################################################################################################################

## Combine max depth by temp tables

Dmax.3$concat<-paste(Dmax.3$Location,Dmax.3$Month,Dmax.3$Hour)

OHA$concat<-paste(OHA$Location,OHA$Month,OHA$Hour)

OHA.3<-subset(OHA,OHA$Location=='3-1')

TOHD.3<-merge(Dmax.3,OHA.3, by='concat')

TOHD.3$Month.Hr<-NULL
TOHD.3$concat<-NULL
TOHD.3$Location.y<-NULL
TOHD.3$Month.y<-NULL
TOHD.3$Hour.y<-NULL

colnames(TOHD.3)[2:4]<-c('Month','Hour','Location')

####################################################################################################

## Determine which depths fall within optimal habtitat depths

TOHD.3$Op.Depth<-ifelse(TOHD.3$Depth>=TOHD.3$zshallow & TOHD.3$Depth<=TOHD.3$zdeep & TOHD.3$Depth<=TOHD.3$Max.Depth, 'Y','N')


## Subset for only depths within optimal range

TOHA.3<-subset(TOHD.3, Op.Depth=='Y')

## Concatenate Month and Location

TOHA.3$concat<-paste(TOHA.3$Location,TOHA.3$Month,sep="|")

## Sum optimal area by concat

HA.3<-tapply(TOHA.3$Cells,TOHA.3$concat,sum)

## Create data table with sum of cells, Region, Period, Hours of daylight in period (average day length*14 days), average OHA

HA.table.3<-data.frame(Concat=names(HA.3), Cells.sum=HA.3)
HA.table.3

class(HA.table.3$Concat)
HA.table.3$Concat<-as.character(HA.table.3$Concat)

Concat.split.3<-strsplit(HA.table.3$Concat, split='\\|')

Region.3<-sapply(Concat.split.3, function(x){x[[1]]})
head(Region.3)

HA.table.3$Region<-Region.3

Period.3<-sapply(Concat.split.3, function(x){x[[2]]})
head(Period.3)

HA.table.3$Period<-Period.3

## Hours Daylight

HA.table.3$Daylight<-ifelse(HA.table.3$Period=='MJ',21,
                          ifelse(HA.table.3$Period=='JJ',21,
                                 ifelse(HA.table.3$Period=='JA',21,
                                        ifelse(HA.table.3$Period=='AS',20,
                                               ifelse(HA.table.3$Period=='SO',19,0)))))

## Average hourly optical habitat area (daylight hours)

HA.table.3$'THermal Optical Habitat Area (m2)'<-(HA.table.3$Cells.sum*30*30)/HA.table.3$Daylight

write.csv(HA.table.3,file="TOHA3.csv")

#################################################################################################################

## Fish occupancy with vs TOHA

Mark<-MARK1

## Dates for summer 2017

Mark$Bin<-as.POSIXct(Mark$Bin)

Mark<-subset(Mark,as.numeric(Mark$Bin)>=as.numeric(as.POSIXct('2017-05-15',tz='GMT', format='%Y-%m-%d')) &
               as.numeric(Mark$Bin)<=as.numeric(as.POSIXct('2017-10-16',tz='GMT', format='%Y-%m-%d')))

Mark$SB<-paste(Mark$Bin,Mark$States,sep=' ')

tapply(Mark$Fish.No, Mark$SB, length) 

A.Occupy<-c(101,73,63,55,61)
B.Occupy<-c(4,21,16,18,22)
D.Occupy<-c(0,0,2,10,3)

TOHA.12<-merge(TOHA.1,TOHA.2, by='Period')
TOHA.12[,2:5]<-NULL
TOHA.12[,3:6]<-NULL
TOHA.12$Area<-(TOHA.12$`THermal Optical Habitat Area (m2).x`)+(TOHA.12$`THermal Optical Habitat Area (m2).y`)

TOHA.A<-c(35654614,33777686,34093971,39651165,26131358)
TOHA.B<-c(2265086,7652229,7277271,7911630,5522163)
TOHA.D<-c(0,0,1263300,3538305,1352700)

plot(A.Occupy~TOHA.A)
plot(B.Occupy~TOHA.B)
plot(D.Occupy~TOHA.D)

D.Occ.lm<-lm(D.Occupy~TOHA.D)
summary(D.Occ.lm)

B.Occ.lm<-lm(B.Occupy~TOHA.B)
summary(B.Occ.lm)

A.Occ.lm<-lm(A.Occupy~TOHA.A)
summary(A.Occ.lm)

## Plot south end TOHA Regression

par(mfrow=c(1,1))

par(mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0),fig=c(0,1,0,1), las=0, bg='white')

plot(D.Occupy~TOHA.D,bty='l', xlab= (expression(paste("Optimal Thermal Optical Habitat Area (m"^"2"*")"))),
     ylab="Number of Walleye", cex=1, cex.lab=1, cex.axis=1, pch=19,xlim=c(0,5500000), ylim=c(0,15) )

clip(-73000,3700000, -100, 100)

abline(D.Occ.lm$coefficients[[1]], D.Occ.lm$coefficients[[2]])

text(0,2,"MJ",cex=1)
text(0,1,"JJ",cex=1)
text(1263300,1,"JA",cex=1)
text(1352700,4,"AS",cex=1)
text(3538305,9,"SO",cex=1)

## Plot north end TOHA regression

par(fig=c(.7, 1, .55, 1), mar=c(3,0,1,0), mgp=c(0, 1, 0), bg='white', new=TRUE)

plot(D.Occupy~TOHA.D,bty='l', xlab="", ylab="", cex=1, cex.lab=1, cex.axis=1, pch=17, col=4)
abline(D.Occ.lm$coefficients[[1]], D.Occ.lm$coefficients[[2]])


text(35654614,96,"MJ",cex=1)
text(33777685,78,"JJ",cex=1)
text(34093971,68,"JA",cex=1)
text(39651165,60,"AS",cex=1)
text(26131357,66,"SO",cex=1)








## All TOHA together

TOHA.tot<-c(35654614,33777686,34093971,39651165,26131358,0,0,1263300,3538305,1352700,2265086,7652229,7277271,7911630,5522163)
Occupy.tot<-c(101,73,63,55,61,0,0,2,10,3,4,21,16,18,22)

TOHA.dat<-data.frame(Area=TOHA.tot,Occupancy=Occupy.tot)
TOHA.dat$Region<-c('N','N','N','N','N','S','S','S','S','S','M','M','M','M','M')
TOHA.dat$col<-c(1,1,1,1,1,4,4,4,4,4,2,2,2,2,2)
TOHA.dat$pch<-c(19,19,19,19,19,17,17,17,17,17,15,15,15,15,15)
TOHA.dat$log.Area<-log(TOHA.dat$Area)
TOHA.dat$log.Occupancy<-log(TOHA.dat$Occupancy)

TOHA.lm<-lm(TOHA.dat$Occupancy~TOHA.dat$Area)
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1),mar = c(5, 4.6, 4, 2))

plot((TOHA.dat$Area/1000000), TOHA.dat$Occupancy, xlab= (expression(paste("Optimal Thermal Optical Habitat Area (km"^"2"*")"))),
     ylab="Number of Walleye", cex=1.7, cex.lab=1.5, cex.axis=1.5, pch=TOHA.dat$pch, col=TOHA.dat$col, bty="l", las=1 )
abline(TOHA.lm$coefficients[[1]], (TOHA.lm$coefficients[[2]]*1000000), lty=2, lwd=2)
abline(D.Occ.lm$coefficients[[1]],(D.Occ.lm$coefficients[[2]]*1000000), lwd=2)

text(35.654614,95,"MJ",cex=1.3)
text(33.777685,78,"JJ",cex=1.3)
text(34.093971,67,"JA",cex=1.3)
text(39.651165,60,"AS",cex=1.3)
text(26.131357,66,"SO",cex=1.3)

rect(-2.000000,-4.2,9,25)

par(fig=c(.16, .47, .45, .9), mar=c(3,0,1,0), mgp=c(0, 1, 0), bg='white', new=TRUE)

plot((TOHA.D/1000000), D.Occupy, bty='l', xlab="", ylab="", cex=1.5, cex.lab=1.5, cex.axis=1.5, pch=17, col=4,
     xlim=c(0,9),ylim=c(0,25), las=1)
points(TOHA.B/1000000,B.Occupy, cex=1.5, col=2, pch=15)
abline(D.Occ.lm$coefficients[[1]], (D.Occ.lm$coefficients[[2]]*1000000), lwd=2)
abline(TOHA.lm$coefficients[[1]], (TOHA.lm$coefficients[[2]]*1000000), lty=2, lwd=2)

text(0.10000,6.6,"MJ",cex=1.3)
text(0.060000,4.3,"JJ",cex=1.3)
text(1.263300,0.8,"JA",cex=1.3)
text(1.352700,6.8,"AS",cex=1.3)
text(3.400000,6.5,"SO",cex=1.3)

text(2.265086,2,"MJ",cex=1.3)
text(7.652229,23.5,"JJ",cex=1.3)
text(7.277271,14,"JA",cex=1.3)
text(8.2,16,"AS",cex=1.3)
text(5.522163,20,"SO",cex=1.3)

par(mfrow=c(1,1))

par(mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0),fig=c(0,1,0,1), las=0, bg='white')









## Log transformed no zeros

TOHA.tot<-c(35654614,33777686,34093971,39651165,26131358,1263300,3538305,1352700)
Occupy.tot<-c(101,73,63,55,61,2,10,3)

TOHA.lm<-lm(log(Occupy.tot)~log(TOHA.tot))
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1))

plot(TOHA.dat$log.Occupancy~TOHA.dat$log.Area,xlab= (expression(paste("Optimal Thermal Optical Habitat Area (m"^"2"*")"))),
     ylab="Number of Walleye", cex=1, cex.lab=1, cex.axis=1, pch=19, col=TOHA.dat$col )
abline(TOHA.lm$coefficients[[1]], TOHA.lm$coefficients[[2]])

##Log zeros are small

TOHA.tot<-c(35654614,33777686,34093971,39651165,26131358,1263300,3538305,1352700,0.00001,0.00001)
Occupy.tot<-c(101,73,63,55,61,2,10,3,0.00001,0.00001)

TOHA.lm<-lm(log(Occupy.tot)~log(TOHA.tot))
summary(TOHA.lm)

par(mfrow=c(2,2))

plot(TOHA.lm)

par(mfrow=c(1,1))

TOHA.dat$log.Occupancy<-log(Occupy.tot)
TOHA.dat$log.Area<-log(TOHA.tot)

plot(TOHA.dat$log.Occupancy~TOHA.dat$log.Area,xlab= (expression(paste("Optimal Thermal Optical Habitat Area (m"^"2"*")"))),
     ylab="Number of Walleye", cex=1, cex.lab=1, cex.axis=1, pch=19, col=TOHA.dat$col )
abline(TOHA.lm$coefficients[[1]], TOHA.lm$coefficients[[2]])





