# FILE: process_excel_filesV1.R
# PROJECT:  River Temperature
# AUTHOR:   Pete Versteegen
# DATE:     5/29/2019
# REF: https://www.datacamp.com/community/tutorials/r-tutorial-read-excel-into-r


fileX <- "../data/Brookfield-SangRunTemps-2015.txt"

x <- read.table(file=fileX, sep="\t", header=TRUE)
summary(x)

write(fileX, file="../data/exceedances.txt", append=TRUE )
#========1=========2=========3=========4=========5=========6=========7=========8=========9=========0
# LOADING NEEDED LIBRARIES
library(lubridate)
date.origin <- lubridate::origin

# Array with the names of the month
m.names <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

(n <- length(x$Date))     # Length of the data file

# Extract the data and lists them under a variable that's easier to think of.
day.date <- as.character(x$Date)	# The days of the month
day.time <- as.character(x$Time)	# The times of the day
temp     <- x$Temp.C


# Paste date and time together and then create POSIX objects
string.date <- paste(day.date, day.time, sep=" ")
# string.date <- as.character(string.date)
true.date   <- as.POSIXct(string.date, format="%m/%d/%y %H:%M:%S", origin = date.origin)   # Different than Version 1
day.date    <- day(true.date)
month.date  <- month(true.date)


# Summaries of the data vectors
summary(string.date)
str(string.date)
head(string.date)
length(string.date)

summary(true.date)
str(true.date)
head(true.date)
length(true.date)

summary(month.date)
str(month.date)
head(month.date)
length(month.date)

summary(day.date)
str(day.date)
head(day.date)
length(day.date)

summary(temp)
str(temp)
head(temp)
length(temp)



# Define the month and year to be shown on the plots
period  <- year(true.date[1])
period

#========1=========2=========3=========4=========5=========6=========7=========8=========9=========0
# We're now going to plot the temperature profiles for each day
file.out <- paste("../results/sangrun_temps_", period, "_plots.pdf", sep="")
pdf(file=file.out, onefile=TRUE)

# Make a panel of pictures.
par(mfrow=c(2,2))        # 2 rows, 2 columns per page
par(col="blue")            # default colour to blue.


# Initialize the vector to be used as temporary store of vules for the day
wtemp <- vector()
wtime <- as.POSIXct(rep(NA, 10))

# Define the extremes for the plot region
(maxtemp <- ceiling(max(temp)))
(mintemp <- floor(min(temp)))

# Some initializations
k <- 0		# Keeps track of the number of data points for the day

# Start processing for each day of the month
for(i in 2:n){
	# First collect all the temperature data points for one and the same day
	# k is the index that collects how many points there are for that day
	if(day.date[i] == day.date[i-1]){
		k <- k + 1
		wtemp[k] <- temp[i]
		wtime[k] <- true.date[i]
		next
	}
	if(k < 2) {
		k <- 0
		next
	}


# List the times at which the 25C limit has been exceeded
	(nc <- length(wtemp))
	for(j in 1:nc){
		if(wtemp[j] >= 25) {
			temptime <- as.character(wtime[j])
			added <- paste("Temperature = ", wtemp[j], " at date and time = ", temptime, sep="" )
			print(added)
			write(added, file="../data/exceedances.txt", append=TRUE )
		}
	}


	d <- day.date[i-1]		       # The current day
	m <- month(true.date[i-1])     # The current month
	y <- year(true.date[i-1])      # The current year
	period <- paste(d, m.names[m], y, sep=" ")
	# print(period)
	
	# print(lubridate::origin)
	# print(head(wtime))
	# print(head(wtemp))
	wtime <- as.POSIXct(wtime, origin = date.origin)
	# print(head(wtime))

# Write a file with the data in a consistant format
	new.date <- as.POSIXct(wtime, origin = date.origin)
	# print(str(new.date))
	data <- data.frame(new.date, wtemp)
	names(data) <- c("DateTime", "Temp.C")
	period1 <- paste(d, m.names[m], y, sep="")
	fileY <- paste("../results2/youghtemp", period1, ".txt", sep="")
	# print(period)
	# print(fileY)
	write.table(data, file=fileY, row.names=FALSE, quote=FALSE, sep="\t")

# We now plot the baseline graph having collected all of the points for a day
	header.text <- paste("Sang Run Station Temperatures\n", period, sep="")
	plot(wtime, wtemp, main=header.text, type="n",
		 xlab="Time of day",
		 ylab="Water Temperature, ºC",
		 xaxt="n", ylim=c(mintemp, maxtemp))

# The following determines if the release duration should be plotted
# If so do so
	day.date.now <- day.date[i]

	# Horizontal axis labeling and background grid.
	dtime <- wtime[k] - wtime[1]
	dtemp <- maxtemp - mintemp
	vlines <- seq(from=wtime[1], to=wtime[k], by=dtime/12)
	hlines <- c(14, 16, 18, 20, 22, 24, 26)
	axis(1, vlines,  labels=c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24))
	abline(h=hlines, v=vlines, lty=2, col="grey")

# To put text on the lower right hand side of the plot (like to script name and date)
# Serves well with identification later on as to what was done with what.
	now.date <- Sys.Date()
	script.txt <- paste("Computed with \"process_excel_filesV1.R\" ", now.date, "  ", sep="")
	mtext(script.txt, side=1,  cex=0.50, line=-1, adj=1)
	# Horizontal line for the 25 ºC limit
	abline(h=25.0,col="red",lty=2, lwd=2)
	points(wtime, wtemp, type="o", cex=0.5, col="blue")

	wtemp[1] <- temp[i]
	wtime[1] <- true.date[i]
	k <- 0
}

dev.off()
warnings()
