function PhaseShift(file,outfile_base,number_out,phaseshift,phaserange,exept);
%function PhaseShift(file,outfile_base,number_out,phaseshift,phaserange,exept);
%
% -3.14 : 0 : 3.14 : 0 --> 0 : -3.14 : 0 : 3.14 
% outfile_base can be ommitted ([])
% number out: either 1 (-pi->0) or 3 (-pi->{-pi/2 0 pi/2})
% phaseshift: default pi, but other values can be given as well
% phaserange: default [-pi pi], but other ranges can be given as well
% exept     : values not to perform shift on, default=-3.5

% ---------------------------------
% 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 < 2,
   outfile_base=[];
end;
if isempty(outfile_base),
   [path,name,outfile_base,baseid]=getbase(deblank(file));
end;
if nargin < 3,
   number_out=1;
end;
if nargin < 4,
   phaseshift=[];
end;
if isempty(phaseshift),
   phaseshift=pi;
end;
if nargin < 5,
   phaserange=[];
end;
if isempty(phaserange),
   phaserange=[-pi pi];
end;
if nargin < 6,
   exept=-3.5;
end;



tic;

% open the minc files
h = openimage(deblank(file));

sizes=getimageinfo(h,'ImageSize');
ImageHeight=sizes(1);
ImageWidth=sizes(2);
len = sizes(1).*sizes(2);
size_ = sizes(1);
ns=getimageinfo(h,'NumSlices');

SliceData    = getimages(h,1:ns);

for n=1:number_out,
   if n==1,
      shift=phaseshift;
   elseif n==2,
      shift=phaseshift/2;
   elseif n==3,
      shift=phaseshift+(phaseshift/2);
   end;

   outfile = [ outfile_base '_PhS' sprintf('%d',n) '.mnc'];
   new_handle = newimage(outfile,[0 ns],file);

   % do phase shift
   SliceDataPhS = SliceData+shift;
   SliceDataPhS(find(SliceDataPhS > phaserange(2)))= ...
     SliceDataPhS(find(SliceDataPhS > phaserange(2)))- ...
       sum(abs(phaserange));
   SliceDataPhS(find(SliceDataPhS < phaserange(1)))= ...
     SliceDataPhS(find(SliceDataPhS < phaserange(1)))+ ...
       sum(abs(phaserange));
   SliceDataPhS(find(SliceData==exept))=exept;
   
   % write to file
   putimages(new_handle,SliceDataPhS,1:ns);
   closeimage(new_handle);
end;

closeimage(h);
disp(sprintf('[%s]: elapsed time = %d seconds.',mfilename,round(toc)));



