#!/bin/tcsh -f
set stat = 0
set RNS = `3dnewid -fun`
set log = /tmp/${RNS}.MLT.log

if ("$1" == "" || "$1" == '-h' || "$1" == '-help') then
   goto HELP
endif

goto PARSE
RETURN_PARSE:

if ($ltdset != '') then
    3dAttribute -ssep ' '  -name  VALUE_LABEL_DTABLE $ltdset \
      | sed 's/^VALUE_LABEL_DTABLE = //' 
    if ($status) then
      if (! $quiet_death) echo "Failed to find VALUE_LABEL_DTABLE"
      goto BEND
    endif
    goto END
endif

if ($showlabels) then
   if ($word_match == 1) set gopt = ("$gopt" "-w")
   if ($KeysLabel != '') then
      niccc -quiet '-#' -stdout file:$labeltable \
         | \grep $gopt $KeysLabel \
         | \sed 's/^ //g' | \sed 's/"//g' \
         | \cut -d ' ' -f 2
   else
      niccc -quiet '-#' -stdout file:$labeltable \
         | \grep -v '^#' | sed '/^$/d' \
         | \sed 's/^ //g' | \sed 's/"//g' \
         | \cut -d ' ' -f 2
   endif
   goto END
endif

if ($showkeys) then
   if ($word_match == 1) set gopt = ("$gopt" "-w")
   if ($KeysLabel != '') then
      niccc -quiet '-#' -stdout file:$labeltable \
         | \grep $gopt $KeysLabel \
         | \sed 's/^ //g' | \sed 's/"//g' \
         | \cut -d ' ' -f 1
   else
      niccc -quiet '-#' -stdout file:$labeltable \
         | \grep -v '^#' | sed '/^$/d' \
         | \sed 's/^ //g' | \sed 's/"//g' \
         | \cut -d ' ' -f 1
   endif
   goto END
endif

if ($maxkey) then
   niccc -quiet '-#' -stdout file:$labeltable \
         | \grep -v '^#' \
         | \sed 's/^ //g' | \sed 's/"//g' \
         | \cut -d ' ' -f 1 > __kk.1D
   set maxkey = `3dBrickStat -slow -max __kk.1D`
   \rm -f __kk.1D
   echo $maxkey
   goto END
endif


