aspecting (version 1.6)
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 of the attitude files are merged.
# DESCRIPTION: Then the merged attitude file is read with the
# DESCRIPTION: <TT>attitude</TT> FTOOL.
# DESCRIPTION: Finally, the output of <TT>attitude</TT> is interpreted by the
# DESCRIPTION: <TT>aspect</TT> STOOL.
# DESCRIPTION: <TT>aspect</TT> bins the attitude points
# DESCRIPTION: into 0.01 degree bins.
# DESCRIPTION: It then finds the most populous group of four adjacent bins
# DESCRIPTION: and calculates the mean pointing among those points.
# DESCRIPTION: The aspect point is stored in the job.par for later use
# DESCRIPTION: in both celestial and galactic coordinates.
# DESCRIPTION: <P>
# DESCRIPTION: Next, it runs the <TT>fixatt</TT> STOOL on all of the attitude
# DESCRIPTION: files. This tool deletes records which are out of time order
# DESCRIPTION: (giving an error when it does so) and interpolates
# DESCRIPTION: linearly between widely spaced attitude records.
# DESCRIPTION: The interpolation is needed since the
# DESCRIPTION: <TT>ascaexpo</TT> FTOOL does not interpolate between
# DESCRIPTION: records.
#
# VERSION: 1.6
#
# 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 for a maximum of 10 times
# HISTORY: before giving up in order to avoid an infinite loop.
# HISTORY:
# HISTORY: 1.0 -> 1.1 12/19/96
# HISTORY: Now writes 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 runs fixatt tool on the attitude file to add keywords to the
# HISTORY: primary header, delete records which are out of time order,
# HISTORY: and 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.
# HISTORY:
# HISTORY: 1.3 -> 1.4 5/16/97
# HISTORY: Moved fixatt to be done after aspecting calculations.
# HISTORY: Now gives an exception if the maximum aspecting trials was exceeded.
# HISTORY:
# HISTORY: 1.4 -> 1.5 7/28/97
# HISTORY: Now reads the maximum aspecting tries from the par file.
# HISTORY: Also changed the row at which to split the attitude list
# HISTORY: to be the total number of rows/the number of tries.
# HISTORY:
# HISTORY: 1.5 -> 1.6 4/24/98
# HISTORY: Now handles both compressed and gzipped attitude files.
#
###########################################################################
#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
#####################################################
# 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}
file=${file%.gz}
echo $file >>files.tmp
let i=$i+1
done
$UTIL/log_entry "Merging the attitude files"
$UTIL/file_to_log files.tmp
################
# Merging files
################
$UTIL/setup_parfile $FTOOL/fmerge.par infiles="@files.tmp" \
outfile=merged.tmp \
copyprime=yes \
lastkey="ORBIT1 MTIME1 DATE1 TIME1 MTIME1-1 DATE1-1 TIME1-1" \
history=yes
$FTOOL/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}
file=${file%.gz}
cp $file merged.tmp
fi
###############
# Run attitude
###############
$UTIL/log_entry "Running attitude FTOOL on merged attitude file"
$UTIL/setup_parfile $FTOOL/attitude.par attitude=merged.tmp \
attpath=./ \
pointing=ATT \
outfile=out.tmp
$FTOOL/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
#######################################################
$STOOL/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"
$STOOL/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
ntries=$($UTIL/read_parfile $PARFILE aspect_tries)
typeset -i attempt=1
while [ $bad -gt $allowed -a $attempt -le $ntries ]; do
#############################
# Find a good place to split
#############################
let split=$total/$ntries
if [ $split -eq 0 ]; then
let split=$total-$bad
fi
$UTIL/log_entry "Shuffling pointing list at row $split and redoing"
##################
# Shuffle out.tmp
##################
rm -f out1.tmp out2.tmp out3.tmp
head -20 <out.tmp >out1.tmp
tail +21 out.tmp | head -$split >out3.tmp
let dum=21+$split
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
############
$STOOL/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
#######################################
# Check if aspect point was calculated
#######################################
if [ $bad -gt $allowed ]; then
$UTIL/exception $0 3 "Unable to calculate an accurate aspect point"
fi
###############
# 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
$STOOL/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
#################################
$STOOL/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"
##########################################
# 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}
file=${file%.gz}
$UTIL/log_entry "Running fixatt on $file"
rm -f att.tmp
$STOOL/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
exit 0