#!/usr/bin/env bash
#
# Shellscript to create Postscript plot of data in grd file
#
# Created by macro mbm_grdplot
#   User eve
#   Computer MONARCH
#   Time: Wed Jul 30 13:21:22 PDT 2025
#
# Command line:
#   mbm_grdplot -I20220906m1_Topo_1mUTM.grd -G1 -C -V -LFile 20220906m1_Topo_1mUTM.grd - Topography Grid:Topography (m)
#
# Define shell variables used in this script:
PS_FILE=20220906m1_Topo_1mUTM.grd.ps
CPT_FILE=20220906m1_Topo_1mUTM.grd.cpt
MAP_PROJECTION=x
MAP_SCALE=0.00085412
MAP_REGION=498627.79628/503838.79628/7856518.6646/7865299.6646
X_OFFSET=2.0246
Y_OFFSET=2
#
# Delete any existing gmt.conf file
if [[ -e gmt.conf ]]; then
  echo Deleting gmt.conf...
  /bin/rm gmt.conf
fi
#
# Set temporary GMT defaults
echo Setting temporary GMT defaults...
gmt gmtset PROJ_LENGTH_UNIT inch
gmt gmtset PS_MEDIA archA
gmt gmtset FONT_ANNOT_PRIMARY 8,Helvetica,black
gmt gmtset FONT_ANNOT_SECONDARY 8,Helvetica,black
gmt gmtset FONT_LABEL 8,Helvetica,black
gmt gmtset FONT_TITLE 10,Helvetica,black
gmt gmtset PS_PAGE_ORIENTATION LANDSCAPE
gmt gmtset COLOR_BACKGROUND black
gmt gmtset COLOR_FOREGROUND white
gmt gmtset COLOR_NAN white
gmt gmtset FORMAT_GEO_MAP ddd:mm
#
# Make color palette table file
echo Making color palette table file...
echo   -300  37  57 175   -275  40 127 251 > $CPT_FILE
echo   -275  40 127 251   -250  50 190 255 >> $CPT_FILE
echo   -250  50 190 255   -225 106 235 255 >> $CPT_FILE
echo   -225 106 235 255   -200 138 236 174 >> $CPT_FILE
echo   -200 138 236 174   -175 205 255 162 >> $CPT_FILE
echo   -175 205 255 162   -150 240 236 121 >> $CPT_FILE
echo   -150 240 236 121   -125 255 189  87 >> $CPT_FILE
echo   -125 255 189  87   -100 255 161  68 >> $CPT_FILE
echo   -100 255 161  68    -75 255 186 133 >> $CPT_FILE
echo    -75 255 186 133    -50 255 255 255 >> $CPT_FILE
#
# Define data files to be plotted:
DATA_FILE=20220906m1_Topo_1mUTM.grd
INTENSITY_FILE=
#
# Make color image
echo Running gmt module grdimage...
gmt grdimage $DATA_FILE -J$MAP_PROJECTION$MAP_SCALE \
	-R$MAP_REGION \
	-C$CPT_FILE \
	-P -X$X_OFFSET -Y$Y_OFFSET -K -V >| $PS_FILE
#
# Make contour plot
echo Running gmt module grdcontour...
gmt grdcontour $DATA_FILE -J$MAP_PROJECTION$MAP_SCALE \
	-R$MAP_REGION \
	-C10 \
	-L-286.148925781/-81.8105010986 -Wc1p\
	-P -K -O -V >> $PS_FILE
#
# Make color scale
echo Running gmt module psscale...
gmt psscale -C$CPT_FILE \
	-Dx0/-0.5000+h+w4.4508/0.1500 \
	-B+l"Topography (m)" \
	-P -K -O -V >> $PS_FILE
#
# Make basemap
echo Running gmt module psbasemap...
gmt psbasemap -J$MAP_PROJECTION$MAP_SCALE \
	-R$MAP_REGION \
	-B1737 -B+t"File 20220906m1_Topo_1mUTM.grd - Topography Grid" \
	-P -O -V >> $PS_FILE
#
# Delete surplus files
echo Deleting surplus files...
/bin/rm -f gmt.conf
/bin/rm -f $CPT_FILE
#
# Display Postscript file
if [[ ! ( $# > 0  && ( $1 == "-N" || $1 == "-n" ) ) ]]; then
  echo Attempting to display the Postscript plot...
  #
  # Get operating system name
  os_name=`uname -s`
  #
  # Check for user defined Postscript Viewer
  ps_viewer=`mbdefaults | grep "ps viewer:" | cut -d " " -f 4`
  if [[ $ps_viewer == "Default" ]]; then
    ps_viewer=""
  fi
  if [[ -x "$(command -v $ps_viewer)" ]]; then
    echo Displaying $PS_FILE using user specified program $ps_viewer...
    if [[ $ps_viewer == "evince" ]]; then
      evince $PS_FILE &
    elif [[ $ps_viewer == "gv" ]]; then
      gv --orientation=portrait --media=BBox -scale=1 $PS_FILE &
    elif [[ $ps_viewer == "ggv" ]]; then
      ggv --geometry=portrait $PS_FILE &
    elif [[ $ps_viewer == "ghostview" ]]; then
      ghostview -portrait -media BBox $PS_FILE &
    elif [[ $ps_viewer == "xpsview" ]]; then
      xpsview -ps a -maxp 4m $PS_FILE &
    elif [[ $ps_viewer == "pageview" ]]; then
      pageview -w 8.5 -h 11 $PS_FILE &
    else
      $ps_viewer $PS_FILE &
    fi
  elif [[ ( -x "$(command -v open)" ) && ( $os_name == "Darwin" ) ]]; then
    echo Displaying  using system default program through open...
    open $PS_FILE
  elif [[ -x "$(command -v gio)" ]]; then
    echo Displaying  using system default program through  gio open...
    gio open $PS_FILE
  elif [[ -x "$(command -v xdg-open)" ]]; then
    echo Displaying  using system default program through xdg-open...
    xdg-open $PS_FILE
  elif [[ -x "$(command -v cygstart)" ]]; then
    echo Displaying  using system default program through cygstart...
    cygstart $PS_FILE
  elif [[ -x "$(command -v open)" ]]; then
    echo Displaying  using system default program through open...
    open $PS_FILE
  fi
fi
#
## Render Postscript file into an image
#echo Using gmt psconvert to render the Postscript plot to an image...
#gmt psconvert $PS_FILE -Tj -E300 -A -P
#
# All done!
echo All done!
