
% -- 
% Jeff Sevadjian
% Monterey Bay Aquarium Research Institute
% 7700 Sandholdt Rd
% Moss Landing, CA 95039, USA
% ph: +1 831-775-1888

% Calculates E0 from pH calibration voltage, temp, CRM batch

% NOTE: THE OUTPUT IS DIFFERENT FROM THE .XLS VERSION OF CO2SYS, 
%  LIKELY BECAUSE THE CHOICES OF CONSTANTS ARE NOT THE SAME.
%  THE BEST WE CAN DO IS A DIFFERENCE OF ~0.005 TOTAL SCALE.
%  FOR THE PURPOSES OF THE STANDARD PH VALUES FOR THE CALIBRATIONS,
%  WE'LL CONTINUE USING THE .XLS VERSION, NOT THIS FUNCTION!

%constants
 F = 96487;
 R = 8.31451;

%cal results
 TEMPIN = 20.17; %[20.86 20.82 20.82] %[C]
 Vout = 0.05509; %[0.07075 0.06576 0.05366] %[V]

% %Batch 144
%  PAR1 = 2238.60; %TA
%  PAR2 = 2031.53; %DIC
%  SAL = 33.571;
%  SI = 2.5; %these should be in umol/kg SW (not mmol/kg SW as in the CO2SYS xls)
%  PO4 = 0.31;

% %Batch 152
%  PAR1 = 2216.94; %TA
%  PAR2 = 2020.88; %DIC
%  SAL = 33.371;
%  SI = 5.7;
%  PO4 = 0.51;

% %Batch 156
%  PAR1 = 2236.51; %TA
%  PAR2 = 2052.72; %DIC
%  SAL = 33.455;
%  SI = 5;
%  PO4 = 0.62;

%Batch 163
%  PAR1 = 2215.34; %TA
%  PAR2 = 2039.43; %DIC
%  SAL = 33.362;
%  SI = 3.9;
%  PO4 = 0.43;

%Batch 187
 PAR1 = 2204.98; %TA
 PAR2 = 2002.98; %DIC
 SAL = 33.602;
 SI = 3.2;
 PO4 = 0.44;

%CO2SYS settings
 PAR1TYPE = 1; %TA
 PAR2TYPE = 2; %DIC
 pHSCALEIN = 1; %total scale (input, not sure if matters)
 K1K2CONSTANTS = 4; %Merbach
 KSO4CONSTANTS = 1; %Dickson
 TEMPOUT = TEMPIN;
 PRESIN = 0;
 PRESOUT = 0;

[RESULT,HEADERS,NICEHEADERS] = CO2SYS(PAR1,PAR2,PAR1TYPE,PAR2TYPE,...
  SAL,TEMPIN,TEMPOUT,PRESIN,PRESOUT,SI,PO4,pHSCALEIN,...
  K1K2CONSTANTS,KSO4CONSTANTS);

%results
 pHstd = RESULT(18)
 Ts = TEMPIN + 273.15
 E0 = Vout - (pHstd * R * Ts * log(10) / F)
