# 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_diagnostics_short_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_diagnostics_short_realtime.csv"
headers=c('Time','gps_lat','gps_lon')
L1=read.csv(filnam, skip=1,header=FALSE)
attr(L1,'names')<-headers

L2=read.csv("E0501_diagnostics_short_realtime.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)]
datefirst=Time[1]
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
med_lat=median(gps_lat); med_lon=median(gps_lon)
plot(time, gps_lat-med_lat, typ='l',lwd=2,col='black', xlab='Day')

out=lowess(time, gps_lat-med_lat,f=0.02)
lines(out, col='green', lwd=3)
lines(time, gps_lon-med_lon, typ='l',lwd=2,col='red')
out=lowess(time, gps_lon-med_lon,f=0.02)
lines(out, col='purple', lwd=3)

mtext("plot(gps_lat)", adj=0.05,line=-1)
mtext("smooth(gps_lat)", adj=0.33, line=-1, col='green')
mtext("plot(gps_lon)", adj=0.66,line=-1, col='red')
mtext("smooth(gps_lon)", adj=0.95, line=-1, col='purple')


mtext(datenow, adj=0.95)
mtext(datefirst, adj=0.05)
detach(L2)
title("gps_lat from beginning")
mtext(filnam)

quartz(width=7,height=7)
attach(L2)
lcol=length(gps_lon)
#cols=rainbow(lcol)
cols=heat.colors(lcol)
plot(gps_lon, gps_lat, typ='n',lwd=4,col=cols)
points(gps_lon, gps_lat, typ='p',lwd=4,col=cols)
points(gps_lon, gps_lat, typ='l',lwd=1,col='black', lty=2)
mtext(datenow, adj=0.95, line=-1)
text(gps_lon[1], gps_lat[2],'begin')
text(gps_lon[lcol], gps_lat[lcol],'end')
detach(L2)
title("gps lat vs lon from beginning")
mtext(filnam)
