#!/bin/bash
#   -f in the above line should not be used else error will happen
#  at the 	$GEOMVIEW $vdir/ptcl???*.vect;; line.

#  convert trace data and display it by  Geomview.

#****************** fix below 
#
# ************* to store the converted data for Geomview
#   such as ptcl2p1.vect etc.
#set vdir = ../Work
vdir=/tmp/$USER
# ptcl code to be displayed. (charged/neutral/both can be
#                             specfied from command line)
ptcls="1 2 3 4 5 6 7 9"
	#***********************************
function showerror() {
    #  error exit
    echo "Usage: "
    echo "  (in Cosmos/Util/Geomview )"
    echo "./disptracebygeomv [-{c|z|b}] tracedata  [earth]|[array  [sx sy zmax]]"
    awk ' $2 ~ /---------/, $2 ~ /===========/' $0 | less
    exit 1
}    
# --------------------
# Display type.
# 1)  only trace data.  Trace data type is arbitrary (Trace=any).
#
#          Usage:  disptracebygeomv [-{c|z|b}]  tracedata 
#
# 2)  Trace data + array.
#     2-a)   trace data is in detector system (Trace = 21)
#
#          Usage: disptracebygeomv  [-{c|z|b}] tracedata  array sx sy zmax
#
#     2-b)   trace data is in  E-xyz sytem (Trace = 41).
#
#          Usage:  disptracebygeomv  tracedata array 
#
# 3)  Trace data +  Earth.          Trace data in E-xyz system(Trace=41)
#         Usage:  disptracebygeomv [-{c|z|b}] tracedata earth
#
# 4)  Trace data + Earth + array.   Trace data in E-xyz system (Trace=41)
#
#         Usage:  disptracebygeomv [-{c|z|b}] tracedata earth array
#
# where
#   [-{c|z|b}] specifies display of charged particles only
#           or neutral particles only, or both. partilce codes
#           to be displayed are given in $ptcls. You must be carful
#           to use -z or -b; huge number of gamma rays must be
#           displayed.  The default is -c.
#   earth:  should be given literally.  Earth sphere is added in the
#           view.   If earth is shown, you should not try to enlarge
#           the view more tht 10^6 times because Geomview uses single
#           precision calculation; you will lose  correct
#           control if you try to look  each detector (~ 1m scale)
#           clearly.
#   array:  should be gvien literally.  Air shower array map is added
#           in the view.  If type 4 display,
#           'matrix' in ./Array directory must be prepared depending on
#           the location of the array.
#        If trace is in detector system
#        you have to add the following 3:
#         sx:     This value is added to  x. (m)
#         sy:     This value is added to  y. (m)
#       zmax:     data of  z > zmax is neglected.  (m)
#
#  To fix the color for each particle code, colortab is used.
#  =======================

if [ !  -d $vdir ]; then
    echo $vdir "not exists; I am creating it"
    mkdir -p $vdir
fi

if [ $# -lt 1 ] || [ $# -gt 6 ]; then
  echo "You gave "  $# " arguments"
  showerror;
fi

sx=0.0
sy=0.0
zmax=1.0e20
chg="-c"
if [[  $1 == "-c"  || $1 == "-z"  ||  $1 == "-b" ]]; then
    chg=$1
    shift
elif [[ $1 =~ "-"?* ]]; then
    echo $1 " is not supported "
    showerror;
fi
  
if [ $# -eq 2 ]; then
    if [ $2 == "earth" ]; then
	case="3"
    elif [ $2 == "array" ];  then
       #              trace must be E-xyz in this case
	case="2-b"
    else
	showerror;
    fi
elif [  $# -eq 3  ]; then 
    if [ $2 == "earth" ]; then
	if [ $3 == "array" ]; then
            case="4"
	else
            showerror;
	fi
    else
        showerror;
    fi
elif [ $# -eq  5 ]; then
    if [ $2 == "array" ];then
        case="2-a"
        sx=$3
	sy=$4
	amax=$5;
    else
	showerror
    fi
elif [ $# -eq 1 ]; then
    case="1"
else
    showerror;
fi



rm -f  $vdir/ptcl*.vect


for code in  $ptcls; do
    for  charge in n z p; do
	if [ $code -eq 1 ]  &&  [ "$charge" != "z" ]; then
	    continue
	elif [[ $code -eq 2 && "$charge" == "z" ]]; then
	    continue
	elif [[ $code -eq 3 && "$charge" == "z" ]]; then
	    continue
	fi
#            neglect pi0 tracks
	if [[ $code -eq 4 ]] && [[ "$charge" == "z" ]]; then
	    continue
	elif [[ ("$chg" == "-c" && "$charge" != "z" ) || \
       	   ("$chg" == "-z" && "$charge" == "z")  ||  ("$chg" == "-b") ]]; then
    	    awk -f first.awk chg=$charge \
		code=$code sx=$sx sy=$sy zmax=$zmax  $1 > $vdir/vertexdata
             x=`ls -l $vdir/vertexdata` 
	     #	     if [ $x[5] > 2 ];  then
	     if [ `echo $x | awk '{print $5}'` -gt 2 ];  then
		 echo "VECT" > $vdir/ptcl${code}${charge}.vect
		 awk -f second.awk $vdir/vertexdata >> $vdir/ptcl${code}${charge}.vect
      		 cat $vdir/vertexdata >> $vdir/ptcl${code}${charge}.vect
		 if [ $code -le 6 ]; then
	   	    awk '$2 == code && $3 == chg {print $4, $5, $6, $7}' \
		    code=$code chg=$charge colortab >> $vdir/ptcl${code}${charge}.vect
		 elif [ $code -eq 9 ];  then
	   	    awk '$2 == code && $3 == chg  {print $4, $5, $6, $7}' \
		    code=$code chg=$charge colortab >> $vdir/ptcl${code}${charge}.vect
		 else
	   	    awk '$2 == "any" {print $4, $5, $6, $7}' \
		    colortab >> $vdir/ptcl${code}${charge}.vect
		 fi
      		 ./splitiftoolarge $vdir/ptcl${code}${charge}.vect
	     fi
	fi
    done
done

## echo  "case is "$case
echo "*********** GEOMVIEW is " $GEOMVIEW
case  $case  in
    "1")
##	echo "now 1";  #
	$GEOMVIEW $vdir/ptcl???*.vect;;
    "2-a")
        $GEOMVIEW ./Array/loadarray  $vdir/ptcl???*.vect;;
    "2-b")
        $GEOMVIEW ./Array/loadarrayOnEarth  $vdir/ptcl???*.vect ;;
    "3")
        $GEOMVIEW ./Globe/earth  $vdir/ptcl???*.vect;;

    "4")
        $GEOMVIEW ./Globe/earth ./Array/loadarrayOnEarth  $vdir/ptcl???*.vect ;
esac
