# 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("http://gyre.umeoce.maine.edu/data/gomoos/buoy/php/view_csv_file.php?ncfile=/data/gomoos/buoy/archive/E0501/realtime/E0501.aanderaa.cond.realtime.nc")
datenow=date()
writeLines(L1, "E0501_aanderaa_cond_now.csv")
# ...
# recruiting headers does not work
#Do it manually after reading line 1 with readLines
filnam="E0501_aanderaa_cond_now.csv"
headers=c('Time','current_speed','current_direction','temp','current_u','current_v','salinity','sigma_t','conductivity')
L2=read.csv(filnam, skip=1,header=FALSE)
attr(L2,'names')<-headers

quartz(width=12.5,height=6)
sec2day=24*60*60
attach(L2)
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=2,col='black', xlab='Day')
points(time, salinity, typ='p',lwd=1,col='red')
detach(L2)
mtext(datenow, adj=0.95)
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=2,col='black', xlab='Day')
points(time, current_direction, typ='p',lwd=1,col='red')
detach(L2)
mtext(datenow, adj=0.95)
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=2,col='black', xlab='Day')
points(time, conductivity, typ='p',lwd=1,col='red')
detach(L2)
mtext(datenow, adj=0.95)
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=3,col='black', xlab='Day')
points(time, current_v, typ='p',lwd=2,col='red')
lines(time, 2*current_u, typ='l',lwd=2,col='green')
points(time, 2*current_u, typ='p',lwd=2,col='blue')
detach(L2)
mtext(datenow, adj=0.95)
title("Current u & 2*v vs Day from beginning")
mtext(filnam)
