#!/bin/tcsh -fe


PARSE: 
   set Narg = $#
   set cnt = 1
   set p0 = 2
   set p1 = 98
   set min = 0
   set max = 255
   set invol = ""
   set opref = ""
   set scale = range
   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]" == "-scale_by_mean") then
         set pLoc = $cnt 
         set scale = 'mean'
         set donext = 0     
      endif
      if ($donext && "$argv[$cnt]" == "-scale_by_median") then
         set pLoc = $cnt 
         set scale = 'median'
         set donext = 0     
      endif
      if ($donext && "$argv[$cnt]" == "-norm") then
         set pLoc = $cnt 
         set scale = 'norm'
         set donext = 0     
      endif
      
      if ($donext && "$argv[$cnt]" == "-perc_clip") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need 2 values between 0 and 100 after -perc_clip"
            goto END
         else
            @ cnt ++
            set p0 = "$argv[$cnt]"
            if ($cnt == $Narg) then
               echo "Need 2 values between 0 and 100 after -perc_clip"
               goto END
            endif
            @ cnt ++
            set p1 = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif
      
      if ($donext && "$argv[$cnt]" == "-val_clip") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need 2 values after -val_clip"
            goto END
         else
            @ cnt ++
            set min = "$argv[$cnt]"
            if ($cnt == $Narg) then
               echo "Need 2 values between 0 and 100 after -val_clip"
               goto END
            endif
            @ cnt ++
            set max = "$argv[$cnt]"
            set donext = 0   
         endif   
      endif
      
      if ($donext && "$argv[$cnt]" == "-input") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need 1 dset after -input"
            goto END
         else
            @ cnt ++
            set invol = "$argv[$cnt]"
            set donext = 0   
         endif
      endif
      
      if ($donext && "$argv[$cnt]" == "-prefix") then
         set pLoc = $cnt      
         if ($pLoc == $Narg) then
            echo "Need a prefix after -prefix"
            goto END
         else
            @ cnt ++
            set opref = "$argv[$cnt]"
            set donext = 0   
         endif
      endif
      
      if ($donext == 1) then
         echo "Error: Option or parameter '$argv[$cnt]' not understood"
         goto END
      endif
      @ cnt ++
   end

      
set ps = `ccalc -i  -expr "($p1 - $p0)"` 
set ipref = `@GetAfniPrefix $invol`
if ($opref == "") set opref = $ipref.sc
if ($invol == "") then
   echo "Error: No input volume"
   echo ""
   goto END
endif

if (`3dnvals $invol` > 1 && "$scale" == 'range') then
   echo "Error `basename $0`"
   echo "Script operates on datasets with one input only"
   echo ""
   goto END
endif

if ($scale == 'range') then
   goto DOIT
else if ($scale == 'median' || $scale == 'mean') then
   goto DOIT_DIV
else if ($scale == 'norm') then
   goto DOIT_NORM
else
   echo "Don't know how to scale by $scale"
   goto END
endif

DOIT:
   set nvals = `3dnvals -all $invol`
   if ($nvals[4] > 1) then
      echo "Not ready to deal with multiple sub-bricks"
      goto END
   endif
   set perc = `3dBrickStat -non-zero -percentile $p0 $ps $p1 $invol`
   echo $perc
   set v0 = $perc[2]
   set v1 = $perc[4]

   #In one step
   3dcalc   -a $invol -prefix $opref  \
            -expr "step(a) * \
                   ( \
                     ( ($v0 * (1-step(a-$v0)) + \
                        $v1 * (  step(a-$v1)) + \
                        step(a-$v0)*(1-step(a-$v1))*a) - $v0 ) / \
                     ($v1-$v0)*($max-$min)+$min \
                   )    "

   goto END

   #Two steps would be
   3dcalc   -a $invol -prefix sss  \
            -expr "step(a) * \
                   ( $v0 * (step($v0-a)) + \
                     $v1 * (step(a-$v1)) + \
                     step(a-$v0)*step($v1-a)*a )"  
   3dcalc   -a sss+orig  \
            -expr "step(a) * \
                   ( (a - $v0)/($v1-$v0)*($max-$min)+$min )"   \
            -overwrite -prefix sss

   goto END

DOIT_NORM:
   set nvals = `3dnvals -all $invol`
   set vvv = `@parse_afni_name $invol`
   set vvv = $vvv[3]
   3dTstat -mean -stdev -prefix ___ts $invol
   3dcalc -a $invol -b ___ts$vvv"[0]" -c ___ts$vvv"[1]" \
            -expr '(a-b)/c' -prefix $opref
   rm -f ___ts$vvv.*
   
   goto END
   
DOIT_DIV:
   set nvals = `3dnvals -all $invol`
   set cnt = 0
   rm -f scl.$opref.1D >& /dev/null &
   if ("$scale" == "mean") then
      while ($cnt < $nvals[4])
       set scl = `3dBrickStat -non-zero -mean $invol"[$cnt]"`
       echo $scl
       if ($scl[1] =~ 'nan') then
         echo 0 >> scl.$opref.1D
       else
        echo $scl[1] >> scl.$opref.1D 
       endif
       @ cnt ++
      end
   else
      while ($cnt < $nvals[4])
       set scl = `3dBrickStat -non-zero -median $invol"[$cnt]"`
       echo $scl
       if ($scl[2] =~ 'nan') then
         echo 0 >> scl.$opref.1D
       else
         echo $scl[2] >> scl.$opref.1D
       endif
       @ cnt ++
      end
   endif
   3dcalc   -a $invol -prefix $opref  \
            -b scl.$opref.1D   \
            -expr "bool(b)*a/b"
   
   goto END
   
HELP:
   echo ""
   echo "Usage: `basename $0` <-input DSET> <-prefix PREFIX>"
   echo "                     [-perc_clip P0 P1] [-val_clip V0 V1]"
   echo "                     [-scale_by_mean] [-scale_by_median]"
   echo "                     [-norm] "    
   echo "Method 1: (default)"
   echo "Scale a volume so that its values range between V0 and V1"
   echo "-val_clip V0 V1: Min and Max of output dset"
   echo "                 Default V0 = $min and V1 = $max"
   echo "-perc_clip P0 P1: Set lowest P0 percentile to Min "
   echo "                  and highest P1 percentile to Max"
   echo "                  Default P0 = $p0 and P1 = $p1"
   echo ""
   echo "At the moment, Method 1 only operates on volumes with one sub-brick"
   echo ""
   echo "Method 2:"
   echo "-scale_by_mean: Divide each sub-brick by mean of non-zero voxels"
   echo "-scale_by_median: Divide each sub-brick by median of non-zero voxels"
   echo ""
   echo "Method 3:"
   echo "-norm: For each time series T, Tnorm= (T-mean(T))/stdev(T)"
   echo ""
   goto END
         
END:
