%Excitability
% clear
addpath('//your_path/brainstorm3')
if ~brainstorm('status')
    brainstorm server
end

%%
ProtocolName = 'iScience';
% get the protocol index, knowing the name
iProtocol = bst_get('Protocol', ProtocolName);
% set the current protocol
gui_brainstorm('SetCurrentProtocol', iProtocol);
ProtocolInfo=bst_get('ProtocolInfo');
my_subjects = bst_get('ProtocolSubjects');

SubjectNames = {...
    'Group_analysis'};

for    jh=1:length(my_subjects.Subject)
    iSubject=bst_get('Subject',jh);
    if isempty(strfind(iSubject.Name, 'Group_analysis'))
        Subject_Studies=bst_get('StudyWithSubject', iSubject.FileName);
        for sd=1:length(Subject_Studies)
            if isempty(strfind(string(Subject_Studies(sd).Condition), 'CAT12'))
                %            try
                [sStudy, iStudy]= bst_get('Study', Subject_Studies(sd).FileName);
                All_sFiles={sStudy.Result.FileName};
                
                %All_sFiles = All_sFiles(not(cellfun(@isempty, strfind(All_sFiles, 'link'))));
                
                % Process: Select file names with tag: excitability
                All_sFiles = bst_process('CallProcess', 'process_select_tag', All_sFiles, [], ...
                    'tag',    'excitability', ...
                    'search', 2, ...  % Search the file names
                    'select', 1);  % Select only the files with the tag
                
                % Process: Average: Everything
                All_sFiles = bst_process('CallProcess', 'process_average', All_sFiles, [], ...
                    'avgtype',         1, ...  % Everything
                    'avg_func',        1, ...  % Arithmetic average:  mean(x)
                    'weighted',        0, ...
                    'scalenormalized', 0);
                
                % Process: Average time: [all]
                All_sFiles = bst_process('CallProcess', 'process_average_time', All_sFiles, [], ...
                    'timewindow', [], ...
                    'isstd',      0, ...
                    'overwrite',  0, ...
                    'source_abs', 0);
                
                % Process: Project on default anatomy: surface
                sFiles_projected = bst_process('CallProcess', 'process_project_sources', All_sFiles, [], ...
                    'headmodeltype', 'surface');  % Cortex surface
                
                Tag = ([fileparts(Subject_Studies(sd).FileName) '_avg']);
                
                % Process: Set name:
                sFiles_projected_time = bst_process('CallProcess', 'process_set_comment', sFiles_projected, [], ...
                    'tag',           Tag, ...
                    'isindex',       0);
                
                % Process: Move files: Group_analysis/Atlas_Desikan_Avg
                sFiles_projected_time = bst_process('CallProcess', 'process_movefile', sFiles_projected_time, [], ...
                    'subjectname', SubjectNames{1}, ...
                    'folder',      'Atlas_Desikan_Avg');
                %%
                
%                 % Process: Delete selected files
%                 All_sFiles = bst_process('CallProcess', 'process_delete', All_sFiles, [], ...
%                     'target', 1);  % Delete selected files
%                 
%                 % Process: Delete selected files
%                 sFiles_projected = bst_process('CallProcess', 'process_delete', sFiles_projected, [], ...
%                     'target', 1);  % Delete selected files
                
            end
        end
    end
end
