#!/bin/sh
# written by Markus Neteler 18. August 1998 / 20. Jan. 1999
#            neteler geog.uni-hannover.de
# mosaic code from Felix Gershunov (Felix spsl.nsc.ru)
# updated for GRASS 5.7 by Michael Barton 2004/04/05
#
# COPYRIGHT:    (C) 1999,2007 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.
#
# TODO: - implement g.findfile for 3 and 4 maps (currently only current mapset supported)
#            [done for 2 maps]
#       - fix isnull() in r.mapcalc for 3 and 4 maps composites
#            [done for 2 maps]
#       - fix color table length (currently only 256 cols supported, make
#         flexible)
#            [done for 2 maps]
#--------------------------------------------------


#% Module
#%  description: Mosaics up to 4 images and extends colormap; creates map *.mosaic
#%  keywords: raster, imagery, mosaicking
#% End
#% option
#% key: image1
#% type: string
#% gisprompt: old,cell,raster
#% description: 1st map for mosaic (top of image stack).
#% required : yes
#% end
#% option
#% key: image2
#% type: string
#% gisprompt: old,cell,raster
#% description: 2nd map for mosaic.
#% required : yes
#% end
#% option
#% key: image3
#% type: string
#% gisprompt: old,cell,raster
#% description: 3rd map for mosaic.
#% required : no
#% end
#% option
#% key: image4
#% type: string
#% gisprompt: old,cell,raster
#% description: 4th map for mosaic.
#% required : no
#% end

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

# save command line
if [ "$1" != "@ARGS_PARSED@" ] ; then
    CMDLINE=`basename "$0"`
    for arg in "$@" ; do
        CMDLINE="$CMDLINE \"$arg\""
    done
    export CMDLINE
    exec g.parser "$0" "$@"
fi


#### check if we have awk
if [ ! -x "`which awk`" ] ; then
    g.message -e "awk required, please install awk or gawk first"
    exit 1
fi

# setting environment, so that awk works properly in all languages
unset LC_ALL
LC_NUMERIC=C
export LC_NUMERIC
   
### setup enviro vars ###
MAPSET=`g.gisenv get=MAPSET`
LOCATION_NAME=`g.gisenv get=LOCATION_NAME`
GISDBASE=`g.gisenv get=GISDBASE`
CURRLOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"

g.message -w message='Do not forget to set region properly to cover all images!'

# two images to patch
if [ -z "$GIS_OPT_IMAGE3" -a  -z "$GIS_OPT_IMAGE4" ]; then
     g.message "Mosaicing two images..."
     r.mapcalc "$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.mosaic = if(isnull($GIS_OPT_IMAGE1),\
					$GIS_OPT_IMAGE2+256,\
					$GIS_OPT_IMAGE1)"
     #modify the color table:
     echo '% 0 1024' > "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.mosaic"

     eval `g.findfile element=cell file=$GIS_OPT_IMAGE1`
     mapset1="${mapset}"
     cat "$GISDBASE/$LOCATION_NAME/$mapset1/colr/$GIS_OPT_IMAGE1" | \
        grep -v '%' | tr -s ' ' '\012' >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.mosaic"

     eval `g.findfile element=cell file="$GIS_OPT_IMAGE2"`
     mapset2="${mapset}"
     cat "$GISDBASE/$LOCATION_NAME/$mapset2/colr/$GIS_OPT_IMAGE2" | \
        grep -v '%' | tr -s ' ' '\012' | awk -F':' '{print $1+256":"$2":"$3":"$4}'\
	>> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.mosaic"
     
     g.message message="Ready. File $GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.mosaic created."

     # write cmd history:
     r.support "$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.mosaic" history="${CMDLINE}"
     exit 0
fi

# three images to patch
if [ -z "$GIS_OPT_IMAGE3" -o  -z "$GIS_OPT_IMAGE4" ]; then
     g.message "Mosaicing three images..."
     r.mapcalc "$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic = eval(t1=if($GIS_OPT_IMAGE1,$GIS_OPT_IMAGE1,$GIS_OPT_IMAGE2+256), t2=if(t1 % 256,t1,$GIS_OPT_IMAGE3+512), t2)"
     #modify the color table:
     echo '% 0 1024' > "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic"
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE1 >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic"
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE2 |awk -F':' '{print $1+256":"$2":"$3":"$4}' \
        >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic"
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE3 |awk -F':' '{print $1+512":"$2":"$3":"$4}' \
        >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic"
     g.message message="Ready. File $GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic created."

     # write cmd history:
     r.support $GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.mosaic history="${CMDLINE}"
     exit 0
fi

# four images to patch
if [ $GIS_OPT_IMAGE3 != "" -a  $GIS_OPT_IMAGE4 != "" ]; then
     g.message "Mosaicing four images..."
     r.mapcalc "$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic = eval(t1=if($GIS_OPT_IMAGE1,$GIS_OPT_IMAGE1,$GIS_OPT_IMAGE2+256), t2=if(t1 % 256,t1,$GIS_OPT_IMAGE3+512), t3=if(t2 % 256,t2,$GIS_OPT_IMAGE4+768),t3)"
     #modify the color table:
     echo '% 0 1024' > $CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE1 >> $CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE2 |awk -F':' '{print $1+256":"$2":"$3":"$4}' \
        >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic"
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE3 |awk -F':' '{print $1+512":"$2":"$3":"$4}' \
        >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic"
     tail -256 $CURRLOCATION/colr/$GIS_OPT_IMAGE4 |awk -F':' '{print $1+768":"$2":"$3":"$4}' \
        >> "$CURRLOCATION/colr/$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic"
     g.message message="Ready. File $GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic created."

     # write cmd history:
     r.support "$GIS_OPT_IMAGE1.$GIS_OPT_IMAGE2.$GIS_OPT_IMAGE3.$GIS_OPT_IMAGE4.mosaic" history="${CMDLINE}"
     exit 0
fi
