#!/bin/csh -f
# mri_motion_correct2
# Performs motion correction of anatomicals. This should produce
# the same output as mri_motion_correct

set VERSION = '$Id: mri_motion_correct2,v 1.7 2004/01/16 20:25:10 tosa Exp $';

set tmpdir      = /tmp;
set inputlist   = ();
set outdir      = ();
set PrintHelp   = 0;
set LF          = ();
set CleanUp     = 1;
set CleanUpList = ();
set PWD = pwd;
set HiRes = '';

# If no args, print usage and exit #
if($#argv == 0) then
  goto usage_exit;
  exit 1;
endif

# Look for version #
set n = `echo $argv | grep version | wc -l` 
if($n != 0) then
  echo $VERSION
  ##### gather version info used in this script
  mri_convert --version 
  set prog = `which rawtominc`
  strings $prog | grep rawtominc.c 
  set prog = `which minctracc`
  strings $prog | grep minctracc.c 
  set prog = `which mincresample`
  strings $prog | grep mincresample.c
  set prog = `which mincaverage`
  strings $prog | grep mincaverage.c 
  exit 0;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

##### Create a log file ######
if($#LF == 0) then
  set LF = $outdir/mri_motion_correct2.log
  if(-e $LF) mv $LF $LF.old
endif

echo "--------------------------------------------------------------"
echo "mri_motion_correct2 logfile is $LF"
echo "--------------------------------------------------------------"

echo "mri_motion_correct2 log file" >> $LF
echo $VERSION      >> $LF
echo $0            >> $LF
echo $argv         >> $LF
pwd               >> $LF
uname -a           >> $LF
date               >> $LF
which mri_convert  >> $LF

set StartTime = `date`;

#------------- Convert to MINC ------------#
set allcor = $tmpdir/allcor
set CleanUpList = ($CleanUpList $allcor);
set mncinputlist = ();
@ run = 0;
foreach input ($inputlist)
  @ run = $run + 1;

  echo "-----------------------------------------" |& tee -a $LF
  echo "Converting $input " |& tee -a $LF
  date |& tee -a $LF

  cat $input/COR-??? > $allcor |& tee -a $LF

  set mncinput = $tmpdir/cor-$run.mnc
  set mncinputlist = ($mncinputlist $mncinput);
  rm -f $mncinput

  if(0) then
    # rawtominc is MNI software without --version option, but it is cvs'ed.
    set cmd = (rawtominc)
    set cmd = ($cmd -coronal -byte -xstep 1 -ystep 1 -zstep 1 -xstart)
    set cmd = ($cmd -128 -ystart -128 -zstart -128 )
    set cmd = ($cmd $mncinput 256 256 256)
    set cmd = ($cmd -input $allcor)
    pwd >> $LF
    echo $cmd >> $LF
    $cmd |& tee -a $LF
    if($status) then
      echo "ERROR: rawtominc failed" |& tee -a $LF
      exit 1;
    endif
  endif

  set cmd = (mri_convert $input $mncinput)
  pwd >> $LF
  echo $cmd >> $LF
  $cmd |& tee -a $LF
  if($status) then
    echo "ERROR: rawtominc failed" |& tee -a $LF
    exit 1;
  endif

  set CleanUpList = ($CleanUpList $mncinput);

end

#----------- Motion Correct ---------------#
set mncresampled = $tmpdir/resampled.mnc
set mnctarget = $mncinputlist[1];
@ run = 0;
foreach mncinput ($mncinputlist)
  @ run = $run + 1;

  if($mncinput == $mnctarget) continue;

  set xfm = $tmpdir/run$run.xfm
  set CleanUpList = ($CleanUpList $xfm);
  rm -f $xfm

  echo "-----------------------------------------" |& tee -a $LF
  echo "Motion Correcting $mncinput" |& tee -a $LF
  date |& tee -a $LF
  # minctracc is MNI software without version option, but cvs'ed.
  set cmd = (minctracc -lsq6 $mncinput $mnctarget $xfm)
  pwd >> $LF
  echo $cmd >> $LF
  $cmd |& tee -a $LF
  if($status) then
    echo "ERROR: mritoself failed" |& tee -a $LF
    exit 1;
  endif

  echo "-----------------------------------------" |& tee -a $LF
  echo "Resampling $mncinput" |& tee -a $LF
  date |& tee -a $LF

  rm -f $mncresampled
  # mincresample is MNI software without version option, but cvs'ed
  set cmd = (mincresample -like $mnctarget -transform $xfm)
  set cmd = ($cmd $mncinput $mncresampled)
  pwd >> $LF
  echo $cmd >> $LF
  $cmd |& tee -a $LF
  if($status) then
    echo "ERROR: mincresample failed" |& tee -a $LF
    exit 1;
  endif

  mv $mncresampled $mncinput |& tee -a $LF

end

#----------- Average Volume Together ---------------#
echo "-----------------------------------------" |& tee -a $LF
echo "Averaging " |& tee -a $LF
date |& tee -a $LF
set avgvol = $tmpdir/avgvol.mnc
set CleanUpList = ($CleanUpList $avgvol);
rm -f $avgvol
set cmd = (mincaverage $mncinputlist $avgvol);
pwd >> $LF
echo $cmd >> $LF
$cmd |& tee -a $LF
if($status) then
  echo "ERROR: mincaverage failed" |& tee -a $LF
  exit 1;
endif

#----------- Convert Average to COR ---------------#
echo "-----------------------------------------" |& tee -a $LF
echo "Converting Average to COR " |& tee -a $LF
date |& tee -a $LF
set cmd = (mri_convert ${HiRes} $avgvol $outdir);
pwd >> $LF
echo $cmd >> $LF
$cmd |& tee -a $LF
if($status) then
  echo "ERROR: mri_convert failed" |& tee -a $LF
  exit 1;
endif

goto cleanup;
cleanup_return:


#------  Add info about TR, TE, TI, and Flip Angle --#
################# initialize
set trlist = ()
set telist = ()
set tilist = ()
set falist = ()

##### create lists for tr, te, flip_angle
################ TR
foreach input ($inputlist)
  set tr = `awk '{ if ($1 == "tr") print $2}' $input/COR-.info`
  if ($#tr != 0) then 
    echo $input " TR vlaue is " $tr |& tee -a $LF
    set trlist = ($trlist $tr)
  else
    echo $input " TR not found" |& tee -a $LF
  endif
################# TE
  set te = `awk '{ if ($1 == "te") print $2}' $input/COR-.info`  
  if ($#te != 0) then
    echo $input " TE vlaue is " $te |& tee -a $LF
    set telist = ($telist $te)
  else
    echo $input " TE not found" |& tee -a $LF
  endif
################# TI
  set ti = `awk '{ if ($1 == "ti") print $2}' $input/COR-.info`  
  if ($#ti != 0) then
    echo $input " TI vlaue is " $ti |& tee -a $LF
    set tilist = ($tilist $te)
  else
    echo $input " TI not found" |& tee -a $LF
  endif
################# Flip angle
  set fa = `awk '{ if ($1 == "flip") if ($2=="angle") print $3}' $input/COR-.info` 
  if ($#fa != 0) then
    echo $input " Flip angle is " $fa |& tee -a $LF
    set falist = ($falist $fa)
  else
    echo $input " Flip angle not found" |& tee -a $LF
  endif
end

##### check the uniqueness ######
################################# TR
@ count = 0
set tr1=0
foreach elem ($trlist)
  @ count = $count + 1
  if ($count == 1) then
    set tr1 = $elem
  else if ($tr1 != $elem) then
    set tr1 = 0
  endif
end

echo "TR is set to " $tr1 |& tee -a $LF

################################# TE
@ count = 0
set te1=0
foreach elem ($telist)
  @ count = $count + 1
  if ($count == 1) then
    set te1 = $elem
  else if ($te1 != $elem) then
    set te1 = 0
  endif
end

echo "TE is set to " $te1 |& tee -a $LF

################################# TI
@ count = 0
set ti1=0
foreach elem ($tilist)
  @ count = $count + 1
  if ($count == 1) then
    set ti1 = $elem
  else if ($ti1 != $elem) then
    set ti1 = 0
  endif
end

echo "TI is set to " $ti1 |& tee -a $LF


################################# Flip angle
@ count = 0
set fa1=0
foreach elem ($falist)
  @ count = $count + 1
  if ($count == 1) then
    set fa1 = $elem
  else if ($fa1 != $elem) then
    set fa1 = 0
  endif
end

echo "Flip angle is set to " $fa1 |& tee -a $LF

################################ write tr, te, ti, fa
cp $outdir/COR-.info $outdir/tmp
# check whether tr is present
echo "Writing TR value " $tr1
set tr = `awk '{ if ($1 == "tr") print $2}' $outdir/COR-.info`
if ($#tr != 0) then 
  echo "found tr field"
  awk -v trval=$tr1 '{ if ($1 == "tr") print $1, trval; else print }' $outdir/tmp > $outdir/tmp2
else
  echo "tr " $tr1 >> $outdir/COR-.info
endif 

echo "Writing TE value " $te1
set te = `awk '{ if ($1 == "te") print $2}' $outdir/COR-.info` 
if ($#te != 0) then
  echo "found te field"
  awk -v teval=$te1 '{ if ($1 == "te") print $1, teval; else print }' $outdir/tmp2 > $outdir/tmp3
else
  echo "te " $te1 >> $outdir/COR-.info
endif

echo "Writing TI value " $ti1
set ti = `awk '{ if ($1 == "ti") print $2}' $outdir/COR-.info`  
if ($#ti != 0) then
  echo "found ti field"
  awk -v tival=$ti1 '{ if ($1 == "ti") print $1, tival; else print }' $outdir/tmp3 > $outdir/tmp4
else  
  echo "ti " $ti1 >> $outdir/COR-.info
endif

echo "Writing Flip angle (degrees) " $fa1
set fa = `awk '{ if ($1 == "flip") if ($2=="angle") print $3}' $outdir/COR-.info` 
if ($#fa != 0) then
  echo "found flip angle field"
  awk -v faval=$fa1 '{ if ($1 == "flip") print "flip angle", faval; else print }' $outdir/tmp4 > $outdir/tmp5
else
  echo "flip angle " $fa1 >> $outdir/COR-.info
endif

mv $outdir/tmp5 $outdir/COR-.info
rm -f $outdir/tmp?

#---------------- end processing--------------------#
echo "Started at: $StartTime"    |& tee -a $LF
echo "Ended at:   `date`"        |& tee -a $LF
echo "mri_motion_correct2: done" |& tee -a $LF

exit 0;

################# subroutines #######################
#################             #######################

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

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

    case "-help"
    case "--help"
      set PrintHelp = 1;
      goto usage_exit;
      breaksw

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

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

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

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

    case "-nocleanup":
      set CleanUp = 0;
      breaksw

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

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

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

    case "-cm":
      set HiRes = ('-cm')
      breaksw

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

end

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

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

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

  if($#inputlist == 1) then
    echo "ERROR: only one input specified"
    exit 1;
  endif

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

  # Create the output directory and get full path
  mkdir -p $outdir
  if($status) then
    echo "ERROR: cannot create $outdir"
    exit 1;
  endif
  pushd $outdir > /dev/null
  set outdir = `pwd`;
  popd > /dev/null

  # Go through each input, make sure it exists
  # and re-create the list with full path names
  set inputlist0 = ($inputlist);
  set inputlist  = ()
  foreach i ($inputlist0)
    set inbase = `basename $i`;
    set indir  = `dirname $i`;
    if(! -e $indir) then
      echo "ERROR: cannot find $indir"
      exit 1;
    endif
    if(! -e $i/COR-.info ) then
      echo "ERROR: cannot find $i/COR-.info"
      exit 1;
    endif
    pushd $indir > /dev/null
    set indir = `pwd`;
    popd > /dev/null
    set input = $indir/$inbase
    set inputlist = ($inputlist $input);
  end

  # Create the tmp directory and get full path
  if($tmpdir != /tmp) then
    mkdir -p $tmpdir
    if($status) then
      echo "ERROR: cannot create $tmpdir"
      exit 1;
    endif
    pushd $tmpdir > /dev/null
    set tmpdir = `pwd`;
    popd > /dev/null
  endif

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

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


############--------------##################
cleanup:

  if($CleanUp) then
    foreach f ($CleanUpList)
      rm -f $f
    end
  endif

goto cleanup_return;
############--------------##################




############--------------##################
usage_exit:
  echo "USAGE: mri_motion_correct2"
  echo ""
  echo "Required Arguments:"
  echo ""
  echo "  -o  outdir : output directory"
  echo "  -i  input1 <-i input2 <-i input3>>"
  echo ""
  echo "Optional Arguments:";
  echo ""
  echo "  -tmpdir tmpdir : directory for temporary files"
  echo "  -nocleanup     : do not delete temporary files"
  echo "  -umask umask   : set unix file permission mask"
  echo "  -cm            : COR volumes conform to min voxel size"
  echo "  -version       : print version and exit"
  echo ""

  if(! $PrintHelp) exit 1;

exit 1;
