#!/bin/sh

############################################################################
#
# MODULE:       r.reclass.area
# AUTHOR(S):    NRCS
# PURPOSE:      Reclasses a raster map greater or less than user specified area size (in hectares)
# COPYRIGHT:    (C) 1999 by the GRASS Development Team
#
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with GRASS
#               for details.
#
#############################################################################
# 3/2007: added label support MN
# 3/2004: added parser support MN
# 11/2001 added mapset support markus
# 2/2001 fixes markus
# 2000: updated to GRASS 5
# 1998 from NRCS, slightly modified for GRASS 4.2.1

#%Module
#%  description: Reclasses a raster map greater or less than user specified area size (in hectares)
#%  keywords: raster, statistics, aggregation
#%End
#%option
#% key: input
#% type: string
#% gisprompt: old,cell,raster
#% description: raster input map
#% required : yes
#%END
#%option
#% key: lesser
#% type: double
#% description: lesser val option that sets the <= area size limit [hectares]
#%END
#%option
#% key: greater
#% type: double
#% description: greater val option that sets the >= area size limit [hectares]
#%END
#%option
#% key: output
#% type: string
#% gisprompt: new,cell,raster
#% description: reclass raster output map
#% required : yes
#%END

if  [ -z "$GISBASE" ] ; then
 echo "You must be in GRASS GIS to run this program." >&2
 exit 1
fi

if [ "$1" != "@ARGS_PARSED@" ] ; then
  exec g.parser "$0" "$@"
fi

PROG=`basename $0`

#### check if we have awk
if [ ! -x "`which awk`" ] ; then
    echo "$PROG: awk required, please install awk or gawk first" 2>&1
    exit 1
fi

# setting environment, so that awk works properly in all languages
unset LC_ALL
LC_NUMERIC=C
export LC_NUMERIC

infile="$GIS_OPT_INPUT"
outfile="$GIS_OPT_OUTPUT"

g.region -p | head -n 1 |grep 0 > /dev/null
if [ $? -eq 0 ] ; then
 echo ""
 echo "Sorry: xy-locations are not supported."
 echo "Need projected data with grids in meter."
 exit
fi

if [ -n "$GIS_OPT_LESSER" ] ; then
  op=0
  limit=$GIS_OPT_LESSER
fi
if [ -n "$GIS_OPT_GREATER" ] ; then
  op=1
  limit=$GIS_OPT_GREATER
fi
if [ -z "$GIS_OPT_GREATER" -a -z "$GIS_OPT_LESSER" ] ; then
  echo "ERROR: you have to specify either lesser= or greater="
  exit 1
fi
 

file2=$infile.clump.$outfile

eval `g.findfile element=cell file=$infile`
filename="${fullname}"
BASE="${name}"
if [ "$filename" = "" ] ; then
  echo "ERROR: raster map [$infile] does not exist."
  exit 1
else
  infile=$filename
fi

eval `g.findfile element=cell file=$file2`
filename2="${fullname}"
BASE="${name}"
if test "$filename2" ; then
  echo "ERROR: temporal raster map [$filename2] exists."
  exit 1
else
 echo
 echo "Generating a clumped raster file.............................."
 r.clump input=$infile output=$file2
fi

## calculation in acres
#if test "$limit" = ""; then
#echo 
#echo "Generating a reclass rules file by acres"
#r.stats -az in=$file2,$file |awk '{acre=$3 * 0.0002471; printf("%d = %.0f\n",$1,acre)}' >$infile.rules
#else
#  if test $op = 0; then
#echo 
#echo "Generating a reclass rules file by acres less than or equal to $limit"
#  r.stats -az in=$file2,$infile | awk '{limit='$limit'; acre=$3 * 0.0002471; 
#{if (acre <= limit) printf("%d = %d\n",$1,$2)}}' >$infile.rules
#  else
#echo 
#echo "Generating a reclass rules file by acres greater than or equal to $limit"
#  r.stats -az in=$file2,$infile | awk '{limit='$limit'; acre=$3 * 0.0002471; 
#{if (acre >= limit) printf("%d = %d\n",$1,$2)}}' >$infile.rules
#  fi
#fi

## calculation in hectares
#if test "$limit" = ""; then
# echo 
# echo "Generating a reclass rules file categorized by hectares"
# r.stats -an in=$file2,$infile |awk '{hectares=$3 * 0.0001; 
# printf("%d = %d %.4f\n",$1,hectares,hectares)}' > $infile.rules
# else
if test $op = 0; then
   echo 
   echo "Generating a reclass rules file with area size less than or equal to $limit hectares"
   r.stats -aln in=$file2,$infile fs='|' | awk -F'|' '{limit='$limit'; hectares=$5 * 0.0001; 
     {if (hectares <= limit) printf("%d = %d %s\n",$1,$3,$4)}}' > $infile.rules
  else
   echo 
   echo "Generating a reclass rules file with area size greater than or equal to $limit hectares"
   r.stats -aln in=$file2,$infile fs='|' | awk -F'|' '{limit='$limit'; hectares=$5 * 0.0001; 
     {if (hectares >= limit) printf("%d = %d %s\n",$1,$3,$4)}}' > $infile.rules
fi
#fi

if test "$outfile" = ""; then
outfile=${infile}_${limit}
fi
echo
echo "Generating reclass raster map <$outfile>"
cat $infile.rules | r.reclass i=$file2 o=$outfile.recl
r.mapcalc "$outfile=$outfile.recl"
echo "Written: $outfile"
g.remove rast=$outfile.recl,$file2 > /dev/null 2> /dev/null

#####cleanup
rm -f $infile.rules
