year=1948:2014;
lon=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/air.mon.mean.nc','lon');
lat=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/air.mon.mean.nc','lat');

load ~/Aerosol_Variaiton/IRUN/SST/ncfile/SST_NCEP.mat


LANDFRAC=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/land.nc','land');
LANDFRAC(:,[1:13,61:73])=1;

m=0;
for i=1:length(lon)
    for j=1:length(lat)
        if LANDFRAC(i,j)==0
            m=m+1;
        end
    end
end

EOF_SST_ANN=zeros(length(lon),length(lat));

SST_ANN_ALL=zeros(length(year),m);

n=1;
for i=1:length(lon)
    for j=1:length(lat)
        if LANDFRAC(i,j)==0
            SST_ANN_ALL(:,n)=SST_ANN(i,j,:);
            n=n+1;
        end
    end
end

[e,PC_SST_ANN,expvar_ANN] = caleof(SST_ANN_ALL,1,1);
n=1;
for i=1:length(lon)
    for j=1:length(lat)
        if LANDFRAC(i,j)==0
            EOF_SST_ANN(i,j)=e(n);
            n=n+1;
        else
            EOF_SST_ANN(i,j)=-999;
        end
    end
end


save('SST_EOF_NCEP.mat','EOF_SST_ANN','PC_SST_ANN','expvar_ANN')

nccreate('SST_EOF_NCEP.nc','lat','Dimensions',{'lat',length(lat)},'Format','classic')
ncwrite('SST_EOF_NCEP.nc','lat', lat)
ncwriteatt('SST_EOF_NCEP.nc','lat','units','degrees_north')
ncwriteatt('SST_EOF_NCEP.nc','lat','long_name','Latitude')
nccreate('SST_EOF_NCEP.nc','lon','Dimensions',{'lon',length(lon)},'Format','classic')
ncwrite('SST_EOF_NCEP.nc','lon', lon)
ncwriteatt('SST_EOF_NCEP.nc','lon','units','degrees_east')
ncwriteatt('SST_EOF_NCEP.nc','lon','long_name','Longitude')
nccreate('SST_EOF_NCEP.nc','EOF_SST_ANN','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('SST_EOF_NCEP.nc','EOF_SST_ANN', EOF_SST_ANN)
nccreate('SST_EOF_NCEP.nc','PC_SST_ANN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('SST_EOF_NCEP.nc','PC_SST_ANN', PC_SST_ANN)
ncdisp('SST_EOF_NCEP.nc')
