clear all;close all;
load ab1202pc_db.mat;


for ii=1:59
    new_temppri(1:length(cnv_tcor(ii).temppri),ii)=cnv_tcor(ii).temppri(1:length(cnv_tcor(ii).temppri));
    new_tempsec(1:length(cnv_tcor(ii).tempsec),ii)=cnv_tcor(ii).tempsec(1:length(cnv_tcor(ii).tempsec));
    new_pressure(1:length(cnv_tcor(ii).pressure),ii)=cnv_tcor(ii).pressure(1:length(cnv_tcor(ii).pressure));
end;


depth_match = 3000;
%depth_match = 3100;
    shallow_depth_match = 900;
    ista_start = 3;
    ista_stop = 5; 
    temp_pressure = vertcat(cnv_tcor(ista_start:ista_stop).pressure);
    temp_pri = vertcat(cnv_tcor(ista_start:ista_stop).temppri);
    temp_sec = vertcat(cnv_tcor(ista_start:ista_stop).tempsec);
    temp_tdiff = temp_pri-temp_sec;

    II=find(temp_pressure>=depth_match);
    JJ=find(temp_pressure<depth_match & temp_pressure>=shallow_depth_match);
    [m_all_deep, b_all_deep, r, sm, sb, rms_err, mean_per_err, mean_per_err2] = ...
        do_mbari_fits2 (temp_pressure(II), temp_tdiff(II), 1);
    [m_all_shal, b_all_shal, r, sm, sb, rms_err, mean_per_err, mean_per_err2] = ...
        do_mbari_fits2 (temp_pressure(JJ), temp_tdiff(JJ), 1);
    
    
%depth_match = 3300;

m_use_deep = m_all_deep(1);
b_use_deep = b_all_deep(1);
%
% make sure T correction is continuous.
% to do that figure out correction at 3000 m
%
tdiff_3000 = m_use_deep * depth_match + b_use_deep;
%
% now use an upper ocean fit that starts at:
% (3000., tdiff_3000)
% Equation for new line is:
% m_use_shall = (tdiff - tdiff_3000) / (pr - 3000.)
% rearranging
% tdiff = m_use_shall*pr + m_use_shall*(-3000)+tdiff_3000
% tdiff = m_use_shall*pr + new_b_shall

m_use_shal = m_all_shal(1);
b_use_shal = m_use_shal*(-depth_match)+tdiff_3000;


ifit_to_use = 1;
for ista_idx = ista_start: ista_stop
    II=find(cnv_tcor(ista_idx).pressure>=depth_match);
    JJ=find(cnv_tcor(ista_idx).pressure<depth_match);
    tdiff = cnv_tcor(ista_idx).temppri - cnv_tcor(ista_idx).tempsec;
    figure;
        plot (cnv_tcor(ista_idx).pressure(II), tdiff(II))
        hold on
        title(['Station ' num2str(cnv_tcor(ista_idx).station)])
        plot(cnv_tcor(ista_idx).pressure(II), m_use_deep * cnv_tcor(ista_idx).pressure(II) + b_use_deep,  'k', 'linewidth', 2)

        plot(depth_match, tdiff_3000, 'ok','markersize',20)
        
        plot (cnv_tcor(ista_idx).pressure(JJ), tdiff(JJ),'r')
        plot(cnv_tcor(ista_idx).pressure(JJ), m_use_shal * cnv_tcor(ista_idx).pressure(JJ) + b_use_shal,  'k', 'linewidth', 2)
        set(gca,'ylim',[-0.002 +0.002]) 
end

close all;

sts=2;
ste=6;
 %depth_match = 3300;
 shallow_depth_match = 900;
for i=sts:ste,
    II=find(cnv_tcor(i).pressure>=depth_match);
    JJ=find(cnv_tcor(i).pressure<depth_match);
    
    %tcor=p*m+b
    t_cor_deep(II,i-1) = m_use_deep*cnv_tcor(i).pressure(II)+b_use_deep;
    new_temppri(II,i) = new_temppri(II,i)-t_cor_deep(II,i-1);
    cnv_tcor(i).t_cor_deep(II) = m_use_deep*cnv_load(i).pressure(II)+b_use_deep;
    cnv_load(i).temppri(II)=cnv_load(i).temppri(II)-cnv_tcor(i).t_cor_deep(II)';
    
    t_cor_shal(JJ,i-1) = m_use_shal*cnv_tcor(i).pressure(JJ)+b_use_shal;
    new_temppri(JJ,i) = new_temppri(JJ,i)-t_cor_shal(JJ,i-1);
    cnv_tcor(i).t_cor_shal(JJ) = m_use_shal*cnv_load(i).pressure(JJ)+b_use_shal;
    cnv_load(i).temppri(JJ)=cnv_load(i).temppri(JJ)-cnv_tcor(i).t_cor_shal(JJ)';
    
    cnv_load(i).tcor=1;
end;



clf
stsr=0;
stse=58;
%bigplot;
%plot origional T differences for stations 1:5
f1
i=0;
for i=stsr:stse,
    i=i+1;
 
    plot(cnv_tcor(i).temppri-cnv_tcor(i).tempsec,cnv_tcor(i).pressure,'b')
    hold on;
    revyaxis
end;
   
  %plot corrected T differences for stations 1:5 that we like as good
  j=0;
  for j=stsr:stse,
      j=j+1;
      plot(cnv_load(j).temppri-cnv_load(j).tempsec,cnv_load(j).pressure,'g')
      hold on;    
      revyaxis
        
  end;
font(14)
ylabel('Pressure ','fonts',16)
xlabel('Temperature Sensor Difference','fonts',16)
title('Temperature Correction Comparison: Stations 1 - 5','fonts',16)

f2
for i=stsr:stse,
    i=i+1;
 
    plot(cnv_tcor(i).temppri-cnv_tcor(i).tempsec,cnv_tcor(i).pressure,'r')
    hold on;
    revyaxis
end;

for j=3,
    II=find(cnv_tcor(j).pressure>=depth_match);
    JJ=find(cnv_tcor(j).pressure<depth_match);
    plot( m_use_deep * cnv_load(j).pressure(II) + b_use_deep, cnv_load(j).pressure(II), 'k', 'linewidth', 2)
    hold on;
    plot(m_use_shal * cnv_load(j).pressure(JJ) + b_use_shal, cnv_load(j).pressure(JJ), 'k', 'linewidth', 2)
    xlim(-0.005,0.005)
    text(0.002,4000,['RMS = +/- ',num2str(rms_err(1))],'fonts',16)
    font(14)
    ylabel('Pressure ','fonts',16)
    xlabel('Temperature Sensor Difference','fonts',16)
    title('Primary Temperature Correction: Stations 1 - 5','fonts',16)
end;
err=rms_err(1)

%Save new structures
if 0,
save('ab1202pc_db.mat','cnv_load','-append')
save('ab1202pc_db.mat','cnv_tcor','-append')
end;