# 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("Lobster024_cal.TXT", header=TRUE, skip=31, nrows=1, sep=',')
# Data File:
filnam<-"Lobster024.TXT"
Conditions<- "Lobster 2d-watch; H-LIX in sea water pH 7.5"
# 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

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

Time=HoursFrom


minAll<-min(AvgOrigin.ZuV)
maxAll<-max(AvgOrigin.ZuV)

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)), 
      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(-4000,-1000), 
      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, AvgOrigin.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, AvgOrigin.ZuV, 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+AvgOrigin.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(-4000,-1000), 
      ty='n', 
      main=paste(filnam, 'Zpos vs Time'))
      mtext("Probe Positions Z vs Time", line=-1.5)
      
lines(Time, Zpos, col="red", typ = 'b')

 
detach(XX)