# Does not read this watch-mode file!
# Read in callibration data

rm(list=ls())
layout(matrix(c(1,2), 1, 1, byrow = TRUE))
opar<- par(mar=c(5, 3, 4, 3) + 0.1)
# Adjust skip to get Conc and V(Conc) headers.
# Calibration File:
Z<- read.table("L2H-3Dgrid_cal1.txt", header=TRUE, skip=9, nrows=1, sep=',')
# Data File:
# Adjust multiplier to make 
iConc<-7:9
iVconc<-10:12
Conc<-as.real(Z[iConc])/1000
Vconc<-as.real(Z[iVconc])
Lconc<-log10(Conc)
ELconc<-predict(lm(Lconc~Vconc))
plot(Vconc, ELconc, ty='b', main="-pH vs mV CALLIBRATION")
points(Vconc, Lconc,col='red')
points(90, -5.25, col='red')
text(93, -5.25, "Obs[-pH]", col='red', pos=4)
points(90, -5.35, col='black')
text(93, -5.35, "E[-pH]", col='black', pos=4)
points(90, -5.45, col='blue', cex=2)
text(93, -5.45, "New E[-pH]", col='blue', pos=4)

NewVC <- data.frame(Vconc = c(60, 80, 100, 120, 140, 160))
points(t(NewVC), predict(lm(Lconc~Vconc), newdata=NewVC), cex=2, col='blue')
browser()

layout(matrix(c(1:9), 3, 3, byrow = TRUE))    # was 2 by 4
filnam<-"L2H0822.TXT"
Conditions<- "Lobster 2d-watch; H-LIX in sea water pH 7.5"

XX<-read.table(filnam, header=TRUE, skip=3, nrows=491, sep=',')
print(names(XX))
attach(XX)

Time=HoursFrom


minAll<-min(AvgZ.OuV)
maxAll<-max(AvgZ.OuV)

Ypos<- ProbeY- sum(ProbeY)/length(ProbeY)
Xpos<- ProbeX- sum(ProbeX)/length(ProbeY)
Zpos<- ProbeZ- sum(ProbeZ)/length(ProbeY)

#  plot(c(min(Xpos),max(Xpos)), c(min(Ypos),max(Ypos)), 
plot(c(min(Xpos),max(Xpos)), c(-200,max(Ypos)), 
      ty='n', 
      main=paste(filnam, 'Ypos vs Xpos'))
      mtext("Probe Positions X & Y", line=-1.5)
      
lines(Xpos, Ypos, col="blue", typ='b')

plot(c(min(Xpos),max(Xpos)), c(-2200, 3200), 
      ty='n', 
      main=paste(filnam, 'Zpos vs Xpos'))
      mtext("Probe Positions X and Z", line=-1.5)
      
lines(Xpos, Zpos, col="red", typ = 'b')

# browser()


plot(c(min(Xpos),max(Xpos)), c(minAll,maxAll), 
      ty='n', 
      main=paste(filnam, 'Obs[uVdiff]'))
      mtext("Observed uV difference (xyz) = (rgb)", line=-1.5)
lines(Xpos, AvgZ.OuV, col="blue")
# browser()



plot(c(min(Time),max(Time)), c(minAll,maxAll), 
      ty='n', 
      main=paste(filnam, 'Obs[uVdiff]'))
      mtext("Observed uV difference (xyz) = (rgb)", line=-1.5)
lines(Time, AvgZ.OuV, col="blue")
# browser()

minAll<-min(Origin.1.mV)
maxAll<-max(Origin.1.mV)

NewV0 <- data.frame(Vconc = Origin.1.mV)

plot(Time, predict(lm(Lconc~Vconc), newdata=NewV0), 
      ty='b', 
      main=paste(filnam, 'E[-pH]'))
      mtext("E[-pH] = by callibration", line=-1.5)
      mtext(Conditions, line=-3)

Oconc<- 10^(predict(lm(Lconc~Vconc), newdata=NewV0))
# browser()
plot(Time, Oconc, 
      ty='b', 
      main=paste(filnam, 'E[H+]'))
      mtext("E[H+] = 10^(E[-pH]) at probe origin", line=-1.5)
      mtext(Conditions, line=-3)

Zconc<- 10^(predict(lm(Lconc~Vconc), newdata=NewV0+AvgZ.OuV/1000))
dZH<- Zconc-Oconc 
minAll<-min(dZH)
maxAll<-max(dZH)
dHtot<-abs(dZH)
Hflux<- dHtot*10^19   
# browser()
  
plot(c(min(Time),max(Time)), c(minAll,maxAll), 
      ty='n', 
      main=paste(filnam, '[H+]dz'))
      mtext("[H+]dxyz", line=-1.5, col='red')
      mtext(Conditions, line=-3)

lines(Time, dZH, col="blue", ty='b')

plot(c(min(Time),max(Time)), c(-2200, 3200), 
      ty='n', 
      main=paste(filnam, 'Zpos vs Time'))
      mtext("Probe Positions Z vs Time", line=-1.5)
      
lines(Time, Zpos, col="red", typ = 'b')

plot(c(min(Time),max(Time)), c(-1000, 5000), 
      ty='n', 
      main=paste(filnam, 'Xpos vs Time'))
      mtext("Probe Positions X vs Time", line=-1.5)
      
