function dop=dopOverallDataCalc(dop)
% dopOSCCI Mark II: dopOverallDataCalc ~ 6-Jan-2011 (last edit)
%
% calculate indicies for Overall Data
%

try
    if isfield(dop,'grd')
        fprintf('\n%s\n\t%s\n',['Running ',mfilename],...
            'Calculations for Graph data (Overall):');
        dop.tmp.rm={'ind','li','sph'};
%         for i=1:numel(dop.tmp.rm)
%             if isfield(dop.grd,dop.tmp.rm{i})
%                 dop.grd=rmfield(dop.grd,dop.tmp.rm{i});
%             end
%         end

        
        for i=1:dop.grd.n % for each data file
            dop.tmp.en=dop.grd.info{i}.evName; % event name
            dop.tmp.evNum=find(strcmp(dop.tmp.en,dop.def.evNames)); % event number
            fprintf('\n\t%s\t%s\n\t\t%s\t%s\n\t\t%s\t%i\n',...
                'File Name:',dop.grd.fileList{i},...
                'Event = ',dop.tmp.en,'Event Number = ',dop.tmp.evNum);
            dg.tmp.ind=(['ind',num2str(i)]);
            dg.tmp.li=(['li',num2str(i)]);
            dg.tmp.sph=(['',num2str(i)]);
            %% index calculation
            switch dop.def.evType(dop.tmp.evNum)
                case 2 % stim & base together
%                     dop.grd.ind(i)=dopDataIndexCalc(dop.grd.data(i).LR,... % data
                    dop.grd.(dg.tmp.ind)=dopDataIndexCalc(dop.grd.data(i).LR,... % data
                        dop.filter.(dop.tmp.en),... filtersv'screen',dis.scrn,...
                        'sampleRate',dop.def.downHertz,...
                        'filterZero',find(dop.filter.(dop.tmp.en).zeroPt,1,'first'));

                case 1 % stim only...need to find a base match...
                    dop.tmp.mn=dop.def.evNames{dop.def.evMatch(dop.tmp.evNum)}; % match name
                    dop.tmp.mNum=0;
                    for j=1:dop.grd.n % for each data file
                        if j~=i && strcmp(dop.grd.info{j}.evName,dop.tmp.mn)...
                                && strcmp(dop.grd.info{j}.graphName,dop.grd.info{i}.graphName)
                            dop.tmp.mNum=j;
                            break
                        end
                    end
                    if dop.tmp.mNum
                        dop.tmp.evType=dop.def.evType(dop.tmp.evNum);
                        
                        dop.grd.(dg.tmp.ind)=dopDataIndexCalc(dop.grd.data(i).LR,... % data
                            dop.filter.(dop.tmp.en),... filtersv'screen',dis.scrn,...
                            'sampleRate',dop.def.downHertz,...
                            'filterZero',find(dop.filter.(dop.tmp.en).zeroPt,1,'first'),...);
                            'evType',dop.tmp.evType,'baseLRdata',...
                            dop.grd.data(dop.tmp.mNum).LR(:,dop.filter.(dop.tmp.mn).base,:)); % base event data with base filter
                    else
                        fprintf('\t%s %s %s\n\t%s\n','No match found for:',...
                            dop.tmp.en,dop.grd.info{i}.graphName,...
                            dop.grd.fileList{i});
                    end
                case 0
                    dop.grd.(dg.tmp.ind)=[];
                    
            end
            %% LI calculation
            switch dop.def.evType(dop.tmp.evNum)
                case {2,1}
                    %                     dop.grd.li(i)=dopDataLIcalc(dop.grd.data(i).LR,... % data
                    dop.grd.(dg.tmp.li)=dopDataLIcalc(dop.grd.data(i).LR,... % data
                        dop.filter.(dop.tmp.en).poi,... filter
                        'actWin',dop.def.actWin(dop.tmp.evNum),...'screen',lis.scrn,...
                        'sampleRate',dop.def.downHertz,...
                        'filterZero',find(dop.filter.(dop.tmp.en).zeroPt,1,'first'));
                    %% Split half calculation
                    %                     'splitOptions',dop.def.splitOptions,'splitBal',dop.def.splitBal,...
                    %         'splitCalcSave',dop.def.splitCalcSave,'splitLabels',dop.def.splitLabels
                    for ii=1:numel(dop.def.splitOptions)
                        dop.tmp.ne=size(dop.grd.data(i).LR,1); % number of elements
                        if dop.def.splitOptions(ii)
                            dop.tmp.splitScrn=[];
                            switch ii
                                case 1 % odd vs even
                                    dop.tmp.splitScrn(:,1)=(mod(1:dop.tmp.ne,2)==1)';
                                    dop.tmp.splitScrn(:,2)=(mod(1:dop.tmp.ne,2)==0)';
                                case 2 % random
                                    dop.tmp.rp=randperm(dop.tmp.ne);
                                    dop.tmp.sel=zeros(dop.tmp.ne,1);
                                    dop.tmp.sel(dop.tmp.rp(1:dop.tmp.ne/2))=1;
                                    dop.tmp.splitScrn(:,1)=dop.tmp.sel==1;
                                    dop.tmp.splitScrn(:,2)=dop.tmp.sel==0;
                                    
                            end
                            if dop.def.splitBal % balancing the epochs in each split
                                while diff(sum(dop.tmp.splitScrn))
                                    dop.tmp.sum=sum(dop.tmp.splitScrn);
                                    dop.tmp.drop=1;% drop epochs from first half
                                    if dop.tmp.sum(2)>dop.tmp.sum(1) % drop epochs from second half
                                        dop.tmp.drop=2;
                                    end
                                    dop.tmp.splitScrn(find(dop.tmp.splitScrn(:,dop.tmp.drop),1,'first'),dop.tmp.drop)=0;
                                end
                            end
                            for jj=1:2;
                                dop.def.spl=dop.def.splitLabels{ii,jj}; %split label
                                dop.def.scrn=dop.tmp.splitScrn(:,jj);
                                fprintf('\n\t\t%s\t%s\n',dop.tmp.en,['(epoch screen ',dop.grd.info{i}.epSel,')']);
%                                 sphs.out.(sphs.en).=sphCalc(sphs,i);
                                dop.grd.sph(i).(dop.def.spl)=dopDataLIcalc(dop.grd.data(i).LR,... % data
                                    dop.filter.(dop.tmp.en).poi,... filter
                                    'actWin',dop.def.actWin(dop.tmp.evNum),...
                                    'screen',dop.def.scrn,...
                                    'sampleRate',dop.def.downHertz,...
                                    'filterZero',find(dop.filter.(dop.tmp.en).zeroPt,1,'first'));
                            end
                        end
                    end
                    
                case 0
%                     dop.grd.li(i)=[];
                    dop.grd.sph(i).evType='baseOnly';
            end
            
            
        end
    end
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,dop,mfilename);
end