function dop=dopEpochScreen(dop)
% dopOSCCI Mark II: dopEpochScreen~ 14-09-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'};
%

% -------------------------------------------------------------------------
%     '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('%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)];           
        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);
            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);
        end
        %% add quality settings
        % if they are different
        if sum(dop.def.saveEpochSelQual) && sum(dop.def.saveEpochSelQual) > sum(dop.scrn.calcData)
            % need to add the data quality calculations
            for i = 1 : numel(dop.def.saveEpochSelectionLabels)
                dop.tmp.lab = dop.def.saveEpochSelectionLabels{i}; % temp label
                dop.tmp.lab_ind = strcmp(dop.scrn.labels,dop.tmp.lab);
                if sum(dop.tmp.lab_ind)
                    % label is being used
                    if ~dop.scrn.calcData(dop.tmp.lab_ind) && dop.def.saveEpochSelQual(i)
                        dop.scrn.calcData(dop.tmp.lab_ind) = 1;
                    end
                else
                    fprintf('\tNo quality calc for %s (not available)\n',dop.tmp.lab);
                end
            end
        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)
                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 = ',...
                        num2str(find(dop.scrn.(dop.tmp.en)(:,j)==0)'),')');
                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);
end



