% =========================================================================
%% PREPROCESS SURFACE EEG DATA - MOTIVATIONAL GO/NOGO LEARNING TASK.
% Reading and vizualizing data to mark channels for interpolation.
%
% EEG STUDY, DCCN, NIJMEGEN.
% By J.C.Swart, 2016.
% =========================================================================
% NOTE: script used at the HPC - interactive.


%% Setup. 

clear all; close all; clc
dbstop if error

% set directories
dirs.home                   = '~';

% add fieldtrip and set defaults
addpath /home/common/matlab/fieldtrip;
ft_defaults;

% get subject directories
dirs.log                    = fullfile(dirs.home,'projects','EEG','Log');
dirs.EEGdata                = fullfile(dirs.log, 'EEG_raw');
cd(dirs.EEGdata);
sublist                     = dir('3017033.03_jesmaa_*_001');
sublist                     = {sublist.name};
par.nSub                    = length(sublist);

% epoch information
for iCue = 1:8
    par.eventCode{iCue}     = sprintf('S%d',110+iCue); % s in ft
end
par.epochtime               = [-1.5 4.5];                  % about 1 sec before & after trial


%% loop over subjects.
for iSub = 1:par.nSub;
    
    % clear screen.
    clc
    
    % get subject specific directory
    dirs.rawData            = fullfile(dirs.EEGdata, sublist{iSub});
    cd(dirs.rawData)
    dirs.fileArray          = dir('*.eeg');
    dirs.fileArray          = dirs.fileArray.name;
    
    % define epochs.
    cfg                     = [];
    cfg.dataset             = fullfile(dirs.rawData,dirs.fileArray);
    cfg.trialdef            = [];
    cfg.trialdef.eventvalue = par.eventCode;
    cfg.trialdef.eventtype  = 'Stimulus';
    cfg.trialdef.prestim    = abs(par.epochtime(1));
    cfg.trialdef.poststim   = abs(par.epochtime(2));
    cfg.event               = ft_read_event(dirs.fileArray);
    cfg                     = ft_definetrial(cfg);
    
    % read in epochs.
    data                    = ft_preprocessing(cfg);
    
    % vizualize epochs to mark channels for interpolation.
    cfg                     = [];
    cfg.viewmode            = 'vertical';
    cfg.continous           = 'no';
    sprintf('\n\nVizualizing %s\n\n',sublist{iSub})
    ft_databrowser(cfg, data);
    keyboard
    
end


%% mark the channels for interpolations.
channel2repair              = [
    13 {NaN};
    14 {NaN};
    15 {NaN};
    16 {NaN};
    17 {NaN}; % 42 rather noisy; 55 bit noisy as well - however, activity follows neighbours.
    18 {NaN}; % 40, 48, 32 noisy - seems to contain data still.
    19 {NaN}; % 59 noisy
    20 {NaN}; % 23, 36, 38, 50 noisy - all contain data
    21 {NaN};
    22 {NaN};
    23 {NaN}; % 52 noisy later on 
    24 {NaN}; % 46 funny at start
    25 {NaN};
    26 {NaN};
    27 {NaN};
    28 {NaN};
    29 {NaN};
    30 {NaN};
    31 {NaN}; % 34 artefacts/spikes -> use ICA, as also data in there
    32 {34}; % flatline 34 throughout
    33 {34}; % flatline 34 throughout
    34 {NaN};
    35 {NaN};
    36 {NaN}; % 47 noisy
    37 {NaN};
    38 {NaN};
    39 {NaN};
    40 {NaN};
    41 {NaN};
    42 {NaN}; % 45 funny - ICA?
    43 {NaN};
    44 {NaN};
    ];


%% end of script.