#!/bin/tcsh -f
# interface for plotting 2D-CUT of elastic-Property as Polar/Cartesian coordinates
# data is generated with Package(IRelakit) provided by Morteza Jamal(2024)"
#
unalias rm

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 ""
IRelakit.tmpPolar
echo ""
sleep 5

set tmp  = tmp
set tmp2 = tmp2
set pname = print.png
set in = "2DB.dat"
set out1 = "2DB.png"
set out2  = "2DBrxy.png"
set out3 = "2DBxy.png"
set stit = "set title"
set ylab = " set ylabel"
set file    = `pwd`
set file    = $file:t
unset help
set splmax = ''
set splmin = ''
set statmax = ''
set statmin = ''
set title = ''
set labT = ''

set tit1 = " notitle"
set tit2 = ""
set tit3 = ""

set fosi = "'Verdana,11'"
set ls = "set style line 1"
set wll = " w l linestyle 1"
set lc = 2
set lw = 2

set ls2 = "#set style line 2"
set wll2 = ""
set lc2 = 3
set lw2 = 2

set ls3 = "#set style line 3"
set wll3 = ""
set lc3 = 4
set lw3 = 2

set smb = "smooth sbezier"
set smb2 = ""
set smb3 = ""
unset you
unset lin
unset bul
unset shr
unset poi
unset dir
unset ppm
unset pfm
unset psm
unset ppfs
unset gpm
unset gfm
unset gsm
unset gpfs
unset all
unset sho
unset rxy
unset xy
set i = 0
set hardcopy = y
set cof = 1
set pname2 = ""
set pname3 = ""
set puls = 0
set puls2 = 0

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 = ($<)'
#     if ($hardcopy == y || $hardcopy == Y ) then; echo "";'\
#	 'echo " $print is generating... "; echo ""; endif'


#------------------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 -poi:
    set poi
    @ 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 -ppfs:
    set ppfs
    @ 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 -gpfs:
    set gpfs
    @ i = $i + 1
    SB
  case -all:
    set all
    set you
    set lin
    set bul
    set dir
    set poi
    set shr
    set ppm
    set pfm
    set psm
    set ppfs
    set gpm
    set gfm
    set gsm
    set gpfs
    set i = 14
    SB

  case -sho:
    set sho
    SB
    
  case -lc: 
    shift; set lc = $1; SB
  case -lw: 
    shift; set lw = $1; SB
#  case -lt: 
#    shift; set lt = $1; SB
  case -lc2: 
    shift; set lc2 = $1; SB
  case -lw2: 
    shift; set lw2 = $1; SB
#  case -lt2: 
#    shift; set lt2 = $1; SB
  case -lc3: 
    shift; set lc3 = $1; SB
  case -lw3: 
    shift; set lw3 = $1; SB
#  case -lt3: 
#    shift; set lt3 = $1; SB
   case -rxy:
     set rxy
     SB
   case -xy:
     set xy
     SB

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

end #while

set j = 1


