#!/bin/tcsh -f
set estat = 1
set eee = `date '+%H %M %S'`
goto PARSE

DEED:
set inpref = `@GetAfniPrefix $input`

if (`@CheckForAfniDset ${inpref}+tlrc` > 0) then 
   set ttpref = ${inpref}
   echo "Have TLRC anat in ${inpref}+tlrc"
   goto CALC_DEED
endif
 
if ($has_skull) then
   if ($ShowTime) echo "Stripping Anat at `date '+%H:%M:%S'`"
   if (`@CheckForAfniDset nosk.${inpref}+orig` < 2) then
      3dSkullStrip -no_pushout -no_use_edge \
                        -ld 20 -o_ply nosk.${inpref}\
                        -prefix nosk.${inpref}  -perc_int 0.1 \
                        -overwrite  \
                        -input ${inpref}+orig   >>& log.tlrc.${prefix}
   else
      echo "Reusing nosk.${inpref}+orig.HEAD"
   endif
   set sspref = nosk.${inpref}
else
   set sspref = ${inpref}
   echo "Have SkullStripped anat in ${sspref}+orig"
endif

#Now tlrc
if ($ShowTime) echo "TLRC transformation at `date '+%H:%M:%S'`"
if (`@CheckForAfniDset ${sspref}+tlrc` < 2) then
   @auto_tlrc -base $basetlrc \
                  -input ${sspref}+orig. \
                  -no_ss $npass -suffix NONE >>& log.tlrc.${prefix}
else
   echo "Reusing ${sspref}+tlrc.HEAD"
endif

if ( `@CheckForAfniDset ${sspref}+tlrc` != 2 ) then
   echo "Error: Failed to create tlrc anat"
   echo "       see log.tlrc.${prefix} for details"
   echo "If failure is due to lack of convergence, try"
   echo "to add the -twopass option to @fast_roi"
   echo ""
   goto END
endif 
set ttpref = ${sspref}

CALC_DEED:
#Now create a mask of a particular region (see whereami -help for details):
if ( $symbolic_roi) then
   if ($ShowTime) echo "TLRC mask generation at `date '+%H:%M:%S'`"
   set noglob
   3dcalc   $regionlist  \
                  -expr "$eq" \
                  -prefix ROIt.${prefix} -overwrite >>& log.tlrc.${prefix}
   unset noglob                  
endif         

#Now put the mask in orig space 
if ($ShowTime) echo "Orig mask generation at `date '+%H:%M:%S'`"
if ( $symbolic_roi) then
   3dfractionize -template $mask_grid  \
                     -input ROIt.${prefix}+tlrc   \
                     -prefix ROI.${prefix}  \
                     -warp  ${ttpref}+tlrc  \
                     -preserve -overwrite \
                     -clip 0.5 >>& log.tlrc.${prefix}
   if ( $status ) then
      echo "Failed running 3dfractionize. See log.tlrc.${prefix} for details."
      goto END
   endif
endif
if ( $drawn_roi != "") then
   3dfractionize -template $mask_grid  \
                     -input $drawn_roi   \
                     -prefix ___ddd  \
                     -warp  ${ttpref}+tlrc  \
                     -preserve -overwrite \
                     -clip 0.5 >>& log.tlrc.${prefix}
   if ( $status ) then
      echo "Failed running 3dfractionize. See log.tlrc.${prefix} for details."
      goto END
   endif
   if (`@CheckForAfniDset ROI.${prefix}+orig` == 2 ) then
      3drename ROI.${prefix}+orig ___tmp >>& log.tlrc.${prefix}
      3dcalc   -a ___tmp+orig -b ___ddd+orig -expr 'a+b' \
               -prefix ROI.${prefix} >>& log.tlrc.${prefix}
   else
      3drename ___ddd+orig ROI.${prefix} >>& log.tlrc.${prefix}
   endif
endif

rm -f ___tmp* >& /dev/null
rm -f ___ddd* >& /dev/null

set eee2 = `date '+%H %M %S'`
set etime = `ccalc "    ($eee2[1] - $eee[1])*3600 \
                     +  ($eee2[2] - $eee[2])*60 \
                     +  ($eee2[3] - $eee[3])  "`
