load ~/NC/grid.mat
load ~/Aerosol_Variaiton/IRUN/SST/ncfile/ENSO_Month.mat
load ~/Aerosol_Variaiton/IRUN/PRECT/ncfile/PRECC_monthly.mat
month=1:1800;

a=sum(EN_Month);
b=sum(LL_Month);

PRECC_EN_month=zeros(144,96,a);
PRECC_LL_month=zeros(144,96,b);

n=0;
m=0;
for monthID=1:length(month)
    if EN_Month(monthID)==1
        n=n+1;
        PRECC_EN_month(:,:,n)=PRECC_MONTH(:,:,monthID);
    end
    if LL_Month(monthID)==1
        m=m+1;
        PRECC_LL_month(:,:,m)=PRECC_MONTH(:,:,monthID);
    end
end

PRECC_EN=mean(PRECC_EN_month,3);
PRECC_LL=mean(PRECC_LL_month,3);

PRECC_ENSO=PRECC_EN-PRECC_LL;

save('PRECC_ENSO_composite_monthly.mat','PRECC_EN','PRECC_LL','PRECC_ENSO')

nccreate('PRECC_ENSO_composite_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('PRECC_ENSO_composite_monthly.nc','lat', lat)
ncwriteatt('PRECC_ENSO_composite_monthly.nc','lat','units','degrees_north')
ncwriteatt('PRECC_ENSO_composite_monthly.nc','lat','long_name','Latitude')
nccreate('PRECC_ENSO_composite_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('PRECC_ENSO_composite_monthly.nc','lon', lon)
ncwriteatt('PRECC_ENSO_composite_monthly.nc','lon','units','degrees_east')
ncwriteatt('PRECC_ENSO_composite_monthly.nc','lon','long_name','Longitude')
nccreate('PRECC_ENSO_composite_monthly.nc','PRECC_EN','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECC_ENSO_composite_monthly.nc','PRECC_EN', PRECC_EN)
nccreate('PRECC_ENSO_composite_monthly.nc','PRECC_LL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECC_ENSO_composite_monthly.nc','PRECC_LL', PRECC_LL)
nccreate('PRECC_ENSO_composite_monthly.nc','PRECC_ENSO','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECC_ENSO_composite_monthly.nc','PRECC_ENSO', PRECC_ENSO)
ncdisp('PRECC_ENSO_composite_monthly.nc')