#---------------------FOR  plot--------------------------------
while ( $j <= $i )


  if ($?shr || $?poi || $?gpfs || $?ppfs) then
    set wll2 = " w l linestyle 2"
    set wll3 = " w l linestyle 3"
    set ls2 = "set style line 2"
    set ls3 = "set style line 3"
    set smb2 = 'smooth sbezier'
    set smb3 = 'smooth sbezier'
  endif

  if ($?gpfs || $?ppfs) then
   set tit1 = ' title "P-Mode"'
   set tit2 = ' title "F-Mode"'
   set tit3 = ' title "S-Mode"'
  endif
  if ($?shr || $?poi) then
   set tit1 = ' title "Ave"'
   set tit2 = ' title "Max"'
   set tit3 = ' title "Min"'
  endif

  if ($?shr) then
    set puls2 = 3
    set pname = Shear-Rati
    set pname2 = Shear-Maxi
    set pname3 = Shear-Mini
    set labT = '"Shear modulus (GPa)"'
    unset shr
    GM
 endif
   
   if ($?poi) then
    set puls2 = 3
    set pname = Poisson-Rati
    set pname2 = Poisson-Maxi    
    set pname3 = Poisson-Mini
    set labT = '"Poissons ratio"'
    unset poi
    GM
  endif
  
  if ($?gpfs) then
    set puls = 2
    set puls2 = 3
    set pname = Group-Prim
    set pname2 = Group-Fast
    set pname3 = Group-Slow
    set cof = 1000
    set labT = '"Group-Velocity (Km/s)"'
    unset gpfs
    GM
  endif
  
  if ($?ppfs) then
    set puls = 1
    set puls2 = 3
    set pname = Phase-Prim
    set pname2 = Phase-Fast
    set pname3 = Phase-Slow
    set cof = 1000
    set labT = '"Phase-Velocity (Km/s)"'
    unset ppfs
    GM
  endif
   
  if ($?lin) then
    set pname = Linear-Comp
    set labT = '"Linear compressibility (TPa-1)"'
    unset lin
    GM
  endif

  if ($?dir) then
    set pname = Direc-Bulk
    set labT = '"Directional Bulk modulus (TPa)"'
    unset dir
    GM
  endif

 
  if ($?you) then
    set pname = Young-Modu
    set labT = '"Young modulus (GPa)"'
    unset you
    GM
  endif
    
  if ($?ppm) then 
    set pname = Phase-Prim
    set cof = 1000
    set labT = '"Phase-Velocity P-Mode (Km/s)"'
    unset ppm
    GM
    endif
    
  if ($?pfm) then
    set pname = Phase-Fast
    set cof = 1000
    set labT = '"Phase-Velocity F-Mode (Km/s)"'
    unset pfm
    GM
  endif
    
    
  if ($?psm) then
    set pname = Phase-Slow
    set cof = 1000
    set labT = '"Phase-Velocity S-Mode (Km/s)"'
    unset psm
    GM
  endif
    

  if ($?gpm) then
    set pname = Group-Prim
    set cof = 1000
    set labT = '"Group-Velocity P-Mode (Km/s)"'
    unset gpm
    GM
  endif
    
  if ($?gfm) then
    set pname = Group-Fast
    set cof = 1000
    set labT = '"Group-Velocity F-Mode (Km/s)"'
    unset gfm
    GM
  endif
    
  if ($?gsm) then
    set pname = Group-Slow
    set cof = 1000
    set labT = '"Group-Velocity S-Mode (Km/s)"'
    unset gsm
    GM
  endif

  if ($?bul) then
    set pname = Bulk-Modu
    set labT = '"Volume Bulk modulus (GPa)"'
    unset bul
    GM
 endif

makegnu:

#---------------------check input file----------
set chinf = "$pname$in"
#echo "$chinf"
if !(-e "$pname$in") then
  set filer = "$pname$in"
  goto error
endif

if ($puls2 == 3) then
  if !(-e "$pname2$in") then
   set filer = "$pname2$in"
   goto error
  endif
  if !(-e "$pname3$in") then
   set filer = "$pname3$in"
   goto error
  endif
 
endif 
#-----------------------------------------------



set c1 = '$3'
set c2 = '$4'
set eve = ""
set eve2 = ""
set eve3 = ""
set wcl = ""
set wcl2 = ""
set wcl3 = ""
set tiyl = `echo "set title $labT"`
set splmax = ""
set splmin = ""
set print = "$pname$out1"
if ($puls == 1 ) set print = `echo "Phase$out1"`
if ($puls == 2 ) set print = `echo "Group$out1"`

if ($?xy) then
set print = "$pname$out3"
set c1 = '$1/'$cof
set c2 = '$2'
if ($puls == 1 ) set print = `echo "Phase$out3"`
if ($puls == 2 ) set print = `echo "Group$out3"`
endif

if ($?rxy) then
set print = "$pname$out2"
set eve = "every::0::nu-2"
set tiyl = `echo "set ylabel $labT"`
set wcl = `echo "nu=system('wc -l < $pname$in')"`

#if ($?shr || $?poi || $?gpfs || $?ppfs) then
if ($puls2 == 3) then
set wcl2 = `echo "numax=system('wc -l < $pname2$in')"`
set wcl3 = `echo "numin=system('wc -l < $pname3$in')"`
set eve2 = "every::0::numax-2"
set eve3 = "every::0::numin-2"
endif
if ($puls == 1 ) set print = `echo "Phase$out2"`
if ($puls == 2 ) set print = `echo "Group$out2"`
endif

set spl = `echo "pl '$pname$in' u ($c1):($c2/$cof) $eve"` 
set stat = "stats '$pname$in' using 4 nooutput"

