function dop=dopEpochScreen(dop)
% dopOSCCI Mark II: dopEpochScreen~ 04-Nov-2011 (last edit)
%
% need some mechanism through which different levels of screening can be
% conducted and presented.
% 1 = allEpochs (no screening)
% 2 = length (no missing starts or ends)
% 3 = actEpochs (acceptable actvation epochs)
% 4 = manEpochs (manually selected epochs)
% + scrnEpochs (if 2 & 3 then combination)
% + balEpochs (if final session in session summary, then scrn + session
%   balance epoch selection).
%
% dop.def.saveEpochSelection=[0 1 0 0 0],... % save data from all, act, man, scrn(act + man), & balanced epochs (session only)
% dop.def.saveEpochSelectionLabels={'all','length','act','man','scrn','bal'};
%
% see also dopEventScreen
%

fprintf('%s\n',['Running ',mfilename]);
try
    if ~dop.sum.skip && ~dop.sum.quit
        %% standard all & activation epochs
        for i=1:length(dop.mk.evNames)
            dop.tmp.en=dop.mk.evNames{i};
            dop.scrn.(dop.tmp.en)=dop.scrn.ev.(dop.tmp.en);
        end
        %% set labels
        if ~isfield(dop.scrn,'labels')
            dop.scrn.labels=dop.scrn.ev.columnLabels; % {'all','length','act'};
            dop.scrn.saveData=dop.def.saveEpochSelection(1:3);
            dop.scrn.saveGraphData=dop.def.saveEpochSelectionGraph(1:3);
            dop.scrn.calcData=[1 dop.def.saveEpochSelection(2:3)+dop.def.saveEpochSelectionGraph(2:3)]; % always calculate all
            dop.scrn.calcGraphData=[1 dop.def.saveEpochSelectionGraph(2:3)];
            dop.scrn.epochQual = dop.def.saveEpochSelQual(1:3);
        end
        %% manual screening
        if dop.def.scrnFileChk
            if ~sum(strcmp(dop.scrn.labels,dop.def.saveEpochSelectionLabels{4})) % if just 2, add the 'man'
                fprintf('\t%s\n','Including Manual screening');
                dop.scrn.labels{end+1}=dop.def.saveEpochSelectionLabels{4}; % 'man'
                dop.scrn.saveData(end+1)=dop.def.saveEpochSelection(4);
                dop.scrn.saveGraphData(end+1)=dop.def.saveEpochSelectionGraph(4);
                dop.scrn.calcData(end+1)=dop.def.saveEpochSelection(4);
                dop.scrn.calcGraphData(end+1)=dop.def.saveEpochSelectionGraph(4);
                dop.scrn.epochQual(end+1) = dop.def.saveEpochSelQual(4);
            end
            % find a match
            dop.scrn.man.match=0;
            for i=1:numel(dop.scrn.man.fileList)
                if strcmp(dop.sum.fileNameLoc,dop.scrn.man.fileList{i})
                    dop.scrn.man.match=i; break
                end
            end
            % create screening for each event... is this necessary...maybe,
            % could be different data
            for i=1:length(dop.mk.evNames)
                dop.tmp.en=dop.mk.evNames{i};
                dop.tmp.scrn=ones(size(dop.scrn.(dop.tmp.en),1),1); % all epochs
                if dop.scrn.man.match
                    if sum(dop.scrn.man.scrn{dop.scrn.man.match})
                        dop.tmp.scrn(dop.scrn.man.scrn{dop.scrn.man.match})=0;
                    end
                else
                    fprintf('\t%s\n','No manual screen match, using default = all epochs');
                    
                end
                dop.scrn.(dop.tmp.en)=[dop.scrn.(dop.tmp.en) logical(dop.tmp.scrn)]; % man
            end
        end
        %% scrn column
        % collection of suitable epochs: dat + act + man (if being used)
        if ~sum(strcmp(dop.scrn.labels,dop.def.saveEpochSelectionLabels{5}))
            dop.scrn.labels{end+1}=dop.def.saveEpochSelectionLabels{5}; % 'scrn'
            %         dop.scrn.select(end+1)=[dop.scrn.select dop.def.saveEpochSelectionGraph(5)];
            
            dop.scrn.saveData(end+1)=dop.def.saveEpochSelection(5);
            dop.scrn.saveGraphData(end+1)=dop.def.saveEpochSelectionGraph(5);
            % always calculate the screen data
            dop.scrn.calcData(end+1)=1;%dop.def.saveEpochSelection(5);
            dop.scrn.calcGraphData(end+1)=1;% dop.def.saveEpochSelectionGraph(5);
            dop.scrn.epochQual(end+1) = dop.def.saveEpochSelQual(5);
            
        end
        for i=1:length(dop.mk.evNames)
            dop.tmp.en=dop.mk.evNames{i};
            dop.scrn.(dop.tmp.en)=[dop.scrn.(dop.tmp.en) sum(dop.scrn.(dop.tmp.en),2)==length(dop.scrn.labels)-1]; % scrn
            fprintf('\t%s\n',['Epochs used, ',dop.tmp.en,':']);
            for j=1:length(dop.scrn.labels)
                dop.tmp.rej = sum(dop.scrn.(dop.tmp.en)(:,j)==0);
                dop.tmp.rejStr = zeros(1,dop.tmp.rej*2-1);
                if dop.tmp.rej
                    dop.tmp.rejStr(1:2:numel(dop.tmp.rejStr)) = find(dop.scrn.(dop.tmp.en)(:,j)==0)';
                    dop.tmp.rejStr = strrep(strrep(num2str(dop.tmp.rejStr),'  0  ','~'),' ','');
                else
                    dop.tmp.rejStr = '0';
                end
                dop.scrn.rej.(dop.tmp.en).(dop.scrn.labels{j}) = dop.tmp.rejStr;
                fprintf('\t\t%s %u',...
                    dop.scrn.labels{j},...
                    sum(dop.scrn.(dop.tmp.en)(:,j)));
                if sum(dop.scrn.(dop.tmp.en)(:,j))<sum(dop.scrn.(dop.tmp.en)(:,1))
                    fprintf(' %s %s%s','(rejected = ',...
                        dop.tmp.rejStr,')');
                end
                 fprintf('\n');
            end
        end
        dop=rmfield(dop,'tmp');
        %% report
        fprintf('\t%s\t%s\t%s\n','Epoch Save:','Data','Graph'); 
        for i=1:length(dop.scrn.labels)
            dop.tmp.s=' '; % space
            if length(dop.scrn.labels{i})==4
                dop.tmp.s='';
            end
            fprintf('\n\t\t%s\t%u\t\t%u\n',....
                [dop.scrn.labels{i},dop.tmp.s],...
                dop.scrn.saveData(i),dop.scrn.saveGraphData(i));
        end
    end
catch err
    %% catch dopOSCCI error
    % 
 save(dopOSCCIdebug(mfilename));rethrow(err); 
%     dopOSCCIcrash(err,dop,mfilename);
end



