function O2 = calc_O2_SBE43(O2V,T,S,P,O2_offset, Tcor, Pcor,SOc,BOc,time,Olag,units);
%% To calculate Oxygen from sbe voltage 
%% Reference: W.B. Owens and R.C. Millard, 1985. A new algorithm for CTD oxygen
%%	calibration, J. Phys. Oceanogr. 15:621-631. 
%%	Also, described in SeaBird application note.
pltit = 'n'; disp(['   Pressure should be in dB']);

%% Frame shifting for oxygen calculations, plumbing lag and sensor response
%% SeaBird advances oxygen 1 to 5 seconds relative to pressure.
%% Oxygen calculations account for both lags, 
%% but pressure is not directly used in the oxygen calculation. 
%% Due to plumbing lag, use T/S (t-plumbing lag = shift forward) ; 
%% Shift oxygen voltages forward (t-total lag = shift forward);
%% Define lags
Ofreq = mean(diff(time)); 	%  oxygen sampling frequency 
plag = Olag(1);	pshift = round(plag/Ofreq); 	% plumbing lag and shift
rlag = Olag(2); rshift = round(rlag/Ofreq); 	% response lag and shift
tlag = plag+rlag; tshift = round(tlag/Ofreq);	% total lag and shift
%% T and S only need plumbing lag shift (+ chamber mixing lag)
%% Estimate is 3 seconds
Tfs=[repmat(NaN,pshift,1); T([1:length(T)-pshift])];
Sfs=[repmat(NaN,pshift,1); T([1:length(T)-pshift])];

%%----------------------------------
%% Oxygen voltage
%%----------------------------------
disp(['   Minimum of oxygen voltage ' num2str(min(O2V)) ' V']);
disp(['   Maximum of oxygen voltage ' num2str(max(O2V)) ' V']);
disp(['   Mean of oxygen voltage ' num2str(mean(O2V)) ' V']);
docdt = [NaN;[diff(O2V)./diff(time)]];	% slope of oxygen current (uA/sec);

%%----------------------------------
%% Oxygen saturation: f(T,S); ml/l
%%----------------------------------
TK = 273.15+Tfs;	% degrees Kelvin
A1 = -173.4292; A2 = 249.6339; A3 = 143.3483; A4 = -21.8492;
B1 = -0.033096; B2 = 0.014259; B3 =  -0.00170;
OXSAT = exp(A1 + A2*(100./TK) + A3*log(TK/100) + A4*(TK/100) + [Sfs .* (B1 + B2*(TK/100) + (B3*(TK/100).*(TK/100)))] );

%%----------------------------------
%% Oxygen concentration (mL/L)
%%----------------------------------
%% Constants
tau=0;
%% Frame-shifted temperature
O2 = [SOc * ((O2V+O2_offset)+(tau*docdt)) + BOc * exp(-0.03*T)].*exp(Tcor*T + Pcor*P).*OXSAT;

%%----------------------------------
%% Frame Shifted Oxygen concentration
%% to account for total lag
%% (T and S for O2 calculation only need pluming lag shift)
%%----------------------------------
Ofs=[O2([tshift+1:length(O2)]); repmat(NaN,tshift,1)];
O2=Ofs;

if strcmp(units,'umolkg')==1
 %%----------------------------------
 %% Convert to umol/kg  
 %%----------------------------------
 %% SeaBird equations are for ml/l computations
 %%  Can convert OXSAT at atmospheric pressure to mg/l by 1.4276
 %%  Convert dissolved O2 to mg/l using density of oxygen = 1.4276 kg/m^3
 dens=sw_dens(Sfs,Tfs,P);
 O2 = (O2 * 1.4276) .* (1e6./(dens*32));
end