set et = ( 0 0 0 )
set et[1] = `ccalc -fint " $etime / 3600"`
set et[2] = `ccalc -fint " ( $etime - $et[1] * 3600 ) / 60"`
set et[3] = `ccalc -fint " ( $etime - $et[1] * 3600 - $et[2] * 60 ) "`

if ($ShowTime) echo "Elapsed time $et[1]h:$et[2]m:$et[3]s"
set estat = 0
goto END

HELP:
   echo  "Usage: `basename $0` <-region REGION1> [<-region REGION2> ...]"
   echo  "                     <-base TLRC_BASE> <-anat ANAT> "
   echo  "                     <-roi_grid GRID >"
   echo  "                     <-prefix PREFIX >"
   echo  "                     [-time] [-twopass] [-help]"
   echo  "Creates Atlas-based ROI masked in ANAT's original space."
   echo  "The script is meant to work rapidly for realtime fmri applications"
   echo  "Parameters:"
   echo  "  -region REGION: Symbolic atlas-based region name. "
   echo  "                  See whereami -help for details."
   echo  "                 You can use repeated instances of this option"
   echo  "                 to specify a mask of numerous regions."
   echo  "                 Each region is assigned a power of 2 integer"
   echo  "                 in the output mask"
   echo  "  -drawn_roi ROI+tlrc: A user drawn ROI in standard (tlrc) space."
   echo  "                       This ROI gets added with the REGION roi."
   echo  ""
   echo  "  -anat ANAT: Anat is the volume to be put in std space. It does not"
   echo  "              need to be a T1 weighted volume but you need to choose"
   echo  "              a similarly weighted TLRC_BASE."
   echo  "              If ANAT is already in TLRC space then there is no need"
   echo  "              for -base option below."
   echo  "  -anat_ns ANAT: Same as above, but it indicates that the skull"
   echo  "                 has been removed already."
   echo  "  -base TLRC_BASE:  Name of reference TLRC volume. See @auto_tlrc"
   echo  "                    for more details on this option. Note that"
   echo  "                    for the purposes of speeding up the process,"
   echo  "                    you might want to create a lower resolution"
   echo  "                    version of the templates in the AFNI. In the"
   echo  "                    example shown below, TT_N27_r2+tlrc was created"
   echo  "                    with: "
   echo  "           3dresample  -dxyz 2 2 2 -rmode Li -prefix ./TT_N27_r2 \"
   echo  "                       -input `@GetAfniBin`/TT_N27+tlrc. "
   echo  "                    where TT_N27+tlrc is usually in the directory "
   echo  "                    under which afni resides."
   echo  "  -roi_grid GRID: The volume that defines the final ROI's grid."
   echo  "  -prefix PREFIX: PREFIX is used to tag the names the ROIs output."
   echo  "  -time: A flag to make the script output elapsed time reports."
   echo  "  -twopass: Make TLRC transformation more robust. Use it if TLRC "
   echo  "            transform step fails."
   echo  "  -help: Output this message."
   echo  ""
   echo  "The ROI of interest is in a volume called ROI.PREFIX+orig."
   echo  ""
   echo  "The script follows the following steps:"
   echo  "  1- Strip skull off of ANAT+orig "
   echo  "     Output is called nosk.ANAT+orig and is reused if present."
   echo  "  2- Transform nosk.ANAT+orig to TLRC space."
   echo  "     Output is called nosk.ANAT+tlrc and is reused if present." 
   echo  "  3- Create ROI in TLRC space using 3dcalc."
   echo  "     Output is ROIt.PREFIX+tlrc and is overwritten if present."
   echo  "  4- Create ROI in GRID's orig space using 3dFractionize."
   echo  "     Output is ROI.PREFIX+orig and is overwritten if present."
   echo  ""
   echo  "Examples ( require AFNI_data3/afni, and "
   echo  "           3dresample's output from command shown above):"
   echo  "     @fast_roi  -region CA_N27_ML::Hip -region CA_N27_ML::Amygda \"
   echo  "                 -base TT_N27_r2+tlrc. -anat anat1+orig.HEAD  \"
   echo  "                 -roi_grid epi_r1+orig -prefix toy -time"
   echo  ""
   echo  "    If you want another ROI given the same -anat and -base volumes:"
   echo  "     @fast_roi  -region CA_N27_ML::Superior_Temporal_Gyrus \"
   echo  "                 -region CA_N27_ML::Putamen \"
   echo  "                 -base TT_N27_r2+tlrc. -anat anat1+orig.HEAD  \"
   echo  "                 -roi_grid epi_r1+orig -prefix toy -time"
   echo  ""
   set estat = 0
   goto END
   
