clear all;
close all;

cd ~/output/H04S1/amoc/
amocS1=importdata('H04S1_amoc.mat');
cd ~/output/H04S2/amoc/
amocS2=importdata('H04S2_amoc.mat');
cd ~/output/H04S3/amoc/
amocS3=importdata('H04S3_amoc.mat');
cd ~/output/H04S4/amoc/
amocS4=importdata('H04S4_amoc.mat');
cd ~/output/H04S5/amoc/
amocS5=importdata('H04S5_amoc.mat');
cd ~/output/H04/amoc/

nave=6; %half of running smooth.

%running smooth every 12 months: first time
len=length(amocS1);
mean_amocS1=zeros(1,len-2*nave);
mean_amocS2=zeros(1,len-2*nave);
mean_amocS3=zeros(1,len-2*nave);
mean_amocS4=zeros(1,len-2*nave);
mean_amocS5=zeros(1,len-2*nave);
for i=1+nave:len-nave
    mean_amocS1(i-nave)=mean(amocS1(i-6:i+6));
    mean_amocS2(i-nave)=mean(amocS2(i-6:i+6));
    mean_amocS3(i-nave)=mean(amocS3(i-6:i+6));
    mean_amocS4(i-nave)=mean(amocS4(i-6:i+6));
    mean_amocS5(i-nave)=mean(amocS5(i-6:i+6));
end

%running smooth every 12 months: second time
amocS1=mean_amocS1;
amocS2=mean_amocS2;
amocS3=mean_amocS3;
amocS4=mean_amocS4;
amocS5=mean_amocS5;

len=length(amocS1);
mean_amocS1=zeros(1,len-2*nave);
mean_amocS2=zeros(1,len-2*nave);
mean_amocS3=zeros(1,len-2*nave);
mean_amocS4=zeros(1,len-2*nave);
mean_amocS5=zeros(1,len-2*nave);
for i=1+nave:len-nave
    mean_amocS1(i-nave)=mean(amocS1(i-6:i+6));
    mean_amocS2(i-nave)=mean(amocS2(i-6:i+6));
    mean_amocS3(i-nave)=mean(amocS3(i-6:i+6));
    mean_amocS4(i-nave)=mean(amocS4(i-6:i+6));
    mean_amocS5(i-nave)=mean(amocS5(i-6:i+6));
end

%running smooth every 12 months: third time
amocS1=mean_amocS1;
amocS2=mean_amocS2;
amocS3=mean_amocS3;
amocS4=mean_amocS4;
amocS5=mean_amocS5;


len=length(amocS1);

for i=1+nave:len-nave
    mean_amocS1(i)=mean(amocS1(i-6:i+6));
    mean_amocS2(i)=mean(amocS2(i-6:i+6));
    mean_amocS3(i)=mean(amocS3(i-6:i+6));
    mean_amocS4(i)=mean(amocS4(i-6:i+6));
    mean_amocS5(i)=mean(amocS5(i-6:i+6));
end

%%running smooth again for every 12 months:forth time
amocS1=mean_amocS1;
amocS2=mean_amocS2;
amocS3=mean_amocS3;
amocS4=mean_amocS4;
amocS5=mean_amocS5;

len=length(amocS1);

for i=1+nave:len-nave
    mean_amocS1(i)=mean(amocS1(i-6:i+6));
    mean_amocS2(i)=mean(amocS2(i-6:i+6));
    mean_amocS3(i)=mean(amocS3(i-6:i+6));
    mean_amocS4(i)=mean(amocS4(i-6:i+6));
    mean_amocS5(i)=mean(amocS5(i-6:i+6));
end

mean_amoc=(mean_amocS1+mean_amocS2+mean_amocS3...
        +mean_amocS4+mean_amocS5)/5;
figure;
hold on;
plot(mean_amocS1,'-k','linewidth',2);
plot(mean_amocS2,'-k','linewidth',1);
plot(mean_amocS3,'--k','linewidth',2);
plot(mean_amocS4,'--k','linewidth',1);
plot(mean_amocS5,'-','linewidth',3,'color',[.5 .5 .5]);
% plot(mean_amoc(4:end-3),'-','color',[0.5 .5 .5],'linewidth',4);
% legend('H04S1','H04S2','H04S3','H04S4','H04S5','Mean');
legend('exp1','exp2','exp3','exp4','exp5');
set(gca,'xlim',[1 len-6],'fontsize',18,'fontweight','normal');
set(gca,'xtick',[12:120:len-6],'xticklabel',[1950:10:2007],'tickdir','out');
set(gca,'xgrid','on','ygrid','on','gridlinestyle',':','linewidth',2)
box on;
%title('AMOC during 1948-2006','fontsize',20,'fontweight','normal');
xlabel('Model Year','fontsize',20,'fontweight','normal');
ylabel('Sv (10^6 m^3/s)','fontsize',20,'fontweight','normal');
set(gcf,'position',[0 400 1024 768],'renderer','painters','PaperPositionMode','auto');
set(gca,'dataaspectratio',[50,1,1]);
print -painters -r300 -dpng H04_amoc_runsmoth.png
print -painters -r300 -depsc H04_amoc_runsmoth.eps
