function dop=dopEventData(dop)
% dopOSCCI Mark II: dopEventData ~ 8-9-2010 (last edit)
%
% prepare the doppler data for summary: find event markers, create event
% epoch stack, and create base & period of interest filters.
%
% Includes:
% - dopEventMarks (find the appropriate markers)
% - dopEventStack (stack the data into events/trials)
% - dopEventFilter (create filter for base and poi selection
% - dopEventActRej (creates epoch screen based upon activation rejection
%       values, see dopTaskDef)
% - dopEventBaseCorrect (runs baseline correction adjustment)
% - dopEventAverage (calculates average activation of left & right channels)
% - dopEventDifference (calculates difference between the left and right
%       doppler channels)
% - dopEventScreen (creates event/epoch screening array)
%

% -------------------------------------------------------------------------
%     '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
%

try
    if ~dop.sum.skip && ~dop.sum.quit
        %% find the markers
        
        dop.mk=dopEventMarks(dop.inData(:,3:end,:),...
            'epochs',dop.def.epochs,'sep',dop.def.sep,'sample',dop.def.downHertz,...
            'eventLabels',dop.def.evNames,'evChs',dop.def.evChs,...
            'evType',dop.def.evType,'evMatch',dop.def.evMatch);
        
        %% stack the data
        dop.stack=dopEventStack(dop.inData(:,1:2),dop.mk,...
            'epoch',dop.def.epoch,'sample',dop.def.downHertz,...
            'eventLabels',dop.def.evNames,'evChs',dop.def.evChs);
        %% raw stack
        % use this to calculate dropout
        dop.raw.stack=dopEventStack(dop.raw.data(:,1:2),dop.mk,...
            'epoch',dop.def.epoch,'sample',dop.def.downHertz,...
            'eventLabels',dop.def.evNames,'evChs',dop.def.evChs,...
            'record',dop.data.rawInfo.sampleRate);
        
        %% event filter
        dop.filter=dopEventFilter('epoch',dop.def.epoch,'base',dop.def.base,...
            'poi',dop.def.poi,'sample',dop.def.downHertz,...
            'eventLabels',dop.def.evNames,'evChs',dop.def.evChs,...
            'evType',dop.def.evType,'evMatch',dop.def.evMatch);
        
        % 1-11-10
        % need to work here down for the indBase line exercise...
        %% activation rejection
        dop.actRej=dopEventActRej(dop.stack,'actRej',dop.def.actRej,'actSep',dop.def.actSep);
        
        %% baseline correction
        dop=dopEventBaseCorrect(dop);
        
        %% calculate mean channel
        dop.avg=dopEventAverage(dop.stack);
        
        %% calculate difference channel
        dop.dif=dopEventDifference(dop.stack);
        
        %% event/epoch screening
        % create screen for problematic epochs - activation screening
        [dop.scrn.ev dop.sum.skip dop.sum.skipReason]=...
            dopEventScreen(dop.mk,dop.stack,dop.actRej,'evChs',dop.def.evChs,...
            'eventLabels',dop.def.evNames,'inShort',dop.def.inShort,...
            'actRej',dop.def.actRej,'epSelLabels',dop.def.saveEpochSelectionLabels);
        
    end
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end



