load ~/NC/grid.mat
load ~/Aerosol_Variaiton/AOD/ncfile/AOD.mat
year=1:150;

AOD_ANN_MAD=zeros(144,96);
AOD_ANN_APDM=zeros(144,96);
AOD_DJF_MAD=zeros(144,96);
AOD_DJF_APDM=zeros(144,96);
AOD_MAM_MAD=zeros(144,96);
AOD_MAM_APDM=zeros(144,96);
AOD_JJA_MAD=zeros(144,96);
AOD_JJA_APDM=zeros(144,96);
AOD_SON_MAD=zeros(144,96);
AOD_SON_APDM=zeros(144,96);

for i=1:144
    for j=1:96
        AOD_ANN_MAD(i,j)=mad(AOD_ANN(i,j,:));
        AOD_ANN_APDM(i,j)=AOD_ANN_MAD(i,j)./(mean(AOD_ANN(i,j,:),3));
        AOD_DJF_MAD(i,j)=mad(AOD_DJF(i,j,:));
        AOD_DJF_APDM(i,j)=AOD_DJF_MAD(i,j)./(mean(AOD_DJF(i,j,:),3));
        AOD_MAM_MAD(i,j)=mad(AOD_MAM(i,j,:));
        AOD_MAM_APDM(i,j)=AOD_MAM_MAD(i,j)./(mean(AOD_MAM(i,j,:),3));
        AOD_JJA_MAD(i,j)=mad(AOD_JJA(i,j,:));
        AOD_JJA_APDM(i,j)=AOD_JJA_MAD(i,j)./(mean(AOD_JJA(i,j,:),3));
        AOD_SON_MAD(i,j)=mad(AOD_SON(i,j,:));
        AOD_SON_APDM(i,j)=AOD_SON_MAD(i,j)./(mean(AOD_SON(i,j,:),3));
    end
end

save('AOD_MAD_APDM.mat','AOD_ANN_MAD','AOD_ANN_APDM','AOD_DJF_MAD','AOD_DJF_APDM','AOD_MAM_MAD','AOD_MAM_APDM','AOD_JJA_MAD','AOD_JJA_APDM','AOD_SON_MAD','AOD_SON_APDM')

nccreate('AOD_MAD_APDM.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','lat', lat)
ncwriteatt('AOD_MAD_APDM.nc','lat','units','degrees_north')
ncwriteatt('AOD_MAD_APDM.nc','lat','long_name','Latitude')
nccreate('AOD_MAD_APDM.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','lon', lon)
ncwriteatt('AOD_MAD_APDM.nc','lon','units','degrees_east')
ncwriteatt('AOD_MAD_APDM.nc','lon','long_name','Longitude')
nccreate('AOD_MAD_APDM.nc','AOD_ANN_MAD','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_ANN_MAD', AOD_ANN_MAD)
nccreate('AOD_MAD_APDM.nc','AOD_ANN_APDM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_ANN_APDM', AOD_ANN_APDM)
nccreate('AOD_MAD_APDM.nc','AOD_DJF_MAD','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_DJF_MAD', AOD_DJF_MAD)
nccreate('AOD_MAD_APDM.nc','AOD_DJF_APDM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_DJF_APDM', AOD_DJF_APDM)
nccreate('AOD_MAD_APDM.nc','AOD_MAM_MAD','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_MAM_MAD', AOD_MAM_MAD)
nccreate('AOD_MAD_APDM.nc','AOD_MAM_APDM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_MAM_APDM', AOD_MAM_APDM)
nccreate('AOD_MAD_APDM.nc','AOD_JJA_MAD','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_JJA_MAD', AOD_JJA_MAD)
nccreate('AOD_MAD_APDM.nc','AOD_JJA_APDM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_JJA_APDM', AOD_JJA_APDM)
nccreate('AOD_MAD_APDM.nc','AOD_SON_MAD','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_SON_MAD', AOD_SON_MAD)
nccreate('AOD_MAD_APDM.nc','AOD_SON_APDM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_MAD_APDM.nc','AOD_SON_APDM', AOD_SON_APDM)
ncdisp('AOD_MAD_APDM.nc')
