fixthings (version 1.5)

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

###########################################################################
#
# SYNTAX: fixthings
#
# BRIEFLY: Fix a number of possible spacecraft-related problems in the data.
#
# DESCRIPTION: This routine adjusts the unfiltered event files to correct for
# DESCRIPTION: various spacecraft problems.
# DESCRIPTION: Currently there are two such problems.
# DESCRIPTION: First, between Feb 10, 1994 and Apr 8, 1994 some of the
# DESCRIPTION: GIS3 PHA telemetry bits were stuck in the "on" position.
# DESCRIPTION: The data from this period can be recovered, but with
# DESCRIPTION: some loss of spectral resolution.
# DESCRIPTION: <P>
# DESCRIPTION: The second problem is that occasionally the spacecraft is
# DESCRIPTION: switched into FAST mode before it is switched from 4 CCD
# DESCRIPTION: to 1 CCD mode. Data is actually taken on one CCD
# DESCRIPTION: (no guarantee that it is the correct one), but
# DESCRIPTION: the FITS keywords show 4 CCD mode. The <TT>fast4fix</TT> STOOL
# DESCRIPTION: adjusts the keywords to reflect the actual mode.
# DESCRIPTION: As of Feb 6, 1996, this only significantly affected three
# DESCRIPTION: sequences: 10010160, 40011000, and 41010000.
#
# VERSION: 1.5
#
# HISTORY: 0.0 -> 1.0 8/21/96 Now removes ascalin.par, flookup.par, and
# HISTORY: fparkey.par after gis3bitfix runs.
# HISTORY:
# HISTORY: 1.0 -> 1.1 7/7/97
# HISTORY: Switched to using the new utility version of fast4fix instead
# HISTORY: of the old STOOL version.
# HISTORY: 7/15/97
# HISTORY: Now removes fcalc.par after gis3bitfix run.
# HISTORY:
# HISTORY: 1.1 -> 1.2 8/4/97
# HISTORY: Now does not give an error with the "Detected Gap..." message from
# HISTORY: gis3bitfix.e .
# HISTORY:
# HISTORY: 1.2 -> 1.3 4/24/98
# HISTORY: Now handles both compressed and gzipped telemetry and attitude files
# HISTORY:
# HISTORY: 1.3 -> 1.4 1998-08-06
# HISTORY: Modified to use the new gis3bitfix perl script. The new script is
# HISTORY: more careful about the start and stop times of the problem and
# HISTORY: now also fixes the associated RISE_TIME problem.
# HISTORY: 
# HISTORY: 1.4 -> 1.5 2000-02-25
# HISTORY: change executable path for gis3bitfix to the FTOOLS scripts
# HISTORY: directory to accomodate FTOOLS 5.0
#
##############################################################################
#DEBUG=1

############################################################################
############################################################################
##
##  2/4CCD fast mode
##
############################################################################
############################################################################

list=$($UTIL/generate_filename unfiltered s[01] any 03 any)
list=$(ls $list 2>/dev/null)
if [ -n "$list" ]; then
    ############################
    # There are FAST mode files
    ############################
    $UTIL/milestone "Checking FAST mode files for correct CCD mode"

    for file in $list ; do

        $UTIL/log_entry "Checking $file"
        $UTIL/fast4fix $file

    done
fi


############################################################################
############################################################################
##
##  GIS3 stuck bits
##
############################################################################
############################################################################
lsb_start=3.5072e+07
lsb_end=3.9963965e+07
rise_start=3.9963694e+07
rise_end=3.998221e+07

tstart=$($UTIL/read_parfile $JOBPAR tstart)
tstop=$( $UTIL/read_parfile $JOBPAR tstop )

