load ~/NC/grid.mat
load ~/Aerosol_Variaiton/IRUN/Z500/ncfile/PNA_Month.mat
load ~/Aerosol_Variaiton/IRUN/PRECT/ncfile/PRECT_monthly.mat
month=1:1800;

a=sum(PO_Month);
b=sum(NE_Month);

PRECT_PO_month=zeros(144,96,a);
PRECT_NE_month=zeros(144,96,b);

n=0;
m=0;
for monthID=1:length(month)
    if PO_Month(monthID)==1
        n=n+1;
        PRECT_PO_month(:,:,n)=PRECT_MONTH(:,:,monthID);
    end
    if NE_Month(monthID)==1
        m=m+1;
        PRECT_NE_month(:,:,m)=PRECT_MONTH(:,:,monthID);
    end
end

PRECT_PO=mean(PRECT_PO_month,3);
PRECT_NE=mean(PRECT_NE_month,3);

PRECT_PNA=PRECT_PO-PRECT_NE;

save('PRECT_PNA_composite_monthly.mat','PRECT_PO','PRECT_NE','PRECT_PNA')

nccreate('PRECT_PNA_composite_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('PRECT_PNA_composite_monthly.nc','lat', lat)
ncwriteatt('PRECT_PNA_composite_monthly.nc','lat','units','degrees_north')
ncwriteatt('PRECT_PNA_composite_monthly.nc','lat','long_name','Latitude')
nccreate('PRECT_PNA_composite_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('PRECT_PNA_composite_monthly.nc','lon', lon)
ncwriteatt('PRECT_PNA_composite_monthly.nc','lon','units','degrees_east')
ncwriteatt('PRECT_PNA_composite_monthly.nc','lon','long_name','Longitude')
nccreate('PRECT_PNA_composite_monthly.nc','PRECT_PO','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_PNA_composite_monthly.nc','PRECT_PO', PRECT_PO)
nccreate('PRECT_PNA_composite_monthly.nc','PRECT_NE','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_PNA_composite_monthly.nc','PRECT_NE', PRECT_NE)
nccreate('PRECT_PNA_composite_monthly.nc','PRECT_PNA','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_PNA_composite_monthly.nc','PRECT_PNA', PRECT_PNA)
ncdisp('PRECT_PNA_composite_monthly.nc')
