function GetVisualMeridians(OutFileBase,PhaseFiles);
% function GetVisualMeridians(OutFileBase,PhaseFiles);
%
%  


% ---------------------------------
% 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 GetVisualQuadrants;
   return;
end;
if nargin < 2,
   unix(['ls *.mnc* > tmp.txt']);
   [PhaseFiles,Nlines]=rdlines('tmp.txt');
   unix(['rm tmp.txt']);
else,
   Nlines=size(PhaseFiles,1);
end;

Mborders(:,1)=[ -7*pi/12; -5*pi/12;  5*pi/12;  7*pi/12;     NaN;     NaN];
Mborders(:,2)=[      -pi;-11*pi/12;   -pi/12;    pi/12;11*pi/12;      pi];

for s=1:2,
%--- get individual borders
   if s==1,
      Ns=2;
   else,
      Ns=3;
   end;
   if Nlines > 1,
      stringM1=['mincaverage '];
      stringM2=['mincaverage '];
      if s==2,stringM3=['mincaverage '];end;
   end;
   for n=1:Nlines,
      disp(sprintf('Processing file # %d of %d',n,Nlines));
      [path,name,base,baseid]=getmincbase(PhaseFiles(n,:));
      for f=1:2:(Ns*2-1),
         unix(['mincmath -seg -const2 ' ...
         sprintf(' %.4f %.4f ',Mborders(f,s),Mborders(f+1,s)) ...
         PhaseFiles(n,:) ' ' ...
         base '_M' sprintf('%d',ceil(f/2)) '.mnc ']);
      end;
      if Nlines > 1,
         stringM1=[stringM1 ' ' base '_M1.mnc '];
         stringM2=[stringM2 ' ' base '_M2.mnc '];
         if s==2,stringM3=[stringM3 ' ' base '_M3.mnc '];end;
      end;
      if (n == Nlines & Nlines > 1),
         stringM1=[stringM1 ' ' OutFileBase '_avgM1.mnc ']; unix(stringM1);
         stringM2=[stringM2 ' ' OutFileBase '_avgM2.mnc ']; unix(stringM2);
         if s==2,stringM3=[stringM3 ' ' OutFileBase '_avgM3.mnc ']; unix(stringM3);end;
         % rough cleanup
         if s==1,
            unix(['rm -f *_M1.mnc *_M2.mnc ']);
         else,
            unix(['rm -f *_M1.mnc *_M2.mnc *_M3.mnc ']);
         end;
      elseif (n == Nlines & Nlines == 1),
         for f=1:(Ns),
            unix(['mv ' base '_M' sprintf('%d',f) '.mnc ' ...
               OutFileBase '_avgM' sprintf('%d',f) '.mnc ']);
         end;
      end;       
   end;

   %--- put all Ms in one file
   disp(sprintf('Putting Ms in one file'));
   if s==1,
      unix(['mincaverage ' ...
            OutFileBase '_avgM1.mnc ' ...
            OutFileBase '_avgM2.mnc ' ...
            OutFileBase '_avgMs1.mnc ']);
      unix(['rm -f ' ...
            OutFileBase '_avgM1.mnc ' ...
            OutFileBase '_avgM2.mnc ']);
   else,
      unix(['mincaverage ' ...
            OutFileBase '_avgM1.mnc ' ...
            OutFileBase '_avgM2.mnc ' ...
            OutFileBase '_avgM3.mnc ' ...
            OutFileBase '_avgMs2.mnc ']);
      unix(['rm -f ' ...
            OutFileBase '_avgM1.mnc ' ...
            OutFileBase '_avgM2.mnc ' ...
            OutFileBase '_avgM3.mnc ']);
   end;
end; % for s
%--- putting meridians together
unix(['mincmath -mult -const -1 ' ...
            OutFileBase '_avgMs1.mnc ' ...
            OutFileBase '_avgMs1c.mnc ']);
unix(['mincmath -add ' ...
      OutFileBase '_avgMs1c.mnc ' ...
      OutFileBase '_avgMs2.mnc ' ...
      OutFileBase '_avgMs.mnc ']);
unix(['rm -f ' ...
            OutFileBase '_avgMs1.mnc ' ...
            OutFileBase '_avgMs1c.mnc ' ...
            OutFileBase '_avgMs2.mnc ']);
