

library(tidyverse)
library(lubridate)
library(hms)



#################################################################
#################################################################
## Processing the Box Turtle Data
##
## We start with the dataset "BoxT 2015 LuLC CC.csv" that includes
##   each box turtles ID, body temperature (Tb), heart rate (HR)
##   the measurement of the accelerometer (odba) and a measure
##   of if the turtle was active based on the accelerometer (Yactiv)
##
## Note, turtle R8NAT123 was mislabeled in the odba data as R8NATL123
##   so we need to do some finangling to fix it and line up the columns
#################################################################
#################################################################

raw_box_turtle <- read_csv("data/BoxT 2015 LuLC CC.csv") %>%
  select(Turtle, Time, Tb, HR, season, Mass, odba, Yactiv)

## One turtle has a typo in its ID
##   R8NAT123 is mislabeled R8NATL123 for the odba data
##   so some fixing here.
R8NATL123 <- raw_box_turtle %>%
  filter(Turtle == "R8NATL123") %>%
  mutate(Turtle = "R8NAT123") %>%
  select(Turtle, Time, odba, Yactiv)
other_turtle <- raw_box_turtle %>%
  filter(Turtle != "R8NATL123",
         Turtle != "R8L8")   ## No HR or Tb data

##########################
## Now we can merge/bridge/join these two together
##   coalesce the odba & Yactiv, and then some cleanup
box_turtle_data <- other_turtle %>%
  left_join(R8NATL123, by=c("Turtle", "Time")) %>%
  mutate(odba = coalesce(odba.x, odba.y),
         Yactiv = coalesce(Yactiv.x, Yactiv.y)) %>%
  select(-c(odba.x, odba.y, Yactiv.x, Yactiv.y)) %>%
  mutate(Turtle = factor(Turtle),
         season = factor(season),
         Date_Time = Time,
         Date = as_date(Time),
         Time = as_hms(Time)) %>%
  select(Turtle, Date_Time, Date, Time, Mass, season, Tb, HR, odba, Yactiv)

## odba data is every 10 minutes
##   Just remove the NA terms, the 5 minute measurements
box_turtle_data_odba <- box_turtle_data %>%
  drop_na(odba)

## Percent active
##   This is done based on +/- 30 minutes to the top of the hour
##   So for example, the 8am time would be 7:30, 7:40, 7:50, 8:00, 8:10, 8:20
##   Calculate the percent of activity based on those 6 measurements
##   We do this for every turtle and every hour
box_turtle_perc_active <- box_turtle_data_odba %>%
  mutate(Hour_for_group = (Date_Time+as.difftime(30, units="mins")) ) %>%
  group_by(Turtle,
           Date,
           Hour = hour(Hour_for_group) ) %>%
  summarize(Perc.activ = mean(Yactiv)*100)


#################################################################
#################################################################
## Processing the Painted Turtle Data
##
## We start with the dataset "2017 PaintTurt.csv" that includes
##   each box turtles ID, body temperature (m.Tb), heart rate (m.HR)
##   a measure of its position. We are only interested in the proportion
##   of time it is Basking.  Fairly straight forward cleanup here.
##
#################################################################
#################################################################


painted_turtle_data <- read_csv("data/2017 PaintTurt.csv") %>%
  mutate(Turtle = factor(turtle),
         season = factor(season),Date_Time = date,
         Time = as_hms(date),
         Date = as_date(date),
         Binary.basking = Position=="Basking") %>%
  select(Turtle, Date_Time, Date, Time, Mass, season,
         Tb=m.Tb, HR=m.HR, Binary.basking)

painted_turtle_data_basking <- painted_turtle_data %>%
  drop_na() %>%
  mutate(Hour = hour(Time)) %>%
  group_by(Turtle, Date, Hour) %>%
  summarise(perc.basking = mean(Binary.basking))

###############################
## Save for repeatability analysis
##
##  These 5 datasets are used in the repeatability analysis
##
save(box_turtle_data, box_turtle_data_odba, box_turtle_perc_active,
     painted_turtle_data, painted_turtle_data_basking,
     file="r_objects/data_for_repeatability.RData")


##################################################################################
##################################################################################
##################################################################################
### 
###  Data for Clustering Analysis
###
##################################################################################
##################################################################################
##################################################################################

###############################################
###############################################
##  "Predictor" variables
##
## Includes corresponding measurement at the same time as the
##   observed turtle value, and the "lag" measurement in the
##   time window before hand.
## e.g. temperature now, and temperature 15 minutes ago
##
## All of these data are obtained from the ERC
##
## These predictor variables are considered in the clustering
##   analysis. Here we get what is necessary.

