function varargout=dopDataDropCalcSetup(eventData,poiFilter,varargin)
% dopOSCCI Mark II: dopDataDropCalcSetup ~ 1-06-2011 (last edit)
%
% set up events for raw data drop calculations
%

fprintf('\n%s\n\t%s',['Running ',mfilename],...
    'Setting up Events for Raw Data Drop 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)
    dds=setGetInputsStruct(inputs);
    
    dds.translation='data drop setup';
    dds.structAbbreviations='dds';
    dds.data=eventData; clear eventData % 27-10-10 now L&R data
    dds.filter=poiFilter; clear poiFilter
    
    % assume data is a stack of data from dopEventStack
    dds.ens=dds.data.evNames; % prior 29-10-10 fieldnames(dds.data); % event names
    for i=1:length(dds.ens)
        dds.en=dds.ens{i}; % event name
        dds.scrn=[];
        if isfield(dds.screen,dds.en)
            for j=1:numel(dds.screen.labels); dds.sl=dds.screen.labels{j}; % screen label
                if or(dds.screen.calcData(j),...
                        and(strcmp(dds.sl,dds.screen.labels{1}),dds.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(dds.beh)
                        dds.tmp.bvs=fieldnames(dds.beh); % behavioural variables
                        for jj=1:length(dds.tmp.bvs); dds.tmp.bv=dds.tmp.bvs{jj};
                            dds.out.behNames{jj}=dds.tmp.bv;
                            dds.tmp.bls=size(dds.beh.(dds.tmp.bvs{jj}),2); % behavioural levels
                            for jjj=1:dds.tmp.bls
                                dds.scrn=and(dds.screen.(dds.en)(:,j),dds.beh.(dds.tmp.bvs{jj})(:,jjj));
                                fprintf('\t%s\n',['Beh epochs (',...
                                    dds.en,' ',dds.out.behNames{jj},' ',num2str(jjj),'): '...
                                    num2str(find( dds.scrn'))]);
                                dds.out.(dds.tmp.bv)(jjj).(dds.en).(dds.sl)=dropCalc(dds,i); % see embedded function below
%                                 fprintf('\t%s\n',['Beh epochs (',...
%                                     dds.en,' ',dds.out.behNames{jj},' ',num2str(jjj),'): '...
%                                     num2str(find( dds.scrn'))]);
                            end
                        end
                    else
                        if dds.evType(i)
                            dds.scrn=dds.screen.(dds.en)(:,j); % only send in one screen column at a time
                            dds.out.(dds.en).(dds.sl)=dropCalc(dds,i); % see embedded function below
                        end
                    end
                end
                %                 switch dds.evType(i) % 0 = base, 1 = stim only, 2 = both
                %                     % no idex calculation for base
                %                     case 1
                %                         dds.bn=dds.ens{dds.evType(i)}; % base name
                %                         dds.ind=dopDataDropCalc(dds.data.(dds.en),... % data
                %                             dds.filter.(dds.en),... filters
                %                             'screen',dds.scrn,...
                %                             'sampleRate',dds.sampleRate,...
                %                             'filterZero',find(dds.filter.(dds.en).zeroPt,1,'first'),...
                %                             'evType',dds.evType(i),'baseLRdata',...
                %                             dds.data.(dds.bn)(:,dds.filter.(dds.bn).base,:)); % base event data with base filter
                %                     case 2
                %                         dds.ind=dopDataDropCalc(dds.data.(dds.en),... % data
                %                             dds.filter.(dds.en),... filters
                %                             'screen',dds.scrn,...
                %                             'sampleRate',dds.sampleRate,...
                %                             'filterZero',find(dds.filter.(dds.en).zeroPt,1,'first'));
                %                 end
                %                 dds.out.(dds.en).(dds.sl)=dds.ind;
            end
        else % without screening
            dds.out.(dds.en)=dopDataDropCalc(dds.data.(dds.en),...
                dds.filter.(dds.en),...
                'actWin',dds.actWin,...
                'sampleRate',dds.sampleRate);
        end
    end
    
    %% set ouputs
    dds.out.note='ind =  data index output';
    varargout{1}=dds.out;
    fprintf('\t%s\n','Done');
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));rethrow(err); 
%     dopOSCCIcrash(err,dds,mfilename);
end

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