function varargout=dopDataIndexCalcSetup(eventData,poiFilter,varargin)
% dopOSCCI Mark II: dopDataIndexCalcSetup ~ 25-11-2010 (last edit)
%
% sets up event channels for:
% calculation of data indices/descriptives
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
%
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
%
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%

fprintf('\n%s\n\t%s',['Running ',mfilename],...
    'Setting up Events for Data Index/Descriptives calculations:');
try
    inputs.varargin=varargin;
    inputs.defaults=struct('actWin',2,...);%,... % activation window duration in seconds
        'screen',[],...); % epoch screen
        'sampleRate',25,...); % sampleRate in hertz to figure out timing
        'evType',2,...; % event type: 0 = base, 1 = stim only, 2 = both
        'evMatch',0,...); % if event type = 0 or 1, it needs a match for with 1 or 0, only 2 is independent
        'beh',[],...); % behavioural filter variable (see dopBehSummary)
        'saveEpoch',0); % is the epoch data requested to be saved ('all' screen)
    dis=setGetInputsStruct(inputs);
    
    dis.translation='data index setup';
    dis.structAbbreviations='dis';
    dis.data=eventData; clear eventData % 27-10-10 now L&R data
    dis.filter=poiFilter; clear poiFilter
    
    % assume data is a stack of data from dopEventStack
    dis.ens=dis.data.evNames; % prior 29-10-10 fieldnames(dis.data); % event names
    for i=1:length(dis.ens)
        dis.en=dis.ens{i}; % event name
        dis.scrn=[];
        if isfield(dis.screen,dis.en)
            for j=1:numel(dis.screen.labels); dis.sl=dis.screen.labels{j}; % screen label
                %                 if or(dis.screen.calcData(j),...
                %                         and(strcmp(dis.sl,dis.screen.labels{1}),dis.saveEpoch))
                if dis.screen.calcData(j) || dis.screen.epochQual(j) and(strcmp(dis.sl,dis.screen.labels{1}),dis.saveEpoch)
                    % don't need to process all levels of the screening
                    % variable - only all for each epoch and scrn - or
                    % whatever is requested to be saved
                    %% behavioural summary
                    if ~isempty(dis.beh)
                        dis.tmp.bvs=fieldnames(dis.beh); % behavioural variables
                        for jj=1:length(dis.tmp.bvs); dis.tmp.bv=dis.tmp.bvs{jj};
                            dis.out.behNames{jj}=dis.tmp.bv;
                            dis.tmp.bls=size(dis.beh.(dis.tmp.bvs{jj}),2); % behavioural levels
                            for jjj=1:dis.tmp.bls
                                dis.scrn=and(dis.screen.(dis.en)(:,j),dis.beh.(dis.tmp.bvs{jj})(:,jjj));
                                fprintf('\t%s\n',['Beh epochs (',...
                                    dis.en,' ',dis.out.behNames{jj},' ',num2str(jjj),'): '...
                                    num2str(find( dis.scrn'))]);
                                dis.out.(dis.tmp.bv)(jjj).(dis.en).(dis.sl)=indCalc(dis,i); % see embedded function below
                                %                                 fprintf('\t%s\n',['Beh epochs (',...
                                %                                     dis.en,' ',dis.out.behNames{jj},' ',num2str(jjj),'): '...
                                %                                     num2str(find( dis.scrn'))]);
                            end
                        end
                    else
                        if dis.evType(i)
                            dis.scrn=dis.screen.(dis.en)(:,j); % only send in one screen column at a time
                            dis.out.(dis.en).(dis.sl)=indCalc(dis,i); % see embedded function below
                        end
                    end
                end
                %                 switch dis.evType(i) % 0 = base, 1 = stim only, 2 = both
                %                     % no idex calculation for base
                %                     case 1
                %                         dis.bn=dis.ens{dis.evType(i)}; % base name
                %                         dis.ind=dopDataIndexCalc(dis.data.(dis.en),... % data
                %                             dis.filter.(dis.en),... filters
                %                             'screen',dis.scrn,...
                %                             'sampleRate',dis.sampleRate,...
                %                             'filterZero',find(dis.filter.(dis.en).zeroPt,1,'first'),...
                %                             'evType',dis.evType(i),'baseLRdata',...
                %                             dis.data.(dis.bn)(:,dis.filter.(dis.bn).base,:)); % base event data with base filter
                %                     case 2
                %                         dis.ind=dopDataIndexCalc(dis.data.(dis.en),... % data
                %                             dis.filter.(dis.en),... filters
                %                             'screen',dis.scrn,...
                %                             'sampleRate',dis.sampleRate,...
                %                             'filterZero',find(dis.filter.(dis.en).zeroPt,1,'first'));
                %                 end
                %                 dis.out.(dis.en).(dis.sl)=dis.ind;
            end
        else % without screening
            dis.out.(dis.en)=dopDataIndexcalc(dis.data.(dis.en),...
                dis.filter.(dis.en),...
                'actWin',dis.actWin,...
                'sampleRate',dis.sampleRate);
        end
    end
    
    %% set ouputs
    dis.out.note='ind =  data index output';
    varargout{1}=dis.out;
    fprintf('\t%s\n','Done');
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end

clear dis inputs ind
end
%% summaries
function ind=indCalc(dis,i)
try
    switch dis.evType(i) % 0 = base, 1 = stim only, 2 = both
        % no idex calculation for base
        case 1
            dis.bn=dis.ens{dis.evType(i)}; % base name
            ind=dopDataIndexCalc(dis.data.(dis.en),... % data
                dis.filter.(dis.en),... filters
                'screen',dis.scrn,...
                'sampleRate',dis.sampleRate,...
                'filterZero',find(dis.filter.(dis.en).zeroPt,1,'first'),...
                'evType',dis.evType(i),'baseLRdata',...
                dis.data.(dis.bn)(:,dis.filter.(dis.bn).base,:)); % base event data with base filter
        case 2
            ind=dopDataIndexCalc(dis.data.(dis.en),... % data
                dis.filter.(dis.en),... filters
                'screen',dis.scrn,...
                'sampleRate',dis.sampleRate,...
                'filterZero',find(dis.filter.(dis.en).zeroPt,1,'first'));
    end
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end
end