#!/bin/tcsh -f
# tkregisterfv

set VERSION = 'tkregisterfv 8.2.0';

set subject = ();
set movvol = ();
set reg = ()
set targ = ();
set fstarg = orig.mgz
set lta = ();
set surfs = 0;
set segcolor = $FREESURFER_HOME/FreeSurferColorLUT.txt
set segopacity = .3;
set UseTkRegister = 0;
set surfcolor = (yellow red yellow red);
set surflist = ();
set surflist0 = ();
set DoSurfs = 0;
set auxlist = ();
set auxreglist = ();
set segvol0 = ();
set segvol = ();
set Plane = cor
set ShowConfig = 1;
set altargs = ()
set movvol2 = ();
set reg2 = ()
set movvol3 = ();
set reg3 = ()
set DoTal = 0;
set ColorMap = grayscale
set params = (0 0 0 0 0 0 1 1 1 0 0 0); # 12dof
set regheader = 0;
set interp = (cubic) 
if($?FV_PATIENT_ORIENTATION == 0) setenv FV_PATIENT_ORIENTATION 1
if($?FV_ROTATE_AROUND_CURSOR == 0) setenv FV_ROTATE_AROUND_CURSOR 0

set tmpdir = ();
set cleanup = 1;
set LF = ();

set inputargs = ($argv);
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

source $FREESURFER_HOME/sources.csh

goto parse_args;
parse_args_return:
goto check_params;
check_params_return:

set StartTime = `date`;
set tSecStart = `date '+%s'`;

#========================================================
if($DoTal) then
  set tmpdir = `fs_temp_dir`
  mkdir -p $tmpdir
  set mni305 = $FREESURFER_HOME/average/mni305.cor.mgz
  set mdir = $SUBJECTS_DIR/$subject/mri
  set sdir = $SUBJECTS_DIR/$subject/surf
  set xfm = $mdir/transforms/talairach.xfm
  set lta = $tmpdir/talairach.lta
  # Convert xfm to tmplta
  set cmd = (lta_convert --src $mdir/$fstarg --trg $mni305 \
     --inxfm $xfm --outlta $lta --subject fsaverage)
  echo $cmd 
  $cmd
  if($status) exit 1;
  # Save a copy
  set ltacopy = $tmpdir/talairach.copy.lta
  cp -p $lta $ltacopy
  # Run freeview (vglrun not needed here)
  set cmd = (tkregisterfv --mov $mdir/$fstarg --reg $lta)
  @ nth = 0
  foreach surf ($surflist)
    @ nth = $nth + 1;
    #set cmd = ($cmd --surface $surf":"edgecolor=$surfcolor[$nth])
  end
  echo $cmd
  $cmd
  # Check whether the user changed the reg
  set n = `diff $lta $ltacopy | wc -l`
  if($n == 0) then
    echo "No change to registration"
    echo "tkregisterfv done"
    rm -rf $tmpdir
    exit 0
  endif
  # Convert it back to xfm
  set cmd = (lta_convert --inlta $lta --outmni $xfm)
  echo $cmd 
  $cmd
  if($status) exit 1;
  rm -rf $tmpdir
  echo "tkregisterfv done"
  exit 0
endif

