#!/bin/sh
############################################################################
#
# MODULE:		r.in.aster v.3 for GRASS 6.0 (2005/11/4)
# AUTHOR(S):	Michael Barton (michael.barton@asu.edu) and Paul Kelly
# PURPOSE:	Rectifies, georeferences, & imports Terra-ASTER imagery 
#			using gdalwarp
# COPYRIGHT:	(C) 2004 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.
#
#############################################################################

#%Module
#%  description: r.in.aster - georeferencing, rectification, and import of Terra-ASTER imagery and relative DEM's using gdalwarp
#%  keywords: raster, imagery, import
#%End
#%option
#% key: input
#% type: string
#% gisprompt: old_file,file,input
#% description: Input ASTER image to be georeference & rectified
#% required : yes
#%end
#%option
#% key: proctype
#% type: string
#% description: ASTER imagery processing type (Level 1A, Level 1B, or relative DEM)
#% options: L1A,L1B,DEM
#% required : yes
#%end
#%option
#% key: band
#% type: string
#% description: L1A or L1B band to translate (1, 2,3n,3b,4-14). Can only translate a single band
#% answer: 1
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% description: Output GRASS image file
#% gisprompt: old,cell,raster
#% required : yes
#%end


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

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


#initialize datasets for L1A and L1B

if [ "$GIS_OPT_PROCTYPE" = "L1A" ]
then
	case $GIS_OPT_BAND in
		1)
		dataset="VNIR_Band1:ImageData" ;;
		2)
		dataset="VNIR_Band2:ImageData" ;;
		3n)
		dataset="VNIR_Band3N:ImageData" ;;
		3b)
		dataset="VNIR_Band3B:ImageData" ;;
		4)
		dataset="SWIR_Band4:ImageData" ;;
		5)
		dataset="SWIR_Band5:ImageData" ;;
		6)
		dataset="SWIR_Band6:ImageData" ;;
		7)
		dataset="SWIR_Band7:ImageData" ;;
		8)
		dataset="SWIR_Band8:ImageData" ;;
		9)
		dataset="SWIR_Band9:ImageData" ;;
		10)
		dataset="TIR_Band10:ImageData" ;;
		11)
		dataset="TIR_Band11:ImageData" ;;
		12)
		dataset="TIR_Band12:ImageData" ;;
		13)
		dataset="TIR_Band13:ImageData" ;;
		14)
		dataset="TIR_Band14:ImageData" ;;
	esac
	srcfile="HDF4_EOS:EOS_SWATH:"\"$GIS_OPT_INPUT\"":"$dataset
elif [ "$GIS_OPT_PROCTYPE" = "L1B" ] 
then
	case $GIS_OPT_BAND in
		1)
		dataset="VNIR_Swath:ImageData1" ;;
		2)
		dataset="VNIR_Swath:ImageData2" ;;
		3n)
		dataset="VNIR_Swath:ImageData3N" ;;
		3b)
		dataset="VNIR_Swath:ImageData3B" ;;
		4)
		dataset="SWIR_Swath:ImageData4" ;;
		5)
		dataset="SWIR_Swath:ImageData5" ;;
		6)
		dataset="SWIR_Swath:ImageData6" ;;
		7)
		dataset="SWIR_Swath:ImageData7" ;;
		8)
		dataset="SWIR_Swath:ImageData8" ;;
		9)
		dataset="SWIR_Swath:ImageData9" ;;
		10)
		dataset="TIR_Swath:ImageData10" ;;
		11)
		dataset="TIR_Swath:ImageData11" ;;
		12)
		dataset="TIR_Swath:ImageData12" ;;
		13)
		dataset="TIR_Swath:ImageData13" ;;
		14)
		dataset="TIR_Swath:ImageData14" ;;
	esac
	srcfile="HDF4_EOS:EOS_SWATH:"\"$GIS_OPT_INPUT\"":"$dataset
elif [ "$GIS_OPT_PROCTYPE" = "DEM" ] 
then
	srcfile=$GIS_OPT_INPUT
fi

tempfile=`g.tempfile $$`.tif

#run gdalwarp with selected options (must be in $PATH)
#to translate aster image to geotiff
echo ""
echo "georeferencing aster image"
echo gdalwarp -t_srs "`g.proj -jf`" $srcfile $tempfile
echo ""

gdalwarp -t_srs "`g.proj -jf`" $srcfile $tempfile

#import geotiff to GRASS
echo ""
echo "importing into GRASS"
echo ""

r.in.gdal input=$tempfile output=$GIS_OPT_OUTPUT

#cleanup
echo ""
echo "cleaning up"
echo ""
rm -f $tempfile

echo ""
echo ""
echo "DONE"

