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

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

AOD_PO_month=zeros(144,96,594);
AOD_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;
        AOD_PO_month(i,j,n)=AOD_MONTH(i,j,monthID);
    end
    if CL_Month(i,j,monthID)==1
        m=m+1;
        AOD_CL_month(i,j,m)=AOD_MONTH(i,j,monthID);
    end
end
end
end

AOD_PO=mean(AOD_PO_month,3);
AOD_CL=mean(AOD_CL_month,3);

AOD_POCL=AOD_PO-AOD_CL;

save('AOD_POCL_composite_monthly.mat','AOD_PO','AOD_CL','AOD_POCL')

nccreate('AOD_POCL_composite_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('AOD_POCL_composite_monthly.nc','lat', lat)
ncwriteatt('AOD_POCL_composite_monthly.nc','lat','units','degrees_north')
ncwriteatt('AOD_POCL_composite_monthly.nc','lat','long_name','Latitude')
nccreate('AOD_POCL_composite_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('AOD_POCL_composite_monthly.nc','lon', lon)
ncwriteatt('AOD_POCL_composite_monthly.nc','lon','units','degrees_east')
ncwriteatt('AOD_POCL_composite_monthly.nc','lon','long_name','Longitude')
nccreate('AOD_POCL_composite_monthly.nc','AOD_PO','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_POCL_composite_monthly.nc','AOD_PO', AOD_PO)
nccreate('AOD_POCL_composite_monthly.nc','AOD_CL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_POCL_composite_monthly.nc','AOD_CL', AOD_CL)
nccreate('AOD_POCL_composite_monthly.nc','AOD_POCL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_POCL_composite_monthly.nc','AOD_POCL', AOD_POCL)
ncdisp('AOD_POCL_composite_monthly.nc')
