#! /bin/csh -f

#
# spatevfilter
#
# 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 VERSION = '$Id: spatevfilter,v 1.2 2007/01/09 22:41:18 nicks Exp $'
set inputargs = ($argv);

set PWDCMD = `getpwdcmd`;

set instem = ();
set outstem = ();
set raw_sevect = ();
set raw_seval = ();
set signal_sevect = ();
set signal_seval = ();
set nkeep = -1;
set regmethod = ();
set firstslice = 0;
set nslices = -1;
set monly = 0;
set MLF = ();
set nocheck = 1;
set QuitOnError = 1;

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

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

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

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

if(! $nocheck) then
  echo "Checking Volume $instem"
  bfvcheck $instem
  if($status) then
    echo "ERROR found with $instem"
    exit 1;
  endif
endif

set outdir = `dirname $outstem`;
mkdir -p $outdir

set LF = $outdir/spatevfilter.log

if($#MLF == 0) set MLF = $outdir/spatev_$$.m
rm -f $MLF

set okfile = $outdir/spatevfilter.ok.$$
rm -f $okfile

echo matlab file is $MLF 
rm -f $MLF;
echo "% Matlab file for spatevfilter `date`" >> $MLF
echo "% This file may be deleted "     >> $MLF
echo "% $MLF"                          >> $MLF;
echo "global QuitOnError;"             >> $MLF;
echo "QuitOnError = $QuitOnError;"     >> $MLF;
echo "r=1;"     >> $MLF;
set c = "";
echo -n "r = fast_spatevfilter("     >> $MLF;
foreach arg ($inputargs)
  echo -n "$c '$arg'"      >> $MLF;
  set c = ",";
end
echo -n ");" >> $MLF;
echo " "        >> $MLF;
echo "if(r==0)" >> $MLF;
echo "  fmri_touch('$okfile');" >> $MLF;
echo "end"  >> $MLF;
echo "qoe;" >> $MLF;

echo "----------- Matlab file --------------" 
cat $MLF | tee -a $LF
echo " " | tee -a $LF
echo "-----------------------------------" 

if(! $monly ) then
  echo "------------------------------------------" 
  echo "------- matlab output --------------------" 
  cat $MLF | $MATLAB -display iconic 
  echo "------------------------------------------" 
  rm $MLF
  if(! -e $okfile) then
    echo "ERROR: fast_spatevfilter() failed\n";
    exit 1;
  endif
  rm -f $okfile
endif

echo " " 
echo " " 
date 
echo "spatevfilter Done" 

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

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

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

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

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

    case "-firstslice":
    case "-fs":
      if ( $#argv == 0) goto arg1err;
      set firstslice = $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 "-rawsevect":
      if ( $#argv == 0) goto arg1err;
      set raw_sevect = $argv[1]; shift;
      breaksw

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

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

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

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

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

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

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

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

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

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

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

end

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

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

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

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

  if($#raw_sevect == 0) then
    echo "ERROR: no raw sevect specified" 
    exit 1;
  endif

  if($#raw_seval == 0) then
    echo "ERROR: no raw seval specified" 
    exit 1;
  endif

  if($#signal_sevect == 0) then
    echo "ERROR: no signal sevect specified" 
    exit 1;
  endif

  if($#signal_seval == 0) then
    echo "ERROR: no signal seval specified" 
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo "USAGE: spatevfilter"
  echo "Options:";
  echo "   -i instem "
  echo "   -o outstem "
  echo "   -rawsevect stem"
  echo "   -rawseval  stem"
  echo "   -signalsevect stem"
  echo "   -signalseval  stem"
  echo "   -nkeep n"
  echo "   -regmethod method"
  echo "   -umask umask   : set unix file permission mask"
  echo "   -version       : print version and exit"
exit 1;
