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

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

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

PRECT_PO_month=zeros(144,96,594);
PRECT_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;
        PRECT_PO_month(i,j,n)=PRECT_MONTH(i,j,monthID);
    end
    if CL_Month(i,j,monthID)==1
        m=m+1;
        PRECT_CL_month(i,j,m)=PRECT_MONTH(i,j,monthID);
    end
end
end
end

PRECT_PO=mean(PRECT_PO_month,3);
PRECT_CL=mean(PRECT_CL_month,3);

PRECT_POCL=PRECT_PO-PRECT_CL;

save('PRECT_POCL_composite_monthly.mat','PRECT_PO','PRECT_CL','PRECT_POCL')

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