#!/bin/sh
## \section{kmeans\_classify (shell script)}
## 
## \subsection*{Purpose}
## Classify with the K-means algorithm
##
## \subsection*{Description}
## Converts dataformats and calls the K-means algorithm of the Khoros
## package.
## 
## \subsection*{Usage}
## \begin{verbatim}
## kmeans_classify [-dec] 
##    [-sn structure number <integer>]
##    [-ff feature file <filename>]
##    [-cf classified file <filename>]
##    [-sf statistics file <filename>]
##    [-classes number of classes <int>]
## \end{verbatim}
## \begin{description}
##   \item[-U]
## 	Print usage information
##   \item[-sn]
## 	filter structure number
##   \item[-dec]
## 	decimate the filter output?
##   \item[-ff]
## 	feature file (HIPS image (sequence))
##   \item[-cf]
## 	classified (output) file (HIPS image)
##   \item[-sf]
## 	output statistics file
##   \item[-classes]
## 	number of classes (default 2)
## \end{description}
## 
## \subsection*{Date created}
## March 1994

# Default parameters:
DEC=FALSE

# Temporary files:
CLASSIFIED_RAW=`tempnam`
CLASSIFIED2_VIFF=`tempnam`
TMP_FEATURE=`tempnam`
FEATURE_RAW=`tempnam`
ROW_FILE=`tempnam`
COL_FILE=`tempnam`
OBJ_FILE=`tempnam`

# Temporary files used in calling remote (sun 4) vkmeans program
FEATURE_VIFF=.tmp.feature.viff
CLASSIFIED_VIFF=.tmp.class.viff
TMP_STAT_FILE=.tmp.stat.viff

print_usage ()
{
  # Print usage
  echo "som_classify"
  echo "\tOptions:"
  echo "\t  -U		-- print usage"
  echo "\t  -sn ##	-- filter structure number"
  echo "\t  -dec @@   	-- decimated feature file (TRUE or FALSE)"
  echo "\t  -ff @@  	-- input feature file"
  echo "\t  -cf @@  	-- output classified file"
  echo "\t  -sf @@  	-- output statistics file"
  echo "\t  -classes ##	-- number of classes in the problem"
  exit 1
}


clean ()
{
  rm -f ${HOME}/${FEATURE_VIFF} $CLASSIFIED2_VIFF
  rm -f $CLASSIFIED_RAW ${HOME}/${CLASSIFIED_VIFF}
  rm -f $TMP_FEATURE $FEATURE_RAW $OBJ_FILE $ROW_FILE $COL_FILE
}



classification ()
{
  echo "+-----------------------------------------------------------------+"
  echo "| Classify with the K-means algorithm                             |"
  echo "+-----------------------------------------------------------------+"

  if [ $DEC = TRUE ] ; then
    dec2seq -sn $STRUCT $FEATURE_FILE > $TMP_FEATURE
  else
    ln -s $FEATURE_FILE $TMP_FEATURE
  fi
  ROWS=`hsize -rows $FEATURE_FILE`
  COLS=`hsize -cols $FEATURE_FILE`
  DEC_ROWS=`hsize -rows $TMP_FEATURE`
  DEC_COLS=`hsize -cols $TMP_FEATURE`
  DEC_FRAMES=`hsize -frames $TMP_FEATURE`
  calcpix -o $OBJ_FILE -s "opix=r*256/${DEC_ROWS}" -c $DEC_ROWS $DEC_COLS \
	> $ROW_FILE
  calcpix -o $OBJ_FILE -s "opix=c*256/${DEC_COLS}" -c $DEC_ROWS $DEC_COLS \
	> $COL_FILE

  # Convert to VIFF format
  catframes $TMP_FEATURE $ROW_FILE $COL_FILE | htof | \
	stripheader > $FEATURE_RAW
  raw2viff -i $FEATURE_RAW -o ${HOME}/${FEATURE_VIFF} \
	-r ${DEC_ROWS} -c ${DEC_COLS}  -t float -b ${DEC_FRAMES}
  
  # Classify
  rsh jo4 "k-means-rsh -ff $FEATURE_VIFF -cf $CLASSIFIED_VIFF \
		-sf $TMP_STAT_FILE -classes $CLASSES "
  mv ${HOME}/${TMP_STAT_FILE} $STAT_FILE

  # Convert to HIPS format
  vremmap -i ${HOME}/$CLASSIFIED_VIFF -o $CLASSIFIED2_VIFF
  vconvert -i $CLASSIFIED2_VIFF -o $CLASSIFIED2_VIFF -t byte
  viff2raw -i $CLASSIFIED2_VIFF -o $CLASSIFIED_RAW
  raw2hips -s $DEC_ROWS $DEC_COLS < $CLASSIFIED_RAW | \
	enlarge -s `calc "${ROWS}/${DEC_ROWS}"` > ${CLASS_FILE}
}



summary ()
{
  echo "	Classifier			K-means"      >  ${STAT_FILE}
  echo "	Classified datafile:            ${CLASS_FILE}">> ${STAT_FILE}
}




# M A I N   P R O G R A M

until [ $# -eq 0 ]; do
  case "${1}" in
    -U		) shift; print_usage ;;
    -sn		) shift; STRUCT="${1}";		shift;;
    -dec	) shift; if [ "${1}" = "TRUE" -o "${1}" = "FALSE" ] ; then
			   DEC=${1}; shift ;
			 else
			   DEC=TRUE
			 fi;;
    -classes	) shift; CLASSES=${1};		shift;;
    -ff		) shift; FEATURE_FILE=${1};	shift;;
    -cf		) shift; CLASS_FILE=${1};	shift;;
    -sf		) shift; STAT_FILE=${1};	shift;;
    *		) shift; print_usage ;;
  esac
done


classification
summary
clean
