%
% DESCRIPTION
%
%   Generate argument list to pass to functions, building automatically the 
%   list of name/value pair options. This script requires variable 
%
%      ma_
%
%   to be defined. This is a structure with the following fields
%
%      ma_.simple = { 'name1', 'name2', ... }
%      ma_.minus  = { 'name1', 'name2', ... }
%      ma_.simple = { 'name1', 'name2', ... }
%
%   for the different types of name/value pair options. Field
%
%      ma_.param = { par1,  par2, ... }
%
%   can be set with the required parameters before calling the script so
%   that built options will be appended to it.
%
% SIMPLE TYPE
%
%   Build a normal list of name/value pair options: i.e. append the
%   following cell array to the PARAM field
%
%      { 'name1', value1, 'name2', value2, ... }
%
%   where VALUEX is the value of variable named NAMEX. Pairs are only
%   appended if the corresponding variable exists and is not empty.
%
% MINUS TYPE
%
%   Each variable name is a cell array of variables 
%
%      { 'x_name_1', 'y_name_2', ... }
%
%   which will be converted into a simple name/value pair option list
%
%      { '-name_1', value_1, 'name_2', value_2, ... }
%
%   and field PARAM is then ended with
%
%      { 'name1', {...}, 'name2', {...}, ... }
%
%   where cell arrays {...} are the computed lists. Again, only lists that
%   exist and are not empty are appended to field PARAM. This is usefull
%   for passing parameters that should be sent further by functions using
%   the minus prefix notation in their name/value pair option lists.
%
% CONCAT TYPE
%
%   Similar to the MINUS type, however option lists for each specified name 
%   will look like
%
%      { 'xname_1', value_1, 'yname_2', value2, ... }
%
%   This is usefull for passing object properties, but still use a
%   userfriendly notation in the main script.
%
% EXAMPLE
%
%   First define parameters for SIMPLE option type:
%
%      longitude_180 = 1;
%      map_script = 'nuka_map';
%      coast_res  = '';
%
%   Parameter COAST_RES above is empty and will therefore not be included
%   in the list of options. The function to be called must therefore supply
%   a default value for it. Then, define parameters for the MINUS type
%
%      p_title     = 'NUKA ARCTICA';
%      p_subtitle  = 'Aalborg to Nuuk';
%      p_institute = 'P. Jaccard, Geophysical Institute, University of Bergen';
%
%   List the desired parameters in the following list
%
%      plot_stuf = { 'p_title', 'p_subtitle', 'p_institute' };
%   
%   The same for the CONCAT type
%
%      line_style = '-';
%      line_width = 1;
%
%   Which have to be listed on the following line if these should be
%   mentionned on the command line
%
%      plot_style = { 'line_width' };
%
%   Now, define the ma_ structure with required parameters
%
%      ma_.param  = { nav_file, year_base };
%
%   and the different option lists
%
%      ma_.simple = { 'map_script', 'coast_res', 'longitude_180' }; 
%      ma_.minus  = { 'plot_stuf' };
%      ma_.concat = { 'plot_style' };
%
%   Call this script to parse information saved in the ma_ structure and
%   generate an argument list
%
%      makeargs;
%
%   Finally call the function with all available parameters
%
%      m_cruis2( param{:} );
%
%   This example will call function M_CRUIS2 with the following arguments
%
%      m_cruis2('getnav.nav', 2000, 'map_script', 'nuka.map', ...
%               'longitude_180', 1, ...
%               'plot_stuf',  { '-title', 'NUKA ...', ...
%                               '-subtitle', 'Aalborg ...', ...
%                               '-institute', 'P. Jaccard ...' },
%               'plot_style', { 'linewidth', 1});
%
%

%--------------------------------------------------------------------------
% 
% makeargs.m -- 
% 
% Author          : Jaccard Pierre
% Created On      : 2000/08/18 08:59:10
% Last Modified By: Jaccard Pierre
% Last Modified On: 2000/08/21 14:08:40
% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
% ----------------------------------------------------------------------- +
% VERSION CONTROL: 
% 
%   $Author$
%   $Date$
%   $Id$
%   $Name$
%   $Revision$
%   $State$
%   $Log$
% 
%--------------------------------------------------------------------------


%---
% Check that the ma_ variable exists and has been defined
%---
if ~exist('ma_', 'var') | isempty(ma_),
	error([ 'Required structure ma_ not defined or empty' ]);
