telemetry (version 3.0)

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 checks that the files are time ordered.
# DESCRIPTION: <P>
# DESCRIPTION: If there is more that one telemetry file, the routine
# DESCRIPTION: checks for and removes overlaps between the files.
# DESCRIPTION: This is done for both the telemetry files and the attitude files.
# DESCRIPTION: <P>
# DESCRIPTION: Finally the routine checks to make sure that the time spanned by
# DESCRIPTION: each telemetry file is covered by the corresponding attitude
# DESCRIPTION: file, within an %frftimetoll second tolerance.
# DESCRIPTION: If not, the telemetry file is trimmed to match the attitude
# DESCRIPTION: file and an error is given to indicate that the processing
# DESCRIPTION: team should request a new attitude file from ISAS.
# DESCRIPTION: If the <TT>ascalin</TT> FTOOL encountered such a gap it
# DESCRIPTION: would leave the entire event file containing the gap
# DESCRIPTION: uncalibrated.
# DESCRIPTION: Therefore the least amount of useful information
# DESCRIPTION: is lost when the telemetry files are trimmed.
# DESCRIPTION: <P>
# DESCRIPTION: Finally, the routine copies the orbit file into the current
# DESCRIPTION: directory and checks that the orbit file covers the time
# DESCRIPTION: of the observation.
#
# VERSION: 3.0
#
# 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 checks 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 is not present in the 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, and 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 using 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.
# HISTORY:
# HISTORY: 1.4 -> 1.5 5/16/97
# HISTORY: Now reports overlaps and gaps as differential number of seconds.
# HISTORY:
# HISTORY: 1.5 -> 1.6 6/24/97
# HISTORY: Fixed a bug where the overlaps between telemetry files were
# HISTORY: reported as negative numbers.
# HISTORY:
# HISTORY: 1.6 -> 1.7 7/29/97
# HISTORY: Made the telemetry/attitude time consistency check less sensitive to
# HISTORY: something like a zero time value in the last row.
# HISTORY:
# HISTORY: 1.7 -> 1.8 8/5/97
# HISTORY: Fixed a bug that caused all multiple frf sequences to be treated
# HISTORY: as if their files overlapped. This bug was ugly but actually harmless
# HISTORY: since no frames would be clipped unless they actually needed to be
# HISTORY: clipped. Also the overlap time was negative of what it should have
# HISTORY: been. The 1.6 version had switched this to be the wrong was
# HISTORY: since all the overlap times were coming up negative, but that was
# HISTORY: because of the bug fixed here.
# HISTORY:
# HISTORY: 1.8 -> 1.9 8/18/97
# HISTORY: Now increments counter when can't read telemetry or attitude.
# HISTORY:
# HISTORY: 1.9 -> 2.0 12/16/97
# HISTORY: Stripped out the code which trimmed the telemetry file
# HISTORY: to match the attitude file. Attitude file time consistency
# HISTORY: is now handled in the linearize subroutine.
# HISTORY:
# HISTORY: 2.0 -> 2.1 4/24/98
# HISTORY: Modified to handle both compressed and gzipped telemetry.
# HISTORY: Changed the check for orbit file consistancy to use the
# HISTORY: new $UTIL/read_fits_date . This is technically y2k compliant,
# HISTORY: but it is dependant on the dates in two ISAS-created fits files
# HISTORY: both being compliant with the new FITS date standard, which is
# HISTORY: out of our control. At worst this will give a spurrious error
# HISTORY: message though.
# HISTORY:
# HISTORY: 2.1 -> 2.2 1998-08-06
# HISTORY: Now calculate observation start and end dates and times in this
# HISTORY: routine (formerly done in $SUBS/information) and in a robust,
# HISTORY: y2k compliant way. Also, modified the check on the orbit file
# HISTORY: epoch to be more robust.
# HISTORY:
# HISTORY: 2.2 -> 2.3 1998-04-05
# HISTORY: Fixed a bug where the observation start and end times were
# HISTORY: not being written to the job.par correctly due to a typo
# HISTORY: with the variable names.
# HISTORY: 
# HISTORY: 2.3 -> 3.0 1998-07-28
# HISTORY: Modified so that processing no longer fetches the telemetry
# HISTORY: and attitude files from the staging directory.
# HISTORY: Processing now requires these files to already exist and
# HISTORY: be uncoompressed in the processing directory.
#
##########################################################################
#DEBUG=1

$UTIL/milestone "Verifying 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 "Zero FRF files listed in $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##*/}

        file=${file%.Z}
        file=${file%.gz}


        if [ -r $file ]; then
            #########################
            # 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

        else
            ###################################
            # can't read file
            ###################################
            $UTIL/exception $0 3 "Can't read $file"
        fi

    done

    let i=$i+1

done

########################################################################
# Check for and clip overlapping rows in telemetry/attitude files
########################################################################
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}
        previous=${previous%.gz}


        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}
            file=${file%.gz}


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

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

            ############################################################
            # Compare beginning of this file to the end of the last one
            ############################################################
            time_cmp="$($STOOL/floatcmp $lasttime $firsttime)"
            if [ "$time_cmp" != "lt" ]; then
                #####################################################
                # Overlap, clip the beginning of this telemetry file
                #####################################################
                overlap=$($STOOL/equals "$lasttime - $firsttime")

                $UTIL/exception $0 1 \
                        "$previous and $file overlap by $overlap seconds"

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

                ################
                # Clipping data
                ################
                $FTOOL/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

###########################################################
# determine the observation start and end dates and times
###########################################################
$UTIL/log_entry "Determining observation start and end"
tstart=1e20
tstop=0.
for telem in $($UTIL/any_filename telemetry); do

   mtime0=$($UTIL/read_fits_keyword $telem[1] MTIME0)
   mtime1=$($UTIL/read_fits_keyword $telem[1] MTIME1)

   if [ $($STOOL/floatcmp $mtime0 $tstart) = "lt" ]; then
      tstart=$mtime0
   fi

   if [ $($STOOL/floatcmp $mtime1 $tstop) = "gt" ]; then
      tstop=$mtime1
   fi

done

############################################################
# convert ascatime to date and time and store in par file
############################################################
$UTIL/date_from_ascatime $tstart | read obsdate obstime
$UTIL/date_from_ascatime $tstop  | read enddate endtime

$UTIL/add_parameter $JOBPAR tstart  "$tstart"  r "Observation start (ascatime)"
$UTIL/add_parameter $JOBPAR tstop   "$tstop"   r "Observation end (ascatime)"

$UTIL/add_parameter $JOBPAR obsdate "$obsdate" s "Observation start date (UT)"
$UTIL/add_parameter $JOBPAR obstime "$obstime" s "Observation start time (UT)"

$UTIL/add_parameter $JOBPAR enddate "$enddate" s "Observation end date (UT)"
$UTIL/add_parameter $JOBPAR endtime "$endtime" s "Observation end time (UT)"

$UTIL/log_entry "Observation begins $tstart $obsdate $obstime"
$UTIL/log_entry "Observation ends   $tstop $enddate $endtime"

#####################
# 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
#########################################################
orbtime=$($UTIL/read_fits_keyword $orbit[1] MTIME1)

if [ $($STOOL/floatcmp $orbtime $tstop) = "lt" ]; then

     $UTIL/exception $0 2 "Orbit file may not cover observation date"
fi


exit 0