
%calibration coefficients for SBE49 S/N 161, 2017/5/14
%pressure
pa0 =  8.580044e-01;
pa1 = 1.108702e-01;
pa2 = -2.247276e-09;
ptempa0 =  5.929376e+01;
ptempa1 = -3.132766e+01;
ptempa2 =  3.934270e+00;
ptca0 =   5.247615e+05;
ptca1 =  1.857443e+00;
ptca2 = 2.311606e-03;
ptcb0 = 2.769200e+01;
ptcb1 = 4.400000e-03;
ptcb2 = 0.;

%temperature
a0 =  8.391167e-04;
a1 =  2.789202e-04;
a2 =  -1.769508e-06;
a3 =  1.831480e-07;

%conductivity
g =  -1.000098e+00;
h =  1.542017e-01;
i =  -4.018137e-04;
j =  5.724026e-05;
cpcor = -9.570000e-08;
ctcor = 3.250000e-06;

%first recalculate pressure
t = ptempa0 + ptempa1.*fastcatlog(:,pressure_temp_comp_voltage_reading)+ ...
        ptempa2.*(fastcatlog(:,pressure_temp_comp_voltage_reading).^2);
x = fastcatlog(:,pressure_counts) - ptca0 - ptca1.*t - ptca2.*t.^2;
n = x.*ptcb0 ./ (ptcb0 + ptcb1.*t + ptcb2.*(t.^2));
my_pres = ((pa0 + pa1.*n + pa2.*(n.^2)) - 14.70).*.6894757;

%then calculate temperature
mv = (fastcatlog(:,temp_counts) - 524288.0) ./ 1.6e7;
r  = (mv .* 2.295e10 + 9.216e8) ./ (6.144e4 - mv.*5.3e5);
ln_r = log(r);
my_temp = (1 ./ ( a0 + a1.*ln_r + a2.*ln_r.^2 + a3.*ln_r.^3)) - 273.15;

%then conductivity
my_cond_freq = fastcatlog(:, cond_frequency)./1000.0;
my_cond = (g + h.*my_cond_freq.^2 + i*my_cond_freq.^3 + ...
    j.*my_cond_freq.^4)./ (1 + ctcor.*my_temp + cpcor.*my_pres);
my_cond  = my_cond .* 10.0;

%now recalculate salinity
my_salinity = sw_salt(my_cond./SW_C3515, my_temp, my_pres);
my_svel = SW_SVEL(my_salinity, my_temp, my_pres);



