clear all
close all

cd ../ctd/raw_data
ctdpath=pwd;
cd ../../uctd

dp=1;p_ctd=[1:dp:1500];
sal1_ctd=NaN.*ones(length(p_ctd),3);sal2_ctd=sal1_ctd;
temp1_ctd=sal1_ctd;temp2_ctd=sal1_ctd;
for ii=1:3
    load([ctdpath,'/pne11_cast',int2str(ii),'.mat'],'sal1','sal2','temp1','temp2','p');
    % downcast
    [ip1,pp]=fitsoak(p,ii,'verb');
    ip2=find(p==max(p));ip2=ip2(1);
    p=p(ip1:ip2);
    sal1=sal1(ip1:ip2);sal2=sal2(ip1:ip2);
    temp1=temp1(ip1:ip2);temp2=temp2(ip1:ip2);
    clear ip1 ip2 pp
    for iii=1:length(p_ctd)
        k=find(p>p_ctd(iii)-dp./2 & p<=p_ctd(iii)+dp./2);
        if ~isempty(k)
           sal1_ctd(iii,ii)=nmedian(sal1(k));
           sal2_ctd(iii,ii)=nmedian(sal2(k));
           temp1_ctd(iii,ii)=nmedian(temp1(k));
           temp2_ctd(iii,ii)=nmedian(temp2(k));
        end
        clear k
    end
    clear sal1 sal2 temp1 temp2 p
    close all
end
casts_ctd=[1 2 2 3];
sal1_ctd=[sal1_ctd(:,1:2) sal1_ctd(:,2) sal1_ctd(:,3)];
sal2_ctd=[sal2_ctd(:,1:2) sal2_ctd(:,2) sal2_ctd(:,3)];
temp1_ctd=[temp1_ctd(:,1:2) temp1_ctd(:,2) temp1_ctd(:,3)];
temp2_ctd=[temp2_ctd(:,1:2) temp2_ctd(:,2) temp2_ctd(:,3)];

% ctd1 -> uctd27, ctd2 -> uctd46&47, ctd3 -> uctd60
load uctd_data.mat t lat lon sal_raw temp p
casts=[27 46 47 60];
t=t(casts);lat=lat(casts);lon=lon(casts);
sal_raw=sal_raw(:,casts);temp=temp(:,casts);

Dsal1=sal1_ctd-sal_raw;
Dsal2=sal2_ctd-sal_raw;
Dtemp1=temp1_ctd-temp;
Dtemp2=temp2_ctd-temp;

[a1,b1]=sal_fit(Dsal1(:,3),temp(:,3));
[a2,b2]=sal_fit(Dsal2(:,3),temp(:,3));

save uctd_ctd_cmp.mat a1 b1

