#!/bin/tcsh -f
#  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
set vdir = /tmp/$USER
# ptcl code to be displayed. (charged/neutral/both can be
#                             specfied from command line)
set ptcls = "1 2 3 4 5 6 9"

	#***********************************


# --------------------
# 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
endif

if( $#argv < 1 || $#argv >6 ) then
        echo "You gave "  $#argv " arguments"
  error:
        echo " Usage: "
	echo "./disptracebygeomv [-{c|z|b}] tracedata  [earth]|[array  [sx sy zmax]]"
	echo " "
        gawk ' $2 ~ /---------/, $2 ~ /===========/' $0 | less
	exit 1
endif


set sx = 0.0
set sy = 0.0
set zmax = 1.0e20
set chg = "-c"
if (  "$1" == "-c"  || "$1" == "-z"  ||  "$1" == "-b"  ) then
	set chg = $1
	shift
else if ( "$1" =~ "-"* )  then
	echo $1 " is not supported "
	goto error
endif


if($#argv == 2) then
        if( $2 == "earth" ) then
            set case = "3"
        else if( $2 == "array" ) then
#              trace must be E-xyz in this case
	    set case = "2-b"
        else
 	    goto error
        endif
else if($#argv == 3 ) then
    if($2 == "earth" ) then
       if($3 == "array" ) then
           set case = "4"
       else
           goto error
       endif
    else
          goto error
    endif
else if( $#argv == 5 ) then
    if( $2 == "array" ) then
         set case = "2-a"
         set sx = $3
	 set sy = $4
	 set zmax = $5
    else
         goto error
    endif
else if( $#argv == 1 ) then
    set case = "1"
else
    goto error
endif



rm -f  $vdir/ptcl*.vect


foreach code( $ptcls )
    foreach charge(`echo "n z p"` )
       if( "$code" == 1 && "$charge" != "z")  goto next
       if( "$code" == 2 && "$charge" == "z")  goto next
       if( "$code" == 3 && "$charge" == "z")  goto next
#            neglect pi0 tracks
       if( "$code" == 4 && "$charge" == "z")  goto next  
       if( ("$chg" == "-c" && "$charge" != "z" ) || \
       	   ("$chg" == "-z" && "$charge" == "z")  ||  ("$chg" == "-b")) then
    	  gawk -f first.awk chg=$charge \
          code=$code sx=$sx sy=$sy zmax=$zmax  $1 > $vdir/vertexdata
          set x = `ls -l $vdir/vertexdata` 
	  if( $x[5] > 2 ) then
            echo "VECT" > $vdir/ptcl${code}${charge}.vect
	    gawk -f second.awk $vdir/vertexdata >> $vdir/ptcl${code}${charge}.vect
      	    cat $vdir/vertexdata >> $vdir/ptcl${code}${charge}.vect
	    if ( $code <= 6 ) then
	   	    gawk '$2 == code && $3 == chg {print $4, $5, $6, $7}' \
		    code=$code chg=$charge colortab >> $vdir/ptcl${code}${charge}.vect
            else if ( $code == 9 )  then
	   	    gawk '$2 == code && $3 == chg  {print $4, $5, $6, $7}' \
		    code=$code chg=$charge colortab >> $vdir/ptcl${code}${charge}.vect
            else
	   	    gawk '$2 == "any" {print $4, $5, $6, $7}' \
		    colortab >> $vdir/ptcl${code}${charge}.vect
            endif	 
      	   ./splitiftoolarge $vdir/ptcl${code}${charge}.vect
          endif
       endif
       next:
     end
end

echo $case
echo "*********** GEOMVIEW is " $GEOMVIEW
switch ( $case )
    case "1":
	   $GEOMVIEW  $vdir/ptcl???*.vect &
	   breaksw
    case "2-a":
           $GEOMVIEW  ./Array/loadarray  $vdir/ptcl???*.vect  &
	   breaksw
    case "2-b":
           $GEOMVIEW  ./Array/loadarrayOnEarth  $vdir/ptcl???*.vect  &
	   breaksw
    case "3":
           $GEOMVIEW  ./Globe/earth  $vdir/ptcl???*.vect  &
	   breaksw
    case "4":
           $GEOMVIEW  ./Globe/earth ./Array/loadarrayOnEarth  $vdir/ptcl???*.vect  &
	   breaksw
endsw     
