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

m=0;
for i=45:105
    for j=59:85
        if OCNFRAC(i,j)==1
            m=m+1;
        end
    end
end

EOF_SST=zeros(144,96);
EOF_SST(:,:)=-999;

SST_ALL=zeros(150,m);

n=1;
for i=45:105
    for j=59:85
        if OCNFRAC(i,j)==1
            SST_ALL(:,n)=SST_ANN(i,j,:);
            n=n+1;
        end
    end
end

[e,PC_SST,expvar] = caleof(SST_ALL,1,1);
n=1;
for i=45:105
    for j=59:85
        if OCNFRAC(i,j)==1
            EOF_SST(i,j)=e(n);
            n=n+1;
        else
            EOF_SST(i,j)=-999;
        end
    end
end

PC_SST_SMOOTH=smooth(PC_SST,11);

save('SST_EOF_PDO.mat','EOF_SST','PC_SST','expvar','PC_SST_SMOOTH')

nccreate('SST_EOF_PDO.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('SST_EOF_PDO.nc','lat', lat)
ncwriteatt('SST_EOF_PDO.nc','lat','units','degrees_north')
ncwriteatt('SST_EOF_PDO.nc','lat','long_name','Latitude')
nccreate('SST_EOF_PDO.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('SST_EOF_PDO.nc','lon', lon)
ncwriteatt('SST_EOF_PDO.nc','lon','units','degrees_east')
ncwriteatt('SST_EOF_PDO.nc','lon','long_name','Longitude')
nccreate('SST_EOF_PDO.nc','EOF_SST','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('SST_EOF_PDO.nc','EOF_SST', EOF_SST)
nccreate('SST_EOF_PDO.nc','PC_SST','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('SST_EOF_PDO.nc','PC_SST', PC_SST)
nccreate('SST_EOF_PDO.nc','PC_SST_SMOOTH','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('SST_EOF_PDO.nc','PC_SST_SMOOTH', PC_SST_SMOOTH)
ncdisp('SST_EOF_PDO.nc')
