function dop=dopBehSummary(dop)
% dopOSCCI Mark II: dopBehSummary ~ 06-Dec-2011 (last edit)
%
% check behavioural information:
%
% outputs:
% - behNames=beh.file.varNames;
% - fileReport=beh.file.report;
% - n=beh.file.n;
% - nVar=beh.file.nVar;
% - data=beh.file.data;
% - levels=beh.file.levels;
%

% -------------------------------------------------------------------------
%     '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 && dop.def.behFileChk
        fprintf('\t%s\n','Calculating behavioural summaries:');
        %% match file
        dop.beh.match=0;
        %         for i=1:length(dop.beh.fileList)
        dop.beh.match=find(strcmp(dop.sum.fileNameLoc,dop.beh.fileList));
        %                 dop.beh.match=i;
        %                 break
        %             end
        %         end
        if isempty(dop.beh.match)
            fprintf('\t%s\n\t%s\n\n\t%s\n','No behavioural match found',...
                'Therefore can not run behavioural summary.',...
                'Dummy values will be included in output.');
        end
        %% create filter/s
        % base upon behvaioural conditions
        if isfield(dop.beh,'filter'); dop.beh=rmfield(dop.beh,'filter');end % remove old filters
        for i=1:length(dop.beh.behNames)
            dop.tmp.bn=dop.beh.behNames{i};
            for j=1:length(dop.beh.levels.(dop.tmp.bn));
                dop.tmp.lv=dop.beh.levels.(dop.tmp.bn)(j); % level value
                if ~isempty(dop.beh.match)
                    dop.beh.filter.(dop.tmp.bn)(:,j)=...
                        dop.beh.data.(dop.tmp.bn)(dop.beh.match,:)==dop.tmp.lv;
                else
                    dop.beh.filter.(dop.tmp.bn)(:,j)=...
                        zeros(1,size(dop.beh.data.(dop.tmp.bn),2));
                end
                %% > check filter size
                % if there's a mismatch between the number of available trials and
                % the number of trials of the behavioural break down we can't match
                % the trials
                if ~isempty(dop.beh.match)
                    for ii = 1 : numel(dop.mk.evNames)
                        dop.tmp.en = dop.mk.evNames{ii}; % event name
                        if size(dop.scrn.(dop.tmp.en),1) ~= size(dop.beh.filter.(dop.tmp.bn),1)
                            fprintf(['\n\tMismatch between number of trials (%u) ',...
                                'and behavioural filter (%u)\n',...
                                '\t\tTherefore can not run behavioural summary.\n',...
                                '\t\tDummy values will be included in output.\n',...
                                '\t\t (match variable cleared to cancel calculations)'],...
                                size(dop.scrn.(dop.tmp.en),1),size(dop.beh.filter.(dop.tmp.bn),1));
                            % adjusting match variable which should cancel
                            % calculations
                            dop.beh.match = [];
                        end
                    end
                end
            end
            % trim the filters so we don't get an error in the
            % dopDataIndexCalcSetup function
            if isempty(dop.beh.match) && size(dop.scrn.(dop.tmp.en),1) ~= size(dop.beh.filter.(dop.tmp.bn),1)
                dop.beh.filter.(dop.tmp.bn)(size(dop.scrn.(dop.tmp.en),1)+1:end,:)=[]; 
            end
                
        end
        
        %% session && last session?
        if dop.def.behFileChk && isfield(dop,'ses') && dop.ses.okay && dop.ses.session==dop.ses.nSes+1
            for i=1:numel(dop.beh.behNames)
                dop.tmp.behFilter=dop.beh.filter.(dop.beh.behNames{i});
                for j=2:dop.ses.nSes
                    % stack them on top of each other
                    dop.beh.filter.(dop.beh.behNames{i})=vertcat(dop.beh.filter.(dop.beh.behNames{i}),dop.tmp.behFilter);
                end
            end
        end
        
        %% run summaries for whatever we want...
        dop.beh.ind=dopDataIndexCalcSetup(dop.stack,dop.filter,...
            'screen',dop.scrn,'sampleRate',dop.def.downHertz,...
            'evType',dop.def.evType,'evMatch',dop.def.evMatch,...
            'beh',dop.beh.filter);
        
        %% laterality indices
        dop.beh.li=dopDataLIcalcSetup(dop.stack,dop.filter,'actWin',dop.def.actWin,...
            'screen',dop.scrn,'sampleRate',dop.def.downHertz,...
            'evType',dop.def.evType,'beh',dop.beh.filter);
        %         else
        %             fprtinf('\t%s\n\t%s\n','No behavioural match found',...
        %                 'Therefore can not run behavioural summary.');
        %         end
    else
        fprintf('\t%s\n','Behavioural summary not requested');
    end
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end