%O2_CTPC      Calculate conductivity, temperature, and pressure.
%   [TEMP,COND] = O2_CTPC( PRESSURE, COND, TEMP, IDSTR ) takes raw sensor
%   values pressure (bar), conductivity frequency output (hz), and
%   temperature frequency output (hz) and returns temperature (deg C),
%   conductivity (S/m).  Choose IDSTR from {'xan0895', 'aph0396',
%   'ody0894'} to specify the calibration coefficients of the SBE CTD
%   instrumentation on board the Odyssey II vehicles.
%
%   B. A. Moran, 07/16/96

%----------------------------------------------------------------------
%                      Copyright (C) 1994-1996
%               Massachussetts Institute of Technology
%              Sea Grant Underwater Vehicles Laboratory
%----------------------------------------------------------------------
%     $Revision: 1.1 $			$Date: 2000/01/10 19:08:14 $
%----------------------------------------------------------------------

function [temp,cond,mask] = o2_ctpc( mpres, mcond_hz, mtemp_hz, idstr )

if ( nargin < 4 )
    errormsg( 'missing identification string' );
end

if strcmp( idstr, 'ody0894' ) == 1

    temp_a = 3.68019025e-03;
    temp_b = 5.99942973e-04;
    temp_c = 1.64477590e-05;
    temp_d = 3.19181044e-06;
    temp_f0 = 5797.18;

    cond_a = 1.9654850e-06;
    cond_b = 3.80027175e-01;
    cond_c = -3.75374877e+00;
    cond_d = 3.27469045e-05;
    cond_m = 4.8;
    eps = -9.57e-08;

elseif strcmp( idstr, 'xan0795' ) == 1 | strcmp( idstr, 'xan0895' ) == 1
%  Calibration Coefficients (BAM 10/19/95)

    temp_a = 3.68167114e-03;
    temp_b = 5.99991159e-04;
    temp_c = 1.62427360e-05;
    temp_d = 3.01282712e-06;
    temp_f0 = 5783.44;

    cond_a = 1.98764111e-06;
    cond_b = 3.79993124e-01;
    cond_c = -3.75323199e+00;
    cond_d = 1.49729678e-04;
    cond_m = 4.8;
    eps = -9.57e-08;

elseif strcmp( idstr, 'aph0396' ) == 1

    temp_a =  3.68029893e-03;
    temp_b =  6.02846243e-04;
    temp_c =  1.61611692e-05;
    temp_d =  2.24647817e-06;
    temp_f0 = 2892.171;

    cond_a =  1.01993560e-07;
    cond_b =  4.85199142e-01;
    cond_c = -4.14559146e+00;
    cond_d = -5.82067262e-05;
    cond_m =  6.1;
    eps = -9.57e-08;

end

% First do temp conversion

mask = find( mpres > 0.15 & finite(mtemp_hz) & mcond_hz > 3500 );

temp = NaN*zeros(size(mtemp_hz));
temp(mask) = -273.15 + 1 ./ polyval([temp_d temp_c temp_b temp_a], ...
				log(temp_f0./mtemp_hz(mask)));

% Now do the conductivity conversion

if nargout > 1

    pdbar = 10*mpres(mask);
    cond_khz = 0.001*mcond_hz(mask);

    cond = NaN*zeros(size(mcond_hz));
    cond(mask) = (cond_a*cond_khz.^cond_m + cond_b*cond_khz.^2 + ...
	cond_c + cond_d*temp(mask))./(10*(1 + eps*pdbar));
end