end

%---
% Check that the required have been inistialized, or initialize them to the 
% empty cell array
%---
ma_.f = { 'param', 'simple', 'minus', 'concat' };
ma_.n = fieldnames(ma_); 
% Loop through required fields
ma_.i = 0;
while ma_.i < length(ma_.f),
	ma_.i = ma_.i + 1;
	ma_.k = 0;
	% Loop through field names
	ma_.j = 0;
	while ma_.j < length(ma_.n),
		ma_.j = ma_.j + 1;
		% Check if field is defined
		if strcmp(ma_.f(ma_.i), ma_.n(ma_.j)),
			ma_.k = 1;
			break;
		end
	end
	% Initialize missing fields:
	if ~ma_.k,
		ma_.s = ma_.f(ma_.i);
		eval(strcat('ma_.', ma_.s{1}, ' = {};'));
	end
end


%--- 
% Field SIMPLE:
%
%   { 'name', ... }
%
% where each NAME is the name of a variable, which if defined is added
% to the PARAM field like
%
%   { 'name', value, ... }
%---

ma_.i = 0;
while ma_.i < length(ma_.simple),
	ma_.i = ma_.i + 1;
	ma_.v = ma_.simple{ma_.i};
	if exist(ma_.v, 'var') & ~isempty(eval(ma_.v)),
		ma_.param = { ma_.param{:}, ma_.v, eval(ma_.v) };
	end
end

%---
% Field MINUS:
%
%   { 'name', ... }
%
% where each NAME is a list of variable names
%
%   { 'x_n', ... }
%
% all prefixed with X_ (but X may be whatever). Each of this list is
% transformed into a list like
%
%   { '-n', value, ... }
%
% and field PARAM is updated with
%
%   { 'name', { '-n', value, ... } }
%---

ma_.i = 0;
while ma_.i < length(ma_.minus),
	ma_.i = ma_.i + 1;
	ma_.v = ma_.minus{ma_.i};
	if exist(ma_.v, 'var') & ~isempty(eval(ma_.v)),
		ma_.items = eval(ma_.v);
		ma_.list = {};
		ma_.j = 0;
		while ma_.j < length(ma_.items),
			ma_.j = ma_.j + 1;
			ma_.item = ma_.items{ma_.j};
			if exist(ma_.item, 'var') & ~isempty(eval(ma_.item)),
				ma_.ind = findstr(ma_.item, '_');
				if isempty(ma_.ind),
					error([ 'Variable ' ma_.item ' does not contain a prefix.' ]);
				end
				ma_.name = [ '-' ma_.item((ma_.ind+1):length(ma_.item)) ];
				ma_.list = { ma_.list{:}, ma_.name, eval(ma_.item) };
			end
		end
		ma_.param = { ma_.param{:}, ma_.v, ma_.list };
	end
end

%---
% Filed CONCAT:
%
%   { 'name', ... }
%
% where each NAME is a list of variable names
%
%   { 'x_n', ... }
%
% all prefixed with X_ (but X may be whatever). Each of this list is
% transformed into a list like
%
%   { 'xn', value, ... }
%
% and field PARAM is updated with
%
%   { 'name', { 'xn', value, ... } }
%---

ma_.i = 0;
while ma_.i < length(ma_.concat),
	ma_.i = ma_.i + 1;
	ma_.v = ma_.concat{ma_.i};
	if exist(ma_.v, 'var') & ~isempty(eval(ma_.v)),
		ma_.items = eval(ma_.v);
		ma_.list = {};
		ma_.j = 0;
		while ma_.j < length(ma_.items),
			ma_.j = ma_.j + 1;
			ma_.item = ma_.items{ma_.j};
			if exist(ma_.item, 'var') & ~isempty(eval(ma_.item)),
				ma_.ind = findstr(ma_.item, '_');
				if isempty(ma_.ind),
					error([ 'Variable ' ma_.item ' does not contain a _ separator' ]);
				end
				ma_.name = [ ma_.item(1:(ma_.ind-1)) ...
							ma_.item((ma_.ind+1):length(ma_.item)) ];
				ma_.list = { ma_.list{:}, ma_.name, eval(ma_.item) };
			end
		end
		ma_.param = { ma_.param{:}, ma_.v, ma_.list };
	end
end





