load ~/NC/grid
year=1:150;

SST_ANN=ncread('~/Aerosol_Variaiton/DRUN/SST/ncfile/SST.nc','SST_ANN');
SST_DJF=ncread('~/Aerosol_Variaiton/DRUN/SST/ncfile/SST.nc','SST_DJF');
SST_JJA=ncread('~/Aerosol_Variaiton/DRUN/SST/ncfile/SST.nc','SST_JJA');
SST_SON=ncread('~/Aerosol_Variaiton/DRUN/SST/ncfile/SST.nc','SST_SON');


for i=1:144
    for j=1:96
        temp=mean(SST_ANN(i,j,:),3);
        SST_ANN(i,j,:)=SST_ANN(i,j,:)-temp;
        temp=mean(SST_DJF(i,j,:),3);
        SST_DJF(i,j,:)=SST_DJF(i,j,:)-temp;
        temp=mean(SST_JJA(i,j,:),3);
        SST_JJA(i,j,:)=SST_JJA(i,j,:)-temp;
        temp=mean(SST_SON(i,j,:),3);
        SST_SON(i,j,:)=SST_SON(i,j,:)-temp;
    end
end

NINO34_ANN=zeros(150,1);
NINO34_ANN(:)=mean(mean(SST_ANN(77:97,46:51,:),2),1);
NINO34_DJF=zeros(150,1);
NINO34_DJF(:)=mean(mean(SST_DJF(77:97,46:51,:),2),1);
NINO34_JJA=zeros(150,1);
NINO34_JJA(:)=mean(mean(SST_JJA(77:97,46:51,:),2),1);
NINO34_SON=zeros(150,1);
NINO34_SON(:)=mean(mean(SST_SON(77:97,46:51,:),2),1);


save('NINO34.mat','NINO34_ANN','NINO34_DJF','NINO34_JJA','NINO34_SON')

nccreate('NINO34.nc','Year','Dimensions',{'time',Inf},'Format','classic')
ncwrite('NINO34.nc','Year', year)
ncwriteatt('NINO34.nc','Year','long_name','Year')
nccreate('NINO34.nc','NINO34_ANN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('NINO34.nc','NINO34_ANN', NINO34_ANN)
nccreate('NINO34.nc','NINO34_DJF','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('NINO34.nc','NINO34_DJF', NINO34_DJF)
nccreate('NINO34.nc','NINO34_JJA','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('NINO34.nc','NINO34_JJA', NINO34_JJA)
nccreate('NINO34.nc','NINO34_SON','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('NINO34.nc','NINO34_SON', NINO34_SON)
ncdisp('NINO34.nc')