#!/bin/csh -f
set fmrianalver = `cat $FMRI_ANALYSIS_DIR/docs/version`;
set PCTSIGCH_VER = '$Id';

set FirstSlice =  0;
set NSlices    = 16;
set hdrstem   = ();
set meanimgstem  = ();
set outstem = ();
set ext        = "bshort";
set monly = 0;
set mfile = ();

if($#argv == 0) then
  echo "USAGE:  pctsigch -m meanimgstem -h hdrstem -o oustem"
  echo "  OPTIONS:"
  echo "    -firstslice      first slice to process       0"
  echo "    -nslices         number of slices to process 16"
  echo "    -monly           mfile"
  echo "    -umask           newumask"
  echo "    -verbose         print out a lot of information"
  echo ""
  echo "$PCTSIGCH_VER"
  echo "$fmrianalver"
  echo ""

  exit 1;
endif

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

echo "$PCTSIGCH_VER"
echo "$fmrianalver"

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set OutDir = `dirname $outstem`;
if( ! -d $OutDir) then
  echo "INFO: output directory $OutDir does not exist, creating."
  mkdir -p $OutDir;
endif
set OutBaseStem = `basename $outstem`;

if ($monly) then
  set TARGET = "tee $mfile"
  rm -f $mfile;
else
  set TARGET = "$MATLAB -display iconic"
endif  

set datfile = $hdrstem.dat;

#---------------------------------------------------------------#
$TARGET <<EOF

  if( ~ $monly ) QuitOnError = 1; end

  LastSlice = $FirstSlice + $NSlices  - 1 ;

  datfile = sprintf('%s.dat','$hdrstem');
  hd = fmri_lddat2(datfile);

  for slice = $FirstSlice : LastSlice
    fprintf('Slice = %2d\n',slice);

    hcovfile = sprintf('%s_hcov_%03d.bfloat','$hdrstem',slice);
    Ch = fmri_ldbfile(hcovfile);

    hdrfile = sprintf('%s_%03d.bfloat','$hdrstem',slice);
    hdr = fmri_ldbfile(hdrfile);
    [hAvg eVar] = fmri_sa2sxa(hdr,hd.Nh);

    meanimgfile = sprintf('%s_%03d.bfloat','$meanimgstem',slice);
    meanimg = fmri_ldbfile(meanimgfile);
    ind = find(meanimg==0);
    meanimg(ind) = 10^10;

    meanimg2 = repmat(meanimg, [1 1 size(hAvg,3)]);
    hAvgpct = 100*hAvg./meanimg2;

    meanimg2 = repmat(meanimg, [1 1 size(eVar,3)]);
    eVarpct = 100*eVar./meanimg2;

    [hdrpct] = fmri_sxa2sa(eVarpct,Ch,hAvgpct,hd);

    pctfile = sprintf('%s_%03d.bfloat','$outstem',slice);
    fmri_svbfile(hdrpct,pctfile);

    hcovfile = sprintf('%s_hcov_%03d.bfloat','$outstem',slice);
    fmri_svbfile(Ch,hcovfile);

    dof1file = sprintf('%s_%03d.dof','$hdrstem',slice);
    dof2file = sprintf('%s_%03d.dof','$outstem',slice);
    cmd = sprintf('cp %s %s',dof1file,dof2file);
    unix(cmd);
  end

  datfile = sprintf('%s.dat','$outstem');
  fmri_svdat2(datfile,hd);

  if( ~ $monly ) quit; end
  return;

EOF
#---------------------------------------------------------------#

exit 0;

############--------------##################
parse_args:

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

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

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

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

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

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

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

    case "-umask":
      if( $#argv == 0) goto arg1err;
      echo INFO: setting umask to $argv[1].
      umask $argv[1]; shift;
      breaksw

    case "-debug":
      set verbose = 1;
      breaksw

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

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

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

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

############--------------##################
check_params:
  if($#hdrstem != 1) then
     echo "ERROR: must specify an hdr stem";
     exit 1
  endif

  if($#meanimgstem != 1) then
     echo "ERROR: must specify a meanimg stem";
     exit 1
  endif

  if($#outstem != 1) then
     echo "ERROR: must specify an output stem";
     exit 1
  endif

goto check_params_return;
############--------------##################


############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################
