function N_features=cp_sar(x,FeatureFN,NonlinId,SmoothId,NormId,FeatDec)
% CP_SAR  Comparative texture measure study: SAR features
%	N_features=cp_sar(x,FeatureFN,NonlinId,SmoothId,NormId,...
%	  Decimation)

WindowSize=25;
ImageFN=tempnam;
TmpFeatFN=tempnam('v');
TmpFile=tempnam;
[M,N]=size(x);

% Write texture image
hipswrite(x,ImageFN,'byte');
  
% Feature extraction
N_features=0;
for order=1:3
  fprintf(1,':');
  eval(sprintf('!sar-v2 %s %d %d < %s',TmpFeatFN,order,WindowSize,ImageFN));
  % Convert all feature files to HIPS format
  for f=1:5
    fprintf(1,'.');
    % eval(sprintf('!raw2hips -s %d %d < %s_%d | decimate -N %d > %s_%03d',...
    % M,N,TmpFeatFN,f,FeatDec,FeatureFN,N_features));
    
    % Replace Not a Number's with NaN
    eval(sprintf('!sed "s/\?.000000/NaN/g" %s_%d > %s',TmpFeatFN,f,TmpFile));
    eval(sprintf('!mv %s %s_%d',TmpFile,TmpFeatFN,f));
    % Replace infinites with Inf
    eval(sprintf('!sed "s/++.000000/Inf/g" %s_%d > %s',TmpFeatFN,f,TmpFile));
    eval(sprintf('!mv %s %s_%d',TmpFile,TmpFeatFN,f));
    % Replace negative infinites with -Inf
    eval(sprintf('!sed "s/---.000000/-Inf/g" %s_%d > %s',TmpFeatFN,f,TmpFile));
    eval(sprintf('!mv %s %s_%d',TmpFile,TmpFeatFN,f));
    % Load the datafile
    N_features=N_features+1;
    eval(sprintf('load %s_%d',TmpFeatFN,f)); 
    % Smoothing
    sigma_p=3;
    f_0=1./(2*sqrt(2)*sigma_p);
    v=cp_energy(v,NonlinId,SmoothId,NormId,f_0);
    % Write feature file
    catframe(v,FeatureFN,[],'Interm',FeatDec);
  end
end
fprintf(1,'\n');

% Clean
eval(sprintf('!rm -f %s %s_?',ImageFN,TmpFeatFN));
