
clear


ifileset=dir('~/...path to data.../*.edf');
ifilepath='~/...path to data.../';
ProtocolName='test';

%% Initialize Brainstorm
if ~brainstorm('status')
    brainstorm nogui
end

% Delete exisitng protocol
gui_brainstorm('DeleteProtocol', ProtocolName);
% Create new protocol
gui_brainstorm('CreateProtocol', ProtocolName, 0, 0);

%% set up the anatomy
% Import standard anatomy for each subject
for sbj = 1:size(ifileset,1)
    SubjectName = ['NewSubject' num2str(sbj)];
    % Check if subject already exists
    if isempty(bst_get('Subject', SubjectName))
        % Create subject with default anatomy
        bst_process('CallProcess', 'process_import_anatomy', [], [], ...
            'subjectname', SubjectName, ...
            'mrifile', 'default', ...
            'nvertices', 15002);
    end
end

%% analysis
for sbj=1:size(ifileset,1)

    % Input files
    sFiles = [];
    SubjectName = {['NewSubject' num2str(sbj)]};
    RawFiles = {[ifilepath '/' ifileset(sbj).name]};
    
    % Start a new report
    bst_report('Start', sFiles);
    
    % Process: Create link to raw file
    sFiles = bst_process('CallProcess', 'process_import_data_raw', sFiles, [], ...
        'subjectname',    SubjectName{1}, ...
        'datafile',       {RawFiles,'EEG-EDF'}, ...
        'channelreplace', 0, ...
        'channelalign',   0, ...
        'evtmode',        'value');

    % Process: Set channel file
    sFiles = bst_process('CallProcess', 'process_import_channel', sFiles, [], ...
        'channelfile',  {'Channel_Loc/STD64.sfp', ''}, ...
        'channelalign', 1, ...
        'fixunits',     1, ...
        'vox2ras',      1);
    %
    % Process: Compute head model
    sFiles = bst_process('CallProcess', 'process_headmodel', sFiles, [], ...
        'Comment',     '', ...
        'sourcespace', 1, ...  % Cortex surface
        'meg',         1, ...  % 
        'eeg',         2, ...  % 3-shell sphere
        'ecog',        1, ...  % 
        'seeg',        1, ...  % 
        'channelfile', '');
    
    % Process: Compute covariance (noise or data)
    sFiles = bst_process('CallProcess', 'process_noisecov', sFiles, [], ...
        'baseline',       [0, 119.998], ...
        'datatimewindow', [0, 119.998], ...
        'sensortypes',    'MEG, EEG, SEEG, ECOG', ...
        'target',         1, ...  % Noise covariance     (covariance over baseline time window)
        'dcoffset',       1, ...  % Block by block, to avoid effects of slow shifts in data
        'identity',       0, ...
        'copycond',       0, ...
        'copysubj',       0, ...
        'copymatch',      0, ...
        'replacefile',    1);  % Replace
    
    % Process: Compute sources [2018]
    sFiles = bst_process('CallProcess', 'process_inverse_2018', sFiles, [], ...
        'output',  1, ...  % Kernel only: shared
        'inverse', struct(...
             'Comment',        'sLORETA: EEG', ...
             'InverseMethod',  'minnorm', ...
             'InverseMeasure', 'sloreta', ...
             'SourceOrient',   {{'fixed'}}, ...
             'Loose',          0.2, ...
             'UseDepth',       0, ...
             'WeightExp',      0.5, ...
             'WeightLimit',    10, ...
             'NoiseMethod',    'reg', ...
             'NoiseReg',       0.1, ...
             'SnrMethod',      'fixed', ...
             'SnrRms',         1e-06, ...
             'SnrFixed',       3, ...
             'ComputeKernel',  1, ...
             'DataTypes',      {{'EEG'}}));
    
    % Save and display report
    ReportFile = bst_report('Save', sFiles);
    bst_report('Open', ReportFile);
    % Delete temporary files
    gui_brainstorm('EmptyTempFolder');

end