if ($KeysLabel != '') then
   if (! -f $labeltable) then
      if (! $quiet_death) echo "Error: $labeltable not found"
      goto BEND
   endif
   
   #get the numbers
   if ($word_match == 1) set gopt = ("$gopt" "-w")
   set keys = `niccc -quiet '-#' -stdout file:$labeltable \
      | \grep $gopt $KeysLabel \
      | \sed 's/^ //g' | \sed 's/"//g' \
      | \cut -d ' ' -f 1`
   
   if ($#keys < 1) then
      if (! $quiet_death) \
         echo "Error: Label $KeysLabel has no match in labeltable $labeltable"
      goto BEND
   endif
   
   if ($range) then
      set range = `3dBrickStat -slow -min -max  "1D:$keys"`
      echo $range
   else
      echo $keys
   endif
   goto END
endif

if ($LabelKey != '') then
   if (! -f $labeltable) then
      if (! $quiet_death) echo "Error: $labeltable not found"
      goto BEND
   endif
   
   #get the numbers
   set labels = `niccc -quiet '-#' -stdout file:$labeltable \
      | \grep '"'$LabelKey'"' \
      | \sed 's/^ //g' | \sed 's/"//g' \
      | \cut -d ' ' -f 2`
   
   if ($#labels < 1) then
      if (! $quiet_death) \
         echo "Error: Key $LabelKey has no match in labeltable $labeltable"
      goto BEND
   else if ($#labels > 1) then
      if (! $quiet_death)  \
echo "Error: Key $LabelKey has $#labels matches in labeltable $labeltable"
      goto BEND
   else
      echo $labels
   endif
   
   goto END
endif

MAKE_TABLE:
   set max_label = `1dcat "1D:$llistmax" | sort -rn`
   set max_label = $max_label[1]
   if ($max_label < 32) then
      set pbar = "ROI_i32"
   else if ($max_label < 64) then
      set pbar = "ROI_i64"
   else if ($max_label < 128) then
      set pbar = "ROI_i128"
   else
      set pbar = "ROI_i256"
   endif
   
   if ( -f /tmp/${RNS}.___lt.txt) rm -f /tmp/${RNS}.___lt.txt
   if ( -f /tmp/${RNS}.___ll.txt) rm -f /tmp/${RNS}.___ll.txt
   if ( -f /tmp/${RNS}.___kk.txt) rm -f /tmp/${RNS}.___kk.txt
   set cnt = 1
   foreach ll ($llist)
      set inds = `count -digits 1 $llistmin[$cnt] $llistmax[$cnt]`
      if ($#inds > 1) then
         set ci = 1
         foreach ind ($inds)
            printf '"%d" "%s%02d"\n' $ind $llist[$cnt] $ci  \
               >> /tmp/${RNS}.___lt.txt
            printf '%d\n' $ind >> /tmp/${RNS}.___kk.txt
            printf '%s%02d\n' $llist[$cnt] $ci >> /tmp/${RNS}.___ll.txt
            @ ci ++
         end
      else
         printf '"%d" "%s"\n' $llistmin[$cnt] $llist[$cnt] \
               >> /tmp/${RNS}.___lt.txt
         printf '%d\n' $llistmin[$cnt] >> /tmp/${RNS}.___kk.txt
         printf '%s\n' $llist[$cnt] >> /tmp/${RNS}.___ll.txt
      endif
      @ cnt ++
   end
   
   set n = `wc -l /tmp/${RNS}.___ll.txt`
   set n = $n[1]
   uniq /tmp/${RNS}.___ll.txt > /tmp/${RNS}.___llu.txt
   set nu = `wc -l /tmp/${RNS}.___llu.txt`
   set nu = $nu[1]
   if ($nu < $n) then
      if (! $quiet_death) \
         echo "ERROR: `ccalc -i $n -$nu` Labels dropped because of duplicates"
      goto BEND
   endif
   set n = `wc -l /tmp/${RNS}.___kk.txt`
   set n = $n[1]
   uniq /tmp/${RNS}.___kk.txt > /tmp/${RNS}.___kku.txt
   set nu = `wc -l /tmp/${RNS}.___kku.txt`
   set nu = $nu[1]
   if ($nu < $n) then
      if (! $quiet_death) \
         echo "ERROR: `ccalc -i $n -$nu` Keys dropped because of duplicates"
      goto BEND
   endif
   
   
      printf '<VALUE_LABEL_DTABLE\n' > $labeltable
      printf 'ni_type="2*String"\n' >> $labeltable
      printf 'ni_dimen="%d"\n' $nu[1] >> $labeltable
      printf 'pbar_name="%s">\n' $pbar >> $labeltable
      uniq /tmp/${RNS}.___lt.txt >> $labeltable
      printf '</VALUE_LABEL_DTABLE>\n' >> $labeltable

PUT_TABLE:
   if ($target_dset != '') then
      3drefit -labeltable $labeltable $target_dset
   endif
   
goto END

PARSE:
   set labeltable = 'LABEL_TABLE_NOT_SET'
   set llist = ''
   set llistmin = ''
   set llistmax = ''
	set Narg = $#
   set target_dset = ''
   set KeysLabel = ''
   set LabelKey = ''
   set verb = 0
   set range = 0
   set showlabels = 0
   set showkeys = 0
   set ltdset = ''
   set quiet_death = 0
   set maxkey = 0
   set word_match = 0
   set gopt = ()
	set cnt = 1
   while ($cnt <= $Narg)
      if ("$argv[$cnt]" == "-d" || "$argv[$cnt]" == "-echo") then
         set verb = 1
         set echo
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-verb") then
         set verb = 1
         goto CONTINUE
      endif
      if ("$argv[$cnt]" == "-max_key") then
         set maxkey = 1
         goto CONTINUE
      endif
      if ("$argv[$cnt]" == "-quiet_death") then
         set quiet_death = 1
         goto CONTINUE
      endif
      if ("$argv[$cnt]" == "-labeltable_of_dset") then
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				if (! $quiet_death) echo "Need label after -labeltable_of_dset"
            goto BEND
			else
				@ cnt ++
				set ltdset = "$argv[$cnt]"
            if (`@CheckForAfniDset $ltdset ` < 1) then
               if (! $quiet_death) echo "Failed to find $ltdset"
               goto BEND
            endif 
			endif
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-all_labels") then
         set showlabels = 1
         set KeysLabel = ""
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-all_keys") then
         set showkeys = 1
         set KeysLabel = ""
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-match_label") then
         set showlabels = 1
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				if (! $quiet_death) echo "Need label after -match_label"
            goto BEND
			else
				@ cnt ++
				set KeysLabel = "$argv[$cnt]"
			endif
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-word_label_match") then
         set word_match = 1
         set SubLoc = $cnt		
         goto CONTINUE
      endif

      if ("$argv[$cnt]" == "-labeltable") then
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				if (! $quiet_death) echo "Need file after -labeltable"
            goto BEND
			else
				@ cnt ++
				set labeltable = `@NoExt "$argv[$cnt]" .niml .niml.lt`
            set labeltable = $labeltable.niml.lt
			endif
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-klabel") then
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				if (! $quiet_death) echo "Need LABEL after -klabel"
            goto BEND
			else
				@ cnt ++
				set LabelKey = `printf %d "$argv[$cnt]"`
            if ($LabelKey != $argv[$cnt]) then
               echo "$argv[$cnt] is not interpreted as an integer key"
               goto BEND
            endif
			endif
         goto CONTINUE
      endif
      if ("$argv[$cnt]" == "-lkeys") then
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				if (! $quiet_death) echo "Need LABEL after -lkeys"
            goto BEND
			else
				@ cnt ++
				set KeysLabel = "$argv[$cnt]"
			endif
         goto CONTINUE
      endif
      if ("$argv[$cnt]" == "-rkeys") then
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				if (! $quiet_death) echo "Need LABEL after -rkeys"
            goto BEND
			else
				@ cnt ++
				set KeysLabel = "$argv[$cnt]"
            set range = 1
			endif
         goto CONTINUE
      endif
      
		if ("$argv[$cnt]" == "-lab_r" || "$argv[$cnt]" == "-label_range") then
         set nneed = `ccalc -i $cnt + 3`
         if ($nneed > $Narg) then
				if (! $quiet_death) echo "Need 2 values after -label_range"
            goto BEND
			else
				@ cnt ++
            if ($argv[$cnt] =~ -*) then
               if (! $quiet_death) \
                  echo "Label name $argv[$cnt] cannot start with '-'"
               goto BEND
            endif 
            set llist = ($llist $argv[$cnt])
            @ cnt ++
            if ($argv[$cnt] =~ -*) then
               if (! $quiet_death) \
                  echo "Label min range $argv[$cnt] cannot be '-'"
               goto BEND
            endif 
            set llistmin = ($llistmin `ccalc -i $argv[$cnt]`)
            @ cnt ++
            if ($argv[$cnt] =~ -*) then
               if (! $quiet_death) \
                  echo "Label min range $argv[$cnt] cannot be '-'"
               goto BEND
            endif 
            set llistmax = ($llistmax `ccalc -i $argv[$cnt]`)
            if ($llistmax[$#llistmax] == 0 ||   \
                $llistmin[$#llistmin] == 0 ||   \
                $llistmin[$#llistmin] > $llistmax[$#llistmax]) then
               if (! $quiet_death) \
                  printf "Bad range values of [%d %d] for label '%s'\n" \
                   $llistmin[$#llistmin] $llistmax[$#llistmax] $llist[$#llist] 
               goto BEND
            endif
			endif
         goto CONTINUE
      endif
		if (  "$argv[$cnt]" == "-lab" || \
            "$argv[$cnt]" == "-lab_v" || \
            "$argv[$cnt]" == "-label_value") then
         set nneed = `ccalc -i $cnt + 2`
         if ($nneed > $Narg) then
				if (! $quiet_death) \
               echo "Need 1 value after -label_value"
            goto BEND
			else
				@ cnt ++
            if ($argv[$cnt] =~ -*) then
               if (! $quiet_death) \
                  echo "Label name $argv[$cnt] cannot start with '-'"
               goto BEND
            endif 
            set llist = ($llist $argv[$cnt])
            @ cnt ++
            if ($argv[$cnt] =~ -*) then
               if (! $quiet_death) \
                  echo "Label min range $argv[$cnt] cannot be '-'"
               goto BEND
            endif 
            set llistmin = ($llistmin `ccalc -i $argv[$cnt]`)
            set llistmax = ($llistmax `ccalc -i $argv[$cnt]`)
			endif
         goto CONTINUE
      endif
      if ("$argv[$cnt]" == "-dset") then
         if ($cnt > $Narg) then
            if (! $quiet_death) \
               echo "Need a dset after -dset"
            goto BEND
         endif
         @ cnt ++
         set target_dset = $argv[$cnt]
         if (`@CheckForAfniDset $target_dset` < 2) then
            if (! $quiet_death) \
               echo "-dset $target_dset not found"
            goto BEND
         endif 
         goto CONTINUE
      endif 
      
      
      echo "Error: Option or parameter '$argv[$cnt]' not understood"
      goto BEND
      
      CONTINUE:		
		@ cnt ++
	end

 
   
   if (0) then
      echo "Have:"
      echo $llist
      echo $llistmin
      echo $llistmax
   endif
      
   goto RETURN_PARSE

HELP:
   echo ""
   echo "Script used to create a label table "
   echo ""
   echo "Usage: `basename $0` <-labeltable LABELTABLE> "
   echo "                     [<-lab_r LAB MIN MAX> <-lab_r LAB MIN MAX> <...>]"
   echo "                     [<-lab_v LAB KEY> <-lab_v LAB VAL> <...>]"
   echo "                     [<-dset DSET>]"
   echo "   -labeltable LABELTABLE: Name of output label table"
   echo "   -lab_r LAB MIN MAX: Define a label LAB its minimum key MIN"
   echo "                       and its maximum value MAX. "
   echo "              For example: -lab_r GM 5 7 generates in the labeltable:"
   echo '                           "5" "GM01"'
   echo '                           "6" "GM02"'
   echo '                           "7" "GM03"'
   echo "   -lab_v LAB KEY: Define a label LAB and its value KEY"
   echo "              For example: -lab_v  WM 8 generates in the labeltable:"
   echo '                           "8" "WM"'
   echo "   -dset DSET: Attach the label table to dataset DSET"
   echo ""
   echo "   Note that you cannot use the same key for multiple labels."
   echo "   When redundancies occur, the script preserves just one entry."
   echo ""
   echo "   Example 1:"
   echo "      @MakeLabelTable   -lab_r CSF 1 3 -lab_r GM 5 7 -lab_v WM 8  \"
   echo "                        -labeltable example1"
   echo ""
   echo "Usage: `basename $0` <-surf_annot_cmap CMAP> <-sname SNAME>"
   echo "   Return the entries matching NAME (case insensitive, partial match)"
   echo "   from the CMAP file"
   echo "   Example:"
   echo "      @FS_roi_label  -sname occi \"
   echo "                     -surf_annot_cmap lh.aparc.a2005s.annot.1D.cmap "
   echo ""
   echo "Usage mode 2: Get information about an existing labeltable"
   echo "    `basename $0` <-labeltable LABELTABLE> "
   echo "        <[-lkeys LABEL] | [-rkeys LABEL] | [-all_labels] | [-all_keys]>"
   echo ""
   echo "    -all_labels: Return a listing of the labels"
   echo "    -all_keys:  Return a listing of all keys"
   echo "    -lkeys LABEL: Return the keys whose labels match LABEL"
   echo "    -rkeys LABEL: Return the range (min max) of keys whose "
   echo "                  labels match LABEL"
   echo "    -klabel KEY: Return the label associated with KEY"
   echo "    -match_label LABEL: Return labels matching LABEL"
   echo "    -labeltable_of_dset DSET: Dump the labeltable from DSET"
   echo "    -word_label_match: Use word matching (grep's -w )"
   echo "                       With this option, 'Out' matches 'Out.l1'"
   echo "                       but no longer matches 'OutSk'"
   echo "    -quiet_death: Do not give error messages when failing"
   echo "   Options in usage 2 are mutually exclusive"
   echo ""
   
   goto END

BEND:
   set stat = 1
   goto END
      
END:
if ($stat == 0 && -f $log) \rm /tmp/${RNS}.$log:t
exit $stat   

 
