load ~/NC/grid.mat
load ~/Aerosol_Variaiton/IRUN/AOD/ncfile/POCL_Month.mat
load ~/Aerosol_Variaiton/IRUN/PRECT/ncfile/PRECL_monthly.mat
month=1:1800;

%PRECL_MONTH=PRECL_MONTH/24; %mm/day->mm/hr

%a=sum(PO_Month);
%b=sum(CL_Month);

PRECL_PO_month=zeros(144,96,594);
PRECL_CL_month=zeros(144,96,594);


for i=1:length(lon)
for j=1:length(lat)
n=0;
m=0;
for monthID=1:length(month)
    if PO_Month(i,j,monthID)==1
        n=n+1;
        PRECL_PO_month(i,j,n)=PRECL_MONTH(i,j,monthID);
    end
    if CL_Month(i,j,monthID)==1
        m=m+1;
        PRECL_CL_month(i,j,m)=PRECL_MONTH(i,j,monthID);
    end
end
end
end

PRECL_PO=mean(PRECL_PO_month,3);
PRECL_CL=mean(PRECL_CL_month,3);

PRECL_POCL=PRECL_PO-PRECL_CL;

save('PRECL_POCL_composite_monthly.mat','PRECL_PO','PRECL_CL','PRECL_POCL')

nccreate('PRECL_POCL_composite_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('PRECL_POCL_composite_monthly.nc','lat', lat)
ncwriteatt('PRECL_POCL_composite_monthly.nc','lat','units','degrees_north')
ncwriteatt('PRECL_POCL_composite_monthly.nc','lat','long_name','Latitude')
nccreate('PRECL_POCL_composite_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('PRECL_POCL_composite_monthly.nc','lon', lon)
ncwriteatt('PRECL_POCL_composite_monthly.nc','lon','units','degrees_east')
ncwriteatt('PRECL_POCL_composite_monthly.nc','lon','long_name','Longitude')
nccreate('PRECL_POCL_composite_monthly.nc','PRECL_PO','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECL_POCL_composite_monthly.nc','PRECL_PO', PRECL_PO)
nccreate('PRECL_POCL_composite_monthly.nc','PRECL_CL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECL_POCL_composite_monthly.nc','PRECL_CL', PRECL_CL)
nccreate('PRECL_POCL_composite_monthly.nc','PRECL_POCL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECL_POCL_composite_monthly.nc','PRECL_POCL', PRECL_POCL)
ncdisp('PRECL_POCL_composite_monthly.nc')
