function [Qs_file]=GetVisualQuadrants2(PhaseFiles,QborderShift,OutFileBase);
% function [Qs_file]=GetVisualQuadrants2(PhaseFiles,QborderShift,OutFileBase);
%
%  Input: 
%         PhaseFiles   : Files to be analysed
%         QborderShift : pos. shift from [-pi -pi/2 0 pi/2 pi] borders
%                        default=[];
%         OutFileBase  : output file base _Qs.mnc will be added
%                        default=[];
%
%  Output:
%         file where phases are seperated into four groups
%         for the four hemifields
%

% ---------------------------------
% 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 GetVisualQuadrants2;
   return;
end;
if nargin < 2,
   QborderShift=-0.361;
end;
if nargin < 3,
   OutFileBase =[];
else,
   if size(PhaseFiles,1) ~= size(PhaseFiles,1),
      disp('Error: not same number of input and output files');
      disp(' -> forcing OutFileBase=[]');
      OutFileBase=[];
   end;
end;

Qborders=[     -pi;   -pi/2;   
             -pi/2;       0;
                 0;    pi/2;
              pi/2;      pi];

% if ~isempty(QborderShift),
%    PhaseShift(file,outfile_base,number_out,phaseshift);
%    Qborders=Qborders + QborderShift;
%    lValue=find(Qborders > pi);
%    if ~isempty(lValue),
%       if length(lValue) > pi/2,
%          disp(sprintf('Error too large QborderShift (%f) value',QborderShift));
%          return;
%       end;
%       resValue = Qborders(lValue)-pi;
%       Qborders(lValue)=pi;
%       Qborders(9:10)=[-pi; -pi+resValue];
%    end;
% end;

for n=1:size(PhaseFiles,1),
   [path,name,base,baseid]=getbase(PhaseFiles(n,:));
   if ~isempty(QborderShift),
      PhaseShift(PhaseFiles,'tmp',1,QborderShift);
      myPhaseFile='tmp_PhS1.mnc';
   else,
      myPhaseFile=deblank(PhaseFiles(n,:));
   end;
   
   h=openimage(myPhaseFile);
   if isempty(OutFileBase),
      [path,name,base,baseid]=getmincbase(deblank(PhaseFiles(n,:)));
   else,
      base=OutFileBase(n,:);
   end;
   
   sizes=getimageinfo(h,'ImageSize');
   NumSlices=getimageinfo(h,'NumSlices');

   base=[base '_Qs.mnc'];
   h_out=newimage(base,[0 NumSlices],PhaseFiles(n,:));
   
   Inslice=getimages(h,1:NumSlices);
   Qs=zeros(size(Inslice));
   
   for q=1:2:7,
      Qs(find(Inslice > Qborders(q) & Inslice < Qborders(q+1))) = ...
         ceil(q/2);
   end;
   if length(Qborders)==10,
      Qs(find(Inslice > Qborders(9) & Inslice < Qborders(10))) = 4;
   end;
   
   putimages(h_out,Qs,1:NumSlices);
   closeimage(h_out);
   closeimage(h);
   if ~isempty(QborderShift),
      delete('tmp_PhS1.mnc');
   end;
end;



