#!/bin/tcsh -f
# interface for making directories and calling SRC_IRelakit's programs
# provided by Morteza Jamal(2024)"
#
alias BTcolor 'tput setab 0;tput setaf 15'	#i define it for Background and Text Color
unalias rm
tty -s < /dev/stdout
set cpvc = $status  #check program value for color#
if ( $cpvc == 0 ) then
BTcolor
endif
echo ""
echo ""
#echo "####################################"
#echo "#        Call programs for         #"
#echo "#   making data and plot files     #"
#echo "#       by Morteza Jamal           #"
#echo "#          (C) 2024                #" 
#echo "####################################"
IRelakit.tmpIRelakit
echo ""
echo ""
sleep 5
set tmp = "hkl_"
set df = "DataFile"
set Pf = "PicFile"
#set w2d = "WrlPng2D"
#set heat = "Heat"
#set pxy = "PolarXy"
set un = "-"
clear
echo ""
echo "I guide you what the steps i run"
echo ""
echo "1) call 'IRelakit' for making data file."
echo "----> For 3D Calculations"
echo "2) call 'xyz2wrl -all -dfc' for making 3D wrl files."
echo "3) call 'xyz2gnu -all -mc'  for making 3D gnu files."
echo "4) call 'Heatmap_lapw -all' for making Heat map files."
echo "----> For 2D-cut Calculations"
echo "5) call 'xyz2wrl -all -dfc -2d' for making 2D-cut wrl files."
echo "6) call 'Heatmap_lapw -all -2d' for making 2D-cut Heat map files.If gnuplot supports"
echo "7) call 'Polarplot_lapw -all' for making 2D-cut files (Polar coordinates)."
echo "8) call 'Polarplot_lapw -all -rxy' for making 2D-cut files (Cartesian coordinates)."
sleep 6

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

IRelakit
if !(-e "hkl") then
  set filer = "hkl"
  goto error
endif

set h = `head -1 < hkl|awk '{printf("%.0f",$1)}'`
set k = `head -1 < hkl|awk '{printf("%.0f",$2)}'`
set l = `head -1 < hkl|awk '{printf("%.0f",$3)}'`
set hkldir = `echo "$tmp$h$k$l"`
if (-e  $hkldir ) then
set i=`/bin/ls -d $hkldir* |wc `
set hkldir2 = `echo "$hkldir$un$i[1]"`
echo " saving pervious $hkldir directory to $hkldir2"
cp -r $hkldir  $hkldir2
rm -r $hkldir
endif

mkdir $hkldir
cd $hkldir
mkdir $df
cd ..
#----------------------------------------------------------------
if ( $cpvc == 0 ) then
BTcolor
endif
echo ""
echo -n " Do you want to plot data files? (y/N)"
    set pdata = ($<)
    
if ($pdata == y || $pdata == Y ) then
    echo ""
	echo " I am generating... "
    echo ""
cd $hkldir
mkdir $Pf
#mkdir $w2d
#mkdir $heat
#mkdir $pxy
cd ..
#------------------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."
else
goto errorEXE
endif
#----------------------------------------------------------------

#------------------check program xyz2wrl-------------------------------
set fexe = `echo "xyz2wrl"`
which $fexe >& /dev/null
set cpv = $status  #check program value#
if ( $cpv == 0 ) then
echo ""
echo "xyz2wrl program found."
else
goto errorEXE
endif
#------------------check program xyz2gnu-------------------------------
set fexe = `echo "xyz2gnu"`
which $fexe >& /dev/null
set cpv = $status  #check program value#
if ( $cpv == 0 ) then
echo ""
echo "xyz2gnu program found."
else
goto errorEXE
endif
#------------------check program HeatMap_lapw--------------------------
set fexe = `echo "HeatMap_lapw"`
which $fexe >& /dev/null
set cpv = $status  #check program value#
if ( $cpv == 0 ) then
echo ""
echo "HeatMap_lapw program found."
else
goto errorEXE
endif
#------------------check program Polarplot_lapw------------------------
set fexe = `echo "Polarplot_lapw"`
which $fexe >& /dev/null
set cpv = $status  #check program value#
if ( $cpv == 0 ) then
echo ""
echo "PolarPlot_lapw program found."
else
goto errorEXE
endif
#----------------------------------------------------------------------
xyz2wrl -all -dfc
xyz2gnu -all -mc
HeatMap_lapw -all
xyz2wrl -all -dfc -2d
echo ""
echo ""
echo "================================================================"
echo "                  HeatMap_lapw -all -2d                         "
echo "Generate a 2D polar heat map in range of theta and r(for 2D-CUT)"
echo "if gnuplot supports polar gridding"
echo "================================================================"
echo ""
echo ""
sleep 4
HeatMap_lapw -all -2d
Polarplot_lapw -all 
Polarplot_lapw -all -rxy

mv *2D.wrl $hkldir/$Pf/.
mv *.wrl $hkldir/$Pf/.
mv *gnu.png $hkldir/$Pf/.

#cd $Pf
#rm *2D.wrl
#cd ..
mv *2DB.png $hkldir/$Pf/.
mv *HM*.png $hkldir/$Pf/.
mv *2DBrxy.png  $hkldir/$Pf/.

endif
#--------------------------------------------------------------------
mv *.dat $hkldir/$df/.
echo ""
echo "===================================="
echo "        Have a nice day             "
echo "                                    "
echo "===================================="
if ( $cpvc == 0 ) then
tput sgr0
endif
exit(0)

error:
echo ">>>"
echo ">>> ERROR: $filer not found\!"
echo ">>> ERROR:"
echo ">>>"
#echo "$filer" > .stopIR
exit(1)

#---------------------------------------------------------------
errorEXE:					#error exit	
printf "\n   stop error: Required program $fexe not found\n" 
printf "\n" 
exit(2)

