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

EOF_AOD_ANN=zeros(144,96);
EOF_AOD_DJF=zeros(144,96);
EOF_AOD_MAM=zeros(144,96);
EOF_AOD_JJA=zeros(144,96);
EOF_AOD_SON=zeros(144,96);
EOF_AOD_JAN=zeros(144,96);
EOF_AOD_JUL=zeros(144,96);

AOD_ANN_ALL=zeros(150,144*72);
AOD_DJF_ALL=zeros(150,144*72);
AOD_MAM_ALL=zeros(150,144*72);
AOD_JJA_ALL=zeros(150,144*72);
AOD_SON_ALL=zeros(150,144*72);
AOD_JAN_ALL=zeros(150,144*72);
AOD_JUL_ALL=zeros(150,144*72);
n=1;
for i=1:144
    for j=13:84
        AOD_ANN_ALL(:,n)=AOD_ANN(i,j,:);
        AOD_DJF_ALL(:,n)=AOD_DJF(i,j,:);
        AOD_MAM_ALL(:,n)=AOD_MAM(i,j,:);
        AOD_JJA_ALL(:,n)=AOD_JJA(i,j,:);
        AOD_SON_ALL(:,n)=AOD_SON(i,j,:);
        AOD_JAN_ALL(:,n)=AOD_JAN(i,j,:);
        AOD_JUL_ALL(:,n)=AOD_JUL(i,j,:);
        n=n+1;
    end
end

[e,PC_AOD_ANN,expvar_ANN] = caleof(AOD_ANN_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_ANN(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_AOD_DJF,expvar_DJF] = caleof(AOD_DJF_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_DJF(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_AOD_MAM,expvar_MAM] = caleof(AOD_MAM_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_MAM(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_AOD_JJA,expvar_JJA] = caleof(AOD_JJA_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_JJA(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_AOD_SON,expvar_SON] = caleof(AOD_SON_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_SON(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_AOD_JAN,expvar_JAN] = caleof(AOD_JAN_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_JAN(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_AOD_JUL,expvar_JUL] = caleof(AOD_JUL_ALL,1,1);
n=1;
for i=1:144
    for j=13:84
        EOF_AOD_JUL(i,j)=e(n);
        n=n+1;
    end
end


save('AOD_EOF.mat','EOF_AOD_ANN','PC_AOD_ANN','expvar_ANN','EOF_AOD_DJF','PC_AOD_DJF','expvar_DJF','EOF_AOD_MAM','PC_AOD_MAM','expvar_MAM','EOF_AOD_JJA','PC_AOD_JJA','expvar_JJA','EOF_AOD_SON','PC_AOD_SON','expvar_SON','EOF_AOD_JAN','PC_AOD_JAN','expvar_JAN','EOF_AOD_JUL','PC_AOD_JUL','expvar_JUL')

nccreate('AOD_EOF.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','lat', lat)
ncwriteatt('AOD_EOF.nc','lat','units','degrees_north')
ncwriteatt('AOD_EOF.nc','lat','long_name','Latitude')
nccreate('AOD_EOF.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('AOD_EOF.nc','lon', lon)
ncwriteatt('AOD_EOF.nc','lon','units','degrees_east')
ncwriteatt('AOD_EOF.nc','lon','long_name','Longitude')
nccreate('AOD_EOF.nc','EOF_AOD_ANN','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_ANN', EOF_AOD_ANN)
nccreate('AOD_EOF.nc','PC_AOD_ANN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_ANN', PC_AOD_ANN)
nccreate('AOD_EOF.nc','EOF_AOD_DJF','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_DJF', EOF_AOD_DJF)
nccreate('AOD_EOF.nc','PC_AOD_DJF','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_DJF', PC_AOD_DJF)
nccreate('AOD_EOF.nc','EOF_AOD_MAM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_MAM', EOF_AOD_MAM)
nccreate('AOD_EOF.nc','PC_AOD_MAM','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_MAM', PC_AOD_MAM)
nccreate('AOD_EOF.nc','EOF_AOD_JJA','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_JJA', EOF_AOD_JJA)
nccreate('AOD_EOF.nc','PC_AOD_JJA','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_JJA', PC_AOD_JJA)
nccreate('AOD_EOF.nc','EOF_AOD_SON','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_SON', EOF_AOD_SON)
nccreate('AOD_EOF.nc','PC_AOD_SON','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_SON', PC_AOD_SON)
nccreate('AOD_EOF.nc','EOF_AOD_JAN','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_JAN', EOF_AOD_JAN)
nccreate('AOD_EOF.nc','PC_AOD_JAN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_JAN', PC_AOD_JAN)
nccreate('AOD_EOF.nc','EOF_AOD_JUL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('AOD_EOF.nc','EOF_AOD_JUL', EOF_AOD_JUL)
nccreate('AOD_EOF.nc','PC_AOD_JUL','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('AOD_EOF.nc','PC_AOD_JUL', PC_AOD_JUL)
ncdisp('AOD_EOF.nc')
