load ~/NC/grid.mat
load ~/Aerosol_Variaiton/IRUN/SST/ncfile/SST_monthly.mat
load ~/NC/OCNFRAC.mat
month=1:1800;

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(1800,m);

n=1;
for i=45:105
    for j=59:85
        if OCNFRAC(i,j)==1
            SST_ALL(:,n)=SST_MONTH(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,121);

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

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