function GetDerivatives2(file,blurring_kernel,add,outputbase);
%function GetDerivatives2(file,blurring_kernel,add);
% creates derivaties (using a 3D 'blurring_kernel' (default = 3)) 
% from input file, whilst getting around edge effects
% by 'mincblur'-ing twice where edge effects have different polarities. 
% non-linear averaging then removes those effects. 
% takes thresh from phase file (-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 GetDerivatives2;
   return;
end;
if nargin < 2,
   blurring_kernel = 3;
end;
if nargin < 3,
   add = 0;
end;
if nargin < 4,
   outputbase=[];
end;

if sum(size(blurring_kernel))==2, % 1 by 1 matrix
   blurring_kernel=sprintf('%d %d %d',blurring_kernel,blurring_kernel,blurring_kernel);
else, % assume 1 by 3 matrix
   [m,n] = size(blurring_kernel);
   if m == 1,       % If a row, turn into column vector
      blurring_kernel = blurring_kernel(:);
   end;
   blurring_kernel=sprintf('%d %d %d',blurring_kernel(1),blurring_kernel(2),blurring_kernel(3));
end;

magthresh = -3.5;

%--- first create derivatives N and P

[path,name,base,baseid]=getbase(deblank(file));

for loop=1:2,
   if loop==1,
      MyConst=sprintf('7');%103.5
      MyAdd  ='_P';
   elseif loop==2,
      MyConst=sprintf('0');%-96.5
      MyAdd  ='_N';
   end;
   mybase=[base MyAdd];
   mybasetmp=[base 'tmp' MyAdd];
  
 if MyConst~=0, 
   % making mask for values below magthresh (-3,5)
   string=['mincmath -segment -clob -const2 ' ...
            sprintf('%0.2f',magthresh-.1) ' ' sprintf('%0.2f',magthresh+.1) ...
            ' ' deblank(file) ' magthresh.mnc'];
   %disp(string); 
   system(string);
   
   % setting mask values (0-1) to  (0-MyConst)
   string=['mincmath -mult -clob  -const ' MyConst ... 
           ' magthresh.mnc magthreshBig.mnc '];
   %disp(string); 
   system(string);

   % adding mask value to values below magthresh
   string=['mincmath -add -clob ' deblank(file) ...
           ' magthreshBig.mnc phase_magthreshBig.mnc  '];
   %disp(string); 
   system(string);

   % blurring phase to tmp file 
   string=['mincblur -gaussian -partial -gradient -clobber -3dfwhm ' blurring_kernel ' '];
   string=[string '  phase_magthreshBig.mnc ' mybasetmp ];
   %disp(string); 
   system(string);

 elseif MyConst==0,
   % blurring phase to tmp file 
   string=['mincblur -gaussian -partial -gradient -clobber -3dfwhm ' ...
            blurring_kernel ' '];
   string=[string '  phase_magthreshBig.mnc ' mybasetmp ];
   %disp(string); 
   system(string);
 end;
  
   % segmenting mask for all values except belong thresh
   string=['mincmath -nsegment -clob -const2 ' ...
            sprintf('%0.2f',magthresh-.1) ' ' sprintf('%0.2f',magthresh+.1) ...
            ' ' deblank(file) ' magthreshZero.mnc'];
   %disp(string); 
   system(string);
   
   % zeroing of background
   string=['mincmath -mult -clob ' ...
           ' magthreshZero.mnc ' mybasetmp '_dx.mnc ' mybase '_dx.mnc '];
   system(string);
   string=['mincmath -mult -clob ' ...
           ' magthreshZero.mnc ' mybasetmp '_dy.mnc ' mybase '_dy.mnc '];
   system(string);
   string=['mincmath -mult -clob ' ...
           ' magthreshZero.mnc ' mybasetmp '_dz.mnc ' mybase '_dz.mnc '];
   system(string);
   string=['mincmath -mult -clob ' ...
           ' magthreshZero.mnc ' mybasetmp '_dxyz.mnc ' mybase '_dxyz.mnc '];
   system(string);
   string=['mincmath -mult -clob ' ...
           ' magthreshZero.mnc ' mybasetmp '_blur.mnc ' mybase '_blur.mnc '];
   system(string);
   system(['rm -f ' mybasetmp '*']);
end;
system(['rm -f magthresh.mnc magthreshBig.mnc phase_magthreshBig.mnc']);
system(['rm -f magthreshZero.mnc phase_magthreshBig2.mnc']);

% --- now create real derivatives
if isempty(outputbase),
   outputbase=base;
end;

for s=1:3,
 if s==1,
   mybase1=[base '_N_dx.mnc'];
   mybase2=[base '_P_dx.mnc'];out=[outputbase '_dx.mnc'];
 elseif s==2,
   mybase1=[base '_N_dy.mnc'];
   mybase2=[base '_P_dy.mnc'];out=[outputbase '_dy.mnc'];
 elseif s==3,
   mybase1=[base '_N_dz.mnc'];
   mybase2=[base '_P_dz.mnc'];out=[outputbase '_dz.mnc'];
 end;
   RetAvg2([mybase1;mybase2], out, 0, 0, 0, add);
end;

% cleaning up
delete([base '_N_*.mnc']);
delete([base '_P_*.mnc']);
