#!/bin/tcsh -f
# tdr-fidmat2 - used tdr_fidmat2.m

set VERSION = '$Id$';

set inputargs = ($argv);

set rawkdir    = ();
set outmat     = ();
set fwhm       = 0;
set monly      = 0;
set cleanup    = 1;
set convmdh    = 1;
set TE         = ();
set PERev      = ();

set fidecho1ped = 1810;   # PED of first echo (us)
set fidechospacing = 820; # Time between FID echoes (Actually use double)
set nfidechoes = 99; # But we'll only use half (odd echoes), should get from file

set epiechospacing = 470; # us
set epidelsamp = 30;      # us
set epitdwell = 3.2;      # us

set nslices    = 35; 
set nrows      = 64;
set ncols      = 128;

# for martin: TE = 40
if(1) then
echo ""
echo "INFO: Using martins parameters"
echo ""
set nfidechoes = 81; 
set nslices    = 23; 
set nrows      = 128; 
set ncols      = 256;
set epiechospacing = 530; # us
set epidelsamp = 60;      # us
set epitdwell = 1.6;      # us
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`;

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

##### 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-fidmat.log
  if(-e $LF) mv $LF $LF.old
endif
echo "--------------------------------------------------------------"
echo "tdr-fidmat2 logfile is $LF"
echo "--------------------------------------------------------------"

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

set StartTime = `date`;

if(0) then
set fidrlf = $rawkdir/fid.rlf
if(! -e $fidrlf) then
  echo "ERROR: cannot find $fidrlf" |& tee -a $LF
  exit 1;
endif

set tmp = `cat $fidrlf`;
set fiddir = $rawkdir/$tmp[1];
if(! -e $fiddir) then
  echo "ERROR: cannot find $fiddir" |& tee -a $LF
  exit 1;
endif
endif

pushd $rawkfiddir > /dev/null
set rawkfiddir = `pwd`;
popd > /dev/null

set fidmghdir = $outdir/tmp/fid
if($convmdh) then
  mkdir -p $fidmghdir
  set cmd = (mri_convert_mdh --srcdir $rawkfiddir \
        --outdir $fidmghdir \
        --dim 1 $nslices $nfidechoes $nrows $ncols 0 \
        --fasterdim echo)
  echo "---------------------------------" |& tee -a $LF
  date |& tee -a $LF
  echo $cmd |& tee -a $LF
  $cmd |& tee -a $LF
  if($status) exit 1;
else
  if(! -e $fidmghdir) then
    echo "ERROR: $fidmghdir does not exist, run without --noconvmdh"
    exit 1;
  endif
endif

date | tee -a $LF

set MLF = $fidmghdir/run_tdr_fidmat-$TE.m
rm -f $MLF

#---------------------------------------------#
tee -a > /dev/null $MLF  <<EOF
  fidecho1ped = $fidecho1ped;   
  fidechospacing = $fidechospacing; 
  nfidechoes = $nfidechoes ; 
  
  % EPI timing parameters - note EPI data not needed here
  epiechospacing = $epiechospacing; 
  delsamp = $epidelsamp ;
  tDwell = $epitdwell;
  
  % Dimensions: applies to both EPI and FID - should just get this from data
  nrows = $nrows;
  ncols = $ncols;
  nslices = $nslices;

  fiddir = '$fidmghdir';
  fidfwhm = [$fwhm];
  TE = $TE;
  perev = $PERev;
  outmat = '$outmat';
  tdr_fidmat2;
EOF
#---------------------------------------------#

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

if($cleanup) then
  echo "Deleting mgh files" |& tee -a $LF
  set cmd = (rm -r $fidmghdir)
  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-fidmat2 done"       |& tee -a $LF


exit 0

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

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

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

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

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

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

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

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

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

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

    case "--verbose":
      set verbose = 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($#rawkfiddir == 0) then
    echo "ERROR: must specify a rawkdir"
    exit 1;
  endif
  if(! -e $rawkfiddir ) then
    echo "ERROR: $rawkfiddir does not exist"
    exit 1;
  endif

  if($#outmat == 0) then
    echo "ERROR: must specify an output mat file"
    exit 1;
  endif

  if($#TE == 0) then
    echo "ERROR: must specify a TE"
    exit 1;
  endif

  if($#PERev == 0) then
    echo "ERROR: must specify PERev"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: tdr-fidmat2"
  echo ""
  echo "  --rawkdir dir"
  echo "  --outmat  matfile"
  echo "  --TE TE"
  echo "  --perev PERev"
  echo "  --fwhm    fwhm"
  echo "  --nocleanup : do not delete intermediate mgh files"
  echo "  --noconvmdh : do not convert mdh files (already there)"
  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

Computes the FID map for a functional set of data
