function pipeline = batch02_niak_preprocess()

clc
clear
close all
fclose all


addpath(genpath('/mfip/mfip1/alexis/Sofwares/fMRI/NIAK/NIAK6.4/niak-0.6.4.1/'));

path_in='/mfip/mfip1/alexis/Analyses/MNS_EEG_fMRI/Analyse3/CA1_PothYan/';

path_in_fmri=[path_in '01_OriginalData/Functional/'];
path_in_anat=[path_in '01_OriginalData/Anatomical/'];

files_in_fmri={'CA1_MNS_BOLD_run01.mnc','CA1_MNS_BOLD_run02.mnc'};
files_in_anat={'CA1_MNS_anatomical.mnc'};

path_out=[path_in '02_preprocessedData/NIAK/'];
if ~exist(path_out,'dir')
    mkdir(path_out);
end

flag_session=1;

% Cleaning option
% delete ([path_out 'logs' filesep '*.lock'])
% deleteLogs([path_out 'logs' filesep])
% if exist(path_out,'dir')
%     rmdir(path_out,'s')
% end

% Restarting jobs
%opt.psom.restart={'segment'};


% Monitoring pipeline
% psom_pipeline_visu([path_out 'logs' filesep],'monitor')
% psom_pipeline_visu('/mfip/mfip1/alexis/Analyses/MNS_EEG_fMRI/Analyse3/CA1_PothYan/02_preprocessedData/NIAK/logs/','log','t1_preprocess_subject1')


%%%%%%%%%%%%%%%%%%%%%%%
% NIAK PIPELINE %%
%%%%%%%%%%%%%%%%%%%%%%%
niak_gb_vars

% files_in
files_in.subject1.anat             = cat(2,path_in_anat,files_in_anat{1});
files_in.subject1.fmri.session1{1} = cat(2,path_in_fmri,files_in_fmri{1});
files_in.subject1.fmri.session1{2} = cat(2,path_in_fmri,files_in_fmri{2});

% folder out
opt.folder_out =path_out ;
opt.size_output = 'quality_control'; % 'minimum', 'quality_control', 'all'
opt.flag_test = 0;

% PSOM Config
opt.psom.path_logs ='';%[path_out 'logs' filesep];
if flag_session
    opt.psom.mode = 'session';
    opt.psom.mode_pipeline_manager = 'session';
    opt.psom.time_between_checks = 0.5;
else
    psom_gb_vars;
end

% slice timing: No
opt.slice_timing.flag_skip = 1; % Turn on/off the slice timing

% Motion correction (niak_brick_motion_correction)
opt.motion_correction.flag_skip    = 0;          % Turn on/off the motion correction
opt.motion_correction.suppress_vol = 2;          % There is no dummy scan to supress.
opt.bricks.motion_correction.vol_ref = 'median'; % Use the median volume of each run as a target.
opt.motion_correction.session_ref  = 'session1'; % The session that is used as a reference. Use the session corresponding to the acqusition of the T1 scan.
opt.bricks.motion_correction.run_ref = 1;        % The first run of each session is used as a reference.

% % Linear and non-linear fit of the anatomical image in the stereotaxic
% space (niak_brick_t1_preprocess)
opt.t1_preprocess.nu_correct.arg = '-distance 50'; % Parameter for non-uniformity correction. 200 is a suggested value for 1.5T images, 50 for 3T images. If you find that this stage did not work well, this parameter is usually critical to improve the results.

% T1-T2 coregistration (niak_brick_anat2func)
opt.anat2func.init = 'identity'; % The 'center' option usually does more harm than good. Use it only if you have very big misrealignement between the two images (say, > 2 cm).

% Temporal filtering (niak_brick_time_filter)
opt.time_filter.hp = 0.01; % Apply a high-pass filter at cut-off frequency 0.01Hz (slow time drifts)
opt.time_filter.lp = Inf;  % Do not apply low-pass filter. Low-pass filter induce a big loss in degrees of freedom without sgnificantly improving the SNR.

% Correction of physiological noise (niak_pipeline_corsica)
opt.corsica.flag_skip                = 1;     % Turn on/off 

% resampling in stereotaxic space
opt.resample_vol.flag_skip= 1;          % Turn on/off the resampling in stereotaxic space

% Spatial smoothing (niak_brick_smooth_vol)
opt.smooth_vol.flag_skip = 0;  % Turn on/off the spatial smoothing
opt.smooth_vol.fwhm      = 6;  % Apply an isotropic 6 mm gaussin smoothing.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Run the fmri_preprocess pipeline  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[pipeline,opt] = niak_pipeline_fmri_preprocess(files_in,opt);
