function data=srdPreProcess(A,T,exact,cv,nIteration,percent,allMix)
% Sanity check the input data
%==========================================================================
% INPUT: see description in srdMaster.m 
% 
% OUTPUT: 
% [1] data: struct encapsulating all of the input parameters
%     - .A: nBlock*nRow x nCol matrix of input values.  If the number of
%       blocks is 1, then nRow is the number of samples.  If nBlock > 1, 
%       then nRow is the number of samples for any given block.
%     - .T: nBlock*nRow x nCol vecotr of target values.  T(i) is the 
%       target associated with A(i,:).
%     - .nBlock: The number of cells if the input argument A was a cell 
%       array.  nBlock wil be set to 1 if A was a matrix. 
%     - .exact: same as input argument
%     - .cv: same as input argument
%     - .nIteration: same as input argument
%     - .percent: same as input argument
%     - .allMix: same as input argument (may be changed--see below)
%==========================================================================

%
% Check input matrix (A).  If A is a cell array of matrices, then 
% stack the matrix from each block into a single matrix.
%
Acopy=A;
if iscell(A)
    nBlock=length(A);
    [A,nRow]=srdStack(A);  
else
    nBlock=1; 
    nRow=size(A,1);
end    

%
% Check target info (T).  If A is a cell array of targets, then 
% stack the targets from each block into a single matrix.
%
if iscell(T)
    % Stack target vectors
    Tcopy=T;
    T=cell(nBlock,1); 
    for i=1:nBlock
        T{i}=targetCheck(Acopy{i},Tcopy{i});
    end  
    T=srdStack(T);
else
    T=targetCheck(A,T);
end       

%
% Check exact logical
%
if ~islogical(exact)
    error('exact must true (1) or false (0).');
else
    % If there are more than 10 rows, force the normal approximation
    % (regardless of what the user specified)
    if size(A,1) > 10
        exact=false; 
    end
end   

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

%
% Check to see if the number of rows in each block (if there are 
% multiple blocks) are the same.  If not, then set allMix to false.
%

if ~islogical(allMix)
    error('exact must true (1) or false (0).');
else
    % If there are more than 10 rows, force the normal approximation
    % (regardless of what the user specified)
    if any(nRow~=nRow(1))
        allMix=true; 
    end    
end
%
% Archive results
%
data=struct('A',{A},...
            'T',{T},...
            'nRow',{nRow},...
            'exact',{exact},...
            'cv',{cv},...
            'nIteration',{nIteration},...
            'percent',{percent},....
            'allMix',{allMix},...
            'nBlock',{nBlock});

%==========================================================================
end

%
%--------------------------------------------------------------------------
%
% H E L P E R   S U B - F U N C T I O N S
%
%--------------------------------------------------------------------------
%

function t=targetCheck(a,t)
%==========================================================================
nRow=size(a,1);
if ischar(t)
    % Target is a string 
    if ismember(t,{'min','mean','median','max'})
        switch t
            case {'mean','median'}, t=feval(t,a,2);
            case {'min','max'},     t=feval(t,a,[],2);
        end
    else
        error('%s not a valid string',t);
    end
else
    % Target is a vector
    t=t(:);
    if length(t)~=nRow
        error('T must be of length %d',nRow);
    end
end
%==========================================================================
end

