aspecting (version 1.3)

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

###########################################################################
#
# 
# SYNTAX: aspecting
#
# BRIEFLY: Calculate the aspect point from the attitude files.
#
# DESCRIPTION: This routine calculates the aspect point from the attitude 
# DESCRIPTION: file(s). 
# DESCRIPTION: The aspect point is a nominal spacecraft pointing to which
# DESCRIPTION: sky coordinate images will be registered.
# DESCRIPTION: The ANG_DIST column in the filter files gives the deviation
# DESCRIPTION: of the spacecraft pointing from the aspect point.
# DESCRIPTION: <p>
# DESCRIPTION: First all the attitude files are merged.
# DESCRIPTION: Then the merged attitude file is read with the attitude FTOOL.
# DESCRIPTION: Finally, the output of attitude is interpreted by the 
# DESCRIPTION: aspect STOOL.
# DESCRIPTION: aspect bins the the attitude points into .01 degree bins.
# DESCRIPTION: It then finds the most populous group of four adjacent bins
# DESCRIPTION: and calculates the mean pointing among those points.
#
# VERSION: 1.3
#
# HISTORY: 0.0 -> 1.0 12/4/96
# HISTORY: Added code to shuffle the attitude output and re-try the 
# HISTORY: aspect solution if more than half of the points fell outside the 
# HISTORY: bin structure. This is only tried a maximum of 10 times
# HISTORY: before giving up to avoid an infinite loop
# HISTORY: 
# HISTORY: 1.0 -> 1.1 12/19/96
# HISTORY: Now write euler angles, RA,dec, and roll and galactic coords
# HISTORY: to the jobpar.
# HISTORY: 1/3/97 deleted write to old euler?_001 parameters
# HISTORY: 
# HISTORY: 1.1 -> 1.2 2/4/97
# HISTORY: Now run fixatt tool on the attitude file to add keywords to the
# HISTORY: primary header, delete records which are out of time order
# HISTORY: and to interpolate large pointing jumps between records
# HISTORY: for the benefit of ascaexpo.
# HISTORY: 
# HISTORY: 1.2 -> 1.3 3/17/97
# HISTORY: straightened out galactic longitude and latitude
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Determine nominal aspect point for the observation"

###############################################
# 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