PARSE:
   set cnum = 140
   set npass = '-onepass'
   set input = ""
   set basetlrc = "TT_N27+tlrc"
   set region1 = 'CA_N27_ML::Amygda'
   set region2 = 'CA_N27_ML::Hip'
   set mask_grid = "" 
   set Narg = $#
   set prefix = "fast_roi"
   set cnt = 1
   set regionlist = ()
   set valregionlist = ()
   set eq = (" (0 ") 
   set ShowTime = 0
   set has_skull = 1
   set symbolic_roi = 0
   set drawn_roi = '' 
   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]" == "-prefix") then
         set pLoc = $cnt      
         if ($pLoc  == $Narg) then
            echo "Need string after -prefix"
            goto END
         else
            @ cnt ++
            set prefix = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif

      if ($donext && "$argv[$cnt]" == "-base") then
         set pLoc = $cnt      
         if ($pLoc  == $Narg) then
            echo "Need volume after -base"
            goto END
         else
            @ cnt ++
            set basetlrc = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif
      if ($donext && "$argv[$cnt]" == "-drawn_roi") then
         set pLoc = $cnt      
         if ($pLoc  == $Narg) then
            echo "Need volume after -drawn_roi"
            goto END
         else
            @ cnt ++
            set drawn_roi = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif
      
      if ($donext && "$argv[$cnt]" == "-roi_grid") then
         set pLoc = $cnt      
         if ($pLoc  == $Narg) then
            echo "Need volume after -roi_grid"
            goto END
         else
            @ cnt ++
            set mask_grid = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif
      if ($donext && "$argv[$cnt]" == "-anat") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need volume after -anat"
            goto END
         else
            @ cnt ++
            set input = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif
      if ($donext && "$argv[$cnt]" == "-anat_ns") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need volume after -anat_ns"
            goto END
         else
            @ cnt ++
            set input = "$argv[$cnt]"
            set has_skull = 0
            set donext = 0   
         endif   
      endif
      if ($donext && "$argv[$cnt]" == "-region") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need region name after -region"
            goto END
         else
            @ cnt ++
            set symbolic_roi  = 1
            set cnum = `expr $cnum + 1`
            set varname = `printf "\$cnum" `
            set regionlist = ($regionlist `printf "%s\$cnum %s" '-' $argv[$cnt]`)
            set eq = ("$eq" " + $varname " "*" " (2**$#valregionlist)") 
            set valregionlist  = ($valregionlist `ccalc "2**($#valregionlist)"`)
            set donext = 0  
         endif   
      endif
      
      if ($donext && "$argv[$cnt]" == "-time") then
         set ShowTime = 1
         set donext = 0   
      endif
      
      if ($donext && "$argv[$cnt]" == "-twopass") then
         set npass = '-twopass'
         set donext = 0   
      endif
      
      if ($donext == 1) then
         echo "Error: Option or parameter '$argv[$cnt]' not understood"
         goto END
      endif
      @ cnt ++
   end
   set eq = ("$eq )")
   #echo "$eq"
   if ($drawn_roi == '' && $symbolic_roi == 0) then
      echo "Error: No ROIs to create. Use -region or -drawn_roi options."
      echo "See -help for details."
      goto END
   endif
   if ($mask_grid == '') then
      echo "Error: No -roid_grid specified. See -help for options."
      goto END
   endif
   goto DEED

END:
exit $estat
