
function dataOut = calcO2(calCoeffs,U,T,S,P)

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

% Calculates oxygen in [ml/l] from raw SBE-63 oxygen sensor phase, 
%  temperature, salinity and pressure. Transcribed from SBE-63 manual, App. 1
% Results are off by ~0.3 ml/l compared to SBE D.P. output, need to check.

% calCoeffs are from SBE calibration
% U is O2 phase delay output in us
% T is CTD temperature in C
% S is CTD salinity
% P is CTD pressure in dbar

%salinity correction constants
 SolB0 = -6.24523e-3;
 SolB1 = -7.37614e-3;
 SolB2 = -1.03410e-2;
 SolB3 = -8.17083e-3;
 SolC0 = -4.88682e-7;

%re-write calCoeffs per SBE-63 cal sheet
 A0 = calCoeffs(1);
 A1 = calCoeffs(2);
 A2 = calCoeffs(3);
 B0 = calCoeffs(4);
 B1 = calCoeffs(5);
 C0 = calCoeffs(6);
 C1 = calCoeffs(7);
 C2 = calCoeffs(8);
 E  = calCoeffs(9);

%convert phase delay output [us] to V
 V = U./39.457071;

%Stern-Volmer constant equation
 Ksv = C0 + (C1*T) + (C2*T.^2);

%Salinity correction
 Ts = log((298.15 - T) ./ (273.15 + T));
 Scorr = exp( S .* (SolB0 + (SolB1 * Ts) + (SolB2 * Ts.^2) + (SolB3 * Ts.^3)) ...
      + (SolC0 * S.^2) );

%pressure correction
 K = T + 273.15;
 Pcorr = exp(E*P./K);

 %convert o2 to ml/l
  dataOut = (( (A0 + A1*T + A2*V.^2) ./ (B0 + B1*V) - 1) ./ Ksv ) .* ...
    Scorr .* Pcorr;
