#!/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:32:55 PDT 2025
#
# Command line:
#   mbm_grdplot -I20220906m1_Pingofield_Topo_1mUTM.grd -G1 -C -V -LFile 20220906m1_Pingofield_Topo_1mUTM.grd - Topography Grid:Topography (m)
#
# Define shell variables used in this script:
PS_FILE=20220906m1_Pingofield_Topo_1mUTM.grd.ps
CPT_FILE=20220906m1_Pingofield_Topo_1mUTM.grd.cpt
MAP_PROJECTION=x
MAP_SCALE=0.0037857
MAP_REGION=499948.62781/501665.62781/7860638.2992/7862212.2992
X_OFFSET=1
Y_OFFSET=2.7707
#
# 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   -150  37  57 175 -143.75  40 127 251 > $CPT_FILE
echo -143.75  40 127 251 -137.5  50 190 255 >> $CPT_FILE
echo -137.5  50 190 255 -131.25 106 235 255 >> $CPT_FILE
echo -131.25 106 235 255   -125 138 236 174 >> $CPT_FILE
echo   -125 138 236 174 -118.75 205 255 162 >> $CPT_FILE
echo -118.75 205 255 162 -112.5 240 236 121 >> $CPT_FILE
echo -112.5 240 236 121 -106.25 255 189  87 >> $CPT_FILE
echo -106.25 255 189  87   -100 255 161  68 >> $CPT_FILE
echo   -100 255 161  68 -93.75 255 186 133 >> $CPT_FILE
echo -93.75 255 186 133  -87.5 255 255 255 >> $CPT_FILE
#
# Define data files to be plotted:
DATA_FILE=20220906m1_Pingofield_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 \
	-C2.5 \
	-L-145.4009552/-92.1506195068 -Wc1p\
	-P -K -O -V >> $PS_FILE
#
# Make color scale
echo Running gmt module psscale...
gmt psscale -C$CPT_FILE \
	-Dx0/-0.5000+h+w6.5000/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 \
	-B524 -B+t"File 20220906m1_Pingofield_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!