set tmpdir = `fs_temp_dir`
mkdir -p $tmpdir
if(! $UseTkRegister) then
  ### FreeView ####----------------------------------------------
  set reg0 = $reg
  if($regheader) then
    # Not sure what to do here. The reg gets created prior to running
    # FV. If the user does not like the reg and quits, then the reg
    # file will still be there. Can use a different regname, but then
    # user has to change the name when saving and, if the user is ok
    # with the reg, there is no way to change the name because the reg
    # has not been changed; it could also be that the user changes the
    # reg, but then saves it as the changed name. Right now, I think
    # the safest course is to change the name since few people will
    # be using --regheader, and it is probably a bigger sin to have
    # a misleading file laying around.
    set headerreg1 = $reg.change-name.lta
    set headerreg2 = $tmpdir/tkregisterfv.$$.tmp.lta; # copy to check for change
    # This was using tkregister_cmdl which outputs vox2vox, coreg outputs RAS2RAS
    set cmd = (mri_coreg) 
    if($#subject) set cmd = ($cmd --s $subject)
    set cmd = ($cmd --par2mat $params $movvol $targ $headerreg1)
    echo $cmd 
    $cmd 
    if($status) then
      echo "cd `pwd`"
      echo "ERROR: $cmd"
      exit 1;
    endif
    cp $headerreg1 $headerreg2; # 2 newer than 1
    set reg = $headerreg1
  endif
  set cmd = (freeview --hide-3d-slices )
  if($FV_PATIENT_ORIENTATION == 2) set cmd = ($cmd -neuro-view)
  if($FV_ROTATE_AROUND_CURSOR) set cmd = ($cmd -rotate-around-cursor)
  if($#interp) set cmd = ($cmd --$interp)
  if($ShowConfig)  set cmd = ($cmd -transform-volume)
  set cmd = ($cmd -viewport $Plane) # currently causes FV to crash
  set targname = `basename $targ`
  set visible = 1
  if($#surflist == 0) set visible = 1
  set arg = ($targ":"visible=$visible":"name=$targname"(targ)"":"colormap=$ColorMap)
  set cmd = ($cmd -v $arg)
  @ nthaux = 0;
  foreach aux ($auxlist)
    @ nthaux = $nthaux + 1;
    set arg = $aux
    if($auxreglist[$nthaux] != 0) set arg = ($arg $arg":"reg=$auxreglist[$nthaux])    
    set arg = ($arg":"colormap=$ColorMap)
    set cmd = ($cmd $arg)
  end
  if($#segvol) set cmd = ($cmd -v "$segvol":"lut=$segcolor":"opacity=$segopacity":"colormap=lut")
  set movname = `basename $movvol`
  if($#reg == 1) set cmd = ($cmd $movvol":"name=$movname"(mov):"reg=$reg":"colormap=$ColorMap)
  if($#reg == 0) set cmd = ($cmd $movvol":"name=$movname"(mov)"":"colormap=$ColorMap)
  if($#movvol2) then
    set movname2 = `basename $movvol2`
    if($#reg2 == 1) set cmd = ($cmd $movvol2":"name=$movname2"(mov2):"reg=$reg2":"colormap=$ColorMap)
    if($#reg2 == 0) set cmd = ($cmd $movvol2":"name=$movname2"(mov2)"":"colormap=$ColorMap)
  endif
  if($#movvol3) then
    set movname3 = `basename $movvol3`
    if($#reg3 == 1) set cmd = ($cmd $movvol3":"name=$movname3"(mov3):"reg=$reg3":"colormap=$ColorMap)
    if($#reg3 == 0) set cmd = ($cmd $movvol3":"name=$movname3"(mov3)"":"colormap=$ColorMap)
  endif
  @ nth = 0;
  foreach surf ($surflist)
    @ nth = $nth + 1;
    set cmd = ($cmd --surface $surf":"edgecolor=$surfcolor[$nth])
  end
  set cmd = ($cmd $altargs)
else
  ### tkregister ####----------------------------------------------
  set cmd = tkregister
  if($DoSurfs) set cmd = ($cmd -surfs)
  endif
endif
echo "cd `pwd`"
echo $cmd
fsvglrun $cmd
set st = $status
if($st) exit $st

if($regheader) then
  # Several things can happen here
  if(-e $reg0) then
    test $reg0 -nt $headerreg1; # returns 0 of reg0 newer than headerreg1
    if(! $status) then
      # A new reg has been created, so delete regheaders
      rm -f $headerreg1 $headerreg2
    endif
  else 
    set n = `diff $headerreg1 $headerreg2 | wc -l`
    if($n) then
      # A new reg has been created as headerreg1, so leave headerreg1,
      # delete headerreg2. This means that the desired reg file will
      # not exit, but it is unclear what the intention of the user is
      rm -f $headerreg2
    else
      # A new reg has not been created, so delete both regheaders. It
      # is unclear what the intention of the user is as they could
      # have looked at the reg and decided it was ok and just
      # quit. But it seems safer to delete the headerregs and make
      # them create the reg with intention. But they will have to use
      # something other than tkregisterfv becaused FV does not allow
      # the user to save the reg unless it has been changed.
      rm -f $headerreg1 $headerreg2
    endif
  endif
endif

rm -r $tmpdir

exit $st

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

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

while( $#argv != 0 )

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

    case "--mov":
      if($#argv < 1) goto arg1err;
      set movvol = $argv[1]; shift;
      if(! -e $movvol) then
        echo "ERROR: cannot find $movvol"
        exit 1;
      endif
      set movvol = `getfullpath $movvol`
      breaksw

    case "--targ":
      if($#argv < 1) goto arg1err;
      set targ = $argv[1]; shift;
      if(! -e $targ) then
        echo "ERROR: cannot find $targ"
        exit 1;
      endif
      breaksw

    case "--aux":
      if($#argv < 1) goto arg1err;
      set auxvol = $argv[1]; shift;
      if(! -e $auxvol) then
        echo "ERROR: cannot find $auxvol"
        exit 1;
      endif
      set auxlist = ($auxlist `pwd`/$auxvol)
      if($#argv > 0) then
        set isarg = `isargflag $argv[1]`
        set auxreg = $argv[1]; shift;        
        if($auxreg != 0 && ! -e $auxreg) then
          echo "ERROR: cannot find $auxreg"
          exit 1;
        endif
        set auxreglist = ($auxreglist `pwd`/$auxreg)
      else
        set auxreglist = ($auxreglist 0)
      endif
      breaksw

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

    case "--subject":
    case "--s":
      if($#argv < 1) goto arg1err;
      set subject = $argv[1]; shift;
      set DoSurfs = 1;
      set surflist0 = (lh.white rh.white)
      set surfcolor = (yellow yellow);
      breaksw

    case "--sd":
      if($#argv < 1) goto arg1err;
      setenv SUBJECTS_DIR $argv[1]; shift
      breaksw

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

    case "--seg":
      if($#argv < 1) goto arg1err;
      set segvol0 = $argv[1]; shift;
      if(! `isargflag $argv`) then
        set segcolor = $argv[1]; shift;
      endif
      breaksw

    case "--aseg":
      set segvol0 = aseg.mgz
      breaksw

    case "--aparc+aseg":
      set segvol0 = aparc+aseg.mgz
      breaksw

    case "--op":
    case "--opacity":
      if($#argv < 1) goto arg1err;
      set segopacity = $argv[1]; shift;
      breaksw

    case "--lta":
    case "--reg":
      if($#argv < 1) goto arg1err;
      set reg = $argv[1]; shift;
      set reg = `getfullpath $reg`
      breaksw

    case "--regheader"
    case "--reg-header"
      set regheader = 1
      breaksw

    case "--params":
      if($#argv < 12) then
        echo "ERROR: --params requires 12 arguments"
        exit 1;
      endif
      set params = ($argv[1-12]);
      shift;shift;shift;shift;shift;shift;shift;shift;shift;shift;shift;shift;
      set regheader = 1
      breaksw

    case "--flip-x":
      set params[4] = 180
      set regheader = 1
      breaksw

    case "--flip-y":
      set params[5] = 180
      set regheader = 1
      breaksw

    case "--flip-z":
      set params[6] = 180
      set regheader = 1
      breaksw

    case "--mov2":
      if($#argv < 1) goto arg1err;
      set movvol2 = $argv[1]; shift;
      if(! -e $movvol2) then
        echo "ERROR: cannot find $movvol2"
        exit 1;
      endif
      set movvol2 = `getfullpath $movvol2`
      breaksw

    case "--lta2":
    case "--reg2":
      if($#argv < 1) goto arg1err;
      set reg2 = $argv[1]; shift;
      set reg2 = `getfullpath $reg2`
      breaksw

    case "--mov3":
      if($#argv < 1) goto arg1err;
      set movvol3 = $argv[1]; shift;
      if(! -e $movvol3) then
        echo "ERROR: cannot find $movvol3"
        exit 1;
      endif
      set movvol3 = `getfullpath $movvol3`
      breaksw

    case "--lta3":
    case "--reg3":
      if($#argv < 1) goto arg1err;
      set reg3 = $argv[1]; shift;
      set reg3 = `getfullpath $reg3`
      breaksw

    case "--surfs":
    case "--wm-surfs":
      set DoSurfs = 1;
      set surflist0 = (lh.white rh.white)
      set surfcolor = (yellow yellow);
      breaksw

    case "--lh-white":
    case "--lh-only":
    case "--lh":
      set DoSurfs = 1;
      set surflist0 = (lh.white)
      set surfcolor = (yellow)
      breaksw

    case "--rh-white":
    case "--rh-only":
    case "--rh":
      set DoSurfs = 1;
      set surflist0 = (rh.white)
      set surfcolor = (yellow)
      breaksw

    case "--pial-surfs":
      set DoSurfs = 1;
      set surflist0 = (lh.pial rh.pial)
      set surfcolor = (red red);
      breaksw

    case "--all-surfs":
      set DoSurfs = 1;
      set surflist0 = (lh.white lh.pial rh.white rh.pial)
      set surfcolor = (yellow red yellow red);
      breaksw

    case "--no-surfs":
    case "--no-surf":
      set DoSurfs = 0;
      set surflist0 = ()
      breaksw

    case "--surface":
    case "--surf":
      if($#argv < 1) goto arg1err;
      set mainsurf = $argv[1]
      set surflist0 = ($surflist0 $mainsurf); shift;
      breaksw

    case "--aux-surface":
    case "--aux-surf":
      if($#argv < 1) goto arg1err;
      set auxsurf = $argv[1]
      set surflist0 = ($surflist0 $auxsurf); shift;
      breaksw

    case "--plane":
      if ( $#argv == 0) goto arg1err;
      set Plane = $argv[1]; shift;
      if($Plane == cor) set Plane = coronal
      if($Plane == sag) set Plane = sagittal
      if($Plane == ax)  set Plane = axial
      breaksw

    case "--linear":
    case "--trilinear":
    case "--trilin":
      set interp = trilinear
      breaksw

    case "--cubic":
      set interp = cubic
      breaksw

    case "--heat":
      set ColorMap = heat
      breaksw

    case "--fstal"
      set DoTal = 1;
      breaksw

    case "-radiological":
    case "-radio":
      setenv FV_PATIENT_ORIENTATION 1
      breaksw
    case "-neurological":
    case "-neuro":
      setenv FV_PATIENT_ORIENTATION 2
      breaksw

    case "-vgl":
    case "--vgl":
      setenv FS_ALLOW_VGLRUN 1
      breaksw
    case "-novgl":
    case "--novgl":
    case "--no-vgl":
    case "-no-vgl":
      unsetenv FS_ALLOW_VGLRUN 
      breaksw

    case "--no-config"
      set ShowConfig = 0;
      breaksw

    case "--tkregister":
    case "--tkr":
      set UseTkRegister = 1;
      breaksw

    case "--nolog":
    case "--no-log":
      set LF = /dev/null
      breaksw

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

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

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

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

    default:
      set altargs = ($altargs $flag)
      #echo ERROR: Flag $flag unrecognized. 
      #echo $cmdline
      #exit 1
      breaksw
  endsw

end

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

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

if($DoTal) then
  if($#subject == 0) then
    echo "ERROR: must have subject with --fstal"
    exit 0
  endif
  if($#reg != 0) then
    echo "ERROR: cannot have --reg with --fstal"
    exit 0
  endif
  set movvol = $SUBJECTS_DIR/$subject/mri/$fstarg
endif

if($#targ == 0) then
  if($#subject == 0 && $#reg == 0) then
    echo "ERROR: must spec either subject or reg or targ"
    exit 1;
  endif
  if($#subject == 0) then
    if(! -e $reg) then
      echo "ERROR: cannot find $reg"
      exit 1;
    endif
    set subject = `reg2subject --r $reg`
  endif
  if(-e $SUBJECTS_DIR/$subject) then
    set targ = $SUBJECTS_DIR/$subject/mri/$fstarg
    if(! -e $targ) then
      echo "ERROR: cannot find $targ"
      exit 1;
    endif
  else if($#reg != 0) then
    set tmp = (`grep filename $reg | awk '{print $3}'`)
    if($#tmp != 2) then
      echo "ERROR: cannot get filenames from $reg"
      exit 1
    endif
    set targ   = $tmp[2];
  endif
  echo "Setting targ to $targ"
endif

endif

if($#movvol == 0 && $#reg != 0) then
  set tmp = (`grep filename $reg | awk '{print $3}'`)
  if($#tmp != 2) then
    echo "ERROR: cannot get filenames from $reg"
    exit 1
  endif
  set movvol = $tmp[1];
  echo "Setting mov  to $movvol"
endif


if($#movvol == 0) then
  echo "ERROR: must spec a moveable volume"
  exit 1;
endif

if($#segvol0) then
  if(-e $segvol0) then
    set segvol = $segvol0
  else
    set segvol = $SUBJECTS_DIR/$subject/mri/$segvol0
  endif
  if(! -e $segvol) then
    echo "ERROR: cannot find $segvol"
    exit 1;
  endif
  if($#segcolor) then
    if(! -e $segcolor) then
      set segcolor0 = $segcolor;
      set segcolor = $SUBJECTS_DIR/$subject/mri/$segcolor0
      if(! -e $segcolor) then
        set segcolor = $FREESURFER_HOME/$segcolor0
        if(! -e $segcolor) then
          echo "ERROR: cannot find $segcolor0"
          exit 1; 
        endif
      endif
    endif
  endif
endif

if($DoSurfs) then
  foreach surf ($surflist0)
    if(-e $surf) then
      set surflist = ($surflist $surf)
    else
      set surfpath = $SUBJECTS_DIR/$subject/surf/$surf
      if(! -e $surfpath) then
        if($DoTal) then
          # When tal fails, the surface will not be created
          set DoSurfs = 0;
          break;
        endif
        echo "ERROR: cannot find $surfpath"
        exit 1;
      endif
      set surflist = ($surflist $surfpath)
    endif
  end
endif

if(! $DoTal) then
  if($#reg == 0) then
    echo "ERROR: must spec --reg"
    exit 1;
  endif
  if(! $regheader) then
    if(! -e $reg) then
      echo "ERROR: $reg does not exist. Spec a reg that does or use --regheader"
      exit 1;
    endif
  endif
  if($regheader && $#reg ) then
    if(-e $reg) then
      echo "ERROR: --regheader requested, but registration file $reg exists"
      echo "  delete it or change the passed registration file"
      exit 1;
    endif
  endif
endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "This is a script that runs freeview with arguments like tkregister."
  echo "Not all tkregister functionality is replicated. Only uses LTA files."
  echo ""
  echo "tkregisterfv --mov mov --targ targ --reg reg.lta --s subject "
  echo "  --aux vol1 reg1 --aux vol2 reg2 ..."
  echo "  Note: you can run it like "
  echo "   tkregisterfv --reg reg.lta             and it will read the mov and targ volumes from the lta"
  echo "   tkregisterfv --reg reg.lta --mov mov   and it will read the targ volume from the lta"
  echo "   tkregisterfv --reg reg.lta --targ targ and it will read the mov  volume from the lta"
  echo ""
  echo "  --fstarg volume : spec volume instead of $fstarg"
  echo "  --sd SUBJECTS_DIR"  
  echo "  --seg segvolume : load segvolume as well"  
  echo "  --aseg : load aseg.mgz as seg volume"  
  echo "  --aparc+aseg : load aparc+aseg.mgz as seg volume"  
  echo "  --opacity opacity : set seg opacity"
  echo "  --surfs : load lh and rh wm surfs"
  echo "  --pial-surfs : load pial surfs instead of wm surfs"
  echo "  --all-surfs : load both pial and wm surfs"
  echo "  --no-surfs : do not load any surfs"
  echo "  --lh-only, --rh-only : surface hemisphere"
  echo "  --surf surfname : explicit path to surf to load"
  echo "  --aux-surf auxsurfname : explicit path to surf to load"
  echo "  --plane plane : cor, sag, ax"
  echo "  --nearest : use nearest neighbor interp"
  echo "  --linear : use trilinear interp"
  echo "  --cubic : use cubic interp (default)"
  echo "  --no-config : do not automatically raise transform config window"
  echo "  --mov2 mov2 <--reg2 reg2> : provide a second mov (and possibly reg)"
  echo "  --mov3 mov3 <--reg3 reg3> : provide a third mov (and possibly reg)"
  echo "  --heat : use heat map color tables for all volumes"
  echo "  --regheader : create registration file assuming the two volumes share a RAS"
  echo "  --params tx ty tz ry rx rz sy sx sz hxy hxz hyz : regheader with affine matrix parameters"
  echo "      translations in mm, rotations in deg"
  echo "  --flip-x : regheader with rx=180"
  echo "  --flip-y : regheader with ry=180"
  echo "  --flip-z : regheader with rz=180"
  echo ""
  echo "  --fstal --s subject : only these two args to modify the talairach.xfm"
  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

