function print_nodisplay(outfile,type,options)
% Usage: print_vps(outfile,type,options)
%
% Writes current figure to a bitmap output file named OUTFILE (no
% filetype extension) 
%
% Supported TYPE values are 'gif','tiff','pcx' but you can 
% add more if you have a translator
%
% Input string OPTIONS is any set of valid options that can be passed
% to the translator. In the case of 'gif', these include e.g.
%   '-interlace' 
%   '-transparent rgbi:1/1/1'   makes the white background transparent
%
% This function uses the Ghostscript application installed with matlab. 
% This option does not require that the plot exist in a 'figure' 
% window, and can therefore be used in a Matlab -nodisplay session, 
% or a batch processed session. 
%
% The graphical output is moderately inferior to the results obtained
% with imwrite or the print -d{tiff,jpeg,png} options
%
% The conversion from ppm to the requested bitmap format uses
% utilities from the netpbm package http://netpbm.sourceforge.net
% which is installed on most IMCS machines. If these do not appear to
% be accessible to you, add /home/ahabopt/bin to your unix path (for
% Solaris machines) or ask mailto:support@marine.rutgers.edu to
% install them locally for you.
%
% John Wilkin

if nargin < 1
  error('You must give the output file name')
end
if nargin < 2
  type = 'gif';
end
if nargin < 3
  options = ' ';
end

switch type(1:3)
  case 'gif'
    translator = ' ';
  case 'tif' 
    translator = 'pnmtotiff';
  case 'pcx' 
    translator = 'ppmtopcx';
  otherwise
    error([ 'I don''t know a ppm to ' type ' translator. ' ...
	  ' If you find one, edit ' which(mfilename) ' to add ' ...
	  ' it as an option'])
end

% create the temporary ppm bitmap
tmpfile = [ 'tmp' int2str(round(100000*rand(1)))];
print('-dpsc','-r200',tmpfile);

% Hint: change string 'cat' to 'pnmcrop' if you want to automatically 
% trim the edges of the plot to the minimal size

% unixstr = [ '/home/wilkin/scripts/pstogif ' tmpfile '.ps | ' translator ...
%      ' ' options ' > ' outfile '.' type];
% unixstr = [ '/home/wilkin/scripts/pstogif ' tmpfile '.ps']

% the following works for the pstogif script on ahab in 
% /opt/SUNWspro/bin/pstogif
unixstr = [ 'pstogif -scale 1.5 -out ' outfile '.gif ' tmpfile '.ps ']
unix(unixstr);

rmstr = [ '/bin/rm ' tmpfile '.ps'];
unix(rmstr);