#if ($?shr || $?poi || $?gpfs || $?ppfs) then
if ($puls2 == 3) then
set splmax = `echo ", '$pname2$in' u ($c1):($c2/$cof) $eve2 "`
set splmin = `echo ", '$pname3$in' u ($c1):($c2/$cof) $eve3 "`
endif

@ j = $j + 1

rm -f $tmp
#--------------------------------------------
  if ($?rxy) then
  goto plotrxy
  endif
  if ($?xy) then
  goto plotxy
  endif

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

  if ($?all) goto down

if ($?sho) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties Vs Angle
 $stat
 print 'Maximum:',STATS_max
 set angles degrees
 set key rmargin vertical top title "Polar gridded surface" font ":Bold"
 unset border
 set size ratio -1
 set polar
 set grid polar
#set rmargin 18.5
#set lmargin 18.5
set grid front
set tics front
unset xtics
unset ytics
cof=$cof

set rtics format "%3.1f"
set rtics (STATS_max/(2*cof))

set ttics 0,60
set border 0 polar
set rrange [*:*] noextend
#set isotropic
 $ls lc $lc lw $lw 
 $ls2 lc $lc2 lw $lw2 
 $ls3 lc $lc3 lw $lw3 
 $tiyl
 $spl$wll$tit1$splmax$wll2$tit2$splmin$wll3$tit3
show polar
show grid
reset
pause -1 "  Press <enter> to continue..."
EOF

RgnuQcop 
endif   #sho

down:

if ($hardcopy == y || $hardcopy == Y ) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties Vs Angle
reset
$stat
set terminal pngcairo enhanced font $fosi
set output  "$print"
 set angles degrees
 set key rmargin vertical top title "Polar gridded surface" font ":Bold"
 unset border
 set size ratio -1
 set polar
 set grid polar
#set rmargin 18.5
#set lmargin 18.5
set grid front
set tics front
unset xtics
unset ytics
cof=$cof

set rtics format "%3.1f"
set rtics (STATS_max/(2*cof))
set ttics 0,60
set border 0 polar
set rrange [*:*] noextend
#set isotropic
 $ls lc $lc lw $lw 
 $ls2 lc $lc2 lw $lw2 
 $ls3 lc $lc3 lw $lw3 
 $tiyl
 $spl$wll$tit1$splmax$wll2$tit2$splmin$wll3$tit3

unset output
reset
EOF

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

endif
goto downwhile
#---------------------------------------------------------------------

plotrxy:

  if ($?all) goto downrxy

if ($?sho) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties Vs Angle
 $tiyl
 set xlabel "{/Symbol f} (Degree)" 
 set xrange [-180:180]

 set xtics format "%3.0f"
 set xtics 30 
 set mxtics 3

#set ytics format  "%3.0f" 
# set ytics 30  
# set mytics 3
 $ls lc $lc lw $lw 
 $ls2 lc $lc2 lw $lw2 
 $ls3 lc $lc3 lw $lw3
 $wcl
 $wcl2
 $wcl3
 $spl$wll$tit1$splmax$wll2$tit2$splmin$wll3$tit3

reset
pause -1 "  Press <enter> to continue..."
EOF

RgnuQcop
endif   #sho

downrxy:

if ($hardcopy == y || $hardcopy == Y ) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties Vs Angle
reset
set terminal pngcairo enhanced font $fosi
set output  "$print"
 $tiyl
 set xlabel "{/Symbol f} (Degree)" 
 set xrange [-180:180]

 set xtics format "%3.0f"
 set xtics 30 
 set mxtics 3

 #set ytics format  "%3.0f" 
 #set ytics 30  
 #set mytics 3
 $ls lc $lc lw $lw 
 $ls2 lc $lc2 lw $lw2 
 $ls3 lc $lc3 lw $lw3 
 $wcl
 $wcl2
 $wcl3
 $spl$wll$tit1$splmax$wll2$tit2$splmin$wll3$tit3

unset output
reset
EOF

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

endif
goto downwhile
#----------------------------------------------------------
plotxy:
  if ($?all) goto downxy

if ($?sho) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties in cartesian coordinate
 $tiyl
 set xlabel "X" 
 set ylabel "Y"

# set xtics format "%3.0f"
# set xtics 30 
# set mxtics 3

#set ytics format  "%3.0f" 
# set ytics 30  
# set mytics 3
 $ls lc $lc lw $lw 
 $ls2 lc $lc2 lw $lw2 
 $ls3 lc $lc3 lw $lw3
 $spl$wll$tit1$splmax$wll2$tit2$splmin$wll3$tit3

