clear
%read parameters
[nx,ny,nz,nt,nvars,neof,nper,undef,fn]=...
    textread('eof_in.inf','%f%f%f%f%f%f%f%f%s','commentstyle','matlab');
fid=fopen('eof_in.gad','r');
U=fread(fid,inf,'float');
U=reshape(U,[nx,ny,nz,nt,nvars]);

%calculate EOFs
tic
[Am,l,EOF]=eofv_grads(undef,nper,neof,0,U);
toc

% save results
fn=char(fn);
fid=fopen([fn '_eof.dat'],'w');
%x,y,z,var,t
fwrite(fid,permute(EOF,[1 2 3 5 4]),'float');
fclose(fid);
fid=fopen([fn '_pc.dat'],'w');
fwrite(fid,Am','float');
fclose(fid);
out=[[1:neof]' l];
fid=fopen([fn '_eval.txt'],'w');
fprintf(fid,'%s\n','% n  lambda     %  ');
for i=1:neof
fprintf (fid,  '  %i %8.2e %6.2f\n', out(i,:));
end
fclose(fid);
