%% Prepare_data_in_different_formats for analyses in each subject

clear
overwrite = 1;

subject = [71]

for isubj = subject
    
    %% Select your parameters to save the data in one file
    
    format.baseline = [-0.2 0];
    format.lpfreq = 30;
    %format.hpfreq = 2;
    format.resamplefs = 200;
    format.detrend = 'no';
    format.trigger_ref = 'target'; % what is the trigger used as a reference
    
    
    data_file_name = ['dformat_s',num2str(isubj)];
    % Define globals
    global G;
    G = [];
    
    %% General paths
    exp_main_path = '/project/3018029.06';
    
    SUBJS = {['0',num2str(isubj)]}
    curSubj = SUBJS{1}
    
    addpath(genpath( [exp_main_path filesep 'Matlab' filesep 'MEG3_scripts'])); % general processing scripts in this exp.
    
    G.subjPath = [exp_main_path filesep 'MEG' filesep 'AVE_MEG3' filesep curSubj filesep]; % main subject folder
    G.outputPath = [G.subjPath ,'func', filesep]; % subject saving artifact and preprocessing matrixes
    G.cfgbehavPath = [exp_main_path filesep 'Behavioral' filesep 'AVE_MEG3' filesep curSubj filesep] ; % behavioral data of participant
    
    subjscriptsPath = [G.subjPath ,'analysis', filesep]; %  participant specific scripts
    
    
%     
%     do_analysis = 0;
%     n = 0;
%     
%     if exist([G.outputPath data_file_name,'.mat'],'file')
%         load([G.outputPath data_file_name,'.mat']);
%         % file exist; checking whether the analyses is new...
%         
%         number_of_datasets = size(dformat,1);
%         disp(['there are ' num2str(number_of_datasets) ' datasets in this participant file'])
%         
%         
%         for i = 1 : number_of_datasets % lets evaluate whether if we have performed this data transformation for this subject before+
%             data_exist = 0
%             if strcmp(num2str(dformat(i).format.baseline), num2str(format.baseline)) & ...
%                     strcmp(num2str(dformat(i).format.lpfreq) ,num2str(format.lpfreq)) & ...
%                     strcmp(num2str(dformat(i).format.resamplefs) ,num2str(format.resamplefs)) & ...
%                     strcmp(num2str(dformat(i).format.detrend) ,num2str(format.detrend)) & ...
%                     strcmp(num2str(dformat(i).format.trigger_ref) ,num2str(format.trigger_ref));  data_exist = 1; end;
%             if (data_exist > 0 & overwrite == 0) ; error(['this data have been already stored this way in dataset number: ' num2str(i)]); break; end;
%             if (data_exist > 0 & overwrite == 1); do_analysis = 1; n = i;  end; % overwrite in case that this is the dataset repeated
%             if (i == number_of_datasets & data_exist == 0); do_analysis = 1;
%                 n = i + 1; end; % if we did not find this analyses already performed, save data in the next folder.
%         end
%         
%     else
        disp(['No datasets for this participant, creating first analysis'])
        dformat = [];
        do_analysis = 1;
        n = 1;
%    end
    
    
    
    if do_analysis
        
        %% loading preproc data
        load([G.outputPath 'data_preproc_clean.mat'])
        
        
        %% Baseline correction
        cfg = [];
        cfg.baseline = format.baseline;
        data_preproc_clean  = ft_timelockbaseline(cfg, data_preproc_clean);
        
        
        %% Filter data: lowpass filter at 30 Hz
        
        cfg = [];
        cfg.lpfilter = 'yes';
        cfg.lpfreq = format.lpfreq;
        %cfg.hpfilter = 'yes';
        %cfg.hpfreq = format.hpfreq;
        data_preproc_clean = ft_preprocessing(cfg, data_preproc_clean);
        
        % Downsampling the data
        cfg                 = [];
        cfg.resamplefs = format.resamplefs; % 200 hz is 5 ms time-res, 20Hz is 50 ms
        cfg.detrend    = format.detrend;
        data_preproc_clean = ft_resampledata(cfg, data_preproc_clean);
        
        % if exist([G.outputPath ''],'file')
        %     load([G.outputPath 't'])
        % else
        %      dformat = [];
        % end
        
        
        % saving format characteristics
        
        
        dformat(n).data = data_preproc_clean;
        dformat(n).format = format;
        
        save([G.outputPath data_file_name,'.mat'],'dformat','-v7.3')
        
    end
    
end

