#!/bin/tcsh -f


PARSE:
   set Narg = $#
   set cnt = 1
   set v1_list = ""
   set v2i = ""
   set v1i = ""
   set usecp = 1
   if ("$1" == '') goto HELP
   while ($cnt <= $Narg)
      set donext = 1;

      if ($donext && "$argv[$cnt]" == "-help" || "$argv[$cnt]" == "-h") then
         goto HELP
      endif

      if ($donext && "$argv[$cnt]" == "-base") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need string after -base"
            goto END
         else
            @ cnt ++
            set v2i = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif

      if ($donext && "$argv[$cnt]" == "-no_cp") then
         set usecp = 0
         set donext = 0   
      endif

      if ($donext && "$argv[$cnt]" == "-dset") then
         set pLoc = $cnt      
         if ($pLoc >= $Narg) then
            echo "Need dset after -dset"
            goto END
         else
            @ cnt ++
            set v1i = ($argv[$cnt])
            set donext = 0   
         endif   
      endif
      if ($donext && "$argv[$cnt]" == "-child") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need dsets after -child"
            goto END
         else
            @ cnt ++
            set v1_list = ($argv[$cnt-])
            set nv1 = $#v1_list
            set cnt = `expr $cnt + $nv1 - 1`
            set donext = 0   
         endif   
      endif
      if ($donext == 1) then
         echo "Error: Option or parameter '$argv[$cnt]' not understood"
         goto END
      endif
      @ cnt ++
   end
   
SET_PARAMS: 
set v2_vw = `@GetAfniView $v2i`
set pth2 = $v2i:h
if ("$pth2" == "$v2i") then 
   set pth2 = .
endif

set v2_pref = `@GetAfniPrefix $v2i`
set v2 = $pth2/$v2_pref$v2_vw
 
CHECKS:
#make sure output doe not exist
if ( ! -f $v2.HEAD || (! -f $v2.BRIK && ! -f $v2.BRIK.gz && ! -f $v2.BRIK.bz2 && ! -f $v2.BRIK.Z)) then
   echo "Error: base brick $v2 not found."
   goto END
endif

SETCENTER:
   set v1_vw = `@GetAfniView $v1i`
   set v1_pth = $v1i:h
   if ("$v1_pth" == "$v1i") then 
      set v1_pth = .
   endif

   set v1_pref = `@GetAfniPrefix $v1i`
   set v1 = $v1_pth/$v1_pref$v1_vw


   #orientation of v1
   set v1_orient = `@GetAfniOrient $v1`
   #center of v1, in native orientation
   set v1_center = `@VolCenter -dset $v1`
   #center of v2, in v1's orientation
   set v2_center_v1or = `@VolCenter -dset $v2 -or $v1_orient`
   #calculate deltas
   set del = ( 0 0 0 )
   foreach i (1 2 3)
      set del[$i] = `ccalc -eval "$v2_center_v1or[$i] - $v1_center[$i]"`
   end
   #multiply deltas by axis signs
   set mp = `@AfniOrientSign $v1_orient`
   set del_refit = ($del)
   foreach i ( 1 2 3)
         set del_refit[$i] = `ccalc -form int -expr "$mp[$i] * $del[$i]"`
   end
   #apply delta
   if ($usecp == 1) then
      if ( `@CheckForAfniDset $v1_pth/${v1_pref}_shft$v1_vw` != 0 ) then
         echo ""
         echo "Error `basename $0`"
         echo "Dset $v1_pth/${v1_pref}_shft$v1_vw found, cleanup first!"
         echo ""
         goto END
      endif

      3dcopy $v1  $v1_pth/${v1_pref}_shft
      3drefit -dxorigin $del_refit[1] -dyorigin $del_refit[2] -dzorigin $del_refit[3] $v1_pth/${v1_pref}_shft$v1_vw
   else
      3drefit -dxorigin $del_refit[1] -dyorigin $del_refit[2] -dzorigin $del_refit[3] $v1
   endif
   
   #get delta in RAI 
   #echo "del: $del"
   set del_RAI = `@ToRAI -xyz $del -or $v1_orient`
   #echo "del_RAI: $del_RAI"
   
