#! /bin/csh -f
#  This program will convert  a file of lon lat pairs to utm (or the inverse) 
#  using the mapproject program of GMT.
#
#  Program By : Paul D. Johnson
#  Last Modified : May 19, 2000
#
#
if ($#argv != 4) then
	echo " "
        echo "  csh script to convert lon lat to UTM (or the inverse)" 
        echo "  "
        echo "  Usage: ll2utm <in_file> <F/I> <zone> <out_file>"
        echo "  Example: ll2utm waypoint.ll F 12 waypoints.utm"
        echo " "
        echo " The option F should be set to convert lon,lat pairs to UTM"
        echo " The option I should be set to convert UTM to lon,lat"
        echo " "
        echo " Currently this program is setup so that only positive zones"
        echo " should be used, even when working in the souther hemisphere."
        echo " This was done as this the convention that ArcView uses."
        echo " "
        exit 1
endif

set infile = $1
set conv = $2
set zone = $3
set outfile = $4

gmtset D_FORMAT %.14lg
gmtset MEASURE_UNIT m

# Determine Bounds of the Zone
if ($zone == 1) set bounds = "-180/-174/0/90"
if ($zone == 2) set bounds = "-174/-168/0/90"
if ($zone == -2) set bounds = "-174/-168/-90/0"
if ($zone == 3) set bounds = "-168/-162/0/90"
if ($zone == 4) set bounds = "-162/-156/0/90"
if ($zone == -4) set bounds = "-162/-156/-90/0"
if ($zone == 5) set bounds = "-156/-150/0/90"
if ($zone == 6) set bounds = "-150/-144/0/90"
if ($zone == 7) set bounds = "-144/-138/0/90"
if ($zone == 8) set bounds = "-138/-132/0/90"
if ($zone == 9) set bounds = "-132/-126/0/90"
if ($zone == 10) set bounds = "-126/-120/0/90"
if ($zone == 11) set bounds = "-120/-114/0/90"
if ($zone == 12) set bounds = "-114/-108/0/90"
if ($zone == 13) set bounds = "-108/-102/0/90"
if ($zone == 14) set bounds = "-102/-96/0/90"
if ($zone == 15) set bounds = "-96/-90/0/90"
if ($zone == 16) set bounds = "-90/-84/0/90"
if ($zone == 17) set bounds = "-84/-78/0/90"
if ($zone == 18) set bounds = "-78/-72/0/90"
if ($zone == 19) set bounds = "-72/-66/0/90"
if ($zone == 20) set bounds = "-66/-60/0/90"
if ($zone == 21) set bounds = "-60/-54/0/90"
if ($zone == 22) set bounds = "-54/-48/0/90"
if ($zone == 23) set bounds = "-48/-42/0/90"
if ($zone == 24) set bounds = "-42/-36/0/90"
if ($zone == 25) set bounds = "-36/-30/0/90"
if ($zone == 26) set bounds = "-30/-24/0/90"
if ($zone == 27) set bounds = "-24/-18/0/90"
if ($zone == 28) set bounds = "-18/-12/0/90"
if ($zone == 29) set bounds = "-12/-6/0/90"
if ($zone == 30) set bounds = "6/0/0/90"
if ($zone == 31) set bounds = "0/6/0/90"
if ($zone == 32) set bounds = "6/12/0/90"
if ($zone == 33) set bounds = "12/18/0/90"
if ($zone == 34) set bounds = "18/24/0/90"
if ($zone == 35) set bounds = "24/30/0/90"
if ($zone == 36) set bounds = "30/36/0/90"
if ($zone == 37) set bounds = "36/42/0/90"
if ($zone == 38) set bounds = "42/48/0/90"
if ($zone == 39) set bounds = "48/54/0/90"
if ($zone == 40) set bounds = "54/60/0/90"
if ($zone == 41) set bounds = "60/66/0/90"
if ($zone == 42) set bounds = "66/72/0/90"
if ($zone == 43) set bounds = "72/78/0/90"
if ($zone == 44) set bounds = "78/84/0/90"
if ($zone == 45) set bounds = "84/90/0/90"
if ($zone == 46) set bounds = "90/96/0/90"
if ($zone == 47) set bounds = "96/102/0/90"
if ($zone == 48) set bounds = "102/108/0/90"
if ($zone == 49) set bounds = "108/114/0/90"
if ($zone == 50) set bounds = "114/120/0/90"
if ($zone == 51) set bounds = "120/126/0/90"
if ($zone == 52) set bounds = "126/132/0/90"
if ($zone == 53) set bounds = "132/138/0/90"
if ($zone == 54) set bounds = "138/144/0/90"
if ($zone == 55) set bounds = "144/150/0/90"
if ($zone == 56) set bounds = "150/156/0/90"
if ($zone == 57) set bounds = "156/162/0/90"
if ($zone == 58) set bounds = "162/168/0/90"
if ($zone == 59) set bounds = "168/174/0/90"
if ($zone == 60) set bounds = "174/180/0/90"

echo " "
if ($conv == F) then
	echo "Doing forward conversion (lonlat --> UTM)"
	mapproject $infile -R$bounds -F -C -Ju$zone/1:1 >! $outfile
else
	echo "Doing inverse conversion (UTM --> lonlat)"
	mapproject $infile -R$bounds -F -C -I -Ju$zone/1:1 | awk '{printf "%4.8f %4.8f %4.2f\n", $1-360, $2, $3}' >! $outfile
endif



echo " "
echo "Generated output file: "$outfile
echo " "
gmtset MEASURE_UNIT inch

	
	
	

