readfrfs (version 1.2)

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

###########################################################################
#
# 
# SYNTAX: readfrfs
#
# BRIEFLY: Unpack the raw telemetry into FITS event and housekeeping files.
#
# DESCRIPTION: This routine runs frfread to unpack the raw telemetry into 
# DESCRIPTION: a large number of science files. A new science file is
# DESCRIPTION: created every time there is a mode change.
# DESCRIPTION: In addition frfread produces five house-keeping files:
# DESCRIPTION: one for each instrument and one general file.
# DESCRIPTION: <p>
# DESCRIPTION: All raw event files with no events are then deleted.
# DESCRIPTION: <p>
# DESCRIPTION: The TSTART and TSTOP keywords in the HK files are checked
# DESCRIPTION: for sensible values. frfread errors have sometimes caused
# DESCRIPTION: TSTART=0.0.
# DESCRIPTION: All of the good time intervals for a given instrument
# DESCRIPTION: are collected into a fits extension which is appended to
# DESCRIPTION: the approprate HK file. Only GTIs wholly contained between
# DESCRIPTION: TSTART and TSTOP of a particular HK file are included
# DESCRIPTION: in that file's extension.
# DESCRIPTION: <p>
#
#
# VERSION: 1.2
#
# HISTORY: 0.0 -> 1.0 1/3/97
# HISTORY: Corrected bug which fed the wrong exit code from frfread to 
# HISTORY: ftool_test
# HISTORY: 
# HISTORY: 1.0 -> 1.1 2/11/97
# HISTORY: Changed frfread errors to level 2 now that they are less common
# HISTORY: Moved check for zero TSTARTS and TSTOPS in HK files to immediately
# HISTORY: after frfread run.
# HISTORY: Added code to append ALLGTI extensions to the instrument HK files.
# HISTORY: Updated the description above.
# HISTORY: 2/15/97 removed ALLGTI generating code until it has GOF approval.
# HISTORY: 2/17/97 added code to save FRAME mode data for posible use 
# HISTORY: in RDD correction.
# HISTORY: 2/18/97 now read ra and dec directly from job.par
# HISTORY: 
# HISTORY: 1.1 -> 1.2 2/26/97
# HISTORY: uncommented the ALLGTI generating code
# HISTORY: Change name of frame mode files to avoid problems later.
#
#
############################################################################
#DEBUG=1

$UTIL/milestone "Running frfread on telemetry files"

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

if [ $nfiles -eq 0 ]; then
     $UTIL/exception $0 3 "Can't get non-zero number of frf files from $JOBPAR"
fi



###########################################
# list each telemetry file into .frffiles
###########################################
echo "frf_directory,s,h,\"./\",,,\"frf directory\"" > .frffiles

typeset -i i=1
while [ $i -le $nfiles ]; do
     ######################
     # telemetry file name
     ######################
     number="$(echo $i | nawk '{printf ("%03d", $0)}')"
     full=$($UTIL/read_parfile $JOBPAR frffile_$number)
     file=${full##*/}
     file=${file%.Z}

     if [ -r "$file" ]; then
          echo "frf_file,s,h,\"$file\",,,\"frf telemetry file\"" >> .frffiles
     else
          $UTIL/exception $0 2 "FRF file $file is unreadable"
     fi

     let i=$i+1

done


#####################
# object name
####################
object=$($UTIL/read_fits_keyword ${file}[1] OBJECT)

##################################################
# seqpi (according to the seq_type)
################################################
seq_type=$($UTIL/read_parfile $JOBPAR seq_type)

case "$seq_type" in
1 ) seqpi="$($UTIL/read_parfile $JOBPAR jppiname)" ;;
2 ) seqpi="$($UTIL/read_parfile $JOBPAR uspiname)" ;;
3 ) seqpi="$($UTIL/read_parfile $JOBPAR jppiname)" ;;
4 ) seqpi="$($UTIL/read_parfile $JOBPAR uspiname)" ;;
6 ) seqpi="$($UTIL/read_parfile $JOBPAR uspiname)" ;;
7 ) seqpi="$($UTIL/read_parfile $JOBPAR jppiname)" ;;
8 ) seqpi="$($UTIL/read_parfile $JOBPAR uspiname)" ;;
9 ) seqpi="Manager" ;;
11) seqpi="$($UTIL/read_parfile $JOBPAR uspiname)" ;;
* ) seqpi="Unknown PI"
    echo $UTIL/exception 1 "Unknown sequence type $seq_type" ;;
esac

#########################
# RA and Dec
#########################
ra=$( $UTIL/read_parfile $JOBPAR ra )
dec=$($UTIL/read_parfile $JOBPAR dec)



######################
# run frfread
######################  
frfread=$($UTIL/read_parfile $PARFILE frfread)
dir=${frfread%/*}
version=${frfread%/frfreadng}
version=${version##*/}

