load ~/NC/grid.mat
load ~/Aerosol_Variaiton/ERUN/SST/ncfile/NINO34.mat
load ~/Aerosol_Variaiton/ERUN/PRECT/ncfile/PRECT.mat
year=1:150;

y=zeros(150,1);
x=zeros(150,1);

regress_DJF=zeros(144,96);
sig_DJF=zeros(144,96);
for i=1:144
    for j=1:96
        y(:)=PRECT_DJF(i,j,:)*1e3*60*60*24;
        x(:)=NINO34_DJF;
        X=[ones(150,1),x];
        [b,bint,r,rint,stats]=regress(y,X);
        regress_DJF(i,j)=b(2);
        sig_DJF(i,j)=double(stats(3)<0.05);        
    end
end


regress_JJA=zeros(144,96);
sig_JJA=zeros(144,96);
for i=1:144
    for j=1:96
        y(:)=PRECT_JJA(i,j,:)*1e3*60*60*24;
        x(:)=NINO34_JJA;
        X=[ones(150,1),x];
        [b,bint,r,rint,stats]=regress(y,X);
        regress_JJA(i,j)=b(2);
        sig_JJA(i,j)=double(stats(3)<0.05);        
    end
end

regress_DJF(isnan(regress_DJF))=-999;
regress_JJA(isnan(regress_JJA))=-999;

save('regress_PRECT_NINO34.mat','regress_DJF','regress_JJA')

nccreate('regress_PRECT_NINO34.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('regress_PRECT_NINO34.nc','lat', lat)
ncwriteatt('regress_PRECT_NINO34.nc','lat','units','degrees_north')
ncwriteatt('regress_PRECT_NINO34.nc','lat','long_name','Latitude')
nccreate('regress_PRECT_NINO34.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('regress_PRECT_NINO34.nc','lon', lon)
ncwriteatt('regress_PRECT_NINO34.nc','lon','units','degrees_east')
ncwriteatt('regress_PRECT_NINO34.nc','lon','long_name','Longitude')
nccreate('regress_PRECT_NINO34.nc','regress_DJF','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('regress_PRECT_NINO34.nc','regress_DJF', regress_DJF)
nccreate('regress_PRECT_NINO34.nc','sig_DJF','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('regress_PRECT_NINO34.nc','sig_DJF', sig_DJF)
nccreate('regress_PRECT_NINO34.nc','regress_JJA','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('regress_PRECT_NINO34.nc','regress_JJA', regress_JJA)
nccreate('regress_PRECT_NINO34.nc','sig_JJA','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('regress_PRECT_NINO34.nc','sig_JJA', sig_JJA)
ncdisp('regress_PRECT_NINO34.nc')