for ii=1:length(casts)
        
    figure(1)
    clf
    orient tall

    subplot(5,2,1);
    plot(Dsal1(:,ii),sal1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;
    ylabel('S_1');
    title(['PNE11: CTD',int2str(casts_ctd(ii)),' - UCTD',int2str(casts(ii))],'fontweight','bold');

    subplot(5,2,2);
    plot(Dsal2(:,ii),sal1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;
    title(['PNE11: CTD',int2str(casts_ctd(ii)),' - UCTD',int2str(casts(ii))],'fontweight','bold');

    subplot(5,2,3);
    plot(Dsal1(:,ii),sal2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;
    ylabel('S_2');

    subplot(5,2,4);
    plot(Dsal2(:,ii),sal2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;

    subplot(5,2,5);
    plot(Dsal1(:,ii),temp1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;
    ylabel('T_1 [{\circ}C]');

    subplot(5,2,6);
    plot(Dsal2(:,ii),temp1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;

    subplot(5,2,7);
    plot(Dsal1(:,ii),temp2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;
    ylabel('T_2 [{\circ}C]');

    subplot(5,2,8);
    plot(Dsal2(:,ii),temp2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;

    subplot(5,2,9);
    plot(Dsal1(:,ii),p_ctd,'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[0 1500],'ytick',[0:300:1500],'ydir','reverse');
    grid on;
    xlabel('{\Delta}S_1');
    ylabel('P [dbar]');
    text(-.65,1350,['mean: ',num2str(round(nmean(Dsal1(:,ii)).*1e2)./1e2)],'horizontalalignment','left','verticalalignment','middle');
    text(-.65,1050,['std: ',num2str(round(nstd(Dsal1(:,ii)).*1e2)./1e2)],'horizontalalignment','left','verticalalignment','middle');
    
    subplot(5,2,10);
    plot(Dsal2(:,ii),p_ctd,'b.','markersize',5);
    set(gca,'xlim',[-.7 .1],'xtick',[-.7:.2:.1],'ylim',[0 1500],'ytick',[0:300:1500],'ydir','reverse');
    grid on;
    xlabel('{\Delta}S_2');
    text(-.65,1350,['mean: ',num2str(round(nmean(Dsal2(:,ii)).*1e2)./1e2)],'horizontalalignment','left','verticalalignment','middle');
    text(-.65,1050,['std: ',num2str(round(nstd(Dsal2(:,ii)).*1e2)./1e2)],'horizontalalignment','left','verticalalignment','middle');
    
    print('-depsc',['uctd',int2str(casts(ii)),'_ctd',int2str(casts_ctd(ii)),'_sal.eps']);

    figure(1)
    clf
    orient tall

    subplot(5,2,1);
    plot(Dtemp1(:,ii),sal1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;
    ylabel('S_1');
    title(['PNE11: CTD',int2str(casts_ctd(ii)),' - UCTD',int2str(casts(ii))],'fontweight','bold');

    subplot(5,2,2);
    plot(Dtemp2(:,ii),sal1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;
    title(['PNE11: CTD',int2str(casts_ctd(ii)),' - UCTD',int2str(casts(ii))],'fontweight','bold');

    subplot(5,2,3);
    plot(Dtemp1(:,ii),sal2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;
    ylabel('S_2');

    subplot(5,2,4);
    plot(Dtemp2(:,ii),sal2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[34.7 37.5],'ytick',[34.7:.7:37.5]);
    grid on;

    subplot(5,2,5);
    plot(Dtemp1(:,ii),temp1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;
    ylabel('T_1 [{\circ}C]');

    subplot(5,2,6);
    plot(Dtemp2(:,ii),temp1_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;

    subplot(5,2,7);
    plot(Dtemp1(:,ii),temp2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;
    ylabel('T_2 [{\circ}C]');

    subplot(5,2,8);
    plot(Dtemp2(:,ii),temp2_ctd(:,ii),'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[4 29],'ytick',[4:5:29]);
    grid on;

    subplot(5,2,9);
    plot(Dtemp1(:,ii),p_ctd,'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[0 1500],'ytick',[0:300:1500],'ydir','reverse');
    grid on;
    xlabel('{\Delta}T_1 [{\circ}C]');
    ylabel('P [dbar]');
    text(-.6125,1350,['mean: ',num2str(round(nmean(Dtemp1(:,ii)).*1e2)./1e2),'{\circ}C'],...
         'horizontalalignment','left','verticalalignment','middle');
    text(-.6125,1050,['std: ',num2str(round(nstd(Dtemp1(:,ii)).*1e2)./1e2),'{\circ}C'],...
         'horizontalalignment','left','verticalalignment','middle');
    
    subplot(5,2,10);
    plot(Dtemp2(:,ii),p_ctd,'b.','markersize',5);
    set(gca,'xlim',[-.7 .7],'xtick',[-.7:.35:.7],'ylim',[0 1500],'ytick',[0:300:1500],'ydir','reverse');
    grid on;
    xlabel('{\Delta}T_2 [{\circ}C]');
    text(-.6125,1350,['mean: ',num2str(round(nmean(Dtemp2(:,ii)).*1e2)./1e2),'{\circ}C'],...
         'horizontalalignment','left','verticalalignment','middle');
    text(-.6125,1050,['std: ',num2str(round(nstd(Dtemp2(:,ii)).*1e2)./1e2),'{\circ}C'],...
         'horizontalalignment','left','verticalalignment','middle');
     
    print('-depsc',['uctd',int2str(casts(ii)),'_ctd',int2str(casts_ctd(ii)),'_temp.eps']);

end
