#!/bin/tcsh -f
# tdr-fidmat

set VERSION = '$Id: tdr-recon,v 1.5 2003/11/07 00:44:32 greve Exp $';

set inputargs = ($argv);

set rawkdir    = ();
set rcolmat    = ();
set outstem    = ();
set bhdrfile   = ();
set nframes    = ();
set nechoes    = ();
set usefid     = 1;
set monly      = 0;
set fidstem    = ();
set cleanup    = 1;
set convmdh    = 1;
set fixpedrift = 1;

# This is for reconning a few frames instead of all
# for debugging purposes
set nframesrecon  = ();

# EPI timing parameters in us
set epitdwell = 3.2;      
set epidelsamp = 30;      
set tRampUp    = 140; 
set tFlat      = 190; 
set tRampDown  = 140; 

set nslices    = 35; 
set nrows      = 64;
set nkcols     = 128;
set npcns      = 3;

# for martin
if(0) then
  echo ""
  echo "INFO: Using martins parameters"
  echo ""
  set nslices    = 23; 
  set nrows      = 128; 
  set nkcols      = 256;
  set epidelsamp = 60;      # us
  set epitdwell = 1.6;      # us
  set tRampUp    = 190; 
  set tFlat      = 150; 
  set tRampDown  = 190; 
endif

set PrintHelp = 0;
if($#argv == 0) goto usage_exit;
set n = `echo $argv | grep -e -help | wc -l` 
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
endif
set n = `echo $argv | grep -e -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 umask = `umask`;

##### Create a log file ######
set logdir = $outdir/log;
mkdir -p $logdir
if(! -e $logdir) then
  echo "WARNING: could not create $logdir"
  set LF = /dev/null
else
  set LF = $logdir/tdr-recon.log
  if(-e $LF) mv $LF $LF.old
endif
echo "--------------------------------------------------------------"
echo "tdr-recon logfile is $LF"
echo "--------------------------------------------------------------"

echo "tdr-recon log file" >> $LF
echo $VERSION >> $LF
id            >> $LF
pwd           >> $LF
echo $0        >> $LF
echo $inputargs  >> $LF
uname -a      >> $LF
date          >> $LF

set StartTime = `date`;

set kepimghdir = $outdir/tmp
if($convmdh) then
  mkdir -p $kepimghdir
  set cmd = (mri_convert_mdh --srcdir $rawkdir \
      --outdir $kepimghdir \
      --dim $nframes $nslices $nechoes $nrows $nkcols $npcns \
      --fasterdim line)
  echo "---------------------------------" |& tee -a $LF
  date |& tee -a $LF
  echo $cmd |& tee -a $LF
  $cmd |& tee -a $LF
  if($status) exit 1;
else
  if(! -e $kepimghdir ) then
    echo "ERROR: $kepiimghdir does not exist, run without --noconvmdh"
    exit 1;
  endif
endif

echo "Echo $nthecho -----------"| & tee -a $LF
date | & tee -a $LF
set funcstem = $outstem
echo "funcstem is $funcstem" | & tee -a $LF

if($#nframesrecon == 0) set nframesrecon = $nframes;

set MLF = $outdir/run_tdr_recon_$nthecho.m
rm -f $MLF
  #---------------------------------------------#
tee -a > /dev/null $MLF  <<EOF
  kepidir = '$kepimghdir';
  epiecho =  $nthecho;
  rcolmatfile = '$rcolmat';
  usefid = $usefid;
  fixpedrift = $fixpedrift;
  %nframes = $nframes;
  nframes = $nframesrecon;
  %nframes = 1;
  funcstem = '$funcstem';
  fidstem  = '$fidstem';
  bhdrfile = '$bhdrfile';
  tDwell     = $epitdwell; % usec
  tDelSamp   = $epidelsamp;  % usec
  tRampUp    = $tRampUp; % usec
  tFlat      = $tFlat; % usec
  tRampDown  = $tRampDown; % usec
  tdelay     = 1.0;
  tdr_recon;
EOF
  #---------------------------------------------#

if(! $monly) then
  cat $MLF >> $LF
  cat $MLF | matlab -display iconic -nojvm -nosplash | & tee -a $LF
  echo "" | & tee -a $LF
endif

if($cleanup) then
  echo "Deleting mgh files" |& tee -a $LF
  set cmd = (rm -r $kepimghdir)
  echo $cmd |& tee -a $LF
  $cmd |& tee -a $LF
endif

echo "Started at $StartTime" |& tee -a $LF
echo "Ended   at `date`"     |& tee -a $LF

echo "tdr-recon done"


exit 0

###############################################

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

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

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

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

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

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

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

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

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

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

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

    case "--nofid":
      set usefid = 0;
      breaksw

    case "--nofixpedrift":
      set fixpedrift = 0;
      breaksw

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

    case "--noconvmdh":
      set convmdh = 0;
      breaksw

    case "--debug":
      set verbose = 1;
      set echo = 1; # turns on terminal echoing
      breaksw

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

end

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

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

  if($#rawkdir == 0) then
    echo "ERROR: must specify a rawkdir"
    exit 1;
  endif
  if(! -e $rawkdir ) then
    echo "ERROR: $rawkdir does not exist"
    exit 1;
  endif
  if(! -e $rawkdir/meas.out ) then
    echo "ERROR: $rawkdir/meas.out does not exist"
    exit 1;
  endif
  if(! -e $rawkdir/meas.asc ) then
    echo "ERROR: $rawkdir/meas.asc does not exist"
    exit 1;
  endif
  if($#nframes == 0 ) then
    echo "ERROR: must spec nframes"
    exit 1;
  endif
  if($#nechoes == 0 ) then
    echo "ERROR: must spec nechoes"
    exit 1;
  endif
  if(! -e $rcolmat ) then
    echo "ERROR: $rcolmat does not exist"
    exit 1;
  endif

  if($#bhdrfile) then
    if(! -e $bhdrfile ) then
      echo "ERROR: $bhdrfile does not exist"
      exit 1;
    endif
  endif

  if($#outstem == 0) then
    echo "ERROR: must specify an out stem"
    exit 1;
  endif
  set outdir = `dirname $outstem`;

  mkdir -p $outdir
  if($status) then
    echo "ERROR: making $outdir"
    exit 1;
  endif

  if($#fidstem != 0) then
    set fidoutdir = `dirname $fidstem`;
    mkdir -p $fidoutdir
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: tdr-recon"
  echo ""
  echo "  --rawk    dir"
  echo "  --rcol    rcolmat"
  echo "  --echo    nthecho"
  echo "  --nofid   don't compensate for decay"
  echo "  --nofixpedrift  don't compensate for PE drift"
  echo "  --nframes nframes"
  echo "  --nechoes nechoes"
  echo "  --o       outstem"
  echo "  --fid     save first echo of fid as an anatomical"
  echo "  --bhdr    bhdrfile (use as a template)"
  echo "  --nocleanup : do not delete intermediate mgh files"
  echo "  --noconvmdh : do not convert mdh files (must exist alreay)"
  echo ""

  if(! $PrintHelp) exit 1;

  echo $VERSION

  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'

exit 1;


#---- Everything below here is printed out as part of help -----#
BEGINHELP

Converts raw k-space data into images using time-domain 
reconstruction  method.
