attitude_adjustment (version 2.0)

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

##############################################################################
#
# SYNTAX: attitude_adjustment comhk_file tstart tstop

# DESCRIPTION: This utility calculates the coordinate offsets due to thermal 
# DESCRIPTION: expansion of the base plate. 
# DESCRIPTION: The x, y , ra, and dec corrections are written to stdout
# DESCRIPTION: in that order.
# DESCRIPTION: The correction algorithm was developed by Eric Gotthelf and
# DESCRIPTION: Y. Ueda.
#
# VERSION: 2.0
#
# HISTORY: 0.0 -> 1.0
# HISTORY: Converted to perl and modified to first calculate the BP4 mean
# HISTORY: and standard deviation, and then to repeat the calculation
# HISTORY: using only points within %bp4_nsigma sigma of the mean. This is to 
# HISTORY: screen out wildly different points caused by telemetry corruption.
# HISTORY: 
# HISTORY: 1.0 -> 2.0 1999-05-07
# HISTORY: Converted from ksh to perl
#
#
# CALLS: $UTIL/setup_parfile
# CALLS: $UTIL/ftool_test
# CALLS: $UTIL/read_parfile
# CALLS: $UTIL/exception
# CALLS: $FTOOL/fselect
# CALLS: $FTOOL/fstatistic
#
##############################################################################
#$DEBUG=1;

($chk, $tstart, $tstop) = @ARGV;

$UTIL   =$ENV{"UTIL"};
$FTOOL  =$ENV{"FTOOL"};
$STOOL  =$ENV{"STOOL"};
$JOBPAR =$ENV{"JOBPAR"};
$PARFILE=$ENV{"PARFILE"};

$tmp = "attadj$$.tmp";

#####################
# Conversion factors
#####################
$k1 = 0.1252707;
$k2 = -0.089241154;
$k3 = 8.477673000;
$k4 = -0.100000000;
$k5 = 0.98220003;

######################################
# Extract the base plate temperatures 
######################################
unlink $tmp;
`$UTIL/setup_parfile $FTOOL/fselect.par \\
                     infile=$chk\[1\]   \\
                     outfile=$tmp       \\
                     expr="NAME=='BP4' && TIME>=$tstart && TIME <= $tstop" \\
                     histkw=no  \\
                     copyall=no \\
                     keycopy=no \\
                     clobber=yes`;

`$FTOOL/fselect >stdout.log 2>stderr.log`;
`$UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log`;


############################################
# Calculate the mean and standard deviation
############################################
`$UTIL/setup_parfile $FTOOL/fstatistic.par infile=$tmp \\
                                           colname="VALUE" \\
                                           rows="-" `;

`$FTOOL/fstatistic >stdout.log 2>stderr.log`;
$code = $?;

chomp($mean  = `$UTIL/read_parfile fstatistic.par mean `);
chomp($sigma = `$UTIL/read_parfile fstatistic.par sigma`);

`$UTIL/ftool_test fstatistic $? $0 2 stdout.log stderr.log`;


##################################################
# now repeat the calculation, but use only the
# points withing 3 sigma from the mean
###############################################
chomp($nsigma=`$UTIL/read_parfile $PARFILE bp4_nsigma `);
$min=$mean-$nsigma*$sigma;
$max=$mean+$nsigma*$sigma;

`$UTIL/setup_parfile $FTOOL/fstatistic.par infile=$tmp \\
                                           colname="VALUE" \\
                                           rows="-" \\
                                           minval=$min \\
                                           maxval=$max  `;

`$FTOOL/fstatistic >stdout.log 2>stderr.log`;
$code = $?;

chomp($mean  = `$UTIL/read_parfile fstatistic.par mean `);
chomp($sigma = `$UTIL/read_parfile fstatistic.par sigma`);

`$UTIL/ftool_test fstatistic $? $0 2 stdout.log stderr.log`;

unlink $tmp;

#################################
# Give a warning for large sigma 
#################################
if ( $sigma >= 5.0 ) {
   `$UTIL/exception $0 1 "BP4 in $chk has standard deviation $sigma"`;
}

####################
# Convert to offset
####################
$delx = $k1;
$dely=$mean * $k2 + $k3;

chomp($roll = `$UTIL/read_parfile $JOBPAR roll`);
chomp($dec  = `$UTIL/read_parfile $JOBPAR dec `);

$r = 3.1415927 * $roll /180.0;
$d = 3.1415927 * $dec  /180.0;

$delra  = -($delx *cos($r) + $dely *sin($r) + $k4)*$k5 /cos($d);
$deldec = (-$delx *sin($r) + $dely *cos($r)      )*$k5;

########################
# Round these off a bit 
########################
$accuracy = 1e7;

$delx   = int($delx   *$accuracy +.5)/$accuracy;
$dely   = int($dely   *$accuracy +.5)/$accuracy;
$delra  = int($delra  *$accuracy +.5)/$accuracy;
$deldec = int($deldec *$accuracy +.5)/$accuracy;

print "$mean $sigma $delx $dely $delra $deldec\n";

exit 0;