##########################################
# run fixatt on all of the attitude files
##########################################
typeset -i i=1
while [ $i -le $nfiles ]; do
     number="$(echo $i | nawk '{printf ("%03d", $0)}')"
     full=$($UTIL/read_parfile $JOBPAR attfile_$number)
     file=${full##*/}
     file=${file%.Z}

     $UTIL/log_entry "Running fixatt on $file"

     rm -f att.tmp
     $STOOLS/fixatt $file att.tmp 0.01 >stdout.log 2>stderr.log
     $UTIL/stool_test fixatt $? $0 1 stdout.log stderr.log

     if [ -s "att.tmp" ]; then
          mv -f att.tmp $file
     fi
       
     let i=$i+1
done



#######################################################
# merge the attitude files if there is more than one
#######################################################
if [ $nfiles -gt 1 ]; then
 
     ##########################
     # list the attitude files
     ##########################
     rm -f files.tmp
     typeset -i i=1
     while [ $i -le $nfiles ]; do
          number="$(echo $i | nawk '{printf ("%03d", $0)}')"
          full=$($UTIL/read_parfile $JOBPAR attfile_$number)
          file=${full##*/}
          file=${file%.Z}
          echo $file >>files.tmp

          let i=$i+1
     done

     $UTIL/log_entry "Mergeing the attitude files"
     $UTIL/file_to_log files.tmp


     
     #################################
     # merging files
     #################################
     $UTIL/setup_parfile $FTOOLS/fmerge.par infiles="@files.tmp" \
                                            outfile=merged.tmp   \
                                            copyprime=yes        \
              lastkey="ORBIT1 MTIME1 DATE1 TIME1 MTIME1-1 DATE1-1 TIME1-1" \
                                            history=yes

     $FTOOLS/fmerge  >stdout.log 2>stderr.log
     $UTIL/ftool_test fmerge $? $0 2 stdout.log stderr.log
     rm -f files.tmp

     ###########################################################
     # make sure merged attitude file is still time ordered
     ##########################################################
     $UTIL/check_order merged.tmp TIME

else
     ########################################################
     # just one attitude file so just copy it to merged.tmp
     ########################################################
     full=$($UTIL/read_parfile $JOBPAR attfile_001)
     file=${full##*/}
     file=${file%.Z}
     cp $file merged.tmp
fi

#####################
# run attitude 
#####################
$UTIL/log_entry "running attitude FTOOL on merged attitude file"

$UTIL/setup_parfile $FTOOLS/attitude.par attitude=merged.tmp \
                                         attpath=./          \
                                         pointing=ATT        \
                                         outfile=out.tmp

$FTOOLS/attitude >stdout.log 2>stderr.log
code=$?
grep -v "Detected gap > 15min in attitude file:" <stderr.log >filt.log
$UTIL/ftool_test attitude $code $0 2 stdout.log stderr.log filt.log

rm -f merged.tmp

##############################################################
# run checkatt on attitude output for logging purposes
##############################################################     
$STOOLS/checkatt ./out.tmp 1.0 y y -1 >stdout.log 2>stderr.log
$UTIL/stool_test checkatt $? $0 1 stdout.log stderr.log

##########################################
# run aspect
########################################
$UTIL/log_entry "Calculating aspect point"

$STOOLS/aspect -w1 <out.tmp >aspect.tmp 2>stderr.log
$UTIL/stool_test aspect $? $0 2 "" ""

$UTIL/log_entry "Output from aspect:"
$UTIL/file_to_log stderr.log

bad=$(grep "out of" stderr.log |awk '{print $1}')
total=$(grep "out of" stderr.log |awk '{print $4}')
rm -f stderr.log

let allowed=$total/2

typeset -i attempt=1
while [ $bad -gt $allowed -a $attempt -le 10 ]; do

     $UTIL/log_entry "Shuffling pointing list and redoing aspecting"

     #########################
     # shuffle out.tmp
     #########################
     rm -f out1.tmp out2.tmp out3.tmp

     head -20 <out.tmp >out1.tmp

     let dum=$total-$bad
     tail +21 out.tmp |head -$dum >out3.tmp

     let dum=21+$dum
     tail +$dum out.tmp >out2.tmp
     

     rm -f out.tmp
     cat out1.tmp out2.tmp out3.tmp >out.tmp
     rm -f out1.tmp out2.tmp out3.tmp


     ######################
     # re-aspect
     ######################
     $STOOLS/aspect -w1 <out.tmp >aspect.tmp 2>stderr.log
     $UTIL/stool_test aspect $? $0 2 "" ""

     $UTIL/log_entry "Output from aspect:"
     $UTIL/file_to_log stderr.log

     bad=$(grep "out of" stderr.log |awk '{print $1}')
     total=$(grep "out of" stderr.log |awk '{print $4}')
     rm -f stderr.log

     let allowed=$total/2

     let attempt=$attempt+1

done



#######################
# Euler angles
#######################
euler1=$(awk '{print $1 }' <aspect.tmp)
euler2=$(awk '{print $2 }' <aspect.tmp)
euler3=$(awk '{print $3 }' <aspect.tmp)

$UTIL/log_entry "Euler angles: $euler1, $euler2, $euler3"

if [ -z "$euler1" ]; then
     euler1=0.0;
fi

if [ -z "$euler2" ]; then
     euler2=0.0;
fi

if [ -z "$euler3" ]; then
     euler3=0.0;
fi

$UTIL/add_parameter $JOBPAR euler1 $euler1 r "First Euler angle of pointing"
$UTIL/add_parameter $JOBPAR euler2 $euler2 r "Second Euler angle of pointing"
$UTIL/add_parameter $JOBPAR euler3 $euler3 r "Third Euler angle of pointing"

rm -f out.tmp

$STOOLS/euler2radec <aspect.tmp >radec.tmp 2>stderr.log
$UTIL/stool_test euler2radec $? $0 2 "" stderr.log
rm -f aspect.tmp

ra=$(  awk '{print $1}' <radec.tmp)
dec=$( awk '{print $2}' <radec.tmp)
roll=$(awk '{print $3}' <radec.tmp)
rm -f radec.tmp

$UTIL/log_entry "RA=$ra Dec=$dec Roll=$roll"

$UTIL/add_parameter $JOBPAR ra   $ra   r "Right Ascension of pointing"
$UTIL/add_parameter $JOBPAR dec  $dec  r "Declination of pointing"
$UTIL/add_parameter $JOBPAR roll $roll r "Roll angle of pointing"



####################################
# calculate galactic coordinates
####################################
$STOOLS/ecl2gal $ra $dec >galactic.tmp 2>stderr.log
$UTIL/stool_test ecl2gal $? $0 1 "" stderr.log

Lii="$(cat galactic.tmp | awk '{print $1}')"
Bii="$(cat galactic.tmp | awk '{print $2}')"
rm -f galactic.tmp

$UTIL/log_entry "Galactic coordinates Lii=$Lii Bii=$Bii"

$UTIL/add_parameter $JOBPAR glon $Lii r "Galactic longitude of pointing"
$UTIL/add_parameter $JOBPAR glat $Bii r "Galactic latitude of pointing"

exit 0