function varargout=dopDataLIcalcSetup(eventData,poiFilter,varargin)
% dopOSCCI Mark II: dopDataLIcalcSetup ~ 25-11-2010 (last edit)
%
% sets up event channels for:
% calculate Laterality Index (etc.) for each event channel within period of
% interest
%
% expects the inputted eventData to be an X (epoch) by Y (samples) by Z = 2
% (doppler channels - left right) array.
%
% copes with it being within a structure for multiple events:
% dop.stack.evenName1: [10x501x2 double], dop.stack.evenName2: [10x501x2
% double], etc.
% OR can just be the data itself, not in a structure.
%

% -------------------------------------------------------------------------
%     '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\n',['Running ',mfilename],...
    'Setting up Events fo Laterality Index calculation:');
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
        'beh',[],...); % behavioural filter variable (see dopBehSummary)
        'saveEpoch',0,...); % is the epoch data requested to be saved ('all' screen)
        'epochGraph',0); % if epochs are to be graphed, need all summary
    lis=setGetInputsStruct(inputs);
    
    lis.translation='laterality index setup';
    lis.structAbbreviations='lis';
    lis.data=eventData; clear eventData % 27-10-10 now L&R data
    lis.filter=poiFilter; clear poiFilter
    
    % assume data is a stack of data from dopEventStack
    lis.ens=lis.data.evNames; % prior 29-10-10 fieldnames(lis.data); % event names
    for i=1:length(lis.ens)
        lis.en=lis.ens{i}; % event name
        lis.scrn=[];
        if isfield(lis.screen,lis.en) && lis.evType(i) % requires stimOnly or stim eventType - not base
            for j=1:length(lis.screen.labels)
                lis.sl=lis.screen.labels{j}; % screen label
                %                 lis.scrn=lis.screen.(lis.en)(:,j); % only send in one screen column at a time
                if or(lis.screen.calcData(j),...
                        and(strcmp(lis.sl,lis.screen.labels{1}),or(lis.saveEpoch,lis.epochGraph)))
                    % 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(lis.beh)
                        lis.tmp.bvs=fieldnames(lis.beh); % behavioural variables
                        for jj=1:length(lis.tmp.bvs); lis.tmp.bv=lis.tmp.bvs{jj};
                            lis.out.behNames{jj}=lis.tmp.bv;
                            lis.tmp.bls=size(lis.beh.(lis.tmp.bvs{jj}),2); % behavioural levels
                            for jjj=1:lis.tmp.bls
                                lis.scrn=and(lis.screen.(lis.en)(:,j),lis.beh.(lis.tmp.bvs{jj})(:,jjj));
                                fprintf('\n\t\t%s\t%s\n',lis.en,...
                                    ['( beh ',lis.tmp.bv, '(',num2str(jjj),')',...
                                    'epoch screen ',num2str(j),' ',lis.sl,')']);
                                lis.out.(lis.tmp.bv)(jjj).(lis.en).(lis.sl)=liCalc(lis,i); % see embedded function below
                                fprintf('\t%s\n',['Beh epochs (',...
                                    lis.en,' ',lis.out.behNames{jj},' ',num2str(jjj),'): '...
                                    num2str(find( lis.scrn'))]);
                            end
                        end
                    else
                        %                     if sum(lis.scrn)
                        if lis.evType(i)
                            lis.scrn=lis.screen.(lis.en)(:,j); % only send in one screen column at a time
                            fprintf('\n\t\t%s\t%s\n',lis.en,['(epoch screen ',num2str(j),')']);
                            lis.out.(lis.en).(lis.sl)=liCalc(lis,i);
                        end
                        
                    end
                end
            end
        elseif lis.evType(i) % without screening
            lis.out.(lis.en)=dopDataLIcalc(lis.data.(lis.en),...
                lis.filter.(lis.en).poi,...
                'actWin',lis.actWin,...
                'sampleRate',lis.sampleRate);
        end
    end
    
    %% set ouputs
    lis.out.note='li = laterality index output';
    varargout{1}=lis.out;
    fprintf('\n\t%s\n','Done');
catch err
    fprintf('\t%s\n','Error...');
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end
clear lis inputs li
end


function li=liCalc(lis,i)
try
    zeroPt = find(lis.filter.(lis.en).zeroPt,1,'first');
    if isempty(zeroPt)
        % if there's a completely positive epoch
        zeroPt = lis.filter.(lis.en).zeroValue;
    end
    li=dopDataLIcalc(lis.data.(lis.en),... % data
        lis.filter.(lis.en).poi,... filter
        'actWin',lis.actWin(i),'screen',lis.scrn,...
        'sampleRate',lis.sampleRate,...
        'filterZero',zeroPt);
catch err
    fprintf('\t%s\n','Error in embedded function');
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end
end
