load ~/NC/grid
month=1:1800;

AOD_temp=ncread('~/Aerosol_Variaiton/ERUN/read_nc/ncfile/read_nc_AOD.nc','AOD');

AOD_MONTH=zeros(144,96,1800);

% Deseasonal
for i=1:144
    for j=1:96
        for monthID=1:12
            temp=mean(AOD_temp(i,j,monthID,:),4);
            for yearID=1:150
                AOD_temp(i,j,monthID,yearID)=AOD_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:144
    for j=1:96
        n=0;
        for yearID=1:150
            for monthID=1:12
                n=n+1;
                AOD_MONTH(i,j,n)=AOD_temp(i,j,monthID,yearID);
            end
        end
    end
end

save('AOD_monthly.mat','AOD_MONTH')

nccreate('AOD_monthly.nc','Month','Dimensions',{'time',Inf},'Format','classic')
ncwrite('AOD_monthly.nc','Month', month)
ncwriteatt('AOD_monthly.nc','Month','long_name','Month')
nccreate('AOD_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('AOD_monthly.nc','lat', lat)
ncwriteatt('AOD_monthly.nc','lat','units','degrees_north')
ncwriteatt('AOD_monthly.nc','lat','long_name','Latitude')
nccreate('AOD_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('AOD_monthly.nc','lon', lon)
ncwriteatt('AOD_monthly.nc','lon','units','degrees_east')
ncwriteatt('AOD_monthly.nc','lon','long_name','Longitude')
nccreate('AOD_monthly.nc','AOD_MONTH','Dimensions',{'lon',144,'lat',96,'month',length(month)},'Format','classic')
ncwrite('AOD_monthly.nc','AOD_MONTH', AOD_MONTH)
ncdisp('AOD_monthly.nc')