#! /bin/csh -f
############################################################################
# run special case AFNI to3d (marty sereno, version 0.1, 4/29/01)
############################################################################
if ($#argv < 5) then
  echo ""
  echo "Use: brik2BRIK <infile.brik> <slices> <reps> <inplane> <thick> [opts]"
  echo ""
  echo "  Args:"
  echo "     <infile>    small brik (=headerless), must have '.brik' suffix"
  echo "     <slices>    number of slices (z-axis)"
  echo "     <reps>      number of repetitions of each slice"
  echo "     <inplane>   inplane x-y pixel width in mm"
  echo "     <thick>     slice thickness in mm"
  echo ""
  echo "  Outputs:"
  echo "     <instem>+orig.BRIK"
  echo "     <instem>+orig.HEAD"
  echo ""
  echo "  Opts: (at end)"
  echo "     -rmident    (-r) remove infile.brik if outfile.BRIK identical"
  echo "     -force      (-f) overwrite outfiles if they already exist"
  echo ""
  echo "  Hacks:"
  echo "     runs special case AFNI to3d: COR TR4 retinotopy, N.B.: centered z"
  echo ""
  exit
endif

### run various binaries
set to3dprog = ~inverse/afni/sgi10k_`uname -m`/to3d
if (! -e $to3dprog) set to3dprog = to3d

### defaults that should be settable
set xysize = 64
set trms = 4000
set interleave = alt+z

### required args
set infile = $1
set slices = $2
set reps = $3
set inplane = $4
set thickness = $5

### opts
set rmident = 0
set force = 0
foreach opt ($argv[6-])
  if ("$opt" == "-rmident" || "$opt" == "-r")  set rmident = 1
  if ("$opt" == "-force"   || "$opt" == "-f")  set force = 1
end

### infile
if (! -e $infile) then
  echo "brik2BRIK: ### infile $infile not found"
  exit
endif
set suffix = $infile:e
if ($suffix != "brik") then
  echo "brik2BRIK: ### only accepts small brik input"
  exit
endif
# to3d OK w/composite paths in infile but *not* in stem! (cd, reset infile)
set stem = `basename $infile .brik`
set brikdir = `dirname $infile`
if ($brikdir != ".") then
  if ($infile !~ /*) set infile = $cwd/$infile  # make abs
  cd $brikdir
endif

### outfile
set outfile1 = ${stem}+orig.BRIK
set outfile2 = ${stem}+orig.HEAD
set outlist = ($outfile1 $outfile2)
foreach outfile ($outlist)
  if (-e $outfile && ! $force) then
    echo "brik2BRIK: ### outfile $outfile exists  ...quitting"
    exit
  endif
end
if ($force) rm $outfile1 $outfile2

### get to3d args
set zt = `echo "scale=1; ($slices*$reps)" | 'bc' -l`
set halfx = `echo "scale=1; ($xysize*$inplane/2.0)" | 'bc' -l`
set halfy = `echo "scale=1; ($xysize*$inplane/2.0)" | 'bc' -l`
set halfz = `echo "scale=1; ($slices*$thickness/2.0)" | 'bc' -l`
#set halfz = `echo "scale=1; (($slices-1)*$thickness/2.0)" | 'bc' -l`
# in Linux 2.31, not yet in IRIX 2.29
set skipoutliers = ""
if (`uname -s` == "Linux") set skipoutliers = "-skip_outliers"

### run to3d (echoing cmdline with set/unset echo kills tcl exec!)
which to3d
echo "$to3dprog -prefix $stem -session $cwd -epan $skipoutliers -time:zt $slices $reps $trms $interleave -xFOV ${halfx}R-L -yFOV ${halfy}S-I -zFOV ${halfz}P-A 3D:0:0:${xysize}:${xysize}:${zt}:${infile}"
#set echo
$to3dprog \
  -prefix $stem \
  -session $cwd \
  -epan \
  $skipoutliers \
  -time:zt $slices $reps $trms $interleave \
  -xFOV ${halfx}R-L \
  -yFOV ${halfy}S-I \
  -zFOV ${halfz}P-A \
  3D:0:0:${xysize}:${xysize}:${zt}:${infile}
#unset echo

### warn (or rm) when in/out same
if (`diff $infile $outfile1 | wc -l` == 0) then
  if ($rmident) then
    rm $infile
    echo "brik2BRIK: ### identical inbrik ($infile) removed"
  else
    echo "brik2BRIK: ### outbrik ($outfile1) identical to inbrik ($infile)"
    echo "           ### you may want to remove duplicate inbrik ($infile)"
  endif
endif
