#! /bin/csh -f

#
# stxpredict - computes a prediction of the statistical map
# given the averages from an already analyzed data set as well
# as a list of paradigm files.
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:19 $
#    $Revision: 1.2 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA). 
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#


set VERSION    = '$Id: stxgpredict,v 1.2 2007/01/09 22:41:19 nicks Exp $'

set hstem = ();
set parlist = ();
set ntrs = ();
set contrast = ();
set tstem = ();
set tsigstem = ();
set tminsigstem = ();
set fstem = ();
set fsigstem = ();
set MLF = ();
set monly = 0;
set firstslice = -1;
set nslices    = -1;

if($#argv == 0) then
  goto usage_exit;
  exit 1;
endif

set n = `echo $argv | grep version | wc -l` 
if($n != 0) then
  echo $VERSION
  exit 0;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set datfile = $hstem.dat
if(! -e $datfile) then
  echo "ERROR: $datfile not found"
  exit 1;
endif

if(! -e $contrast && ! -e $contrast.mat ) then
  echo "ERROR: $contrast not found"
  exit 1;
endif

foreach stem ($tstem $tsigstem $tminsigstem $fstem $fsigstem)
  set outdir = `dirname $stem`;
  mkdir -p $outdir
end

if($#MLF == 0) set MLF = stxgpredict-$$.m
rm -f $MLF

set okfile = stxgpredict-$$.ok
rm -f $okfile

#--------------------------------------------------------#
tee $MLF <<EOF
  hstem       = '$hstem';
  firstslice  = $firstslice;
  nslices     = $nslices;
  parlist     = splitstring('$parlist');
  ntrs        = $ntrs;
  contrast    = '$contrast';
  tstem       = '$tstem';
  tsigstem    = '$tsigstem';
  tminsigstem = '$tminsigstem';
  fstem       = '$fstem';
  fsigstem    = '$fsigstem';
  okfile      = '$okfile';
  monly       = $monly;

  if(~monly) QuitOnError = 1; end

  stxgpredict;

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

if(! $monly) then
  cat $MLF | matlab -display iconic
  rm -f $MLF
endif

echo "Done"

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 hstem = $argv[1]; shift;
      breaksw

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

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

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

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

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

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

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

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

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

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

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

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

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

    case "-echo":
      set echo = 1;
      breaksw

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

    case "--version":
    case "-version":
      echo $VERSION
      exit 0;
      breaksw

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

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

end

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

############--------------##################
check_params:

  if($#hstem == 0) then
    echo "ERROR: no hstem specified"
    exit 1
  endif

  if($#parlist == 0) then
    echo "ERROR: no paradigm files specified"
    exit 1
  endif

  if($#ntrs == 0) then
    echo "ERROR: number of trs not specified"
    exit 1
  endif

  if($#contrast == 0) then
    echo "ERROR: no contrast file specified"
    exit 1
  endif

  if($#tstem == 0 && $#tsigstem == 0 &&  $#tminsigstem == 0 && \
      $#fstem == 0 &&  $#fsigstem == 0) then
    echo "ERROR: no output specified"
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: stxgpredict"
  echo ""
  echo "   -h hstem (result of selxavg )"
  echo "   -p parfile1 ... (one for each run)"
  echo "   -ntrs number of trs per run"
  echo "   -contrast contrast.mat"
  echo "   -t       tstem"
  echo "   -tsig    tsigstem"
  echo "   -tsigmin tsigminstem"
  echo "   -f       fstem"
  echo "   -fsig    fsigstem"
  echo ""
  echo "   -monly   matlab file"

exit 1;
