#!/usr/bin/env bash
#
# Shellscript to create TIFF image of data in grd file
#
# Created by macro mbm_grdtiff
#   User eve
#   Computer MONARCH
#   Time: Wed Jul 30 13:35:24 PDT 2025
#
# Command line:
#   mbm_grdtiff -I 20220906m1_Pingofield_Topo_1mUTM.grd -G5 -D0/1 -A1.0 -S -V -O 20220906m1_Pingofield_Topo_1mUTM
#
# Define shell variables used in this script:
TIFF_FILE=20220906m1_Pingofield_Topo_1mUTM.tif
CPT_FILE=20220906m1_Pingofield_Topo_1mUTM.cpt
MAP_REGION=499948.63/501665.63/7860638.3/7862212.3
#
# 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 COLOR_BACKGROUND black
gmt gmtset COLOR_FOREGROUND white
gmt gmtset COLOR_NAN white
#
# Make color palette table file
echo Making color palette table file...
echo -145.933  37  57 175 -126.289  40 127 251 > $CPT_FILE
echo -126.289  40 127 251 -124.579  50 190 255 >> $CPT_FILE
echo -124.579  50 190 255 -123.187 106 235 255 >> $CPT_FILE
echo -123.187 106 235 255 -121.667 138 236 174 >> $CPT_FILE
echo -121.667 138 236 174 -120.104 205 255 162 >> $CPT_FILE
echo -120.104 205 255 162 -118.521 240 236 121 >> $CPT_FILE
echo -118.521 240 236 121 -116.862 255 189  87 >> $CPT_FILE
echo -116.862 255 189  87 -114.677 255 161  68 >> $CPT_FILE
echo -114.677 255 161  68 -111.114 255 186 133 >> $CPT_FILE
echo -111.114 255 186 133 -91.6181 255 255 255 >> $CPT_FILE
#
# Define data files to be plotted:
DATA_FILE=20220906m1_Pingofield_Topo_1mUTM.grd
#
# Get slope array
echo Getting slope array...
echo Running gmt module grdgradient to get x component of the gradient...
gmt grdgradient $DATA_FILE -A90 -G$DATA_FILE.drvx
echo Running gmt module grdgradient to get y component of the gradient...
gmt grdgradient $DATA_FILE -A0 -G$DATA_FILE.drvy
echo Running gmt module grdmath to get slope magnitude...
gmt grdmath $DATA_FILE.drvx 2.0 POW \
	$DATA_FILE.drvy 2.0 POW ADD SQRT \
	-1 MUL \
	= $DATA_FILE.slope
/bin/rm -f $DATA_FILE.drvx $DATA_FILE.drvy 
#
# Make tiff image
echo Running gmt module mbgrdtiff...
gmt mbgrdtiff -I$DATA_FILE \
	-O$TIFF_FILE \
	-R$MAP_REGION -Jx1.0 \
	-C$CPT_FILE \
	-I$DATA_FILE.slope \
	-V 
#
# Delete surplus files
echo Deleting surplus files...
/bin/rm -f gmt.conf
/bin/rm -f $CPT_FILE
/bin/rm -f $DATA_FILE.slope
#
# Display image file
if [[ ! ( $# > 0  && ( $1 == "-N" || $1 == "-n" ) ) ]]; then
  echo Attempting to display the image...
  #
  # Get operating system name
  os_name=`uname -s`
  #
  # Check for user defined image viewer
  image_viewer=`mbdefaults | grep "img viewer:" | cut -d " " -f 4`
  if [[ $image_viewer == "Default" ]]; then
    image_viewer=""
  fi
  if [ -x "$(command -v $image_viewer)" ]; then
    echo Displaying $TIFF_FILE using user specified program $image_viewer...
    if [[ $image_viewer == "feh" ]]; then
      feh --scale-down $TIFF_FILE &
    else
      $image_viewer $TIFF_FILE &
    fi
  elif [[ ( -x "$(command -v open)" ) && ( $os_name == "Darwin" ) ]]; then
    echo Displaying  using system default program through open...
    open $TIFF_FILE
  elif [[ -x "$(command -v gio)" ]]; then
    echo Displaying  using system default program through  gio open...
    gio open $TIFF_FILE
  elif [[ -x "$(command -v xdg-open)" ]]; then
    echo Displaying  using system default program through xdg-open...
    xdg-open $TIFF_FILE
  elif [[ -x "$(command -v cygstart)" ]]; then
    echo Displaying  using system default program through cygstart...
    cygstart $TIFF_FILE
  elif [[ -x "$(command -v open)" ]]; then
    echo Displaying  using system default program through open...
    open $TIFF_FILE
  fi
fi
#
#
# All done!
echo All done!
