function varargout = parsearg(varg, varargin)
%--------------------------------------------------------------------------
% 
% parsearg.m -- 
% 
% Author          : Jaccard Pierre
% Created On      : 2000/08/18 08:09:44
% Last Modified By: Jaccard Pierre
% Last Modified On: 2000/08/18 08:57:30
% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
% ----------------------------------------------------------------------- +
% VERSION CONTROL: 
% 
%   $Author$
%   $Date$
%   $Id$
%   $Name$
%   $Revision$
%   $State$
%   $Log$
% 
% --------------------------------------------------------------------------
%
% SYNOPSIS
%
%   [ var1, var2, ... ] = parsearg(user, 'var1', def1, 'var2', def2, ...)
%
% DESCRIPTION
%
%   Set variables value to either its default or a user specified. This
%
%   function is usefull at beginning of scripts accepting name/value pairs 
%   variable arguments. A pair like  
%
%      'var_name', def_value 
%			
%	 in parameter list provides a default value DEF_VALUE to variable named
%	 VAR_NAME. The order of these pairs must follow that of output
%	 arguments. In order to override default values, a pair like
%
%      'var_name', usr_value
%	 
%   can be specified in cell array USER. The order on how name/value pairs
%   are listed in USER does not matter.
%
% EXAMPLE
%
%   Assume the following function
%
%      function m_cruis2(file, year_base, varargin)
%
%   where VARARGIN can be a list of name/value pair options. To set default 
%   values to these and parse VARARGIN for user specified options, just use
%   the command
%
%      [ longitude_180, plot_stuf, map_script, coast_res, line_style ] = ...
%           parsearg( varargin, ...
%		          'longitude_180', 0, ...
%		          'plot_stuf', { '-title' , 'Cruise Track' }, ...
%		          'map_script', '', ...
%		          'coast_res', 'i', ...
%		          'line_style', { ...
%                              'LineStyle', 'none', ...
%															'Marker', '.', ...
%															'MarkerSize', 5 });
% 
%--------------------------------------------------------------------------

%--- 
% Initializations
%---
varargout = {};

%---
% Loop through input name/value pairs
%---
i = 0;
k = 0;

while (i < nargin) & (k < nargout),
	
	%--- Get variable name and its default value
	i = i + 1;
	var = varargin{i};
	i = i + 1;
	val = varargin{i};
	
	%--- Look for a user specified value
	j = find(strcmpi(varg, var));
	if ~isempty(j),
		if j > length(varg),
			error([ 'Missing value for parameter ' var ]);
		end
		val = varg{j+1};
	end
	
	%--- Set the output argument:
	k = k + 1;
	varargout{k} = val;

end

return;

	
