telemetry (version 1.4)

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

###########################################################################
#
# 
# SYNTAX: telemetry
#
# BRIEFLY: Fetch the telemetry and attitude files.
#
# DESCRIPTION: This routine copies telemetry and attitude files
# DESCRIPTION: into the working directory and uncompresses them.
# DESCRIPTION: It checks the SEQNUM (sequence number) and OBJECT
# DESCRIPTION: FITS keywords for validity
# DESCRIPTION: and then check that the files are time ordered.
#
# VERSION: 1.4
#
# HISTORY: 0.0 -> 1.0 8/19/96
# HISTORY: Changed orbit file milestone to a log entry.
# HISTORY: 
# HISTORY: 1.0 -> 1.1 2/4/97
# HISTORY: Changed "%keyword" to "$keyword" in SEQNUM check exception call.
# HISTORY: Now only check time order for telemetry files since this
# HISTORY: will be checked with the fixatt tool in aspecting.
# HISTORY: 
# HISTORY: 1.1 -> 1.2 3/14/97
# HISTORY: supplied missing "OBJECT" keyword name in add_fits_keyword call
# HISTORY: when OBJECT keyword not present in telemetry file.
# HISTORY: 3/18/97 modified so that it uses $UTIL/read_bintable
# HISTORY: Also added a check for whether the telemetry file timespan
# HISTORY: is covered by the corresponding attitude file.
# HISTORY: if not, trim the telemetry file to make it fit ad issue an
# HISTORY: exception.
# HISTORY: 
# HISTORY: 1.2 -> 1.3 3/24/97
# HISTORY: Added a check to make sure the orbit file is up-to-date.
# HISTORY: Added a chack for whether telemetry and attitude files exist
# HISTORY: when checking time consistency.
# HISTORY: Corrected a bug attstart instead of attstop in code which 
# HISTORY: clips the end of the telemetry file that isn't covered by the 
# HISTORY: attitude file
# HISTORY: 
# HISTORY: 1.3 -> 1.4 5/8/96
# HISTORY: added missing continuation character for exception call 
# HISTORY: when sequence number is incorrect in job.par or frf.
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Fetching and uncompressing telemetry, attitude and orbit 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

#########################################
# fetch the telemetry and attitude files
#########################################
typeset -i i=1
while [ $i -le $nfiles ]; do

     number="$(echo $i | nawk '{printf ("%03d", $0)}')"

     for type in frf att; do
          ##################
          # telemetry file
          ##################
          full=$($UTIL/read_parfile $JOBPAR ${type}file_$number)
          file=${full##*/}
          dir=${full%${file}}

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

          #############
          # fetch it
          #############
          $UTIL/fetch_file "$dir" "$file"

          if [ -r $file ]; then
               #################
               # uncompress it
               #################
               $UTIL/log_entry "Uncompressing $file"
               uncompress -f $file 2>/dev/null
               $UTIL/exit_test $? $0 3 "Can't uncompress $file"
     
               file=${file%.Z}

               #############################
               # verify sequence number
               #############################
               seq=$($UTIL/read_parfile $JOBPAR sequence)
               keyword=$($UTIL/read_fits_keyword ${file}[1] SEQNUM)
               if [ "$seq" != "$keyword" ]; then
                    $UTIL/exception $0 3 \
          "SEQNUM keyword $keyword in ${file} does not match $seq from $JOBPAR"
               fi

               ##########################################
               # set OBJECT keyword if it isn't already
               ##########################################
               object=$( $UTIL/read_fits_keyword ${file}[1] OBJECT )

               if [ -z "$object" ]; then
                    #####################################
                    # object undefined, try the job par 
                    #####################################
                    $UTIL/log_entry \
                         "can't get object name from $file, trying $JOBPAR"
                    object=$($UTIL/read_parfile $JOBPAR object)
                    if [ -z "$object" ]; then
                         ####################################################
                         # job par didn't work, so just use sequence number
                         ####################################################
                         object=$($UTIL/read_parfile $JOBPAR sequence )
                         $UTIL/exception $0 1 \
                            "can't get object name from job par, using $object"

                    fi
                    #####################
                    # update keyword
                    #####################
                    $UTIL/add_fits_keyword $file[1] OBJECT "$object"

               fi  

               #######################################
               # check that the file is time ordered
               #######################################
               case "$type" in
               frf)
                    $UTIL/check_order $file TIME
                    ;;
               esac


          fi

               

        
     done


     let i=$i+1
done