lines(Time, Xpos, col="red", typ = 'b')
 
detach(XX)
browser()

# Late data using new motion protocol.

layout(matrix(c(1:9), 3, 3, byrow = TRUE))    # was 2 by 4
filnam<-"L2H0822late.TXT"
Conditions<- "New PreMove: Lobster watch; ASW pH 7.?"

XX<-read.table(filnam, header=TRUE, skip=495, nrows=-1, sep=',')
print(names(XX))
attach(XX)

Time=HoursFrom

minXav<-min(AvgXo.XuV)
maxXav<-max(AvgXo.XuV)

minYav<-min(AvgYo.YuV)
maxYav<-max(AvgYo.YuV)

minZav<-min(AvgZo.ZuV)
maxZav<-max(AvgZo.ZuV)

minAll<-min(c(minXav,minYav,minZav))
maxAll<-max(c(maxXav,maxYav,maxZav))



Ypos<- ProbeY- sum(ProbeY)/length(ProbeY)
Xpos<- ProbeX- sum(ProbeX)/length(ProbeY)
Zpos<- ProbeZ- sum(ProbeZ)/length(ProbeY)

#  plot(c(min(Xpos),max(Xpos)), c(min(Ypos),max(Ypos)), 
plot(c(min(Xpos),max(Xpos)), c(-200,max(Ypos)), 
      ty='n', 
      main=paste(filnam, 'Ypos vs Xpos'))
      mtext("Probe Positions X & Y", line=-1.5)
      
lines(Xpos, Ypos, col="blue", typ='b')

plot(c(min(Xpos),max(Xpos)), c(-2200, 3200), 
      ty='n', 
      main=paste(filnam, 'Zpos vs Xpos'))
      mtext("Probe Positions X and Z", line=-1.5)
      
lines(Xpos, Zpos, col="red", typ = 'b')

# browser()


plot(c(min(Xpos),max(Xpos)), c(minAll,maxAll), 
      ty='n', 
      main=paste(filnam, 'Obs[uVdiff]'))
      mtext("Observed uV difference (xyz) = (rgb)", line=-1.5)
lines(Xpos, AvgXo.XuV, col="red")
lines(Xpos, AvgYo.YuV, col="green")
lines(Xpos, AvgZo.ZuV, col="blue")
# browser()



plot(c(min(Time),max(Time)), c(minAll,maxAll), 
      ty='n', 
      main=paste(filnam, 'Obs[uVdiff]'))
      mtext("Observed uV difference (xyz) = (rgb)", line=-1.5)
lines(Time, AvgXo.XuV, col="red")
lines(Time, AvgYo.YuV, col="green")
lines(Time, AvgZo.ZuV, col="blue")
# browser()

minXo<-min(Xo.1.mV)
maxXo<-max(Xo.1.mV)

minYo<-min(Xo.1.mV)
maxYo<-max(Xo.1.mV)

minZo<-min(Zo.1.mV)
maxZo<-max(Zo.1.mV)

minAll<-min(c(minXo,minYo,minZo))
maxAll<-max(c(minXo,minYo,minZo))
minT<- min(Time)
maxT<- max(Time)

NewV0 <- data.frame(Vconc = ((Xo.1.mV+Yo.1.mV+Zo.1.mV)/3))

plot(c(minT,maxT), c(min(NewV0), max(NewV0)), typ='n', main=paste(filnam, 'E[-pH]'))
lines(Time, predict(lm(Lconc~Vconc), newdata=NewV0),  typ='b')
      mtext("E[-pH] = by callibration", line=-1.5)
      mtext(Conditions, line=-3)

Oconc<- 10^(predict(lm(Lconc~Vconc), newdata=NewV0))
# browser()
plot(Time, Oconc, 
      ty='b', 
      main=paste(filnam, 'E[H+]'))
      mtext("E[H+] = 10^(E[-pH]) at probe origin", line=-1.5)
      mtext(Conditions, line=-3)

Zconc<- 10^(predict(lm(Lconc~Vconc), newdata=NewV0+AvgZo.ZuV/1000))
dZH<- Zconc-Oconc 
minAll<-min(dZH)
maxAll<-max(dZH)
dHtot<-abs(dZH)
Hflux<- dHtot*10^19   
# browser()
  
plot(c(min(Time),max(Time)), c(minAll,maxAll), 
      ty='n', 
      main=paste(filnam, '[H+]dz'))
      mtext("[H+]dxyz", line=-1.5, col='red')
      mtext(Conditions, line=-3)

lines(Time, dZH, col="blue", ty='b')

plot(c(min(Time),max(Time)), c(-2200, 3200), 
      ty='n', 
      main=paste(filnam, 'Zpos vs Time'))
      mtext("Probe Positions Z vs Time", line=-1.5)
      
lines(Time, Zpos, col="red", typ = 'b')

plot(c(min(Time),max(Time)), c(-1000, 5000), 
      ty='n', 
      main=paste(filnam, 'Xpos vs Time'))
      mtext("Probe Positions X vs Time", line=-1.5)
      
lines(Time, Xpos, col="red", typ = 'b')
 
detach(XX)
