#!/bin/csh
#
# gmt2arc
#
# Shell script to take GMT grids and format them for input to Arc/Info
#
# July 1996 - Ray Wood - Original coding 
# July 1997 - Dawn Wright - Fix grids with irregular cell size
#			    Incorporation into ArcGMT toolset
# November 1997 - Ray Wood - Resample irregular grids to smallest 
#                            cellsize
# July 1998 - Dawn - fixed redirect problem on creation of ascii file
# July 2002 - Dawn - changed variables XLLCENTER, YLLCENTER to
#                    XLLCORNER, YLLCORNER so that import into
#                    ArcInfo and ArcView will not suffer from a
#                    0.5 pixel offset, as pointed out by Trent Hare
#		     USGS Astrogeology. Filename extension for
#                    output grid changed from ".ascii" to ".asc"
#####################################################################
#
# arguments : $1 grid name with extension, $2 grid name without extension
#
echo " "
echo Converting GMT grid to ASCII file. . .
echo " "
# define variables
#
set temp = `grdinfo $1 | grep x_min`
while (1)
  if ($temp[1] == "x_min:") then
    shift temp
    set xmin = $temp[1]
    goto next1
  endif
  shift temp
end
next1:
while (1)
  if ($temp[1] == "x_inc:") then
    shift temp
    set xcell = $temp[1]
    goto next2
  endif
  shift temp
end
next2:
while (1)
  if ($temp[1] == "nx:") then
    shift temp
    set ncols = $temp[1]
    goto next3
  endif
  shift temp
end
next3:
set temp = `grdinfo $1 | grep y_min`
while (1)
  if ($temp[1] == "y_min:") then
    shift temp
    set ymin = $temp[1]
    goto next4
  endif
  shift temp
end
next4:
while (1)
  if ($temp[1] == "y_inc:") then
    shift temp
    set ycell = $temp[1]
    goto next5
  endif
  shift temp
end
next5:
while (1)
  if ($temp[1] == "ny:") then
    shift temp
    set nrows = $temp[1]
    goto next6
  endif
  shift temp
end
next6:
#
# Arc/Info requires same cellsize in X and Y directions
#
if ($xcell != $ycell) then
  echo " "
  echo 'WARNING: Grid cells not square'
  echo 'Arc/INFO will not accept this'
  echo 'Fixing the problem for you...'
#
# 	find smallest cellsize
#
        set cell = `echo $xcell $ycell | awk '{if ($1 < $2) {print $1} else print $2}'`#
#
# 	resample grid to smallest cell size
#
        grdsample $1 -Gtemp-gmt.grd -I$cell # 	put header in file
set temp = `grdinfo temp-gmt.grd | grep x_min`
while (1)
  if ($temp[1] == "nx:") then
    shift temp
    set ncols = $temp[1]
    goto next11
  endif
  shift temp
end
next11:
set temp = `grdinfo temp-gmt.grd | grep y_min`
while (1)
  if ($temp[1] == "ny:") then
    shift temp
    set nrows = $temp[1]
    goto next12
  endif
  shift temp
end
next12:
#
	echo NCOLS $ncols >!  tempfile
	echo NROWS $nrows >>  tempfile
	echo XLLCORNER $xmin >>  tempfile
	echo YLLCORNER $ymin >>  tempfile
	echo CELLSIZE $cell >>  tempfile
	echo NODATA_VALUE -99999 >>  tempfile
#
# 	append grid values to file and sub for nodata values
#
	grd2xyz temp-gmt.grd -Z >>  tempfile
	sed 's/NaN/-99999/' tempfile >! $2.asc
	/bin/rm tempfile temp-gmt.grd
	echo " "
	echo All fixed: GMT grid converted to ASCII file. . .
	echo " "
else
#
# 	put header in file
#
	echo NCOLS $ncols >>  $2.asc
	echo NROWS $nrows >>  $2.asc
	echo XLLCORNER $xmin >>  $2.asc
	echo YLLCORNER $ymin >>  $2.asc
	echo CELLSIZE $xcell >>  $2.asc
	echo NODATA_VALUE -99999 >>  $2.asc
#
# 	append grid values to file and sub for nodata values
#
	grd2xyz $1 -Z >>  tempfile
	sed 's/NaN/-99999/' tempfile >> $2.asc
	/bin/rm tempfile
	echo " "
	echo 	GMT grid converted to ASCII file. . .
	echo " "
endif
