function [usedfilename]=RetPhaseAnalysis(file,par,transfile,ResFile,fft_thresh, ...
                          Ncycles,Nblanks,outbase,int_thresh,meth,tmap);
% function [usedfilename]RetPhaseAnalysis(file,par,transfile,ResFile,fft_thresh, ...
%                           Ncycles,Nblanks,outbase,int_thresh,meth,tmap);
%  do resampling to tal-space and phase analysis 
%  Input:
%     file      : files to be analysed, if not specified all files 
%                 in the directory will be analysed
%                 more files willbe averaged before analysis
%     par       : 1 (retinotopy.par), 2 (wedge.par [default])
%                 anything else -> use input from Ncycles & Nblanks
%     transfile : xfm file to talairach space 
%     ResFile   : file which dimensions will be used for final results
%     fft_thresh: fft threshold above which data will be analysed
%     Ncycles   : number of stimulus cycles [default = 10]
%     Nblanks   : number of initial time frames to be ignored [default = [1:8]]
%     outputbase: output
%     int_thresh: intensity threshold to use
%     meth      : order to follow: resample - phase map ([] or 0) [default]
%                                  phase map - resample (else)
%     tmap      : create tmap n0=0 or [], [default], yes=else.

% ---------------------------------
% 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 < 11,
   tmap = []
end;
if nargin < 10,
   meth = [];
end;
if meth == 0,
   meth = [];
end;
if nargin < 9,
   int_thresh=128;
end;
if nargin < 8,
   outbase=[];
end;
if nargin < 7,
   Nblanks = [1:8];
end;
if nargin < 6,
   Ncycles = 10;
end;
if nargin < 5,
   fft_thresh = 0;
end;
if nargin < 4,
    ResFile='/exports/analysis/3Dret/serge/Anat/tal_serge_global_vol_nuc_toRet.mnc.gz';
end;
if nargin < 3,
   transfile=[];
end;
if nargin < 2,
   par = 2;
end;
if nargin < 1,
   f=getfiles([],'*.mnc*');
   [p file]=getbase(f);
end;

if isempty(tmap),
   tmap=0;
end;

% checks
if ~isempty(par),
   if par == 1,
      Nblanks = [1:8];
      Ncycles = 3;
   elseif par == 2,
      Nblanks = [1:8];
      Ncycles = 10;
   end;
else,
   if (isempty(Nblanks) | isempty(Ncycles)),
      help(mfilename);
      return;
   end;
end;

global RET_TMP_SUBDIR;
if isempty(RET_TMP_SUBDIR),
   tmpfile ='tmpfile.mnc';
   tmpfile2='tmpfile2.mnc';
   tmpfile3='tmpfile3.mnc'; 
else,
   tmpfile =[RET_TMP_SUBDIR filesep 'tmpfile.mnc'];
   tmpfile2=[RET_TMP_SUBDIR filesep 'tmpfile2.mnc'];
   tmpfile3=[RET_TMP_SUBDIR filesep 'tmpfile3.mnc'];
end;

if isempty(meth),
   for n=1:size(file,1),
      disp(sprintf('[%s]: Resampling file %d of %d',mfilename,n,size(file,1)));
      if n==1,
         if ~isempty(transfile),
            if size(transfile,1) == 1,
               system(['mincresample -clobber ' file(n,:) ' ' ...
                       tmpfile ' -like ' ResFile ' -transf ' transfile]);
            else,
               system(['mincresample -clobber ' file(n,:) ' ' ...
                       tmpfile ' -like ' ResFile ' -transf ' ...
                       transfile(n,:)]);
            end;
         else,
            system(['mincresample -clobber ' file(n,:) ' ' ...
                    tmpfile ' -like ' ResFile]);
         end;
      else,
         if ~isempty(transfile),
            if size(transfile,1) == 1,
               system(['mincresample -clobber ' file(n,:) ' ' ...
                       tmpfile2 ' -like ' ResFile ' -transf ' transfile]);
            else,
               system(['mincresample -clobber ' file(n,:) ' ' ...
                       tmpfile2 ' -like ' ResFile ' -transf ' transfile(n,:)]);
            end;
         else,
            system(['mincresample -clobber ' file(n,:) ' ' ...
                    tmpfile2 ' -like ' ResFile]);
         end;
         system(['mincmath -add -clobber ' tmpfile ' ' tmpfile2 ...
                 ' ' tmpfile3]);
         delete(tmpfile);
         copyfile(tmpfile3,tmpfile);
         delete(tmpfile2);
         delete(tmpfile3);
      end;
%       if n==size(file,1) & n~=1,
%          system(['mincmath -div -const ' sprintf('%d',size(file,1)) ...
%                  ' ' tmpfile ' ' tmpfile3]);
%          delete(tmpfile);
%          copyfile(tmpfile3,tmpfile);
%          delete(tmpfile2);
%          delete(tmpfile3);
%       end;      
   end;
else,
   tmpfile=[];
end;

%--- actual phase fit
if isempty(outbase),
   [path,name,outbase,baseid]=getbase(deblank(file(n,:)));
end;
if ~isempty(RET_TMP_SUBDIR),
   outbase=[RET_TMP_SUBDIR outbase];
end;

usedfilename=[];
if isempty(tmpfile),
   % skipped everything -> now do phase analysis first
   for n=1:size(file,1),
      outbase2=[outbase sprintf('_%d',n)];
      phase_fit(deblank(file(n,:)), ...
                     Ncycles,int_thresh,fft_thresh,outbase2,Nblanks,tmap);
      usedfilename=strvcat(usedfilename,deblank(file(n,:)));
   end;
   % and resample now
   fm_resample2(transfile,ResFile,outbase);
else,
   % phase analysis (on already resampled data)
   phase_fit(tmpfile,Ncycles,int_thresh,fft_thresh,outbase,Nblanks,tmap);
   usedfilename=tmpfile;
end;

if ~isempty(tmpfile),
   delete(tmpfile);
end;









