% function [V,L,U,frac]=eof_svd(data)
%
% Computes eofs of data using singular
% value decomposition (SVD)
%  V*L*U' = data
% 
% If the data are entered into the matrix, data, such that the 
% rows are time series and columns correspond to locations, then
%   V columns are the orthogonal spatial functions
%   L diagonal contains singular values (square root of eigenvalues) 
%   U columns are the orthogonal and normalized time series
%
% If D is M X N, then only the p=min(M,N) columns of U and V are 
% computed and returned (to save computation time)
%
% To get the dimensional time series
%   A(t) = (U*L')'
%
% also finds frac(tion) of variance in each eof using the
%    diagonal singular value matrix, S
%
function [V,L,U,frac]=eof_svd(data)
[m,n]=size(data);p=min(m,n);
[V,L,U]=svd(data,'econ');		% 
Sp=L(1:p,1:p);
lambda=Sp*Sp';
var=trace(lambda);
frac=diag(lambda)/var;


