#!/bin/sh
#
#	$Id: img2grd,v 1.2 2001/03/06 01:54:51 pwessel Exp $
#
#      Copyright (c) 1991-2001 by P. Wessel and W. H. F. Smith
#      See COPYING file for copying and redistribution conditions.
#
#      This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; version 2 of the License.
#
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#      Contact info: www.soest.hawaii.edu/gmt
#
# img2grd is a convenience script that makes the use of img2mercgrid
# easier.  It takes the same arguments as img2mercgrid, as well as one
# extra argument: -M.  If -M is present we will simply call img2mercgrid
# with the remainder of the arguments.  However, if -M is not given then
# will will use grdproject to undo the implicit Mercator projection and
# return a geographic grid.
#
# Author:	Paul Wessel
# Date:		05-MAR-2001
# Version:	1.2
#------------------------------------------------------------------------

if [ $# = 0 ] ; then	# No arguments, give full usage message
	echo 'usage: img2grd <world_image_filename> -R<w>/<e>/<s>/<n> -G<grdfile> -T<type>' 1>&2
        echo '	[-L] [-M] [-N<navg>] [-S<scale>] [-V] [-m<minutes>] [-x<maxlon>] [-y<minlat>/<maxlat>]' 1>&2
	img2mercgrd 2>&1 | sed -n -e '/REQUIRED/,$p' > $$	# Get the bulk of the usage message
	cat $$ 1>&2
	echo "	-L List all available *.img files [Ignored if other options are given]"  1>&2
	echo "	-M Write a Mercator grid [Default writes a geographic grid]"  1>&2
	\rm -f $$
	exit 1
fi

if [ $# = 1 ] && [ $1 = '-' ] ; then	# Quick synopsis check
	echo 'usage: img2grd <world_image_filename> -R<w>/<e>/<s>/<n> -G<grdfile> -T<type>' 1>&2
        echo '	[-L] [-M] [-N<navg>] [-S<scale>] [-V] [-m<minutes>] [-x<maxlon>] [-y<minlat>/<maxlat>]' 1>&2
	exit 1
fi

if [ $# = 1 ] && [ $1 = '-L' ] ; then	# List all img files
	echo "img2grd: Currently recognized *.img files:"  1>&2
	if [ $?GMT_IMGDIR ]; then
		ls $GMT_IMGDIR/*.img
	else
		ls *.img
	fi
	exit 1
fi

# First initialize all variables to blanks

merc=0
R=""
G=""
T=""
V=""
S=""
N=""
m=""
x=""
y=""
img=""

# Process the command line arguments

while [ ! x"$1" = x ]; do
	case $1
	in
		-R*)	R=$1;		# Got the region
			shift;;
		-G*)	G=$1;		# The output file argument
			shift;;
		-T*)	T=$1;		# The type argument
			shift;;
		-M)	merc=1;		# Want Mercator grid output
			shift;;
		-L)	echo "$0: Option -L ignored uness it is the single argument" 1>&2;
			shift;;
		-V)	V=$1;		# Verbose run
			shift;;
		-S*)	S=$1;		# The scale argument
			shift;;
		-N*)	N=$1;		# The average argument
			shift;;
		-m*)	m=$1;		# The minutes argument
			shift;;
		-x*)	x=$1;		# The maxlon argument
			shift;;
		-y*)	y=$1;		# The minlat argument
			shift;;
		-*)	echo "$0: Unrecognized option $1" 1>&2;	# Bad option argument
			exit 1;;
		*)	img=$1;		# The input file name
			shift;;
	esac
done

# Got all the options, now try if it works and let the programs deal with error messages

file=`echo $G | awk -F= '{print substr($1, 3, length($1)-2)}'`	# File name without any =<format> suffix
name=`echo $G | awk '{print substr($1, 3, length($1)-2)}'`	# File name specification, including formatting
if [ -w $file ]; then	# Delete old file with this name so we can test for success below
	\rm -f $file
fi
if [ X"$V" = X-V ]; then
	echo "$0: Extract data from file $img:" 1>&2
fi
img2mercgrd $img $R $G $T $N $S $m $x $y $V
if [ ! -f $file ]; then
	exit 1
fi

# OK, seems to have worked.  Now do the optional -I -Jm1

if [ $merc = 0 ]; then	# We want a lonlat output grid
	if [ X"$V" = X-V ]; then
		echo "img2grd: Undo the implicit Mercator projection:" 1>&2
	fi
#	Create temporary .gmtdefaults file in /tmp w/ ELLIPSOID to Sphere
	gmtdefaults -L > /tmp/$$.def
	gmtset -G/tmp/$$.def ELLIPSOID Sphere
	exactR=`grdinfo $name | grep "Spherical Mercator Projected with -Jm1" | awk '{print $8}'`
	grdproject $exactR -Jm1 -I $name $V $G +/tmp/$$.def	# Overwrite the Mercator file
	\rm -f /tmp/$$.def	# Remove the temporary defaults file
fi

exit 0
