# 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("L2LWAT_cal.TXT", header=TRUE, skip=0, nrows=1, sep=',')
# Adjust multiplier to make 
iConc<-7:8
iVconc<-10:11
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")
plot(Vconc, Lconc, 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)

# Log File:
filnam<-"L2Lwat_9logs.txt"
ZZ<-read.table(filnam, header=TRUE, nrows=-1, sep=',')
attach(ZZ)
print(ZZ[,c(1,6,7)])
detach(ZZ)

browser()
# Data File:
filnam<-"L2LWAT.TXT"
Conditions<- "Lobster 2L-watch; H-LIX in SW and ASW mixtures"

layout(matrix(c(1:2), 2, 1, byrow = TRUE))    # was 2 by 4

# XX<-read.table(filnam, header=TRUE, skip=109, nrows=1200, sep=',')
XX<-read.table(filnam, header=TRUE, skip=109, nrows=750, sep=',')
print(names(XX))
attach(XX)

minAll<-min(min(Xo.X_1.uV),min(Zo.Z_1.uV))
maxAll<-max(max(Xo.X_1.uV),max(Zo.Z_1.uV))
MinT<- min(HoursFrom)
MaxT<- max(HoursFrom)

# plot(c(MinT,MaxT), c(minAll,maxAll), 
plot(c(MinT,MaxT), c(-125,100), 
      ty='n', 
      main=paste(filnam, 'Obs[uVdiff]'))
      mtext("Observed uV difference (x&z) = (r&b)", line=-1.5)
# lines(HoursFrom, Xo.X_1.uV, col="red")
# lines(HoursFrom, Zo.Z_1.uV, col="blue")
# 0.893414 LogEntry Started with real sea water till now. we will wait a bit and replace with real sea water again.

SW1<-1:62
lines(HoursFrom[SW1], Xo.X_1.uV[SW1], col="red")
lines(HoursFrom[SW1], Zo.Z_1.uV[SW1], col="blue")
text(HoursFrom[SW1[1]],75,"SW1", pos=4)

SW2<-75:165
lines(HoursFrom[SW2], Xo.X_1.uV[SW2], col="red")
lines(HoursFrom[SW2], Zo.Z_1.uV[SW2], col="blue")
text(HoursFrom[SW2[1]],75,"SW2", pos=4)

ASW1<-173:288
lines(HoursFrom[ASW1], Xo.X_1.uV[ASW1], col="red")
lines(HoursFrom[ASW1], Zo.Z_1.uV[ASW1], col="blue")
text(HoursFrom[ASW1[1]],75,"ASW", pos=4)

CaMg<-302:415
lines(HoursFrom[CaMg], Xo.X_1.uV[CaMg], col="red")
lines(HoursFrom[CaMg], Zo.Z_1.uV[CaMg], col="blue")
text(HoursFrom[CaMg[1]],75,"ASW+CaMg", pos=4)

Mg<-421:597
lines(HoursFrom[Mg], Xo.X_1.uV[Mg], col="red")
lines(HoursFrom[Mg], Zo.Z_1.uV[Mg], col="blue")
text(HoursFrom[Mg[1]],75,"ASW+Mg", pos=4)

CaMg2<-605:763
lines(HoursFrom[CaMg2], Xo.X_1.uV[CaMg2], col="red")
lines(HoursFrom[CaMg2], Zo.Z_1.uV[CaMg2], col="blue")
text(HoursFrom[CaMg2[1]],75,"ASW+CaMg", pos=4)

browser()


minAll<-min(c(Xo_1.mV, Zo_1.mV))
maxAll<-max(c(Xo_1.mV, Zo_1.mV))
mVavg <- (Xo_1.mV + Zo_1.mV)/2 

plot(HoursFrom, mVavg, 
      ty='b', 
      main=paste(filnam, 'electrode mV'))
      mtext("Observed mV", line=-1.5)
      mtext(Conditions, line=-3)

Ylev<-47
text(HoursFrom[SW1[1]],Ylev,"SW1", pos=4)
text(HoursFrom[SW2[1]],Ylev,"SW2", pos=4)
text(HoursFrom[ASW1[1]],Ylev,"ASW", pos=4)
text(HoursFrom[CaMg[1]],Ylev,"ASW+CaMg", pos=4)
text(HoursFrom[Mg[1]],Ylev,"ASW+Mg", pos=4)
text(HoursFrom[CaMg2[1]],Ylev,"ASW+CaMg", pos=4)

#      mtext("E[-pH] = by callibration", line=-1.5)

detach(XX)