% field_map_batch.m is a batch script that calculates a voxel displacement 
% map (VDM) from field map data. It uses the Field Map Toolbox in SPM and 
% the Siemens Verio 3T field map data acquired through a gradient echo 
% (GRE) field sequence.
%
% All files need to be in the analyze or nifti format (.hdr, .img). The
% above sequence produces three files: 2 magnitude files (because of 2 
% different echo times) and a phase difference file. All three files must 
% be in the same directory.
%
% Please note, that even though the code explicitly asks for the EPI
% directory, no EPI unwarping will be performed.
%
% The default code uses the following directory structure. The first two
% files are the magnitude images and the second one is the phase difference
% image. The script will only take into account the first magnitude image.
% DATA_PATH
%       |___ spm
%       |     |___subject1
%       |     |      |___ field_map
%       |     |      |           |__sxxxxxxxxxx-0002-00001-000001-01.img
%       |     |      |           |__sxxxxxxxxxx-0002-00001-000001-02.img
%       |     |      |           |__sxxxxxxxxxx-0003-00001-000001-02.img
%       |     |      |___ epi_dir
%       |     |___ subject2
%       |           |...
%       |...
%
% To use this script, you will have to change the following:
% - Experiment folder
% - Subjects folders
% - Field map directory
% - EPI directory of first scan
% - Short and long TE
%
% It is advisable to run the script from the data_path.
%
% Glad Mihai, 08.11.2011
% mihaip@uni-greifswald.de
% You can use and change and pass on the script as much as you can. The only
% thing I ask is to keep the original author's name.

clear, clc
%% Experiment folder
data_path = '/media/System/Glad/Matlab Kurs';

%% Subjects folders
subjects = {    
    'subject1',...    % 1
    'subject2',...    % 2
};

% field map directory
fm_dir = '01_gre_field_34_2x_betrag';

% epi directory of first scan
epi_dir = 'wasser';

%% parameters used to calculate vdm 
te1 = 4.92; % short TE
te2 = 7.38; % long TE
epifm = 0;  % no epi-based field map, since GRE
tert = 11.56;   % total epi read out time in ms
kdir = -1;     % blip direction (+1/-1)
mask = 0;   % don't mask brain, done by default with a siemens field map
match = 0;  % don't match field map to EPI
write_unw = 0;  % don't write unwarped epi

%% field map calculation #1
for i = 1:numel(subjects)

    % full path of field map
    % needs to be adjusted if data not in 'spm' folder
    FP_fm_dir = fullfile(data_path,subjects{i},'spm',fm_dir);
    FP_epi_dir = fullfile(data_path,subjects{i},'spm',epi_dir);
    
    VDM = FieldMap_preprocess(FP_fm_dir,FP_epi_dir, [te1, te2, epifm, tert, kdir, mask, match, write_unw] );

end
