#!/bin/tcsh -f
# interface for plotting elastic-Property as 3D (x,y,z) and as Pm3d plot(xyz2gnu)
# data is generated with Package provided by Morteza Jamal(2024)"
# Demonstrate use of Z data column to color a 3D surface(3 data columns x/y/z/)

unalias rm
alias BTcolor 'tput setab 0;tput setaf 11'	#i define it for Background and Text Color
alias BTcolor2 'tput setab 0;tput setaf 15'	#i define it for Background and Text Color

tty -s < /dev/stdout
set cpvc = $status  #check program value for color#
if ( $cpvc == 0 ) then
BTcolor
endif
echo ""
echo ""
echo ""
IRelakit.tmpxyz
sleep 5
echo ""                                                                        

set tmp  = tmp
set tmp2 = tmp2
set print = print
set file    = `pwd`
set file    = $file:t
unset help
set rgbc = 'set palette defined (-1 "blue", 0 "white", 1 "red") #default' 
set splmax = ''
set splmin = ''
set multi = 'set multiplot layout 1,1'
set orig = ''
set title = ''
set tit = ''
set size = ''
set fosi = "'Verdana,8'"
set pasvan = ".png"
unset you
unset lin
unset bul
unset shr
unset sma
unset smi
unset poi
unset pma
unset pmi
unset dir
unset ppm
unset pfm
unset psm
unset gpm
unset gfm
unset gsm
unset all
unset sho
unset mc   #Monochrome
set i = 0
set hardcopy = y
set cof = 1
set mview = ""
alias SB 'shift; breaksw'	#i define it for using in switch
alias GM 'goto makegnu'
alias RgnuQcop 'gnuplot $tmp; echo ""; echo -n " Do you want a hardcopy? (y/N)";'\
     'set hardcopy = ($<)'

#------------------check program gnuplot-------------------------------
set fexe = `echo "gnuplot"`
which $fexe >& /dev/null
set cpv = $status  #check program value#
if ( $cpv == 0 ) then
echo ""
echo " gnuplot program found..."
echo ""
else
goto errorEXE
endif
if ( $cpvc == 0 ) then
BTcolor2
endif
#----------------------------------------------

