function fm_resample2(transfile,ResFile,outbase);
% function fm_resample2(transfile,ResFile,outbase);


% ---------------------------------
% 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 < 1,
   help(mfilename);
   return;
end;
if nargin < 3,
   outbase=[];
end;
if isempty(outbase),
   outbase=strvcat('phase1ecc___','phase2angle_');
end;
myfiles=outbase;

global NUMBER_PHASE_SHIFTS RET_TMP_SUBDIR;

% sequence:
% phaseshift - resample - average - inv. phaseshift - mode
for loop=1:size(myfiles,1), % loop for ecc and angle
   % get initial files
   allphasefiles=getfiles(RET_TMP_SUBDIR,...
                          sprintf('%s*fft_phase.mnc',deblank(myfiles(loop,:))));
   allmagfiles  =getfiles(RET_TMP_SUBDIR,...
                          sprintf('%s*fft_magnitude.mnc',deblank(myfiles(loop,:))));
   alltmapfiles =getfiles(RET_TMP_SUBDIR,...
                          sprintf('%s*dspear_tmap.mnc',deblank(myfiles(loop,:))));
   
   [path,name_phase,base_phase]=getbase(allphasefiles);

   for n=1:size(name_phase,1), %loop for # initial files files
      % phase shift
      PhaseShift(deblank(name_phase(n,:)),[],NUMBER_PHASE_SHIFTS);
      shiftedfiles=getfiles([],[deblank(base_phase(n,:)) '*']);
      
      % resample
      for loop2=1:size(shiftedfiles,1), % loop for # phaseshifts
         [path,name_phase2,base_phase2]=getbase(shiftedfiles(loop2,:));
         if ~isempty(transfile),
            if size(transfile,1) == 1,
               system(['mincresample -like ' ResFile ' -transform ' transfile ...
                       ' ' name_phase2 ' ' base_phase2 '_res.mnc']);
            else,
               system(['mincresample -like ' ResFile ' -transform ' transfile(n,:) ...
                       ' ' name_phase2 ' ' base_phase2 '_res.mnc']);
            end;               
         else,
            system(['mincresample -like ' ResFile ...
                    ' ' name_phase2 ' ' base_phase2 '_res.mnc']);
         end; 
       end; % end loop phaseshifts
   end;% end #files

   % average and inverse shift
   out=[];
   for n=1:NUMBER_PHASE_SHIFTS+1,
      shift=2*pi - (n-1)* (pi/(NUMBER_PHASE_SHIFTS-1));
      if n==1,
         phasefiles=getfiles(RET_TMP_SUBDIR,...
                             sprintf('%s*fft_phase_res.mnc',...
                                     deblank(myfiles(loop,:))));
         out=sprintf('%s_fft_phase_res.mnc',deblank(myfiles(loop,:)));
         AvgVolumes(phasefiles,deblank(out),'mean');
      else,
         phasefiles=getfiles(RET_TMP_SUBDIR,...
                             sprintf('%s*fft_phase_PhS%d_res.mnc',...
                                     deblank(myfiles(loop,:)),n-1));
         outtmp=sprintf('%s_fft_phase_PhS%d_res.mnc',deblank(myfiles(loop,:)),n-1);  
         AvgVolumes(phasefiles,outtmp,'mean');
         PhaseShift(outtmp,[],1,shift);
         out=strvcat(out,sprintf('%s_fft_phase_PhS%d_res_PhS1.mnc',...
                                 deblank(myfiles(loop,:)),n-1));  
      end;
   end;
   final_output=sprintf('%s_fft_phase.mnc',deblank(myfiles(loop,:)));
   AvgVolumes(out,final_output,'mode');
   
   
   % rest
   outstring=['mincaverage '];
   outstring2=['mincaverage '];
   for n=1:size(allmagfiles),
      [path name base]=getbase(deblank(allmagfiles(n,:)));
      out=[base '_res.mnc'];
      outstring=[outstring ' ' out];
      if ~isempty(transfile),
         if size(transfile,1) == 1,
            system(['mincresample -like ' ResFile ' -transform ' transfile ...
                    ' ' name ' ' out]);
         else,
            system(['mincresample -like ' ResFile ' -transform ' transfile(n,:) ...
                    ' ' name ' ' out]);
         end;               
      else,
         system(['mincresample -like ' ResFile ...
                 ' ' name ' ' out]);
      end;
      if ~isempty(alltmapfiles),
         [path name base]=getbase(deblank(alltmapfiles(n,:)));
         out=[base '_res.mnc'];
         outstring2=[outstring2 ' ' out];
         if ~isempty(transfile),
            if size(transfile,1) == 1,
               system(['mincresample -like ' ResFile ' -transform ' transfile ...
                       ' ' name ' ' out]);
            else,
               system(['mincresample -like ' ResFile ' -transform ' transfile(n,:) ...
                       ' ' name ' ' out]);
            end;               
         else,
            system(['mincresample -like ' ResFile ...
                    ' ' name ' ' out]);
         end;
      end;
   end;
   outstring=[outstring ' ' sprintf('%s_fft_magnitude_tmp.mnc',deblank(myfiles(loop,:)))];
   system(outstring);
   system(['mincmath -mult ' sprintf('%.4f',sqrt(size(allmagfiles,1))) ' '...
           sprintf('%s_fft_magnitude_tmp.mnc',deblank(myfiles(loop,:))) ' '...
           sprintf('%s_fft_magnitude.mnc',deblank(myfiles(loop,:)))]);
   if ~isempty(alltmapfiles),
      outstring2=[outstring2 ' ' sprintf('%s_dspear_tmap_tmp.mnc',deblank(myfiles(loop,:)))];
      system(outstring2);
      system(['mincmath -mult ' sprintf('%.4f',sqrt(size(allmagfiles,1))) ' '...
              sprintf('%s_dspear_tmap_tmp.mnc',deblank(myfiles(loop,:))) ' '...
              sprintf('%s_dspear_tmap.mnc',deblank(myfiles(loop,:)))]);
   end;
   for n=1:size(allphasefiles,1),
      delete(deblank(allphasefiles(n,:)));
      delete(deblank(allmagfiles(n,:)));
      delete(deblank(alltmapfiles(n,:)));      
   end;
end;
