function RetAnatPreProcess(RawFile,AnatFile,XfmFile,InvXfmFile,blurring_kernel,blurring_kernel2,blurring_kernel3,res_like_file,outbase,smooth_ders);
%function RetAnatPreProcess(RawFile,AnatFile,XfmFile,InvXfmFile,blurring_kernel,blurring_kernel2,blurring_kernel3,res_like_file,outbase,smooth_ders);
%
% preforms preanalysis for anatomical files
% anatomical can either be t1 or classified white matter only volumes
% Input:
%        RawFiles          : where fmri files can be found
%                            fmri files are assumed to be paired (ecc & pol)
%        AnatFile          : anatomical file to be used in tal-space
%        XfmFile           : transormation to tal-space
%        InvXfmFile        : inverse of previous one
%        blurring_kernel   : used in pre-analysis [default=[]]
%        blurring_kernel2  : used to calculate dx,dy en dz maps [default=12]
%        blurring_kernel3  : used to calculate ders of fmri-maps [default=[]]
%

% outname = base '_toFMRI_blur_toRet_blur.mnc'

% ---------------------------------
% Copyright (c) 2003 Serge Dumoulin
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License version 2
% as published by the Free Software Foundation.
% ---------------------------------



if nargin < 2,
   help RetAnatPreProcess;
   return;
end;
if nargin < 3,
   XfmFile=[];
end;
if nargin < 4;
   if ~isempty(XfmFile),
      [path,name,base,baseid]=getmincbase(deblank(XfmFile));
      InvXfmFile=[baseid '_inv.xfm'];
      system(['xfminvert ' XfmFile ' ' InvXfmFile]);
   else,
      InvXfmFile=[];
   end;
end;
if nargin <5,
   blurring_kernel = [];
end;
if nargin <6,
   blurring_kernel2 = 12;
end;
if nargin <7,
   blurring_kernel3 = [];
end;
if nargin <8,
   res_like_file=AnatFile;
end;
if nargin <9,
   outbase=[];
end;
if nargin < 10,
   smooth_ders=0
end;

%-----------------------------------------------------------------------
%--- checking of blurring kernels
%-----------------------------------------------------------------------
if ~isempty(blurring_kernel),
   if sum(size(blurring_kernel))==2, % 1 by 1 matrix
      MyBlurKernel=sprintf('%d %d %d', ...
                           blurring_kernel,blurring_kernel,blurring_kernel);
   else, % assume 1 by 3 matrix
      [m,n] = size(blurring_kernel);
      if m == 1,       % If a row, turn into column vector
         blurring_kernel = blurring_kernel(:);
      end;
      MyBlurKernel=sprintf('%d %d %d', ...
                           blurring_kernel(1),blurring_kernel(2),blurring_kernel(3));
   end;
end;

if sum(size(blurring_kernel2))==2, % 1 by 1 matrix
   My2ndBlurKernel=sprintf('%d %d %d', ...
                           blurring_kernel2,blurring_kernel2,blurring_kernel2);
else, % assume 1 by 3 matrix
   [m,n] = size(blurring_kernel2);
   if m == 1,       % If a row, turn into column vector
      blurring_kernel2 = blurring_kernel2(:);
   end;
   My2ndBlurKernel=sprintf('%d %d %d', ...
                           blurring_kernel2(1),blurring_kernel2(2),blurring_kernel2(3));
end;

if ~isempty(blurring_kernel3),
   if sum(size(blurring_kernel3))==2, % 1 by 1 matrix
      MyFMRIBlurKernel=sprintf('%d %d %d', ...
                               blurring_kernel3,blurring_kernel3,blurring_kernel3);
   else, % assume 1 by 3 matrix
      [m,n] = size(blurring_kernel3);
      if m == 1,       % If a row, turn into column vector
         blurring_kernel3 = blurring_kernel3(:);
      end;
      MyFMRIBlurKernel=sprintf('%d %d %d', ...
                               blurring_kernel3(1),blurring_kernel3(2),blurring_kernel3(3));
   end;
end;


%-----------------------------------------------------------------------
%--- start pre-processing through the same steps as functional data
%-----------------------------------------------------------------------

if isempty(outbase),
   [path,name,outbase,baseid]=getbase(deblank(AnatFile));
end;
base=outbase;

for n=1:size(RawFile,1),
   string='mincaverage ';

   %--- resampling to native space
   disp(sprintf('%s',RawFile(n,:)));
   if ~isempty(InvXfmFile),
      if size(InvXfmFile,1) == 1,
         system(['mincresample -clobber ' AnatFile ' ' ...
                 base '_toFMRI.mnc ' ...
                 ' -like ' RawFile(n,:) ' ' ...
                 ' -transform ' InvXfmFile ]);
      else,
         system(['mincresample -clobber ' AnatFile ' ' ...
                 base '_toFMRI.mnc ' ...
                 ' -like ' RawFile(n,:) ' ' ...
                 ' -transform ' InvXfmFile(n,:) ]);
      end;
   else,
      system(['mincresample -clobber ' AnatFile ' ' ...
              base '_toFMRI.mnc ' ...
              ' -like ' RawFile(n,:) ]);
   end;

   %--- preprocess blurring
   if ~isempty(blurring_kernel),
      system(['mincblur -clobber -gaussian -3dfwhm  ' MyBlurKernel ' ' ...
              base '_toFMRI.mnc ' ...
              base '_toFMRI' ]);
   else,
      copyfile([base '_toFMRI.mnc'],[base '_toFMRI_blur.mnc']);
   end;

   %--- resample to space where final analysis will be done
   if ~isempty(XfmFile),
      if size(XfmFile,1) == 1,
         system(['mincresample ' base '_toFMRI_blur.mnc ' ...
                 base '_toFMRI_blur_toRet' sprintf('%d',n) '.mnc ' ...
                 ' -like ' res_like_file ' ' ...
                 ' -transform ' XfmFile ]);
      else,
         system(['mincresample ' base '_toFMRI_blur.mnc ' ...
                 base '_toFMRI_blur_toRet' sprintf('%d',n) '.mnc ' ...
                 ' -like ' res_like_file ' ' ...
                 ' -transform ' XfmFile(n,:) ]);
      end;
   else,
      system(['mincresample ' base '_toFMRI_blur.mnc ' ...
              base '_toFMRI_blur_toRet' sprintf('%d',n) '.mnc ' ...
              ' -like ' res_like_file ]);
   end;
   string=[string ' ' base '_toFMRI_blur_toRet' sprintf('%d',n) '.mnc '];
