#! /bin/csh -f

#
# par2kspar -- convert a paradigm file to something suitable for KSS_grinder
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:18 $
#    $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 inputargs = ($argv);
set fmrianalver = `cat $FMRI_ANALYSIS_DIR/docs/version`;
set VERSION = '$Id: par2kspar,v 1.2 2007/01/09 22:41:18 nicks Exp $'

## If there are no arguments, just print useage and exit ##
if ( $#argv == 0  )  goto usage_exit;

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

set PWDCMD = `getpwdcmd`;

# Set Default Values #
set parfile = ();
set ksparfile = ();
set poscondlist = ();
set negcondlist = ();
set delay = 0;

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set outdir = `dirname $ksparfile`;
mkdir -p $outdir;
pushd $outdir > /dev/null
set outdir = `$PWDCMD`;
popd > /dev/null

# check that all the conditions are in the paradigm file #
foreach cond ($poscondlist $negcondlist)
  set n = `cat $parfile | awk '{print $2}' | grep $cond | wc -l`;
  if($n == 0) then
    echo "ERROR: cannot find condition $cond in $parfile"
    exit 1;
  endif
end

#--------------------------------------------------------#
matlab -display iconic <<EOF
  parfile = '$parfile';
  ksparfile = '$ksparfile';
  poscondlist = [$poscondlist];
  negcondlist = [$negcondlist];
  delay = round($delay);

  par = fmri_ldpar(parfile);
  par = par(:,2);
  ntrs = size(par,1);

  if(delay > 0)
    tmp = par;
    par(1:delay) = 0;
    par(delay+1:ntrs) = tmp(1:ntrs-delay);
  end

  kspar = zeros(ntrs,1);

  for poscond = poscondlist
    ind = find(par==poscond);
    kspar(ind) = 1;
  end

  for negcond = negcondlist
    ind = find(par==negcond);
    kspar(ind) = -1;
  end

  fid = fopen(ksparfile,'w');
  if(fid == -1) 
    fprintf('ERROR: cannot open %s\n',ksparfile);
    quit;
  end

  fprintf(fid,'%d\n',kspar);
  fclose(fid);
  quit;
EOF
#--------------------------------------------------------#

echo " "

exit 0;
############################################################

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

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

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

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

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

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

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

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

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

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

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

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

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

  if($#parfile == 0 ) then
    echo "ERROR: must specify a paradigm file"
    exit 1;
  endif
  if(! -e $parfile ) then
    echo "ERROR: cannot find $parfile"
    exit 1;
  endif

  if($#ksparfile == 0) then
    echo "ERROR: must specify a ks paradigm file"
    exit 1;
  endif

  if($#poscondlist == 0) then
    echo "ERROR: must specify a positive condition list"
    exit 1;
  endif
  if($#negcondlist == 0) then
    echo "ERROR: must specify a negative condition list"
    exit 1;
  endif

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


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

#--------------------------------------------------------------------#
usage_exit:
  echo "USAGE: par2kspar [options]"
  echo "  -parfile paradigm file"
  echo "  -delay  ntrs : hemodynamic delay in TRs"
  echo "  -p positive-condition"
  echo "  <-p positive-condition>"
  echo "  -n negative-condition"
  echo "  <-n negative-condition>"
  echo "  -ksparfile ks-paradigm file"
exit 1;
#--------------------------------------------------------------------#
