function [srd, data, options] = srdPack(A, T, fusionMeth,cv, niter, percent, allmix, exact, wfRules)
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% Collate the input matrix in each cell component of A into one numeric 
% matrix input A
%
% Brett Edit (Updated 1/8/16):
% 1) Now runes srd directly from srdPack instead of having to feed data and options into srdMaster
% 2) Added a new input fusionMeth, where srd fusion rules ('srd'), other fusion rules
% ('wf') or both ('both') are computed with A and T
% 3)Also added fix on line 40 to make A into a cell array if A is matrix to make the
% code run correctly
% 4) Added option to pick which willet fusion rules you can run, can be
% 'all', or a cell array of the options: {'sum','min','max','med','l2','std'}
%
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

%--------------------------------------------------------------------------
% Check and parse inputs
%
if (nargin<3), fusionMeth='both'; end
%
if ( nargin < 4 ), cv = []; end; 
if isempty(cv), cv = 'none'; end;
%
if ( nargin < 5 ), niter = []; end; 
if isempty(niter), niter = 'none'; end;
%
if ( nargin < 6 ), percent = []; end;
if isempty(percent), percent = 50; end;
%
if ( nargin < 7 ), allmix = []; end;
if isempty(allmix), allmix = true; end;
%
if ( nargin < 8 ), exact = []; end; 
if isempty(exact), exact = false; end;
% 
if ( nargin <9 ), wfRules=[]; end
if isempty(wfRules), wfRules='all'; end
%

%--------------------------------------------------------------------------
% Check input matrix (A).  
%
%makes A into a cell (Brett 10/27/15)
if ~iscell(A), A={A}; end
%
A = A(:);
Acopy = A;
nblock = length(A);
nsample = cell2mat( cellfun(@(x)(size(x,1)), A, 'Un', false) );
A = cell2mat(A);    

%--------------------------------------------------------------------------
% Check target info (T).  
% 
% Make sure that target is cell array 
if ~iscell(T), T = { T }; end
if (length(T) == 1), T = repmat(T, nblock, 1); end
T = T(:);
Trule = cell(nblock, 1);
% Cycle through each target associated with each input matrix
for i = 1:nblock
    % Create numeric target vector from string operation
    t = T{i};
    if ischar(t)
        a = Acopy{i};
        Trule{i} = t;
        if ismember(t,{'min','mean','median','max'})
            switch t
                case {'mean','median'}, v = feval(t,a,2);
                case {'min','max'},     v = feval(t,a,[],2);
            end
        else
            error('%s not a valid string',t);
        end
    else
        % User-supplied target vector
        v = t(:);
        Trule{i} = 'min';  % set 'min' target as default rule such that 
                           % the fusion ranking can proceed.
        if ( length(v) ~= nsample(i) )
            error('T must be of length %d', nsample);
        end
    end
    T{i} = v;
end    
T = cell2mat(T);

%--------------------------------------------------------------------------
% Assign exact to false (force normal approximation) if the number 
% of samples is greater than 10
%
if any( nsample > 10 ), exact = false; end

%--------------------------------------------------------------------------
% If the number of iterations is 1, do not perform cross validation
%
if ( niter == 1 ), cv = 'none'; end   

%--------------------------------------------------------------------------
% Check to see if the number of rows in each block (if there are 
% multiple blocks) are the same.  
%
if any( nsample ~= nsample(1) ), allmix = true; end  

%--------------------------------------------------------------------------
% % 
data = struct('A',{A}, 'T',{T});
options = struct('nblock',{nblock}, ...
	'nsample',{nsample(:)'},...
	'exact',{exact},...
	'cv',{cv},...
	'niter',{niter},...
	'percent',{percent},....
	'allmix',{allmix},...
	'targetRule',{Trule(:)'},...
	'fusionMeth', fusionMeth);
if strcmpi(wfRules, 'all')
	options.fusionRule={'sum','min','max','med','l2','std'};
else
	if iscell(wfRules) % Brett 1/8/16
		options.fusionRule=wfRules;
	else
		options.fusionRule={wfRules};
	end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

srd=srdMaster(data, options); % Brett 10/22/15
end


