#!/usr/bin/sh

# This Shell script can help you to do PC reduction on data files with a regular
# pattern. Also, this is very helpful for using Condor system. Make changes
# before you use to match your naming system and directory structure.
#
# Wei Wang, Sep 2004

echo "starting run $1"
echo "ending run $2"
count=$1

# Your Perl script, binary and source and target directories.

BINDIR=/home/atmpd/pc_red/sk2_may05/bin_sk04a/patcomb
BINARY=/home/atmpd/pc_red/sk2_may05/bin_sk04a/patcomb/solaris_sparc/patall
WORKDIR=/net/sukul1/atmpd1/fdufour
#MCDIR=${WORKDIR}/pat2nd
DATADIR=/net/sukatmd1/work20/0609/patrd2

while [ $count -le $2 ]; do
#  Pattern of the data file names.
   RUNUM=`echo $count | awk '{str=""}; {if (length($0)<6) for (i=1; i<=6-length($0); i++) str=str"0"}; {print str$0}'`
   COMMPRT="${DATADIR}/patrd2.run${RUNUM}"
   FLNUM=`grep run${RUNUM} ${WORKDIR}/allrun.list | wc -l`

   echo "Run ${RUNUM} has $FLNUM files"

   if [ ${FLNUM} -ge 1 ]; then

   num=0
   PC4LST=`ls ${COMMPRT}.*`
   for tgtfile in ${PC4LST}; do
       echo $tgtfile
       VOL=`ls -l ${tgtfile} | awk '{print $5}'`
       if [ ${VOL} -gt 0 ]; then
          echo ${VOL}
	  FILELIST="${FILELIST} ${tgtfile}"
          num=`expr ${num} + 1`
       fi
   done

   if [ $num -ge 1 ]; then
      echo Processing ${FILELIST} 
# Provide FLAGNAME for MC: 6 digits
      FLAGNAME=${RUNUM}.001
      export FLAGNAME
      ${BINDIR}/patall.pl 1 0 2 4 ${BINARY} ${WORKDIR}/log ${WORKDIR} LOCAL LOCAL 1 ${FILELIST} > ${WORKDIR}/log/logs/log-pc2.${RUNUM} 2>&1
   fi
   fi

   count=`expr ${count} + 1`
   unset FILELIST
   unset FLNUM
done

exit
