function dop=dopOverallCalcSave(dop)
% dopOSCCI Mark II: dopGraphCalcSave ~ 12-Jan-2011 (last edit)
%
% save indicies for Overall Data
%

try
    if dop.def.saveData && isfield(dop,'grd')
        fprintf('\n%s\n\t%s\n',['Running ',mfilename],...
            'Saving Calculations for Overall data:');
        %         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
        dop.sav.saveLabelsOverall=1;
        dop.sav.labelsOverall={'fileName','eventName'};
        dop.sav.fileNameOverall='dopOverallSummaryData.dat';
        dop.sav.fileNameLocOverall=[dop.def.saveDir,dop.sav.fileNameOverall];
        
        dop.tmp.chks={'grp','beh','ses'};
        for j=1:numel(dop.tmp.chks)
            if dop.def.([dop.tmp.chks{j},'FileChk']) && dop.(dop.tmp.chks{j}).okay %
                dop.sav.labelsOverall{end+1}=[dop.tmp.chks{j},'Name'];
                dop.sav.labelsOverall{end+1}=[dop.tmp.chks{j},'Num'];
            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
            dop.tmp.evType=dop.def.evType(dop.tmp.evNum); % event type: 0 = base, 1 = stim only, 2 = both
            dop.tmp.esel=dop.grd.info{i}.epSel;
            fprintf('\n\t%s\t%s\n\t\t%s\t%s\n\t\t%s\t%i\n\t\t%s\t%i\n',...
                'File Name:',dop.grd.fileList{i},...
                'Event = ',dop.tmp.en,'Event Number = ',dop.tmp.evNum,...
                'Event Type = ',dop.tmp.evType);
            dg.tmp.ind=(['ind',num2str(i)]);
            dg.tmp.li=(['li',num2str(i)]);
            dg.tmp.sph=(['',num2str(i)]);
            
            % not everything can be saved for each event type base and stim
            % only are restricted...
            % event type: whatever they might be
            if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                dop.sav.data={dop.grd.info{i}.graphName};
                %                 dop.sav.labelsOverall{end+1}='eventName';
                dop.sav.data{end+1}=dop.tmp.en;
                % opening data
                for j=1:numel(dop.tmp.chks)
                    
                    if dop.def.([dop.tmp.chks{j},'FileChk']) && dop.(dop.tmp.chks{j}).okay %
                        if isfield(dop.grd.info{i},[dop.tmp.chks{j},'Name'])
                            dop.sav.data{end+1}=dop.grd.info{i}.([dop.tmp.chks{j},'Name']);
                            dop.sav.data{end+1}=dop.grd.info{i}.([dop.tmp.chks{j},'Num']);
                        else
                            dop.sav.data{end+1}='not part of file';
                            dop.sav.data{end+1}=0;
                        end
                    end
                end
                %% > Index data
                
                
                %                 for ii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                %                     if dop.scrn.saveData(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened % dop.def.saveEpochSelection(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                
                % index channels: left right difference
                for j=1:numel(dop.def.saveIndexChsOverall); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                    if dop.def.saveIndexChsOverall(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                        % filters/sections: epoch base poi
                        for jj=1:numel(dop.def.saveIndexFiltOverall); dop.tmp.inf=jj; % index filter
                            if dop.def.saveIndexFiltOverall(dop.tmp.inf); dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                                % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                                for jjj=1:numel(dop.def.saveIndicesOverall); dop.tmp.ind=dop.def.saveIndicesOverall(jjj); % index
                                    if dop.tmp.ind; dop.tmp.indAbb=dop.def.saveIndexLabels{jjj}; dop.tmp.indVar=dop.def.saveIndexVarLabels{jjj};
                                        if dop.sav.saveLabelsOverall
                                            % event, '_',epoch selection, ch, filt, index
                                            dop.sav.labelsOverall{end+1}=[dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb];% 10 Mar 2011 ,'_',dop.tmp.esel,];%,dop.sav.sesLabel,'_',dop.tmp.en];
                                        end
                                        dop.sav.data{end+1}=...
                                            dop.grd.(dg.tmp.ind).(dop.tmp.infAbb).(dop.tmp.indVar)(1,1,dop.tmp.ich);
                                    end
                                end
                            end
                        end
                    end
                end
                %                 end
                %                     end
                %                 end
                
                
                %% --------------------------------------------
                %% > LI data
                %         dop.tmp.ens=dop.mk.evNames; % event names
                %             for i=1:length(dop.tmp.ens)
                %                 dop.tmp.en=dop.tmp.ens{i}; % event name
                %                 dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
                % not everything can be saved for each event type base and stim
                % only are restricted...
                %             if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                %                 for ii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                %                     if dop.scrn.saveData(ii); % dop.def.saveEpochSelection(ii) % saving particular set of data
                %                         dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
%                 dop.tmp.esel=dop.grd.info{1}.epSel;
                for j=1:length(dop.sav.LIoptions)
                    dop.tmp.sop=dop.sav.LIoptions{j}; % save option
                    %                             if dop.def.(dop.tmp.sop)
                    for jj=1:length(dop.sav.abb.(dop.tmp.sop))
                        dop.tmp.abb=dop.sav.abb.(dop.tmp.sop){jj}; % data abbreviation
                        if dop.def.([dop.tmp.sop,'Overall'])(jj)
                            if dop.sav.saveLabelsOverall
                                dop.sav.labelsOverall{end+1}=dop.tmp.abb;% 10 Mar 2011 ,'_',dop.tmp.esel,];'_',dop.tmp.esel];%,'_',dop.tmp.en,dop.sav.sesLabel]; % could
                            end
                            dop.sav.data{end+1}=...
                                dop.grd.(dg.tmp.li).all.(dop.tmp.abb);
                        end
                    end
                end
                %                     end
                %                 end
                %             end
                %             end
                %% > Split-Half LI data
                %         dop.tmp.ens=dop.mk.evNames; % event names
                if dop.def.splitCalcSave(2)
                    %                 for i=1:length(dop.tmp.ens)
                    %                     dop.tmp.en=dop.tmp.ens{i}; % event name
                    %                     dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
                    % not everything can be saved for each event type base and stim
                    % only are restricted...
                    %                 if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                    for iii=1:numel(dop.def.splitOptions)
                        if dop.def.splitOptions(iii);
                            for jjj=1:2%numel(dop.def.splitLabels{iii});
                                dop.tmp.sphl=dop.def.splitLabels{iii,jjj}; % split half label
                                %                                 for ii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                                %                                     if dop.scrn.saveData(ii); % dop.def.saveEpochSelection(ii) % saving particular set of data
