combine (version 1.6)

You can also look at:
#! /usr/bin/ksh

###########################################################################
#
# 
# SYNTAX: combine
#
# BRIEFLY: Merge the event files created by frfread.
#
# DESCRIPTION: This routine combines the event files produced by frfread
# DESCRIPTION: to produce unprocessed, unfiltered event files.
# DESCRIPTION: The observation mode of an event file is specified by
# DESCRIPTION: about 70 FITS keywords. Event files which have identical values
# DESCRIPTION: for all of these keywords are merged into a single event
# DESCRIPTION: file for further processing.
#
# VERSION: 1.6
#
# HISTORY: 0.0 -> 1.0 8/23/96 
# HISTORY: Now only deletes the raw files which
# HISTORY: are used to make unfiltered files. The remaining "scraps"
# HISTORY: are tarred together to make the scraps file which is kept as
# HISTORY: a trend product.
# HISTORY:
# HISTORY: 1.0 -> 1.1 9/20/96
# HISTORY: Fixed a bug which used i for the index in two nested loops
# HISTORY: The result was that only the first telemetry file's raw files
# HISTORY: were being merged. This was fixed by changing the outermost
# HISTORY: loops index from i to telnum.
# HISTORY:
# HISTORY: 1.1 -> 1.2 9/25/96
# HISTORY: The last fix was still not good enough since the index i was being
# HISTORY: reset each time the script went to a new telemetry file. The result
# HISTORY: was that unscreened files were getting overwritten.
# HISTORY: Fixed things by saving the running count for each instrument
# HISTORY: individually.
# HISTORY: 
# HISTORY: 1.2 -> 1.3 10/8/96
# HISTORY: Modified to use the cmerge STOOL to merge event files instead
# HISTORY: of the merge utility script.
# HISTORY: 
# HISTORY: 1.3 -> 1.4 12/10/96
# HISTORY: Corrected a bug where the call to the merge utility was never
# HISTORY: deleted. cmerge was called immediately afterwards so the cmerge
# HISTORY: output would clobber the merge output, but it must have been
# HISTORY: slowing things down considerably
# HISTORY: 
# HISTORY: 1.4 -> 1.5 1/23/97
# HISTORY: added a check to see if there are any scraps files to tar before 
# HISTORY: trying to tar them. Also fixed bad syntax in the exception call 
# HISTORY: for the tar.
# HISTORY: 2/5/97 added a log entry giving the file names of the merged 
# HISTORY: event files.
# HISTORY: 
# HISTORY: 1.5 -> 1.6 3/18/97
# HISTORY: Now add ORIGMODE keyword to all eventfiles. This is to later
# HISTORY: distinguish on-board and ground converted BRIGHT mode files
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Merging event files from frfread"

###############################################
# read number of telemetry files from job par
##############################################
nfiles=$($UTIL/read_parfile $JOBPAR frfcnt)
if [ -z "$nfiles" ]; then
     nfiles=0
fi

