function gsconvert(filename_base,old_extension,new_extension,gs_switches)
%     gsconvert(filename_base,old_extensopn,new_extension,gs_switches)
%
% converts filename_base.eps (or ps) to filenamebase.new_extension
%  using ghostscript to convert to ppm and ppmto[something] for the 2nd step
%
% your path should include:
%
%                      sun                          linux
% ghostscript   /usr/local/bin                    /usr/bin
% ppmto___      /usr/local/pbm/bin                /usr/bin
%

%Jules 00/09/07

%BUG - OK, so it will accept other output types than gif, but it's still bad.
%  needs work.

% JH 2001/03/22 added remote path info so you don't have to
%   be in the same directory as the file

% EF 2002/03/22
% Added png via direct call to gs, device png16m.
% (png256 and png16m usually produce good quality,
% but on vector plots with topog and temperature,
% png256 messes up the colors; therefore we are
% stuck with png16m, unless this apparent gs bug is fixed
% in mdk8.2.)
% Notes: ppmtogif is part of libgr_progs, which does not
% seem to be in MDK 8.1, although it is in 7.2.
% There seems to be no advantage at all in going through
% an intermediate ppm step if it is not necessary.
% Imagemagick convert seems to do antialiasing (or some
% sort of smoothing) by default; the result is a much larger
% file, but higher quality.  It is also very slow compared to
% the gs method used here.


echobin=sprintf('echo ""');

if (~exist('gs_switches') | (isempty(gs_switches)))
  gs_switches = [' '];
end

infile = sprintf('%s.%s',filename_base,old_extension)
if (~exist(infile))
  error(sprintf('cannot find file %s\n',infile))
end

%find the path
[filepath, nn, ext] = fileparts(infile);
if isempty(filepath)
   filepath = '.';
end


%check for gs path
gsbin = 'gs';
[errmsg,bin]=unix(sprintf('which %s',gsbin));
badi = find(bin==10); bin(badi)=[];  %eliminate existing newlines
if (strcmp(gsbin,bin(end-length(gsbin)+1:end)))
  gsbin = bin;
else
  help gsconvert
  error(sprintf('Check your path to %s\n',gsbin))
end

if (~strcmp(old_extension,'ps') & (~strcmp(old_extension,'eps')))
  error(sprintf('can only convert *.ps and *.eps to %s',new_extension))
end

%if converting to gif, do it in two steps; else assume the user
% gave the right switches to convert directly
if (strcmp(new_extension,'gif'))

  basic_switches=sprintf('-sOutputFile="%s/junk.ppm" -sDEVICE=ppmraw -q -r72 ', ...
                         filepath);

  %use the extension to generate the binary file if it doesn't exist
  ppmbin = sprintf('ppmto%s',new_extension);
%  ppmbin = sprintf('pnmto%s',new_extension);
  [errmsg,bin]=unix(sprintf('which %s',ppmbin));
  badi = find(bin==10); bin(badi)=[];  %eliminate existing newlines
  if (strcmp(ppmbin,bin(end-length(ppmbin)+1:end)))
   ppmbin = bin;
  else
   help gsconvert
   error(sprintf('Check your path to %s\n',ppmbin))
  end

  %do the gs part
  execute_this = ...
     sprintf('%s | %s  %s  %s %s', echobin, gsbin, basic_switches, gs_switches, infile);
  fprintf('executing ghostscript command:%s\n',execute_this)
  unix(execute_this);

  if (~exist(sprintf('%s/junk.ppm', filepath),'file'))
   error('gs conversion must have failed. cannot find junk.ppm')
  end

  gif_file = sprintf('%s/%s.%s', filepath, filename_base, new_extension);
  if exist(gif_file, 'file'), delete(gif_file); end
  execute_this = sprintf('%s %s/junk.ppm > %s.%s', ppmbin, filepath, ...
                         filename_base, new_extension);
  fprintf('executing conversion to gif or png:%s\n',execute_this)
  unix(execute_this);

  delete(sprintf('%s/junk.ppm', filepath))

else
  outdev = new_extension;
  if new_extension == 'png', outdev = 'png16m'; end
  basic_switches=sprintf('-sOutputFile="%s/%s.%s" -sDEVICE=%s -q ',...
     filepath,  filename_base, new_extension, outdev);
  execute_this = ...
     sprintf('%s | %s  %s  %s %s',echobin,...
     gsbin, basic_switches, gs_switches, infile);
  fprintf('executing ghostscript command:%s\n',execute_this)
  unix(execute_this);

end