%                                 dop.tmp.esel=dop.grd.info{1}.epSel; %dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                                for j=1:length(dop.sav.LIoptions)
                                    dop.tmp.sop=dop.sav.LIoptions{j}; % save option
                                    for jj=1:numel(dop.def.([dop.tmp.sop,'SplitOverall']))
                                        if dop.def.([dop.tmp.sop,'Split'])(jj)
                                            %                                                 for jj=1:length(dop.sav.abb.(dop.tmp.sop))
                                            dop.tmp.abb=dop.sav.abb.(dop.tmp.sop){jj}; % data abbreviation
                                            if dop.sav.saveLabelsOverall
                                                dop.sav.labelsOverall{end+1}=['splitHalf',dop.tmp.sphl,'_',dop.tmp.abb,'_',dop.tmp.esel];%,'_',dop.tmp.en,dop.sav.sesLabel]; % could
                                            end
                                            dop.sav.data{end+1}=...
                                                dop.grd.sph(i).(dop.tmp.sphl).all.(dop.tmp.abb); % all? 2-Jan-2011
                                        end
                                    end
                                end
                                %                                     end
                                %                                 end
                            end
                        end
                    end
                    %                 end
                    %                 end
                end
                
                
                %             %% 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).(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
                %% save data
                if dop.sav.saveLabelsOverall
                    %             if ~exist(dop.sav.fileNameLoc,'file')
                    %                 dop.tmp.fid=fopen(dop.sav.fileNameLoc,'w'); % open the file to write
                    %             else
                    dop.tmp.fid=fopen(dop.sav.fileNameLocOverall,'w'); % open the file to write
                    %             end
                    for j=1:length(dop.sav.labelsOverall) % write each label
                        %                 dop.sav.labels{i}
                        fprintf(dop.tmp.fid,'%s\t',dop.sav.labelsOverall{j});
                    end
                    fprintf(dop.tmp.fid,'\n');
                    fclose(dop.tmp.fid);
                    dop.sav.saveLabelsOverall=0;
                end
                
                
                
                dop.tmp.fid=fopen(dop.sav.fileNameLocOverall,'a+'); % open the file to append
                for j=1:length(dop.sav.data) % write each label
                    if isnumeric(dop.sav.data{j}) || islogical(dop.sav.data{j})
                        fprintf(dop.tmp.fid,'%+3.2f\t',dop.sav.data{j});
                    else
                        fprintf(dop.tmp.fid,'%s\t',dop.sav.data{j});
                    end
                end
                fprintf(dop.tmp.fid,'\n');
                fclose(dop.tmp.fid);
            end
        end
    end
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,dop,mfilename);
end