SETCHILDREN:
foreach v1foli ( $v1_list )
   set v1fol_vw = `@GetAfniView $v1foli`
   set v1fol_pth = $v1foli:h
   if ("$v1fol_pth" == "$v1foli") then 
      set v1fol_pth = .
   endif

   set v1fol_pref = `@GetAfniPrefix $v1foli`
   set v1fol = $v1fol_pth/$v1fol_pref$v1fol_vw

   if ("`@GetAfniID $v1fol`" != "`@GetAfniID $v1i`" && "`@GetAfniID $v1fol`" != "`@GetAfniID $v2i`") then
      #orientation of v1fol
      set v1fol_orient = `@GetAfniOrient $v1fol`
      #change del_RAI to v1fol_orient
      set del = `@FromRAI -xyz $del_RAI -or $v1fol_orient`
      echo $del

      #multiply deltas by axis signs
      set mp = `@AfniOrientSign $v1_orient`
      set del_refit = ($del)
      foreach i ( 1 2 3)
            set del_refit[$i] = `ccalc -form int -expr "$mp[$i] * $del[$i]"`
      end

      #apply the  delta
      if ($usecp == 1) then
         if ( `@CheckForAfniDset $v1fol_pth/${v1fol_pref}_shft$v1fol_vw` != 0 ) then
            echo ""
            echo "Error `basename $0`"
            echo "Dset $v1fol_pth/${v1fol_pref}_shft$v1fol_vw found, cleanup first!"
            echo ""
            goto END
         endif
         3dcopy $v1fol  $v1fol_pth/${v1fol_pref}_shft
         3drefit -dxorigin $del_refit[1] -dyorigin $del_refit[2] -dzorigin $del_refit[3] $v1fol_pth/${v1fol_pref}_shft$v1fol_vw
      else   
         3drefit -dxorigin $del_refit[1] -dyorigin $del_refit[2] -dzorigin $del_refit[3] $v1fol
      endif
   else
      echo "Child dset $v1fol is same as $v1i or $v2i, skipping it."
   endif
end   

goto END  


HELP:
   echo ""
   echo "Usage: `basename $0` <-base BASE> <-dset DSET> [-no_cp] [-child CHILD_2 ... CHILD_N] "
   echo ""
   echo "   Moves the center of DSET to the center of BASE."
   echo "   -base BASE: Base volume, typically a template."
   echo "   -dset DSET: Typically an anatomical dset to be"
   echo "               aligned to BASE."
   echo "   -child CHILD_'*': A bunch of datasets, originally"
   echo "                     in register with DSET, that"
   echo "                     should be shifted in the same"
   echo "                     way."
   echo "   -no_cp: Do not create new data, shift existing ones"
   echo "           This is a good option if you know what you "
   echo "           are doing. It will save you a lot of space."
   echo "           See NOTE below before using it."
   echo "    DSET and CHILD_'*' are typically all the datasets "
   echo "    from a particular scanning session that"
   echo "    you want to eventually align to BASE."
   echo "    Such an operation is needed when DSET and CHILD_'*'"
   echo "    overlap very little, if at all with BASE"
   echo ""
   echo " Note that you can specify *.HEAD for the children even "
   echo " if the wildcard substitution would contain DSET "
   echo " and possibly even BASE. The script will not process"
   echo " a dataset twice in one execution."
   echo ""
   echo "   See also @Center_Distance"
   echo ""
   echo " NOTE: Running the script multiple times on the same data"
   echo "       will cause a lot of trouble. That is why the default"
   echo "       is to create new datasets as opposed to shifting the"
   echo "       existing ones. Do not use -no_cp unless you know what"
   echo "       you are doing."
   echo "       To undo errors caused by repeated executions"
   echo "       look at the history of each dset and undo"
   echo "       the excess 3drefit operations."
   echo ""
   echo "Requires 3drefit newer than Oct. 02/02."
   echo ""
   echo "Ziad Saad (ziad@nih.gov)"
   echo "SSCC/NIMH/ National Institutes of Health, Bethesda Maryland"
   echo ""
   goto END


goto END

 
END:


syntax highlighted by Code2HTML, v. 0.9.1