end;

%--- averaging of files
if size(RawFile,1) > 1,
   string=[string ' ' base '_toFMRI_blur_toRet.mnc'];
   system(string);
else,
   copyfile([base '_toFMRI_blur_toRet1.mnc'], ...
            [base '_toFMRI_blur_toRet.mnc']);
   delete([base '_toFMRI_blur_toRet1.mnc']);
end;

%--- getting ders
if ~isempty(blurring_kernel3),
   system(['mincblur -gaussian -partial -3dfwhm  ' My2ndBlurKernel ' ' ...
           base '_toFMRI_blur_toRet.mnc ' ...
           base '_toFMRI_blur_toRet']);
   system(['mincblur -gaussian -partial -3dfwhm  ' MyFMRIBlurKernel ' ' ...
           base '_toFMRI_blur_toRet.mnc ' ...
           base '_toFMRI_blur_toRet_fmri']);
   [avg,median_val,stdev,mi,ma]= ...
       get_stats([base '_toFMRI_blur_toRet_fmri_dx.mnc']);
   median_val1=median_val+(mi-median_val)/2;
   median_val2=median_val+(ma-median_val)/2;
   mymedstring=sprintf(' %.5f %.5f ',median_val1,median_val2 );
   
   for n=1:3,
      if n==1, der='dx.mnc ';
      elseif n==2, der='dy.mnc ';
      elseif n==3, der='dz.mnc ';
      end;
      system(['mincmath -seg -const2 ' mymedstring ...
              base '_toFMRI_blur_toRet_fmri_' der  ' ' ...
              base '_toFMRI_blur_toRet_fmri_seg' der]);
      system(['mincmath -nseg -const2 ' mymedstring ...
              base '_toFMRI_blur_toRet_fmri_' der  ' ' ...
              base '_toFMRI_blur_toRet_fmri_nseg' der]);
      system(['mincmath -mult ' ...
              base '_toFMRI_blur_toRet_fmri_' der  ' ' ...
              base '_toFMRI_blur_toRet_fmri_nseg' der ' ' ...
              base '_toFMRI_blur_toRet_fmri_nomed' der]);
      system(['mincmath -mult ' ...
              base '_toFMRI_blur_toRet_' der  ' ' ...
              base '_toFMRI_blur_toRet_fmri_seg' der ' ' ...
              base '_toFMRI_blur_toRet_fmri_med' der]);
      system(['mincmath -add -clobber ' ...
              base '_toFMRI_blur_toRet_fmri_med' der ' ' ...
              base '_toFMRI_blur_toRet_fmri_nomed' der ' ' ...
              base '_toFMRI_blur_toRet_' der]);
   end;      
else,
   system(['mincblur -gaussian -partial -3dfwhm  ' My2ndBlurKernel ' ' ...
           base '_toFMRI_blur_toRet.mnc ' ...
           base '_toFMRI_blur_toRet']);
end;
if smooth_ders ~= 0,
   for n=1:3,
      if n==1,
         der='_dx';
      elseif n==2,
         der='_dy';
      elseif n==3,
         der='_dz';
      end;
      copyfile([base '_toFMRI_blur_toRet' der  '.mnc'], ...
               [base '_toFMRI_blur_toRet_nsm' der '.mnc']);
      delete([base '_toFMRI_blur_toRet' der  '.mnc']);
      system(['mincblur -gaussian -3dfwhm  ' My2ndBlurKernel ' ' ...
              base '_toFMRI_blur_toRet_nsm' der '.mnc ' ...
              base '_toFMRI_blur_toRet_nsm' der]);
      copyfile([base '_toFMRI_blur_toRet_nsm' der '_blur.mnc'], ...
               [base '_toFMRI_blur_toRet' der '.mnc']);
      delete([base '_toFMRI_blur_toRet_nsm' der '_blur.mnc']);
   end;
end;

            


%--- cleaning up
%for n=1:size(RawFile,1),
%   unix(['rm -f ' base '_toFMRI_blur_toRet' sprintf('%d',n) '.mnc ']);
%end;
%unix(['rm -f ' ...
%         base '_toFMRI.mnc ' ...
%         base '_toFMRI_blur.mnc ' ...
%         base '_toFMRI_blur_toRet.mnc ' ...
%         base '_toFMRI_blur_toRet_dxyz.mnc ' ...
%         base '_toFMRI_blur_toRet_blur.mnc ']);