#########################################################################
# check for and clip overlapping superframes in telemetry/attitude file
#########################################################################
if [ $nfiles -gt 1 ]; then
     
     for type in frf att; do

          $UTIL/log_entry "Checking for overlaps in $type files"

          ###############################
          # read last time of first file
          ###############################
          number=001
          full=$($UTIL/read_parfile $JOBPAR ${type}file_$number)
          previous=${full##*/}
          previous=${previous%.Z}

          naxis2=$($UTIL/read_fits_keyword $previous NAXIS2 )
          lasttime=$($UTIL/read_bintable ${previous}[1] TIME $naxis2 )

          #########################################################
          # loop through the rest of the telemetry/attitude files
          #########################################################
          i=2
          while [ $i -le $nfiles ]; do

               number="$(echo $i | nawk '{printf ("%03d", $0)}')"
               full=$($UTIL/read_parfile $JOBPAR ${type}file_$number)
               file=${full##*/}
               file=${file%.Z}

               ########################
               # read first TIME value
               ########################
               firsttime=$($UTIL/read_bintable ${file}[1] TIME 1 )

               ############################################################
               # compare begining of this file to the end of the last one
               ############################################################
               time_cmp="$($STOOLS/floatcmp $last_time $first_time)"
               if [ "$time_cmp" != "lt" ]; then
                    ######################################################
                    # overlap, clip the beginning of this telemetry file
                    ######################################################
          
                     $UTIL/exception $0 1 \
                 "$previous ends at $lasttime while $file begins at $firsttime"

                    $UTIL/log_entry \
                      "clipping redundant superframes from beginning of $file"


                     ######################
                     # clipping data
                     ######################
                     $FTOOLS/fselect infile=$file \
                                     outfile=out.tmp \
                                     expr="(TIME - $lasttime) .gt. 0.1" \
                                          >stdout.log 2>stderr.log
                     $UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log

                     ########################################
                     # adjust the header to reflect changes
                     ########################################
                     value=$($UTIL/read_fits_keyword $previous ORBIT1)
                     $UTIL/add_fits_keyword $file ORBIT0 $value

                     value=$($UTIL/read_fits_keyword $previous MTIME1)
                     $UTIL/add_fits_keyword $file MTIME0 $value


                     value=$($UTIL/read_fits_keyword $previous DATE1)
                     $UTIL/add_fits_keyword $file DATE0 $value


                     value=$($UTIL/read_fits_keyword $previous TIME1)
                     $UTIL/add_fits_keyword $file TIME0 $value

                     mv -f out.tmp $file
               fi

               #########################################################
               # switch file to previous and read the last time value 
               #########################################################
               previous=$file

               naxis2=$($UTIL/read_fits_keyword $previous NAXIS2 )
               lasttime=$($UTIL/read_bintable ${previous}[1] TIME $naxis2 )

               let i=$i+1
          done
     done
          
      
fi

######################################################################
# check if telemetry file is covered by the attitude file 
# within a tolerance. If not, trim the telemetry file to make it fit
######################################################################
$UTIL/log_entry "Checking telemetry - attitude time consistency"
timetoll=$($UTIL/read_parfile $PARFILE frftimetoll )
i=1
while [ $i -le $nfiles ]; do

     ########################
     # get file names
     ########################
     number="$(echo $i | nawk '{printf ("%03d", $0)}')"

     tel=$($UTIL/read_parfile $JOBPAR frffile_$number)
     tel=${tel##*/}
     tel=${tel%.Z}

     att=$($UTIL/read_parfile $JOBPAR attfile_$number)
     att=${att##*/}
     att=${att%.Z}

     if [ ! -r "$tel" ]; then
          $UTIL/exception $0 3 "Can't read telemetry file $tel"
          continue
     fi

     if [ ! -r "$att" ]; then
          $UTIL/exception $0 3 "Can't read attitude file  $att"
          continue
     fi


     ############################
     # read start and stop times
     ############################
     attstart=$($UTIL/read_bintable $att[1] TIME 1 )
     telstart=$($UTIL/read_bintable $tel[1] TIME 1 )

     attrows=$($UTIL/read_fits_keyword $att[1] NAXIS2 )
     telrows=$($UTIL/read_fits_keyword $tel[1] NAXIS2 )

     attstop=$($UTIL/read_bintable $att[1] TIME $attrows )
     telstop=$($UTIL/read_bintable $tel[1] TIME $telrows )

     #####################################
     # check the beginnings of the files
     #####################################
     thing=$(equals "$attstart - $timetoll -$telstart" )

     if [ $($STOOLS/floatcmp $thing 0 ) = "gt" ]; then
          $UTIL/exception $0 2 \
                     "$att starts at $attstart while $tel starts at $telstart"
          $UTIL/log_entry "trimming beginning of $tel"

          ######################
          # clipping data
          ######################
          $UTIL/setup_parfile $FTOOLS/fselect.par \
                              infile=$tel         \
                              outfile=out.tmp     \
                              expr="($attstart - TIME) .lt. $timetoll" 
          $FTOOLS/fselect  >stdout.log 2>stderr.log
          $UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log

          mv -f out.tmp $tel

     fi

     #####################################
     # check the ends of the files
     #####################################
     thing=$(equals "$attstop + $timetoll -$telstop" )

     if [ $($STOOLS/floatcmp $thing 0 ) = "lt" ]; then
          $UTIL/exception $0 2 \
                     "$att ends at $attstop while $tel ends at $telstop"
          $UTIL/log_entry "trimming end of $tel"

          ######################
          # clipping data
          ######################
          $UTIL/setup_parfile $FTOOLS/fselect.par \
                              infile=$tel         \
                              outfile=out.tmp     \
                              expr="(TIME - $attstop) .lt. $timetoll" 
          $FTOOLS/fselect  >stdout.log 2>stderr.log
          $UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log

          mv -f out.tmp $tel

     fi

     let i=$i+1

done


          
  



     




#############################################
# get the orbit file
###############################################

$UTIL/log_entry "Fetching the latest orbit file"
          

full=$($UTIL/read_parfile $JOBPAR orbit )
orbit=${full##*/}
dir=${full%${orbit}}

$UTIL/fetch_file "$dir" "$orbit"

##########################################################
# check telemetry file epoch against telemetry file date
##########################################################
tel=$($UTIL/any_filename telemetry |tail -1 )
obsdate=${tel#ft}
obsdate=${obsdate%%_*}

orbdate=$($UTIL/read_fits_keyword $orbit[1] DATE1 |\
          awk -F'/' '{print $3$2$1}' )

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

if [ $orbdate -lt $obsdate ]; then
     $UTIL/exception $0 2 "Orbit file may not cover observation date"
fi


exit 0