
function dataOut = calcCO2(CO2cal,xCO2meas,xCO2zero,stndSF,T,tempi,sali)

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

% Corrects raw xCO2 data for zero-offset and standard measurements;
%  Applies temperature calibration; 
%  Converts xCO2 to pCO2 using sea-surface T, S.

% Inputs:
%  CO2cal is [A B C D]
%  xCO2meas is measured xCO2 in air or water stage
%  xCO2zero is measured xCO2 in 'zero' stage
%  stndSF is a scale factor [ratio of known to measured standard conc'n]
%  T is temperature [C] in air or water stream
%  tempi is sea surface temperature [C] interpolated from CTD
%  sali is sea surface salinity interpolated from CTD

%subtract zero
 xCO2 = xCO2meas - xCO2zero;

%apply T-cal
 xCO2conv = CO2cal(1) + CO2cal(2)*T + CO2cal(3)*xCO2 + CO2cal(4).*T.*xCO2;

%correct for standard measurement
 xCO2convStnd = xCO2conv.*stndSF;

%convert xCO2 to pCO2
 TempK = tempi + 273.15;
 VPWP = exp(24.4543 - 67.4509 * (100./TempK) - 4.8489 * ...
   log(TempK./100));
 VPCorrWP = exp(-0.000544 * sali);
 VPSWWP = VPWP .* VPCorrWP;
 VPFac = 1 - VPSWWP;
 dataOut = xCO2convStnd.*VPFac;