$UTIL/setup_parfile ${frfread%/*}/frfread.par origin="GSFC"    \
                                              object="$object" \
                                              seqpi="$seqpi"   \
                                              ranom="$ra"      \
                                              decnom="$dec"   
                       
if [ -s .frffiles ]; then
          
     $UTIL/log_entry "Running $version on the following telemetry files:"
     $UTIL/file_to_log .frffiles

     $frfread >stdout.log 2>stderr.log
     code=$?
     grep -v "coordinate error" stderr.log >filt.log
     $UTIL/ftool_test frfread $code $0 2 stdout.log stderr.log filt.log


else
     $UTIL/exception $0 3 "No files listed in .frffiles"
fi


rm -f frfread.par
rm -f .frffiles

#######################################
# remove files with zero events
#######################################
typeset -i nevents 

for file in $(ls ft*.fits | grep -v HK ); do

     nevents=$($UTIL/read_fits_keyword ${file}[0] NEVENTS )

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


     if [ $nevents -eq 0 ]; then
          datamode=$($UTIL/read_fits_keyword ${file}[0] DATAMODE )
          case "$datamode" in
          FRAME)
               ############################
               # frame mode file, save it.
               ############################
               $UTIL/log_entry "Saving FRAME mode file fr${file#ft}"
               mv $file fr${file#ft}
               ;;
          *)
               ################################
               # not scientifically useful
               ################################
               $UTIL/log_entry "removing $file since it has $nevents events"
               rm -f $file
               ;;
          esac
     fi
done



##################################################
# check HK files for non-zero TSTART and TSTOP
##################################################
$UTIL/log_entry "Checking housekeeping files for valid TSTART and TSTOP"
list=$(ls ft*HK.fits 2>/dev/null )
for file in $list ; do
     
     for keyword in TSTART TSTOP ; do

          value=$($UTIL/read_fits_keyword ${file}[0] $keyword )

          if [ -z "$value" ]; then
               $UTIL/exception $0 3 "Can't read $keyword  in $file"
          else
               ##########################
               # check if value is zero
               ##########################
               if [ $($STOOLS/floatcmp $value 0.0 ) = "eq" ]; then
                    $UTIL/exception $0 3 "$keyword = $value in $file"
               fi
          fi


     done
done



########################################
# create ALLGTI extensions for HK files
########################################
$UTIL/log_entry "Appending ALLGTI extensions to housekeeping files"

for inst in S0 S1 G2 G3; do

     ############################################
     # list all event files for that instrument
     ############################################
     rm -f list.tmp
     rm -f allgti.tmp
     ls ft*${inst}*.fits 2>/dev/null |grep -v HK |\
                                      awk '{print $1"[3]"}' >list.tmp

     #############################
     # merge the GTIs
     #############################
     $UTIL/gti_merge allgti.tmp list.tmp
     rm -f list.tmp
     if [ -a allgti.tmp ]; then
          #############################################
          # append the GTI extension to the HK file
          #############################################
          list=$(ls ft*${inst}HK.fits 2>/dev/null)
          for house in $list; do
               ####################################################
               # make sure only GTIs between the TSTART and TSTOP
               # of the HK file are included
               ####################################################
               tstart=$($UTIL/read_fits_keyword $house[0] TSTART )
               tstop=$( $UTIL/read_fits_keyword $house[0] TSTOP  )

               rm -f gti.tmp
               $UTIL/setup_parfile $FTOOLS/fselect.par       \
                                   infile=allgti.tmp \
                                   outfile=gti.tmp   \
                                   expr="START>= $tstart && STOP <=$tstop"

               $FTOOLS/fselect >stdout.log 2>stderr.log
               $UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log

               ###############################################
               # transfer keyword values from primary header
               ###############################################
               for key in TSTART TSTOP TELAPSE DATAMODE \
                          DATE-OBS TIME-OBS DATE-END TIME-END; do

                    value=$($UTIL/read_fits_keyword $house[0] $key )
                    $UTIL/add_fits_keyword gti.tmp[1] $key $value 
               done



               ###########################
               # append the GTI extension
               ###########################
               $UTIL/setup_parfile $FTOOLS/fappend.par infile=gti.tmp \
                                                       outfile=$house

               $FTOOLS/fappend >stdout.log 2>stderr.log
               $UTIL/ftool_test fappend $? $0 2 stdout.log stderr.log

               ############################################
               # set the ONTIME keyword in all extensions
               ############################################
               ontime=$($STOOLS/compute_exposure $house )
               for ext in 0 1 2; do
               
                    $UTIL/add_fits_keyword ${house}[${ext}] ONTIME $ontime

               done 


          done
     fi

     rm -f allgti.tmp

done
                 

#######################################
# remove files with zero events
#######################################
typeset -i nevents 

for file in $(ls ft*.fits | grep -v HK ); do

     nevents=$($UTIL/read_fits_keyword ${file}[0] NEVENTS )

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


     if [ $nevents -eq 0 ]; then
          $UTIL/log_entry "removing $file since it has $nevents events"
          rm -f $file
     fi
done

          


exit 0