clear;
close all;
load CPS_results.mat;

set(0, 'DefaultAxesFontName', 'Helvetica','DefaultAxesFontWeight','bold')
set(0, 'DefaultTextFontName', 'Helvetica','DefaultTextFontWeight','bold')  
set(0,'defaultaxesfontsize',11); set(0,'defaulttextfontsize',11);

med_rec=nanmedian(rec,2);

for i=1:size(diagn,2)
    errors(:,i)=diagn(i).error;
end

for i=1:size(diagn,2)
    RE(:,i)=diagn(i).RE;
end

for i=1:size(diagn,2)
    CE(:,i)=diagn(i).CE;
end

for i=1:size(diagn,2)
    Rsq(:,i)=diagn(i).Rsq;
end

for i=1:size(diagn,2)
    RMSE(:,i)=diagn(i).rmse;
end

med_error=nanmedian(RMSE,2);

med_RE=nanmedian(RE,2);
med_CE=nanmedian(CE,2);
med_Rsq=nanmedian(Rsq,2);

RE_cont=NaN(size(datayearclip));

for i=1:length(diagn(1).jumps)-1
    RE_cont(diagn(1).jumps(i)+1:diagn(1).jumps(i+1))=med_RE(i);
end

CE_cont=NaN(size(datayearclip));

for i=1:length(diagn(1).jumps)-1
    CE_cont(diagn(1).jumps(i)+1:diagn(1).jumps(i+1))=med_CE(i);
end

Rsq_cont=NaN(size(datayearclip));

for i=1:length(diagn(1).jumps)-1
    Rsq_cont(diagn(1).jumps(i)+1:diagn(1).jumps(i+1))=med_Rsq(i);
end
%%
figure(1); clf;
s1=shadedErrorBar(datayearclip(1:size(med_error,1)),med_rec(1:size(med_error,1)),med_error.*2,{'color','r','linewidth',1});
hold on;

s3=plot(sstyear_mod,sst_ano,'k','linewidth',1);
set(gca,'xlim',[min(datayearclip) max(datayearclip)],'linewidth',1);
ylabel('SST ANOMALY (REL. TO 1961-1990)');
xlabel('YEAR');
L1=legend([s1.mainLine,s1.patch,s3],'CORAL RECONSTRUCTED SST','2\sigma error','HADISST, 15S-20N, 40-100E','Location','Northwest');
legend('boxoff');
title('TROPICAL ATLANTIC OCEAN SST RECONSTRUCTION','fontsize',14);
pbaspect([1 1 1]);
%printeps('indian_cps_west.eps');
%print(gcf,'-painters','-depsc2','indian_cps_west.eps');
%%
figure(2); clf;
set(gcf,'pos',[300 300 400 600]);

a=subplot(3,1,1);
%nosig=find(RE_cont<0.4);
s1=shadedErrorBar(datayearclip(1:size(med_error,1)),med_rec(1:size(med_error,1)),med_error.*2,{'color','r','linewidth',1});
hold on;

%s2=shadedErrorBar(datayearclip(nosig),med_rec(nosig),med_error(nosig).*2,{'color',[.4 .4 .4],'linewidth',1});
%hold on;

s3=plot(sstyear_mod,sst_ano,'k','linewidth',1);
set(gca,'xlim',[min(datayearclip) max(datayearclip)],'linewidth',1,'xticklabel',[],'ylim',[-1.5 1.5]);
%title('RECONSTRUCTED SST');
%ylabel('SST ANOMALY (REL. TO 1961-1990)');
L1=legend([s1.mainLine,s3],'CPS SST','HADISST','location','northwest','orientation','horizontal');
legend('boxoff');
set(a,'pos',[0.1300 0.7 0.8 0.28]);

b=subplot(3,1,2);
plot(datayearclip,nproxy_i,'color','k','linewidth',1);
text(min(datayearclip)+10,max(nproxy_i)-1,'# PROXIES');
set(gca,'xlim',[min(datayearclip) max(datayearclip)],'linewidth',1,'xticklabel',[],'yaxislocation','right','ylim',[0 max(nproxyclip)+1]);
set(b,'pos',[0.1300 0.4 0.8 0.28]);

c=subplot(3,1,3);
r1=plot(datayearclip,Rsq_cont,'color','k','linewidth',1);
hold on;
r2=plot(datayearclip,RE_cont,'color','b','linewidth',1);
hold on;
r3=plot(datayearclip,CE_cont,'color','r','linewidth',1);
set(gca,'xlim',[min(datayearclip) max(datayearclip)],'linewidth',1,'ylim',[-.3 1]);
L3=legend([r1,r2,r3],'Rsq','RE','CE','location','northwest','orientation','horizontal');
legend('boxoff');
xlabel('YEAR');
set(c,'pos',[0.1300 0.1 0.8 0.28]);
%print2eps('cps_noscreen_weighted.eps');
