function varargout=dopEventFilter(varargin)
% dopOSCCI Mark II: dopEventFilter ~ 10-11-2010 (last edit)
%
% create logical filters for baseline correction (base) and period of
% interest (poi).
%
% Base & Stim Only Event Types:
% if dealing with multiple events with types 0 and 1 (base & stim only) -
% creates epoch & base for base and epoch & poi for stim only.
%

fprintf('\n%s\n\t%s\n',['Running ',mfilename],...
    'Creating Event/Epoch Filters:');
try
    inputs.varargin=varargin;
    inputs.defaults=struct('epoch',[-10 10],... % epoch around event marker in seconds
        'base',[-10 0],... % period of baseline data in seconds
        'poi',[0 10],... % period of interest in seconds
        'sample',25,... % sample rate of input (to figure out timing)
        'evChs',0,... % doppler external channel numbers
        'eventLabels','mark1',...); % names of event channels
        '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
    
    ef=setGetInputsStruct(inputs);
    
    ef.translation='event filter';
    ef.structAbbreviations='ef';
    
    %% create filters
    % size row logical array selecting particular period
    % I think it's worth having epoch in here for later loops...
    ef.tmp.sections={'epoch','base','poi'};
    
    for i=1:length(ef.evChs)
        ef.tmp.ch=i;
        ef.tmp.evName=ef.eventLabels;
        if iscell(ef.eventLabels)
            ef.tmp.evName=ef.eventLabels{ef.tmp.ch};
        end
        ef.tmp.rowLength=1+(sum(abs(ef.epoch(ef.tmp.ch,:)))/(ef.sample^-1)); % number of samples
        ef.tmp.zeroPt=1+(abs(ef.epoch(ef.tmp.ch,1))/(ef.sample^-1)); % + 1 = zero point
        
        if sum(ef.epoch(ef.tmp.ch,:)<0)==0
            % if the event has a completely positive epoch then it's the
            % difference, not the absolute sum of these numbers that gives
            % you the length
            ef.tmp.rowLength=1+(diff(ef.epoch(ef.tmp.ch,:))/(ef.sample^-1)); % number of samples
            ef.tmp.zeroPt=1-(abs(ef.epoch(ef.tmp.ch,1))/(ef.sample^-1)); 
        end
        
        for j=1:length(ef.tmp.sections)
            fprintf('\t%s\t',['Type ',num2str(ef.evType(ef.tmp.ch)),...
                ': Section ',ef.tmp.sections{j}]);
            if or(ef.evType(ef.tmp.ch)==2, ... % no match = base and stim event
                    or(ef.evType(ef.tmp.ch)==0 && ~strcmp(ef.tmp.sections{j},'poi'),... % is base type, no period of interest
                    ef.evType(ef.tmp.ch)==1 && ~strcmp(ef.tmp.sections{j},'base'))) % is stim type, no base
                fprintf('%s','Running');
                ef.tmp.sec=ef.tmp.sections{j}; % section name
                
                ef.tmp.filter=zeros(1,ef.tmp.rowLength); % empty filter
                
                ef.out.(ef.tmp.evName).zeroPt=ef.tmp.filter;
                ef.tmp.secFilter=ef.tmp.zeroPt+(ef.(ef.tmp.sec)(ef.tmp.ch,:)/(ef.sample^-1));
                ef.tmp.filter(ef.tmp.secFilter(1):ef.tmp.secFilter(2))=1;
                ef.out.(ef.tmp.evName).(ef.tmp.sec)(1,:,1)=logical(ef.tmp.filter);
 
                %             ef.out.(ef.tmp.evName).(ef.tmp.sec)(1,:,2)=logical(ef.tmp.filter);
                if sum(ef.epoch(ef.tmp.ch,:)<0)>0
                    ef.out.(ef.tmp.evName).zeroPt(ef.tmp.zeroPt)=1;
                    ef.out.zeroPt.(ef.tmp.evName)=find(ef.out.(ef.tmp.evName).zeroPt);
                    
                else % no actual zero point...
                    % not sure what to do with this or where it will cause
                    % problems... probably in graphing...
                    ef.out.zeroPt.(ef.tmp.evName) = ef.tmp.zeroPt;
                    ef.out.(ef.tmp.evName).zeroValue = ef.tmp.zeroPt;
%                     ef.out.(ef.tmp.evName).zeroPtNote = 'positive epoch with reference to event marker therefore no actual zero point';
                    
                    ef.tmp.secFilter=ef.tmp.zeroPt+(ef.(ef.tmp.sec)(ef.tmp.ch,:)/(ef.sample^-1));
                end

            end
            fprintf('\n');
        end
    end
    %% set ouputs
    varargout{1}=ef.out;
    fprintf('\n\t%s\n','Done');
catch err
    %% catch dopOSCCI error
    %
    save(dopOSCCIdebug(mfilename));rethrow(err);
    %     dopOSCCIcrash(err,ef,mfilename);
end

clear ef inputs