if [ ! $($STOOL/floatcmp $tstart $rise_end ) = "gt" -a \
     ! $($STOOL/floatcmp $tstop  $lsb_start) = "lt" ]; then
   ###################################################
   # this sequence is affected by the problem
   ###################################################
   $UTIL/milestone "Fixing stuck bits in GIS3 unfiltered event files"

   ########################################################
   # check if this sequence runs over a time boundary
   # and split event files if necessary
   ########################################################
   for time in $lsb_start $lsb_end $rise_start $rise_end; do

      if [ $($STOOL/floatcmp $tstart $time) = "lt" -a \
           $($STOOL/floatcmp $tstop  $time) = "gt" ]; then
         ############################################
         # this sequence stradles a boundary
         ############################################
         $UTIL/log_entry "This sequence stradles time $time"

         ###########################################
         # get the next unused file index
         ###########################################
         list=$($UTIL/generate_filename unfiltered g3 any 70 any)
         last=$(ls $list 2>/dev/null | tail -1)

         if [ -z "$last" ]; then
            continue
         fi

         index=$($UTIL/parse_filename index $last)
         index=$(echo $index | nawk '{printf "%03d",$1+1}')

         ###########################
         # loop over all files
         ###########################
         for file in $(ls $list 2>/dev/null); do

            #############################################################
            # check if this particular file stradles the boundary time
            ###########################################################
            file_tstart=$($UTIL/read_fits_keyword $file[0] TSTART)
            file_tstop=$( $UTIL/read_fits_keyword $file[0] TSTOP )

            if [ $($STOOL/floatcmp $file_tstart $time) = "lt" -a \
                 $($STOOL/floatcmp $file_tstop $time) = "gt" ]; then
               #############################
               # we need to split this file
               #############################
               bitrate=$($UTIL/parse_filename bitrate $file)

               new_file=$($UTIL/generate_filename unfiltered \
                                                  g3 $index 70 $bitrate)

               $UTIL/log_entry \
                            "Splitting $file at $time into $file and $new_file"

               #############################################################
               # first clip out the time after the boundary and save the
               # result as the new file
               #############################################################
               rm -f gti.tmp
               echo " $time $file_tstop" >gti.tmp

               $UTIL/setup_parfile $FTOOL/extractor.par filename=$file \
                                                        eventsout=$new_file \
                                                        imgfile=NONE \
                                                        phafile=NONE \
                                                        fitsbinlc=NONE \
                                                        binlc=64 \
                                                        regionfile=NONE \
                                                        timefile=gti.tmp \
                                                        xcolf=DETX \
                                                        ycolf=DETY \
                                                        tcol=TIME \
                                                        ecol=PI \
                                                        xcolh=DETX \
                                                        ycolh=DETY \

               $FTOOL/extractor >stdout.log 2>stderr.log
               $UTIL/ftool_test extractor $? $0 2 stdout.log stderr.log

               ##################################################
               # check if there are any events in the new file
               ##################################################
               naxis2=$($UTIL/read_fits_keyword $new_file[1] NAXIS2)
               if [ $naxis2 -eq 0 ]; then
                  $UTIL/log_entry "Removing $new_file with $naxis2 events"
                  rm -f $new_file
               fi

               #############################################################
               # now clip out the time before the boundary and save the
               # result with the same name as the original file
               #############################################################
               rm -f gti.tmp
               echo "$file_tstart $time" >gti.tmp

               rm -f events.tmp
               $UTIL/setup_parfile $FTOOL/extractor.par filename=$file \
                                                        eventsout=events.tmp \
                                                        imgfile=NONE \
                                                        phafile=NONE \
                                                        fitsbinlc=NONE \
                                                        binlc=64 \
                                                        regionfile=NONE \
                                                        timefile=gti.tmp \
                                                        xcolf=DETX \
                                                        ycolf=DETY \
                                                        tcol=TIME \
                                                        ecol=PI \
                                                        xcolh=DETX \
                                                        ycolh=DETY \

               $FTOOL/extractor >stdout.log 2>stderr.log
               $UTIL/ftool_test extractor $? $0 2 stdout.log stderr.log

               ##################################################
               # check if there are any events in the new file
               ##################################################
               naxis2=$($UTIL/read_fits_keyword events.tmp[1] NAXIS2)
               if [ $naxis2 -eq 0 ]; then
                   $UTIL/log_entry "Renaming $new_file to $file"
                   mv $new_file $file
               else
                   mv events.tmp $file
               fi

               ####################################################
               # increment the index if we created a new file
               ####################################################
               if [ -a $new_file ]; then
                   index=$(echo $index | nawk '{printf "%03d",$1+1}')
               fi


            fi # end if this file stradles boundary

         done # loop over files

      fi # end if this sequence stradles boundary

   done # end of loop over times




   ########################################################
   # loop over GIS3 PH mode files, fixing them as we go.
   ########################################################
   list=$($UTIL/generate_filename unfiltered g3 any 70 any)
   list=$(ls $list 2>/dev/null)

   for file in $list ; do

        $UTIL/log_entry "Fixing stuck bits in $file"

        ############################################
        # gather the names of all the files we need
        ############################################
        index=$($UTIL/parse_filename index $file)
        bitrate=$($UTIL/parse_filename bitrate $file)
        teldef=$($UTIL/generate_filename teldef g3 $index 70 $bitrate)

        telemetry=$($UTIL/read_fits_keyword $file[0] TLM_FILE)
        attitude=$(echo $telemetry | tr 't' 'a')
        ghf=$(echo $telemetry | tr '.' '_').ghf


        #############################
        # run the fixing script
        #############################
        $PERLLIB/gis3bitfix $file $teldef $ghf $attitude >stdout.log \
                                                        2>stderr.log

        code=$?
        grep -v "Detected gap" <stderr.log >filtered.log
        $UTIL/ftool_test gis3bitfix $code $0 2 stdout.log \
                                              stderr.log filtered.log
        rm -f ascalin.par
        rm -f flookup.par
        rm -f fparkey.par
        rm -f fcalc.par
        rm -f gis3bitfix.kw

        #####################################################
        # Rename the fixed file to replace the original file
        #####################################################
        if [ -a $file.fix ]; then
            mv $file.fix $file
        fi

   done

fi







exit 0