function iconvert(filename_base, old_extension, new_extension, ...
	   convert_switches)
% iconvert(filename_base, old_extension, new_extension, convert_switches)
%
%    filename_base is the part before the extension
%    old_extension is the extension of the original file
%    new_extension is the extension of the converted file
%    convert_switches (switches to use) is optional (defaults to none)
%    convert_bin (path to binary file "convert") should be on your path
%                   when you invoke matlab
%
% try these:
%    sun                                        linux        
%    /usr/local/imagemagick/bin              /usr/X11R6/bin   
%
% usage: iconvert('cont_a','ps','gif')
%

%
% 2000/9/1 Jules - NOTE iconvert makes a large gif file if it isn't recompiled
%                 with the gif compression turned on
%      In the context of the web_adcp assemblage, convert_switches is 
%                 passed in from the global variables set in webglobs.

if (~exist('convert_switches'))  %if it hasn't been set, set it as nothing
  convert_switches = [' '];
end

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


%does the original file exist?
origfile = sprintf('%s.%s',filename_base, old_extension);
newfile = sprintf('%s.%s',filename_base, new_extension);
if (~exist(origfile,'file'))
  error(sprintf('cannot find file <%s> to convert\n',origfile))
end

if (~exist(convert_bin,'file') ) 
  fprintf('cannot find convert binary %s\n',convert_bin)
  error('check variable convert_bin in webglobs')
end

if (isempty(convert_switches))
  convert_switches = ' ';  %so it will format nicely
end


execute_this=...
	sprintf('%s %s %s %s',...
	convert_bin, convert_switches, origfile, newfile);
fprintf('converting %s:\n%s\n',origfile,execute_this)
unix(execute_this);


