"Automatic" Zoom

Hi people,
I need a function, a tool or something else, that allows me to restrict
a region only where there are significant data (i.e. not zero!), without
use d.zoom utility or interactive tools.

Thank you in advance for any suggestion.

-- FRY.

==============================================================
Davide Frisoni | e-mail(internet):
Forschungsinstitut fuer anwendungs- | frisoni@faw.uni-ulm.de
orientierte Wissensverarbeitung FAW |
Helmholzstr. 16 | Tel.: +49-731-501 8626
D-89081 Ulm, Germany | FAX: +49-731-501 999

Hi people,
I need a function, a tool or something else, that allows me to restrict
a region only where there are significant data (i.e. not zero!), without
use d.zoom utility or interactive tools.

Thank you in advance for any suggestion.

-- FRY.

I wrote a little script a couple years ago, that zooms in around specified
categories. Maybe you can get some ideas from this script. In my case the
significant data is the categories that I specify.

Lars

Lars Schylberg Email: lasc@celsiustech.se
CelsiusTech Systems AB or: larss@fmi.kth.se
S-175 88 Jarfalla Tel. +46 8 580 847 03
Sweden Fax. +46 8 580 872 59

------------------------------------------------------------------
r.zoom_cats.sh
------------------------------------------------------------------
#!/bin/sh
#
# r.zoom_cats.sh
#
# Zoom the region setting so that the region have one or a specified
# number of cells free around the max and min coordinates of one or
# several categories.
#
# It's useful for zooming in on one category or many category areas
# that has been creted with the r.clump program for local processing
# of that area.
#
# Author: Lars Schylberg
# email: larss@fmi.kth.se
#
# Date: 920304
#
#------------------------------------------------------------------------
# Check if GRASS is running
#
test "$GISRC" || echo "GRASS is not running" || exit 2
#----------------------------------------------------------------------------
#
# Evaluate arguments
#
if [ $# != 4 ]
then
    echo
    echo Usage: `basename $0`
    echo ' input=mapname '
    echo ' cats=cat[,cat,cat] '
    echo ' border=cells '
    echo ' visual=yes|no'
    echo
    echo ' This program zooms the region around the max and min'
    echo ' coordinates of one or several category values '
    echo
    exit 1
fi

# Parse input arguments

for i do
  case $i in
    input=*)
      IN=`echo $i | sed s/input=//` ;;
    in=*)
      IN=`echo $i | sed s/in=//` ;;
    i=*)
      IN=`echo $i | sed s/i=//` ;;
    cats=*)
      CATS=`echo $i | sed s/cats=//` ;;
    c=*)
      CATS=`echo $i | sed s/c=//` ;;
    border=*)
      BORDER=`echo $i | sed s/border=//` ;;
    b=*)
      BORDER=`echo $i | sed s/b=//` ;;
    visual=*)
      VIS=`echo $i | sed s/visual=//` ;;
    vis=*)
      VIS=`echo $i | sed s/vis=//` ;;
    v=*)
      VIS=`echo $i | sed s/v=//` ;;
    *)
      echo ""
      echo "Unrecognized option: $i"
      echo 'Options: input=mapname '
                        echo ' cats=cat[,cat,cat] '
                        echo ' border=cells '
                        echo ' visual=yes|no '
      exit 1
  esac
done
#-------------------------------------------------------------------------

# Check the input arguments

eval `g.findfile element=cell file=$IN`
if [ ! "$file" ]
then
    echo "$IN - cell file not found"
    exit 2
fi

TMP=/tmp/.`basename $0`.$$
DUMMY=/tmp/dummy.$$
RCRULE=/tmp/rcrule.`basename $0`.$$

if test -f $RCRULE
then
   /bin/rm $RCRULE
fi
touch $RCRULE

for i in `echo $CATS | awk -F, '{ for (i=1; i<=NF; i++) print $i }'`
do
   echo $i' = 1' >> $RCRULE
done
cat $RCRULE | r.reclass input=$IN output=OBJ.cell
/bin/rm $RCRULE

# Get the categories max and min coordinates

g.region rast=OBJ.cell zoom=OBJ.cell
g.region -p | sed "s/\..*//" > $TMP
g.remove rast=OBJ.cell >> $DUMMY

N=`grep north $TMP | sed "s/[^0-9]*//"`
S=`grep south $TMP | sed "s/[^0-9]*//"`
E=`grep east $TMP | sed "s/[^0-9]*//"`
W=`grep west $TMP | sed "s/[^0-9]*//"`
/bin/rm $TMP

# Determine current resolution to place
# a n pixel border around the categories

g.region -p | sed "s/\..*//" > $TMP
NSRES=`grep nsres $TMP | sed "s/[^0-9]*//"`
EWRES=`grep ewres $TMP | sed "s/[^0-9]*//"`
/bin/rm $TMP

NSBORDER=`expr $NSRES \* $BORDER`
EWBORDER=`expr $EWRES \* $BORDER`
north=`expr $N + $NSBORDER`
south=`expr $S - $NSBORDER`
east=`expr $E + $EWBORDER`
west=`expr $W - $EWBORDER`

# Check that the new region not is
# outside the default region

g.region -dp | sed "s/\..*//" > $TMP
DN=`grep north $TMP | sed "s/[^0-9]*//"`
DS=`grep south $TMP | sed "s/[^0-9]*//"`
DE=`grep east $TMP | sed "s/[^0-9]*//"`
DW=`grep west $TMP | sed "s/[^0-9]*//"`
/bin/rm $TMP

if [ $north -gt $DN ]; then
north=$DN
fi
if [ $south -lt $DS ]; then
south=$DS
fi
if [ $east -gt $DE ]; then
east=$DE
fi
if [ $west -lt $DW ]; then
west=$DW
fi

# Set the new region

g.region n=$north s=$south e=$east w=$west nsres=$NSRES ewres=$EWRES
#
if [ $VIS = yes ] ; then
  d.erase
  d.rast $IN
fi

# Clean up

/bin/rm $DUMMY