reset
pause -1 "  Press <enter> to continue..."
EOF

RgnuQcop
endif   #sho

downxy:

if ($hardcopy == y || $hardcopy == Y ) then

cat <<EOF >$tmp
#Gunplot file for plotting Elastic Properties in cartesian coordinate
reset
set terminal pngcairo enhanced font $fosi
set output  "$print"
 $tiyl
 set xlabel "X" 
 set ylabel "Y"

# set xtics format "%3.0f"
# set xtics 30 
# set mxtics 3

 #set ytics format  "%3.0f" 
 #set ytics 30  
 #set mytics 3
 $ls lc $lc lw $lw 
 $ls2 lc $lc2 lw $lw2 
 $ls3 lc $lc3 lw $lw3 
 $spl$wll$tit1$splmax$wll2$tit2$splmin$wll3$tit3

unset output
reset
EOF

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

endif
#-----------------------------------------------------------

downwhile:
set splmax = ''
set splmin = ''
set title = ''
set tiyl = ''
set labT = ''
set tit1 = 'notitle'
set tit2 = ''
set tit3 = ''
set fosi = "'Verdana,11'"
set smb2 = ''
set smb3 = ''
set ls2 = "#set style line 2"
set ls3 = "#set style line 3"
set wll2 = ''
set wll3 = ''
set tmp = tmp
set puls = 0
set puls2 = 0

end  #while


exit
#----------------------------END plot--------------------
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: Polarplot_lapw

                A plotting interface to plot 2D-CUT visualizations of elastic properties(convex hull) 
                within Polar or Cartesian coordinates.

       PURPOSE: 2D-CUT plot(Polar/Cartesian coordinates) of elastic constants properties as png format
      
       USAGE:   Polarplot_lapw [OPTIONS] [FLAGS]
       FLAGS:   -h/-H ---->  help

                 
       OPTIONS:
       -you ---> 2D-CUT plot(Polar Cord) of Young Modulus
       -lin:     2D-CUT plot(Polar Cord) of Linear Compressibility
       -shr ---> 2D-CUT plot(Polar Cord) of Shear Ratio
       -poi:     2D-CUT plot(Polar Cord) of Poisson's Ratio 
       -dir ---> 2D-CUT plot(Polar Cord) of Directional Bulk Modulus
       -bul:     2D-CUT plot(Polar Cord) of Bulk Modulus
       -ppm ---> 2D-CUT plot(Polar Cord) of Phase Velocity: P-Mode
       -psm:     2D-CUT plot(Polar Cord) of Phase Velocity: S-Mode
       -pfm ---> 2D-CUT plot(Polar Cord) of Phase Velocity: F-Mode
       -gpm:     2D-CUT plot(Polar Cord) of Group Velocity: P-Mode
       -gsm ---> 2D-CUT plot(Polar Cord) of Group Velocity: S-Mode
       -gfm:     2D-CUT plot(Polar Cord) of Group Velocity: F-Mode
      -ppfs:---> 2D-CUT plot(Polar Cord) of Phase Velocity: P,F,S Mode
      -gpfs:     2D-CUT plot(Polar Cord) of Group Velocity: P,F,S Mode
      -all :---> 2D-CUT plot(Polar Cord) of all properties
      -sho :     Show and then generates 2D-CUT plots (excluding "-all")
      -rxy :---> 2D-CUT plot(r vs theta) within cartesian coordinates
       -xy :     2D-CUT plot(x and y)    within cartesian coordinates
 
       COLOR PLOT OPTIONS:
       -lc :      set line color 1
       -lw :      set line width 1
       
        for Poisson, Shear ratio and Phase, Group velocity (P,F,S Mode)
       -lc2:      set line color 2
       -lw2:      set line width 2
       -lc3:      set line color 3
       -lw3:      set line width 3
 
        example:
          ==========For plot in polar coordinate==============    
          Polarplot_lapw -you -sho -lc 1 -lw 2
          Polarplot_lapw -ppfs -sho -lc 1 -lw 1 -lc2 2 -lw2 2 -lc3 3 -lw3 3           
          Polarplot_lapw -all -lc 1 -lw 1 -lc2 2 -lw2 2 -lc3 3 -lw3 3    
          ==========For plot in cartesian coordinate==========    
          Polarplot_lapw -you -sho -rxy -lc 1 -lw 2
              
EOF
