function warpMRI_DEV2(indx)
if nargin == 0
    indx =1;
end;
%%
addpath('/data/common/FieldtripCurrent/fieldtrip-20120105/');
addpath('/data/common/FieldtripCurrent/fieldtrip-20120105/fieldtrip_private/');
ft_defaults;
spm2_path = '/data/common/FieldtripCurrent/fieldtrip-20120105/external/spm2/';
spm8_path = '/data/common/FieldtripCurrent/fieldtrip-20120105/external/spm8/';
rmpath(spm2_path,spm8_path);
%%
path2files = '/data/projects2/Development/DevelopmentData/MRI_DEVELOPMENT/V2mri/';
savepath = '/data/projects2/Development/DevelopmentData/matFiles/MRI/test_data/';

mrifiles = dir([path2files,'THA07_V2.mri']);
mriIDs = {mrifiles(:).name}';
%%   
for it = 1:length(indx)
    
    subjIt = indx(it);
    fprintf([num2str(subjIt),'/',num2str(length(mriIDs))]);
    
    % read the single subject anatomical mri    
    mri = ft_read_mri([path2files,mrifiles(subjIt).name]);
    outfilename = [savepath,mriIDs{subjIt},'_MRI4MNI.mat'];
    
%     cfg = [];
%     cfg.downsample = 2;
%     mri = ft_volumedownsample(cfg,mri);
    save(outfilename,'mri');
    
    % segment the anatomical mri
    cfg = [];    
    cfg.cordsys = 'ctf';
    cfg.template    = '/data/common/SPM8current/spm8/canonical/single_subj_T1.nii';
    seg = ft_volumesegment(cfg, mri);        
    seg.anatomy = mri.anatomy;
    
    % construct volume conductor model (i.e. head model) for each subject
    % this is optional, use can also use another model, e.g. single sphere
    % model
    cfg = [];    
    hdm = ft_prepare_singleshell(cfg, seg);
    
    % normalize this subjects mri to the template mri, the purpose is to
    % obtain the (inverse) transformation

    cfg = [];    
    cfg.template = '/data/common/SPM8current/spm8/canonical/single_subj_T1.nii';
    %cfg.downsample = 2;
    cfg.coordinates = 'ctf';
    cfg.nonlinear = 'no';
    
    norm = ft_volumenormalise(cfg, mri);
    clear mri;
    
    % reverse-normalise the template grid to this individual subject
    load('/data/projects2/Development/DevelopmentData/matFiles/MRI/test_data/template_GRID_29-Mar-2012.mat');

    grid = [];    
    grid.pos = warp_apply(inv(norm.cfg.final), template_grid.pos, 'homogenous')/10;    
    grid.inside = template_grid.inside;
    grid.outside = template_grid.outside;
    
    % also remember the transformation matrix
    transform_ctf2spm = norm.cfg.final;
    
    
    %make a figure of the single subject headmodel, sensor positions and
    %grid positions
    
    figure;
    hold on;
    %ft_plot_sens(ft_read_sens(['/data/MEGArchive/data/meg/CTF_Data/DEVELOPMENT.proc/DATASETS/',mriIDs{subjIt}(1:5),'_DEVELOPMENT_20090924_12.ds']));
    ft_plot_sens(ft_read_sens(['/data/MEGArchive/data/meg/CTF_Data/DEVELOPMENT.proc/DATASETS/THA07_DEVELOPMENT_20090924_12.ds']),'style','b.');
    ft_plot_vol(hdm,'facecolor','skin','vertexcolor','skin','egdecolor','skin','faceindex',0,'vertexindex',0);
    ft_plot_mesh(grid.pos(grid.inside,:));
    
    outfilename = [savepath,mriIDs{subjIt},'_MNIGRID5mmres_newInwardshift.mat'];
    
    save(outfilename, 'hdm', 'grid','seg','transform_ctf2spm');
    clear('seg','hdm','grid','transform_ctf2spm','template_grid', 'template_grad', 'template_hdm');
    fprintf('\n');
end;
%exit;
return