function s = next_psf(psf)
%  1.  NAME
%
%  next_psf - Create a new postscript file name
%
%  2.  SYNOPSIS
%
%  s = next_psf(psf )
%
%  3.  DESCRIPTION
%
%  Generates a new file name by appending a number to it. To get the
%  current number, it reads the directory where the new file is expected
%  to be created. Although this function is used for generating
%  successive postscript files, it will work with any kind of file names.
%
%  4.  PARAMETERS
%
%     psf
%        Generic name of the file. The next name wil have a number of
%        three digits appended to it.
%
%  5.  RETURNS
%
%  The next file name.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

ext = '000';

f = [ psf '.*' ];
lst = dir(f);
imax = length(lst);
nmax = -Inf;
for i=1:imax,
  f = lst(i);
  f = f.name;
  f = strsplit(f, ';');  % Matlab puts a ';' at the end of file names
  [f, nf] = strsplit(f(1,:),'.');
  n = str2num(f(nf,:));
  if ~isempty(n),
	 nmax = max(nmax, n);
  end
end
if ~isfinite(nmax),
  nmax = 0;
else
  nmax = nmax + 1;
end
ext = sprintf('.%03d', nmax);
s= [ psf ext ];

return;

