#!/bin/sh
#
#	$Id: img2grd,v 1.5 2006/04/11 22:33:41 pwessel Exp $
#
#      Copyright (c) 1991-2005 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: gmt.soest.hawaii.edu
#
# 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:		17-OCT-2005
# Version:	1.4
#------------------------------------------------------------------------

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 '	[-D[<minlat>/<maxlat>]] [-E] [-L] [-M] [-N<navg>] [-S<scale>] [-V] [-m<minutes>] [-W<maxlon>]' 1>&2
	img2mercgrd 2>&1 | sed -n -e '/REQUIRED/,$p' > $$	# Get the bulk of the usage message
	cat $$ 1>&2
	echo "	-E Resample to the specified -R [Ignored if -M is given]"  1>&2
	echo "	   Default gives the exact -R of the Mercator grid"  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 ' [-D[<minlat>/<maxlat>]] [-E] [-L] [-M] [-N<navg>] [-S<scale>] [-V] [-m<minutes>] [-W<maxlon>]' 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
exact=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;;
		-E)	exact=1;	# Want given -R grid output
			shift;;
		-L)	echo "$0: Option -L ignored unless 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;;
		-W*)	x=$1;		# The maxlon argument
			shift;;
		-D*)	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
	exactR=`grdinfo $name | grep "Spherical Mercator Projected with -Jm1" | awk '{print $8}'`
	grdproject $exactR -Jm1 -I $name $V $G --ELLIPSOID=Sphere	# Overwrite the Mercator file
	if [ $exact = 1 ]; then	# Resample again using the given -R and the dx/dy in even minutes
		dx=`grdinfo $name -C | cut -f8`
		grdsample $R -I$dx $name $G $V
	fi
fi

exit 0