######################
## Solar radiation data
radiation_data <- read_csv("data/Light_ERCweatherStation_2021-02-11.csv") %>%
  mutate(Time = mdy_hm(EndTime),
         Date = date(Time),
         Hour = hour(Time)) %>%
  arrange(Time) %>%
  mutate(LagRadiation = lag(Value),
         DayTime = (Value > 0) ) %>%
  select(Time, Radiation=Value, LagRadiation, DayTime)

#############################
## Temperature data
temps_data <- read_csv("data/TemperatureAir_ERCweatherStation_2020-03-04.csv") %>%
  filter(Variable %in% c("Air_temperature", "Air_temperature_AirTC"),
         Source == "ERC_local") %>%
  mutate(Time = mdy_hm(EndTime),
         Date = date(Time),
         Hour = hour(Time)) %>%
  arrange(Time) %>%
  mutate(LagAir = lag(Value) ) %>%
  dplyr::select(Time, AirTemp=Value, LagAir)

########################
## Precipitation data
##
## NOTE:  at a daily level
##      we scale to 15 minute intervals below
precip_data <- read_csv("data/Precipitation_ERCweatherStation_2020-03-04.csv") %>%
  mutate(EndTime = mdy_hm(EndTime),
         Raining = (Value>0) ) %>%
  arrange(EndTime) %>%
  dplyr::select(Time = EndTime, Raining)

##############################
## windspeed data
wind_data <- read_csv("data/Windspeed_ERCweatherStation_2020-05-12.csv") %>%
  mutate(EndTime = mdy_hm(EndTime) ) %>%
  arrange(EndTime) %>%
  mutate(LagWind = lag(Value)) %>%
  dplyr::select(Time=EndTime, Windspeed=Value, LagWind)

#########################################
#########################################
##
## Turtle Aggregation
##
## We aggregate the turtle measurements to 
##   15 minute intervals,
##   for heart rate & body temperature, we find
##   the average value in the 15 minute interval
##   for whether the box turtle was active or if
##   a painted turtle was basking, we take
##   the last non-NA measurements.

box_turtle_for_cluster <- box_turtle_data %>%
  ## Remove turtles with no data (from email)
  mutate(Hour = hour(Date_Time),
         Minute = minute(Date_Time),
         Minute = case_when(Minute %in% c(5,10,15) ~ 15,
                            Minute %in% c(20,25,30) ~ 30,
                            Minute %in% c(35,40,45) ~ 45,
                            TRUE ~ 0) ) %>%
  group_by(Turtle, Date, Hour, Minute) %>%
  summarize(Body_temp = mean(Tb, na.rm=TRUE),
            Heart_rate = mean(HR, na.rm=TRUE),
            odba = mean(odba, na.rm=TRUE),
            Active = last(Yactiv, na_rm=TRUE)) %>%
  ungroup() %>%
  mutate(Time = ymd_hm(paste0(Date,"-", Hour, "-", Minute))) %>%
  left_join(radiation_data, by="Time") %>%
  left_join(temps_data, by="Time") %>%
  left_join(wind_data, by="Time") %>%
  left_join(precip_data, by="Time") %>%
  fill(Raining)



painted_turtle_for_cluster <- painted_turtle_data %>%
  ## Remove turtles with no data (from email)
  mutate(Hour = hour(Date_Time),
         Minute = minute(Date_Time),
         Minute = case_when(Minute %in% c(5,10,15) ~ 15,
                            Minute %in% c(20,25,30) ~ 30,
                            Minute %in% c(35,40,45) ~ 45,
                            TRUE ~ 0) ) %>%
  group_by(Turtle, Date, Hour, Minute) %>%
  summarize(Body_temp = mean(Tb, na.rm=TRUE),
            Heart_rate = mean(HR, na.rm=TRUE),
            Basking = last(Binary.basking, na_rm=TRUE)) %>%
  ungroup() %>%
  mutate(Time = ymd_hm(paste0(Date,"-", Hour, "-", Minute))) %>%
  left_join(radiation_data, by="Time") %>%
  left_join(temps_data, by="Time") %>%
  left_join(wind_data, by="Time") %>%
  left_join(precip_data, by="Time") %>%
  fill(Raining)


###############################
## Save for clustering analysis
##
##  These 2 datasets are used in the clustering analysis
##
save(box_turtle_for_cluster, painted_turtle_for_cluster,
     file="r_objects/data_for_clustering.RData")

