#!/bin/csh -f
if ( $#argv != 3) then
	echo chvscomp array config outfile
        echo " where array is the  basic detector info. file"
	exit 1
endif

#   if you need more info.(say, area) added output here. and change gawk below
#      make a.out to get expanded config

(cd $EPICSTOP/Util; rm -f a.out;make clean; make -f testCnf1.mk)

#   extract only scin ch,x,y,z;  NEXTSTEP has to be treated strangely
#if( -f /usr/bin/arch ) then
#   if( `/usr/bin/arch` == 'i386')  then
#                   nextstep has limitaions due to absoft stupid compiler
     (cd $EPICSTOP/Util; echo "../UserHook/TibetArray/$2" | a.out | awk '$4=="SCIN" && $8 =="/" {print $1, $9, $10, $11;next}; $4=="SCIN" {print $1, $10, $11, $12}' > ../UserHook/TibetArray/scinCXYZ )
#   else
#               normal arch
#          echo $2 | $EPICSTOP/Util/a.out | awk '$4=="SCIN" {print $1, $9,$10,$11}' > scinCXYZ
#   endif
#else
#               normal arch
#     echo $2 | $EPICSTOP/Util/a.out | awk '$4=="SCIN" {print $1, $9,$10,$11}' > scinCXYZ
#endif	


if( $3 == "chvscomp" ) then
	echo "output file name invalid"
	exit 1
endif
if( -f $3 ) then
   rm -f $3
endif

echo "#  ch number vs component number, x, y, z are  being made in " $3

foreach g ("`cat scinCXYZ`")
   set f = `echo $g`
   set comp = $f[1]
   set x = $f[2]
   set y = $f[3]
   set z = $f[4]
#        $7>0 is to get live detectors  

   gawk '$7>=0 {x= (ix+35.35)/100.; y=(iy+35.35)/100.; if(x-$2 < 1.e-2 && x-$2 > -1.e-2 && y-$3 < 1.e-2 && y-$3 > -1.e-2) {print  $1, comp, x*100, y*100, iz, $0}}' ix=$x iy=$y iz=$z comp=$comp $1 >> $3
end



