read_campbell <- function(filename) {
    columnnames <- as.character(unlist(read.csv(filename, nrow = 1, 
        skip = 1, header = FALSE)))
    dat <- read.csv(filename, skip = 4, header = FALSE)
    names(dat) <- columnnames
    dat$TIMESTAMP <- as.POSIXct(dat$TIMESTAMP)
    return(dat)
}


folder <- "C:/Users/Eric.Krantz/Dropbox (RESPEC)/R/Projects/Cargill/Cleveland Bulkhead/"

filename <- paste(folder, "Stress_RebarMeter.dat", sep = "")
dat <- read_campbell(filename)
colnames(dat)[c(7, 8)] <- c("Therm1", "Therm2")

T_Off <- 22.7
T_Comp <- -10.9
digit_zero <- -7161
GF <- 0.9409

newDigs <- dat$Freq^2/1000 + digit_zero
newStrain <- newDigs * GF
dat$Strain <- newStrain
dat$Strain_comp <- GF * (newDigs - T_Comp * (dat$Therm_Rebar - 
    T_Off))

dat1 <- filter(dat, Therm_Rebar < 25)
dat <- filter(dat, dat$TIMESTAMP > "2018-01-02 18:00:00")
dat <- filter(dat, lubridate::second(dat$TIMESTAMP) == 0)
ggplot(dat1, aes(x = TIMESTAMP, y = Strain)) + geom_point(alpha = 0.1) + 
    ggtitle("uStrain calculated from frequency", subtitle = "no temperature correction") + 
    theme(plot.title = element_text(hjust = 0.5)) + theme(plot.subtitle = element_text(hjust = 0.5))

ggplot(dat1, aes(x = TIMESTAMP, y = Strain_comp)) + geom_point(alpha = 0.1) + 
    ggtitle("uStrain calculated from frequency", subtitle = "With temperature correction") + 
    theme(plot.title = element_text(hjust = 0.5)) + theme(plot.subtitle = element_text(hjust = 0.5))

ggplot(dat1, aes(x = TIMESTAMP, y = Therm_Rebar)) + geom_point(alpha = 0.1) + 
    ggtitle("Temperature, C") + theme(plot.title = element_text(hjust = 0.5))

ggplot(dat1, aes(Therm_Rebar, Strain_comp, color = TIMESTAMP)) + 
    geom_point(alpha = 0.5) + labs(title = "Strain vs Temp", 
    subtitle = "Colored by timestamp") + theme(plot.title = element_text(hjust = 0.5)) + 
    theme(plot.subtitle = element_text(hjust = 0.5))