########################
# debugging output
#########################
if [ -n "$DEBUG" ]; then
     echo ${0##*/}: nfiles=$nfiles
fi



###################################################################
# loop through the parent telemetry files so that
# event files from different telemetry files will not be merged
#################################################################

####################################
# initialize running file indicies
####################################
sis0index=1
sis1index=1
gis2index=1
gis3index=1
typeset -i i


typeset -i telnum=1
while [ $telnum -le $nfiles ]; do

     ####################################
     # get base filename for event files
     ####################################
     number="$(echo $telnum | nawk '{printf ("%03d", $0)}')"
     telem=$($UTIL/read_parfile $JOBPAR frffile_$number)
     telem=${telem##*/}
     telem=${telem%.Z}
     telem=$(echo $telem | tr '.' '_')

     ########################
     # debugging output
     #########################
     if [ -n "$DEBUG" ]; then
          echo ${0##*/}: telnum=$telnum
          echo ${0##*/}: telem=$telem
     fi
       

     for inst in g2 g3 s0 s1; do

          case "$inst" in 
          g2) 
               caps=G2
               display=GIS2
               min_events=$($UTIL/read_parfile $PARFILE gis_events_min );;
          g3) 
               caps=G3
               display=GIS3
               min_events=$($UTIL/read_parfile $PARFILE gis_events_min );;

          s0) 
               caps=S0
               display=SIS0
               min_events=$($UTIL/read_parfile $PARFILE sis_events_min );;

          s1) 
               caps=S1
               display=SIS1
               min_events=$($UTIL/read_parfile $PARFILE sis_events_min );;

          esac

          ###########################
          # sort files by keywords
          ###########################
          $UTIL/log_entry "Collecting $display event files by mode"

          case $inst in
          g?)
               ####################
               # GIS files
               ####################
               ls ${telem}${caps}*[HML].fits \
                                 |$STOOLS/gissortcode >code.tmp 2>stderr.log
               $UTIL/sort_test gissortcode $0 1 stderr.log

               sort code.tmp | $STOOLS/gissortsplit >split.tmp 2>stderr.log
               $UTIL/sort_test gissortsplit $0 1 stderr.log;;
          s0)
               ####################
               # SIS0 files
               ####################
               ls ${telem}${caps}*[HML].fits \
                                |$STOOLS/sis0sortcode >code.tmp 2>stderr.log
               $UTIL/sort_test sis0sortcode $0 1 stderr.log
 
               sort code.tmp | $STOOLS/sis0sortsplit >split.tmp 2>stderr.log
               $UTIL/sort_test sis0sortsplit $0 1 stderr.log;;
 
          s1)
               ####################
               # SIS1 files
               ####################
               ls ${telem}${caps}*[HML].fits \
                                |$STOOLS/sis1sortcode >code.tmp 2>stderr.log
               $UTIL/sort_test sis1sortcode $0 1 stderr.log

               sort code.tmp | $STOOLS/sis1sortsplit >split.tmp 2>stderr.log
               $UTIL/sort_test sis1sortsplit $0 1 stderr.log;;

          esac

          rm -f code.tmp

          sort -r split.tmp >listlist.tmp
          rm -f split.tmp

          #########################################################
          # set the index to the correct value for that instrument
          #########################################################
          case "$inst" in
          s0) i=$sis0index;;
          s1) i=$sis1index;;
          g2) i=$gis2index;;
          g3) i=$gis3index;;
          esac

          for list in  $(awk '{print $2}' listlist.tmp); do

               nevents="$(grep $list listlist.tmp | awk '{print $1}')"
 
               if [ $nevents -ge $min_events ]; then
                    #############################
                    # there are plenty of events
                    ##############################
                    index="$(echo $i | nawk '{printf ("%03d", $0)}')"


                    mode=${list#?????}
                    mode=${mode%?.prelist}
 
                    bitrate=${list#???????}
                    bitrate=${bitrate%.prelist}

                    unfiltered=$($UTIL/generate_filename unfiltered \
                                   $inst $index $mode $bitrate )

                    ####################
                    # debugguing output
                    ####################
                    if [ -n "$DEBUG" ]; then
                         echo ${0##*/}: inst=$inst
                         echo ${0##*/}: index=$index
                         echo ${0##*/}: mode=$mode
                         echo ${0##*/}: bitrate=$bitrate
                         echo ${0##*/}: unfiltered=$unfiltered
                    fi

              
                    #################
                    # merge files
                    #################
                    $UTIL/log_entry "Creating $unfiltered"
                    $STOOLS/cmerge $unfiltered @$list >stdout.log 2>stderr.log
                    $UTIL/stool_test cmerge $? $0 2 stdout.log stderr.log

                    ########################################################
                    # add ORIGMODE keyword with same value as DATAMODE
                    # this is to later distinguish ground converted BRIGHT
                    # mode from on-board BRIGHT mode
                    #######################################################
                    datamode=$($UTIL/read_fits_keyword $unfiltered DATAMODE )

                    $UTIL/add_fits_keyword $unfiltered[0] ORIGMODE $datamode \
                                           "DATAMODE before any conversion"

                    $UTIL/add_fits_keyword $unfiltered[1] ORIGMODE $datamode \
                                           "DATAMODE before any conversion"

                    $UTIL/add_fits_keyword $unfiltered[2] ORIGMODE $datamode \
                                           "DATAMODE before any conversion"



                    ###############################
                    # remove the raw files
                    ###############################
                    for file in $(cat $list ); do
                         rm -f $file
                    done

                    let i=$i+1

               else
                    ####################################
                    # not enough events to bother with
                    ####################################
                    $UTIL/log_entry \
                "Ignoring the following files since they total $nevents events"
                    $UTIL/file_to_log $list


               fi

               rm -f $list

          done #loop over unscreened files

          rm -f listlist.tmp

          ##################################
          # remember the running file index
          ##################################
          case "$inst" in 
          s0) sis0index=$i;;
          s1) sis1index=$i;;
          g2) gis2index=$i;;
          g3) gis3index=$i;;
          esac

     
     done #loop over instruments

     let telnum=$telnum+1

done # loop over telemetry files

##########################
# tar together the scraps
##########################

$UTIL/log_entry "tar-ing together the leftover raw files"
scraps=$($UTIL/generate_filename scraps )

list=$(ls ft*[HML].fits 2>/dev/null)
if [ -n "$list" ]; then

     tar -cvf $scraps ft*[HML].fits >stdout.log 2>stderr.log
     code=$?

     $UTIL/file_to_log stdout.log
     if [ $code -ne 0 -o -s stderr.log ]; then

          $UTIL/exception $0 1 "Error in tar, exit code=$code"

          if [ -s stderr.log ]; then
               $UTIL/log_entry "stderr output from tar"
               $UTIL/file_to_log stderr.log
          fi
     fi

     rm -f stdout.log
     rm -f stderr.log

     rm -f ft*[HML].fits
fi
     
     



exit 0