% fit_sigtemp - run show_sigvar first - need tvec and svec
% now need a calibration between tpods and sled (see t-p-pod
% directory)
% have these factors but revisit the T/Sigma calibration and 
% make a table from which to interpolate rather than the 
% polynomial above

% make t bins
%binsize = (25.1-18.9)/50; % 51 bins
binsize = (25.1-18.9)/30; % 31 bins
tbins = [18.9:binsize:25.1];
clear tline
aa=ones(length(tbins),length(tvec))* nan;
for ii = 1:length(tbins)-1
  bb=find(tvec >= tbins(ii) & tvec<= tbins(ii+1));
  aa(ii,1:length(bb))= bb;
  cc(ii)=length(bb);
  if 0
    if cc(ii) >= 3
      [p,s] = polyfit(tvec(bb),svec(bb),1);
      tline(ii,1)=polyval(p,tbins(ii));
      tline(ii,2)=polyval(p,tbins(ii+1));  
    else
      tline(ii,1) = min(svec(bb));
      tline(ii,2) = max(svec(bb));
    end
  else
    %tbin(ii) = mean(tbins(ii:ii+1));
    tbin(ii) = mean(tvec(bb));
    sigbin(ii) = mean(svec(bb));    
  end  
end

clf
plot(tvec,svec,'.')
hold on

if 0
  plot(tbins(1:end-1),tline(:,1),'.r')
  plot(tbins(2:end),tline(:,2),'.c')
else
  plot(tbin,sigbin,'.r')
end

xlabel('Temperature (^oC)')
ylabel('\sigma_\theta')
title(['EDDIES oc415-4 CTD downcasts 1-7 temperature/sigma' ...
       ' relationship/fit'])

legend('CTD','fit')
print -dpng ctd_tsig_fit.png

save ctd_tsig_fit tbin sigbin

