# Read buoy data from http://gyre.umeoce.maine.edu/data/gomoos/buoy/html/E05.html
# Data stored in a chosen directory which is now current.

rm(list=ls())
L1=readLines("E0501_aanderaa_cond_realtime.csv", n=1)
L1=paste('c(',L1,')',sep='')
# ...
# recruiting headers does not work
#Do it manually after reading line 1 with readLines
filnam="E0501_aanderaa_cond_realtime-1.csv"
headers=c('Time','current_speed','current_direction','temp','current_u','current_v','salinity','sigma_t','conductivity')
L1=read.csv(filnam, skip=1,header=FALSE)
attr(L1,'names')<-headers

L2=read.csv("E0501_aanderaa_cond_realtime-1.csv", skip=1,header=FALSE)
attr(L2,'names')<-headers
quartz(width=12.5,height=6)
sec2day=24*60*60
attach(L2)
datenow=Time[length(Time)]
time1_0=as.double(as.POSIXct(strptime(Time[1], "%Y-%m-%d")))/sec2day
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
plot(time, salinity, typ='l',lwd=4,col='black', xlab='Day')
mtext(datenow, adj=0.95)
detach(L2)

attach(L1)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
lines(time, salinity, typ='b', lty=3,lwd=1,col='red')
detach(L1)
title("Salinity vs Day from beginning")
mtext(filnam)

# current_direction
quartz(width=12.5,height=6)
attach(L2)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
plot(time, current_direction, typ='l',lwd=4,col='black', xlab='Day')
mtext(datenow, adj=0.95)
detach(L2)

attach(L1)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
lines(time, current_direction, typ='b', lty=3,lwd=1,col='red')
detach(L1)
title("Direction-of-Current vs Day from beginning")
mtext(filnam)

# current_direction
quartz(width=12.5,height=6)
attach(L2)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
plot(time, conductivity, typ='l',lwd=4,col='black', xlab='Day')
mtext(datenow, adj=0.95)
detach(L2)

attach(L1)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
lines(time, conductivity, typ='b', lty=3,lwd=1,col='red')
detach(L1)
title("Conductivity vs Day from beginning")
mtext(filnam)
# current_u & current_v
quartz(width=12.5,height=6)
attach(L2)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
plot(time, current_v, typ='l',lwd=6,col='black', xlab='Day')
lines(time, 2*current_u, typ='b',lwd=6,col='green')
mtext(datenow, adj=0.95)
detach(L2)

attach(L1)
time=as.double(as.POSIXct(strptime(Time, "%Y-%m-%d %H:%M")))/sec2day - time1_0
lines(time, current_v, typ='l', lty=3,lwd=2,col='red')
lines(time, 2*current_u, typ='b', lty=3,lwd=2,col='blue')
detach(L1)
title("Current u & v vs Day from beginning")
mtext(filnam)
