#!/bin/csh -f
############################################################

set FPR_VERSION = '$Id: fpr,v 1.4 2000/03/02 01:38:37 greve Exp greve $';


set pfile  = ();
set outfile = ();
set pformat = "log10";
set monly   = 0;
set mfile   = ();
set sigroi     = ();
set MaxAlpha   = 1;
set offset = 0;

if($#argv < 2) then
  echo "USAGE: fpr -i pfile [options]"
  echo "  options:"
  echo "    -o outfile"
  echo "    -f pformat ($pformat)"
  echo "    -sigroi row1 col1 row2 col2"
  echo "    -maxalpha maxalpha"
  echo "    -offset n : 0-based offset"
  echo "    -monly mfile"
  exit 1;
endif


goto parse_args;
parse_args_return:

if($#pfile == 0) then
  echo "ERROR: Must specify an input pfile"
  exit 1;
endif

if(! -r $pfile ) then
  echo "ERROR: Cannot find $pfile"
  exit 1;
endif

set MATLAB = `getmatlab`;
if($status) exit 1;

if(! $monly) then
  set MLF = /tmp/"fpr_"$$"_tmp.m"
  set Wait = 1;
  set QOE  = 1;
else 
  set MLF = $mfile
  set Wait = 0;
  set QOE  = 0;
endif
rm -f $MLF

echo "pfile = '$pfile';" >> $MLF
echo "QuitOnError = $QOE;" >> $MLF
echo "Wait = $Wait;"          >> $MLF
echo "outfile = '$outfile';"  >> $MLF
echo "pformat = '$pformat';"  >> $MLF
echo "MinAlphaFit = $MaxAlpha;"     >> $MLF
echo "ROI = [$sigroi];"     >> $MLF
echo "offset = $offset;"  >> $MLF

set m = "#M"
set m = $m"#"
cat $0 | grep $m | sed s/$m// >> $MLF

if(! $monly) then
  cat $MLF | $MATLAB
  rm -f $MLF
endif

exit 0;

############--------------##################
parse_args:
set cmdline = "$argv";
while( $#argv != 0 )

  set flag = $argv[1]; shift;
  
  switch($flag)

    case "-i":
      if( $#argv == 0) goto arg1err;
      if( $#pfile != 0 ) then
        echo ERROR: only one input pfile allowed.
        exit 1;
      endif
      set pfile = $argv[1]; shift;
      breaksw

    case "-o":
      if( $#argv == 0) goto arg1err;
      if( $#outfile != 0 ) then
        echo ERROR: only one out file allowed.
        exit 1
      endif
      set outfile = $argv[1]; shift;
      breaksw

    case "-sigroi":
    case "-roi":
      if( $#argv < 4) then
        echo "ERROR: $flag flag requires 4 arguments"
        exit -1;
      endif
      set sigroi = ($argv[1] $argv[2] $argv[3] $argv[4]);
      shift;shift;shift;shift;
      breaksw

    case "-f":
      if( $#argv == 0) goto arg1err;
      set pformat = $argv[1]; shift;
      breaksw

    case "-offset":
      if( $#argv == 0) goto arg1err;
      set offset = $argv[1]; shift;
      breaksw

    case "-maxalpha":
    case "-ma":
      if( $#argv == 0) goto arg1err;
      set MaxAlpha = $argv[1]; shift;
      breaksw

    case "-monly":
      if( $#argv == 0) goto arg1err;
      set monly = 1; 
      set mfile = $argv[1]; shift;    
      set QuitOnError = 0;
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      exit 1
      breaksw
  endsw

end
goto parse_args_return;
############--------------##################

#M#
#M# planeno = offset + 1;
#M# tmp = fmri_ldbfile(pfile);
#M# logp = tmp(:,:,planeno);
#M# fprintf('pformat = %s\n',pformat);
#M# if(strcmp(pformat,'ln'))
#M#   fprintf('INFO: converting to exp(-p)\n');
#M#   p = sign(logp) .* exp(-abs(logp));
#M# elseif(strcmp(pformat,'log10'))
#M#   fprintf('INFO: converting to 10^(-p)\n');
#M#   p = sign(logp) .* 10.^(-abs(logp));
#M# else
#M#   fprintf('INFO: not converting \n');
#M#   p = logp;
#M# end
#M# p2 = abs(p);
#M#
#M# if(~isempty(ROI))
#M#   SignalMask = zeros(size(p2));
#M#   Rroi = [ROI(1):ROI(3)];
#M#   Croi = [ROI(2):ROI(4)];
#M#   SignalMask(Rroi,Croi,:) = 1;
#M#   SignalMask = reshape1d(SignalMask);
#M#   iSignal   = find(SignalMask==1);
#M#   iNoSignal = find(SignalMask==0);
#M#   p2 = p2(iNoSignal);
#M#   % clear SignalMask, iSignal, iNoSignal;
#M# end
#M#
#M# inz = find(p2 > 0);
#M# if(isempty(inz))
#M#   msg = 'No non-zero voxels';
#M#   qoe(msg); error(msg);
#M# end
#M#
#M# nNZ = length(inz);
#M# fprintf('N Non-Zero = %d\n',nNZ);
#M#
#M# [FPR alpha] = ComputeFPR(reshape1d(p2(inz)));
#M# %[pdf alpha nx FPR ] = ComputePDF(reshape1d(p2(inz)));
#M# indAlphaFit = find(alpha < MinAlphaFit);
#M# [slope offset] = linfit(FPR(indAlphaFit),alpha(indAlphaFit));
#M#
#M# fprintf(1,'\nslope = %g, offset = %g, MinAlphaFit = %g\n\n',slope,offset,MinAlphaFit);
#M# if(~isempty(outfile))
#M#   fp = fopen(outfile,'w');
#M#   fprintf(fp,'# slope = %g, offset = %g, MinAlphaFit = %g\n',...
#M#           slope,offset,MinAlphaFit);
#M#   fprintf(fp,'%g %g %g\n', [alpha; FPR; alpha*slope+offset]);
#M#   fclose(fp);
#M# end
#M#
#M# h1 = figure;
#M# %plot(alpha,alpha,'g-+',alpha,FPR,'r',alpha,alpha*slope+offset,'b');
#M# plot(alpha,alpha,'g-+',alpha,FPR,'r');
#M# axis([0 MinAlphaFit 0 MinAlphaFit]);
#M# title(pfile)
#M# if(Wait) uiwait(h1); end
#M# fprintf(0,'\n');
#M# if(QuitOnError) quit; end

