#SES time series comparisons
library(dplyr)
library(readr)
library(readxl)
library(anytime)

MAIN_MergedDaily50_600_mass_flux_infil <- read_excel("Z:/STORE/Files/PULSE/DATA/DATA CONSOLIDATION/Carbon/MAIN_MergedDaily50_600_mass flux infil.xlsx", 
                                                     sheet = "R_Plots")
View(MAIN_MergedDaily50_600_mass_flux_infil)

names(MAIN_MergedDaily50_600_mass_flux_infil)
Sediment_traps<- MAIN_MergedDaily50_600_mass_flux_infil %>% 
  dplyr::select("Collect_date", "InFill_Mass", "TCInfil", "Infill600OrgCarbon")


Sediment_traps$Collect_date_PLOT<- anydate(Sediment_traps$Collect_date)+1

Sediment_traps$Collect_date<- NULL
Sediment_traps$m600Duration.open<- NULL


SCOC_daily_by_instrument_expand <- read_csv("Z:/STORE/Files/PULSE/DATA/DATA CONSOLIDATION/Carbon/SCOC_daily_by_instrument_expand.csv",
                                            col_types = cols(`SCOC mg C` = col_number(),
                                                             Collect_date = col_date(format = "%m/%d/%Y")))

SCOC_daily_by_instrument_expand$Collect_date_PLOT<- anydate(SCOC_daily_by_instrument_expand$Collect_date)
#COllect date is wrong based on incubation dates. MUST FIX before moving forward

names(SCOC_daily_by_instrument_expand)
SCOC_daily_by_instrument_expand$Collect_date<- NULL
SCOC_daily_by_instrument_expand$Collect_date_PLOT<- NULL

#delete rows with no SCOC
SCOC_daily_by_instrument_expand <- subset(SCOC_daily_by_instrument_expand,!is.na(SCOC_daily_by_instrument_expand$`SCOC mg C`))

#make dates recognizable
SCOC_daily_by_instrument_expand$Begin_incubation_DATE<- anydate(SCOC_daily_by_instrument_expand$Begin_incubation)

SCOC_daily_by_instrument_expand$End_incubation_DATE<- anydate(SCOC_daily_by_instrument_expand$End_incubation)

#average by date open
SCOC_daily_by_instrument_expand_a<-SCOC_daily_by_instrument_expand %>% 
  group_by(Begin_incubation_DATE, End_incubation_DATE) %>% 
  summarise_all(funs(mean), na.rm = TRUE) 
  
SCOC_daily_by_instrument_expand_a$Begin_incubation<- NULL
SCOC_daily_by_instrument_expand_a$End_incubation<- NULL
SCOC_daily_by_instrument_expand_a$Instrument<- NULL
SCOC_daily_by_instrument_expand_a$Year_fraction<- NULL

#Copy rows to yield number of collect days
SCOC_daily_by_instrument_expand <- SCOC_daily_by_instrument_expand_a[rep(row.names(SCOC_daily_by_instrument_expand_a), SCOC_daily_by_instrument_expand_a$Duration_open), 1:6]

#write results to file, copy down dates
#Excel formula=Collect date =IF(Date_open=Date_open in row above,cell above +1,Date_open)
#Replace all NA with blanks while we're in there

write.csv(SCOC_daily_by_instrument_expand, "SCOC.csv")

SCOC_fixed_dates <- read_csv("Z:/STORE/Files/PULSE/DATA/DATA CONSOLIDATION/Carbon/SCOC/SCOC_fixed_dates.csv")

SCOC_fixed_dates$Collect_date_PLOT<- anydate(SCOC_fixed_dates$Collect_date)

names(SCOC_fixed_dates)
SCOC_fixed_datesPLOT<- SCOC_fixed_dates %>% 
  dplyr::select(Collect_date_PLOT,"SCOC mg C" )


DailyAggregates <- read_csv("Z:/STORE/Files/PULSE/DATA/DATA CONSOLIDATION/AreaMeasurements/DailyAggregates.csv")
View(DailyAggregates)
names(DailyAggregates)

DailyAggregates$PhytoCover<- DailyAggregates$`PHYTO Percent cover VARS (for those post 2006)`
DailyAggregates$GelatCover<- DailyAggregates$`gelatinous % cover`

DailyAggregates$Collect_date_PLOT<- anydate(DailyAggregates$Collect_date)
DailyAggregates<- DailyAggregates %>% 
  dplyr::select(PhytoCover, GelatCover, Collect_date_PLOT)



Combined_comparisons<- full_join(Sediment_traps,SCOC_fixed_datesPLOT, by = "Collect_date_PLOT") %>% 
  full_join(DailyAggregates, by = "Collect_date_PLOT")

Combined_comparisons$InFill_Mass<- as.numeric(Combined_comparisons$InFill_Mass)
Combined_comparisons$TCInfil<- as.numeric(Combined_comparisons$TCInfil)
Combined_comparisons$Infill600OrgCarbon<- as.numeric(Combined_comparisons$Infill600OrgCarbon)
Combined_comparisons$SCOCmgC<- as.numeric(Combined_comparisons$`SCOC mg C`)
Combined_comparisons$PhytoCover<- as.numeric(Combined_comparisons$PhytoCover)
Combined_comparisons$GelatCover<- as.numeric(Combined_comparisons$GelatCover)
Combined_comparisons$TotalPC<- Combined_comparisons$PhytoCover+ Combined_comparisons$GelatCover

ts_mean <- function(variable) {
  var_mean<- mean(variable, na.rm = TRUE)
  return(var_mean)
}

ts_SD <- function(variable) {
  var_SD<- sd(variable, na.rm = TRUE)
  return(var_SD)
}

ts_pulsethreshold <- function(variable) {
  twosigma <- ts_SD(variable) * 2
  var_pulsethreshold <- ts_mean(variable) + twosigma
  return(var_pulsethreshold)
}

ts_decimal_pulsethreshold <- function(variable) {
  var_pulsethreshold_decimal <- round((ts_pulsethreshold(variable)), digits = 2)
  return(var_pulsethreshold_decimal)
}
ts_mean(Combined_comparisons$Infill600OrgCarbon)
ts_SD(Combined_comparisons$Infill600OrgCarbon)
ts_pulsethreshold(Combined_comparisons$Infill600OrgCarbon)
ts_decimal_pulsethreshold(Combined_comparisons$Infill600OrgCarbon)


Combined_comparisons$IsPulse <- ifelse(Combined_comparisons$Infill600OrgCarbon >= 23.58,"P","N")

write.csv(Combined_comparisons, "Combined_comparisons.csv")
