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

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

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

PRECC_PO_month=zeros(144,96,594);
PRECC_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;
        PRECC_PO_month(i,j,n)=PRECC_MONTH(i,j,monthID);
    end
    if CL_Month(i,j,monthID)==1
        m=m+1;
        PRECC_CL_month(i,j,m)=PRECC_MONTH(i,j,monthID);
    end
end
end
end

PRECC_PO=mean(PRECC_PO_month,3);
PRECC_CL=mean(PRECC_CL_month,3);

PRECC_POCL=PRECC_PO-PRECC_CL;

save('PRECC_POCL_composite_monthly.mat','PRECC_PO','PRECC_CL','PRECC_POCL')

nccreate('PRECC_POCL_composite_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('PRECC_POCL_composite_monthly.nc','lat', lat)
ncwriteatt('PRECC_POCL_composite_monthly.nc','lat','units','degrees_north')
ncwriteatt('PRECC_POCL_composite_monthly.nc','lat','long_name','Latitude')
nccreate('PRECC_POCL_composite_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('PRECC_POCL_composite_monthly.nc','lon', lon)
ncwriteatt('PRECC_POCL_composite_monthly.nc','lon','units','degrees_east')
ncwriteatt('PRECC_POCL_composite_monthly.nc','lon','long_name','Longitude')
nccreate('PRECC_POCL_composite_monthly.nc','PRECC_PO','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECC_POCL_composite_monthly.nc','PRECC_PO', PRECC_PO)
nccreate('PRECC_POCL_composite_monthly.nc','PRECC_CL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECC_POCL_composite_monthly.nc','PRECC_CL', PRECC_CL)
nccreate('PRECC_POCL_composite_monthly.nc','PRECC_POCL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECC_POCL_composite_monthly.nc','PRECC_POCL', PRECC_POCL)
ncdisp('PRECC_POCL_composite_monthly.nc')