while ($#argv)
 set label = `echo "$1" | tr "A-Z" "a-z"`
  switch ($label)
  case -h:
    set help
    goto help
    SB

  case -you:
    set you
    @ i = $i + 1
    SB

  case -lin:
    set lin
    @ i = $i + 1
    SB

  case -dir:
    set dir
    @ i = $i + 1
    SB

  case -bul:
    set bul
    @ i = $i + 1
    SB

  case -shr:
    set shr
    @ i = $i + 1
    SB

  case -sma:
    set sma
    @ i = $i + 1
    SB

  case -smi:
    set smi
    @ i = $i + 1
    SB

  case -poi:
    set poi
    @ i = $i + 1
    SB

  case -pma:
    set pma
    @ i = $i + 1
    SB

  case -pmi:
    set pmi
    @ i = $i + 1
    SB

  case -ppm:
    set ppm
    @ i = $i + 1
    SB

  case -pfm:
    set pfm
    @ i = $i + 1
    SB

  case -psm:
    set psm
    @ i = $i + 1
    SB

  case -gpm:
    set gpm
    @ i = $i + 1
    SB

  case -gfm:
    set gfm
    @ i = $i + 1
    SB

  case -gsm:
    set gsm
    @ i = $i + 1
    SB

  case -all:
    set all
    set you
    set lin
    set bul
    set dir
    set poi
    set pma
    set pmi
    set shr
    set sma
    set smi
    set ppm
    set pfm
    set psm
    set gpm
    set gfm
    set gsm
    set i = 16
    SB
  case -sho:
    set sho
    SB
  case -mc    #Monochrome
    set mc
    SB
  case -mv
    set mview = "set view map"
    SB  
  case -cdef:
    set rgbc = 'set palette defined (-1 "blue", 0 "white", 1 "red") #default'
    SB

  case -cbgr:
    set rgbc = 'set palette defined (-1 "blue", 0 "green", 1 "red")'
    SB
    
  case -cbyr:
    set rgbc = 'set palette defined (-1 "blue", 0 "yellow", 1 "red")'
    SB

  default:
    echo " Your Parameter not found..."
    echo " see              xyz2gnu -h"
    
    exit
    SB
  endsw

end #while
#---------------------check input file----------
if !(-e "ThPhCalP.dat") then
  set filer = "ThPhCalP.dat"
  goto error
endif
#-----------------------------------------------
set j = 1
while ( $j <= $i )
  if ($?you) then
    set p = '$3'
    set print = Young-Modu-gnu
    set tit = 'set title "Young modulus (GPa)" '
    unset you
    GM
  endif
    
  if ($?lin) then
    set p='$4'
    set print = Linear-Comp-gnu
    set tit = 'set title "Linear compressibility (TPa-1)" '
    unset lin
    GM
  endif

  if ($?dir) then
    set p='$5'
    set print = Direc-Bulk-gnu
    set tit = 'set title "Directional Bulk modulus (TPa)" '
    unset dir
    GM
  endif

 if ($?shr) then
    set p='$6'
    set print = Shear-Rati-gnu
    set tit = 'set title "{Shear modulus}_{ave} (GPa)" '
    unset shr
    GM
 endif

 if ($?sma) then
    set p='$7'
    set print = Shear-Maxi-gnu
    set tit = 'set title "{Shear modulus}_{Max} (GPa)" '
    unset sma
    GM
 endif

 if ($?smi) then
    set p='$8'
    set print = Shear-Mini-gnu
    set tit = 'set title "{Shear modulus}_{Min} (GPa)" '
    unset smi
    GM
 endif

  if ($?poi) then
    set p='$9'
    set print = Poisson-Rati-gnu
    set tit = 'set title "{Poissons ratio}_{ave}"'
    unset poi
    GM
 endif

  if ($?pma) then
    set p='$10'
    set print = Poisson-Maxi-gnu
    set tit = 'set title "{Poissons ratio}_{Max}"'
    unset pma
    GM
 endif

  if ($?pmi) then
    set p='$11'
    set print = Poisson-Mini-gnu
    set tit = 'set title "{Poissons ratio}_{Min}"'
    unset pmi
    GM
 endif
    

  if ($?bul) then
    set p='$12'
    set print = Bulk-Modu-gnu
    set tit = 'set title "Volume Bulk modulus (GPa)" '
    unset bul
    GM
 endif

  if ($?ppm) then 
    set cof = 1000
    set p='$13'
    set print = Phase-Prim-gnu
    set tit = 'set title "Phase-Velocity P-Mode (Km/s)" '
    unset ppm
    GM
    endif
    
  if ($?pfm) then
    set cof = 1000
    set p='$14'
    set print = Phase-Fast-gnu
    set tit = 'set title "Phase-Velocity F-Mode (Km/s)" '
    unset pfm
    GM
  endif
    
  if ($?psm) then
    set cof = 1000
    set p='$15'
    set print = Phase-Slow-gnu
    set tit = 'set title "Phase-Velocity S-Mode (Km/s)" '
    unset psm
    GM
  endif

  if ($?gpm) then
    set cof = 1000
    set p='$16'
    set print = Group-Prim-gnu
    set tit = 'set title "Group-Velocity P-Mode (Km/s)" '
    unset gpm
    GM
  endif
    
  if ($?gfm) then
    set cof = 1000
    set p='$17'
    set print = Group-Fast-gnu
    set tit = 'set title "Group-Velocity F-Mode (Km/s)" '
    unset gfm
    GM
  endif
    
  if ($?gsm) then
    set cof = 1000
    set p='$18'
    set print = Group-Slow-gnu
    set tit = 'set title "Group-Velocity S-Mode (Km/s)" '
    unset gsm
    GM
  endif

makegnu:

@ j = $j + 1

rm -f $tmp
set c1='$1'
set c2='$2'
set c3='$3'
#if ($?terminal) then
#endif

  if ($?all) then 
    if ($?mc) then 
      goto downmc
     else
      goto down
    endif
  endif 
if ($?sho) then
   if ($?mc)  then
   
cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties as 3D and as Pm3d (Monochromic).
reset
cof=$cof
$multi

set format z "%3.0f"
set xlabel "X"  
set ylabel "Y" 
set zlabel "Z"
#set view 60, 60, 1.1, 1.3
#set view 60,30,1.1 
set view 54,358,1.1 
#set view 49, 108, 1, 1.48
#set view 49, 108,1
set bmargin 6
set style fill transparent solid 0.90 border
unset key
unset colorbox
set samples 40, 40
set xyplane 0
$mview
set pm3d depthorder border linewidth 0.100
set pm3d clip
set pm3d lighting primary 0.5 specular 0.2 spec2 0
set grid x y z vertical
set xtics format "%3.0f"
set ytics format  "%3.0f" 
set ztics format  "%3.0f" 

$size
$orig
$tit
set table "result.dat" 
plot "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) w table
unset table

stats "result.dat" using 1 nooutput
print "Max x: ", STATS_max
print "Min x: ", STATS_min
if (STATS_max < 1) {set xtics format "%3.1f"}
set xtics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 2 nooutput
print "Max y: ", STATS_max
print "Min y: ", STATS_min
if (STATS_max < 1) {set ytics format "%3.1f"}
set ytics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 3 nooutput
print "Max z: ", STATS_max
print "Min z: ", STATS_min
if (STATS_max < 1) {set ztics format "%3.1f"}
set ztics (0.9*STATS_min,0,0.9*STATS_max) 

#set pm3d clip4in
#royalblue,cyan,steelblue,goldenrod,orange-red,sienna1,tan1
sp "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) with pm3d fc "steelblue"
unset multiplot
pause -1 "  Press <enter> to continue..."
reset
EOF
   
  else
  
cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties as 3D and as Pm3d.
reset
cof=$cof
$multi
set format cb "%4.1f"
set colorbox user size .02, .6 
set cbtics scale 0

set format z "%3.0f"
set xlabel "X"  
set ylabel "Y" 
set zlabel "Z"
$rgbc

#set view 60,30,1.1 
set view 54,358,1.1 
#set view 49, 108, 1, 1.48
#set view 49, 108,1
set grid x y z vertical
$mview
set pm3d depthorder explicit
#set view equal xyz
set xyplane 0
#set cblabel "Young (GPa)"
#unset ztics
set xtics format "%3.0f"
set ytics format  "%3.0f" 
set ztics format  "%3.0f" 

$size
$orig
$tit
set table "result.dat" 
plot "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) w table
unset table

stats "result.dat" using 1 nooutput
print "Max x: ", STATS_max
print "Min x: ", STATS_min
if (STATS_max < 1) {set xtics format "%3.1f"}
set xtics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 2 nooutput
print "Max y: ", STATS_max
print "Min y: ", STATS_min
if (STATS_max < 1) {set ytics format "%3.1f"}
set ytics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 3 nooutput
print "Max z: ", STATS_max
print "Min z: ", STATS_min
if (STATS_max < 1) {set ztics format "%3.1f"}
set ztics (0.9*STATS_min,0,0.9*STATS_max) 

sp "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) w pm3d notitle
unset multiplot
reset
pause -1 "  Press <enter> to continue..."
EOF

endif #mc
RgnuQcop
endif   #sho

downmc:

if ($?mc) then
set pasvan = ".png"
if ($hardcopy == y || $hardcopy == Y ) then
cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties as 3D and as Pm3d (Monochromic).
reset
cof=$cof
set terminal pngcairo enhanced font 'Verdana,11'
#font 'Verdana,26' size 2000,1400
set output  "$print$pasvan"
$multi

set format z "%3.0f"
set xlabel "X"  
set ylabel "Y" 
set zlabel "Z"
#set view 60, 60, 1.1, 1.3
#set view 60,30,1.1 
set view 54,358,1.1 
#set view 49, 108, 1, 1.48
#set view 49, 108,1
set bmargin 6
set style fill transparent solid 0.90 border
unset key
unset colorbox
set samples 40, 40
set xyplane 0
$mview
set pm3d depthorder border linewidth 0.100
set pm3d clip
set pm3d lighting primary 0.5 specular 0.2 spec2 0
set grid x y z vertical
set xtics format "%3.0f"
set ytics format  "%3.0f" 
set ztics format  "%3.0f" 

$size
$orig
$tit
set table "result.dat" 
plot "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) w table
unset table

stats "result.dat" using 1 nooutput
print "Max x: ", STATS_max
print "Min x: ", STATS_min
if (STATS_max < 1) {set xtics format "%3.1f"}
set xtics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 2 nooutput
print "Max y: ", STATS_max
print "Min y: ", STATS_min
if (STATS_max < 1) {set ytics format "%3.1f"}
set ytics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 3 nooutput
print "Max z: ", STATS_max
print "Min z: ", STATS_min
if (STATS_max < 1) {set ztics format "%3.1f"}
set ztics (0.9*STATS_min,0,0.9*STATS_max) 

#set pm3d clip4in
#royalblue,cyan,steelblue,goldenrod,orange-red,sienna1,tan1
sp "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) with pm3d fc "steelblue"
unset multiplot
unset output
reset
EOF

gnuplot $tmp >& /dev/null 
echo " $print$pasvan was generated... "
echo ""

endif

else

down:
set pasvan = ".pdf"
if ($hardcopy == y || $hardcopy == Y ) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties as 3D and as Pm3d.
reset
cof=$cof
set terminal pdfcairo enhanced font $fosi 
set output  "$print$pasvan"
$multi
set format cb "%4.1f"
set colorbox user size .02, .6 
set cbtics scale 0

set format z "%3.0f"
set xlabel "X"  
set ylabel "Y" 
set zlabel "Z"
$rgbc

#set view 60,30,1.1 
set view 54,358,1.1 
#set view 49, 108, 1, 1.48
#set view 49, 108,1
set grid x y z vertical
$mview
set pm3d depthorder explicit
#set view equal xyz
set xyplane 0
#set cblabel "Young (GPa)"
#unset ztics
set xtics format "%3.0f"
set ytics format  "%3.0f" 
set ztics format  "%3.0f" 

$size
$orig
$tit
set table "result.dat" 
plot "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) w table
unset table

stats "result.dat" using 1 nooutput
print "Max x: ", STATS_max
print "Min x: ", STATS_min
if (STATS_max < 1) {set xtics format "%3.1f"}
set xtics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 2 nooutput
print "Max y: ", STATS_max
print "Min y: ", STATS_min
if (STATS_max < 1) {set ytics format "%3.1f"}
set ytics (0.9*STATS_min,0,0.9*STATS_max) 

stats "result.dat" using 3 nooutput
print "Max z: ", STATS_max
print "Min z: ", STATS_min
if (STATS_max < 1) {set ztics format "%3.1f"}
set ztics (0.9*STATS_min,0,0.9*STATS_max) 

sp "ThPhCalP.dat" u ($p*sin($c1*pi/180)*cos($c2*pi/180)/cof):($p*sin($c1*pi/180)*sin($c2*pi/180)/cof):($p*cos($c1*pi/180)/cof) w pm3d notitle

unset multiplot
unset output
reset
EOF



gnuplot $tmp >& /dev/null 
echo " $print$pasvan was generated... "
echo ""

endif

endif #mc

set cof = 1
set multi = 'set multiplot layout 1,1'
set orig = ''
set title = ''
set tit = ''
set size = ''
set fosi = "'Verdana,8'"


end  #while


exit



#---------------------------------------------------------------

errorEXE:					#error exit	
printf "\n   stop error: Required program $fexe not found\n" 
printf "\n" 
exit(1)
#---------------------------------------------------------------
#---------------------------------------------------------------
error:
echo ">>>"
echo ">>> ERROR: $filer not found\!"
echo ">>> ERROR:"
echo ">>>"
exit(2)
#---------------------------------------------------------------

help:

cat <<EOF
       PROGRAM: xyz2gnu

                A plotting interface to plot elastic properties as 3D (x,y,z) and as Pm3d plot.

       PURPOSE: 3D-plot of elastic properties as pm3d and as pdf or png (-mc option) format using gnuplot"
      
       USAGE:   xyz2gnu [OPTIONS] [FLAGS]
       FLAGS:   -h/-H ---->  help

                 
       OPTIONS:
       -you ---> 3D plot of Young Modulus
       -lin:     3D plot of Linear Compressibility
       -shr ---> 3D plot of Shear Ratio
       -sma:     3D plot of Shear Ratio(Max) 
       -smi:---> 3D plot of Shear Ratio(Min)
       -poi:     3D plot of Poisson's Ratio
       -pma:---> 3D plot of Poisson's Ratio(Max)
       -pmi:     3D plot of Poisson's Ratio(Min)
       -dir ---> 3D plot of Directional Bulk Modulus
       -bul:     3D plot of Bulk Modulus
       -ppm ---> 3D plot of Phase Velocity: P-Mode
       -psm:     3D plot of Phase Velocity: S-Mode
       -pfm ---> 3D plot of Phase Velocity: F-Mode
       -gpm:     3D plot of Group Velocity: P-Mode
       -gsm ---> 3D plot of Group Velocity: S-Mode
       -gfm:     3D plot of Group Velocity: F-Mode
       -all ---> 3D plot of all properties
       -sho:     Show and then generates 3D plots (excluding "-all")
       -mc  ---> 3D plot of elastic properties as pm3d and as MonoChrome (png format)
       -mv :     To change perspective of a 3D plot to a 2D Map View
       
       3D PLOT COLOR OPTIONS:
       -cdef:     #DEFault(blue-white-red)
       -cbgr:     #(blue-green-red)
       -cbyr:     #(blue-yellow-red)
EOF
