linearize (version 1.0)

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

###########################################################################
#
# 
# SYNTAX: linearize
#
# BRIEFLY: Run ascalin to calibrate the unfiltered event files.
#
# DESCRIPTION: This routine runs the ascalin FTOOL on all of the unfiltered
# DESCRIPTION: event files. This does several things. 
# DESCRIPTION: PI values are calculated from PHA (energy) values.
# DESCRIPTION: For the SIS this means normalizing the four chips to a common 
# DESCRIPTION: scale. For the GIS, corrections are made for the temperature
# DESCRIPTION: dependance of the gain. 
# DESCRIPTION: To do this ascalin uses a gain history file which is
# DESCRIPTION: created by the ISAS code temp2gain.
# DESCRIPTION: <p>
# DESCRIPTION: For the GIS, raw X, and Y coordinates of events are converted
# DESCRIPTION: to a linear detector X and Y scale.
# DESCRIPTION: For both instruments, the detector coordinates are converted
# DESCRIPTION: to R.A. and Dec. coordinates registered about a commom aspect 
# DESCRIPTION: point.
#
# VERSION: 1.0
#
# HISTORY: 0.0 -> 1.0 12/19/96
# HISTORY: now read RA and dec from the jobpar
# HISTORY: 12/23/96 also skip MPC mode event files
# HISTORY: 12/27/96 take name of temp2gain executable from the parfile
# HISTORY: assume that the temp2gain parfile has the same name and location
# HISTORY: as the executable but with ".par" appended
#
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Creating GIS gain history file"

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

################################
# loop through telemetry files
################################

ISASBIN=$($UTIL/read_parfile $PARFILE isasbin )

caldir=$($UTIL/read_parfile $PARFILE caldir)
calfile=$($UTIL/read_parfile $PARFILE gainhist )
leaptable=$($UTIL/read_parfile $PARFILE leaptable )

$UTIL/fetch_file $caldir $calfile
$UTIL/fetch_file $caldir $leaptable

export LEAPTABLE="./$leaptable"





typeset -i i=1
while [ $i -le $nfiles ]; do
     number="$(echo $i | nawk '{printf ("%03d", $0)}')"
     full=$($UTIL/read_parfile $JOBPAR frffile_$number)
     file=${full##*/}
     file=${file%.Z}
     ghf=$(echo $file | tr '.' '_').ghf

     $UTIL/log_entry "Creating gain history file $ghf"

     temp2gain=$($UTIL/read_parfile $PARFILE temp2gain)


     $UTIL/setup_parfile $temp2gain.par frffile="$file" \
                                        outfile="$ghf"  \
                                        tblfile="${file}.tbl" \
                                        type="0"         \
                                        steptime="600"   \
                                        histfile=$calfile

     rm -f $ghf
     $temp2gain >stdout.log 2>stderr.log
     code=$?
     errorlines=$(grep "Error|Warning" stdout.log |wc -l )

     if [ $code -ne 0 -o $errorlines -gt 0 ]; then
          $UTIL/exception $0 2 \
                "Error from ISAS program temp2gain. Exit code=$code"
     fi

     $UTIL/log_entry "Output from ISAS program temp2gain"
     $UTIL/file_to_log stderr.log
     if [ -s stdout.log ]; then
          $UTIL/log_entry "Stdout output from ISAS program temp2gain"
          $UTIL/file_to_log stdout.log
     fi

     rm -f stdout.log stderr.log
     rm -f temp2gain.par
     rm -f ${file}.tbl

     
     ###########################################
     # check if ghf file is time ordered
     ##########################################
     $UTIL/check_order $ghf[1] CAL_START 
     $UTIL/check_order $ghf[1] CAL_STOP

     ######################################
     # tell the ghf files their names
     #####################################
     $UTIL/add_fits_keyword $ghf[0] FNAME $ghf
     $UTIL/add_fits_keyword $ghf[1] FNAME $ghf

                                            
                     
     let i=$i+1

done

###################################
###################################
##
##  ASCALIN
##
###################################
###################################


$UTIL/milestone "Running ASCALIN on unfiltered event 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

##############################
# calibration file directory
##############################
caldir=$($UTIL/read_parfile $PARFILE caldir)


###########################################
# loop through the unfiltered event files
###########################################
list=$($UTIL/generate_filename unfiltered any any any any)
for unfiltered in ${list} ; do

     inst=$(   $UTIL/parse_filename inst    $unfiltered )
     index=$(  $UTIL/parse_filename index   $unfiltered )
     mode=$(   $UTIL/parse_filename mode    $unfiltered )
     bitrate=$($UTIL/parse_filename bitrate $unfiltered )

     #######################
     # skip FAST and MPC mode files
     #######################
     if [ "$mode" = "03" -o "$mode" = "71" ]; then
          continue
     fi

     ###############
     # log entry
     ###############
     $UTIL/log_entry "Running ascalin on $unfiltered"


     teldef=$($UTIL/generate_filename teldef $inst $index $mode $bitrate )
     telemetry=$($UTIL/generate_filename telemetry $inst $index $mode $bitrate )

     case "$inst" in 
     g*) 
          ##############
          # GIS
          ##############
          tempofile="$(echo ${telemetry} | tr '.' '_').ghf";;
     s*) 
          ##############
          # SIS
          ##############
          tempofile=$($UTIL/read_parfile $PARFILE sispical )
          $UTIL/fetch_file $caldir $tempofile;;
     esac


     #######################################################
     # find attitude file corresponding to telemetry file
     #######################################################
     file=""
     typeset -i i=1;
     while [ $i -le $nfiles -a "$file" != "$telemetry" ]; do
          number="$(echo $i | nawk '{printf ("%03d", $0)}')"

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


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

          fi


          let i=$i+1
     done

     if [ "$file" = "$telemetry" ]; then
          attitude=$($UTIL/read_parfile $JOBPAR attfile_$number)
          attitude=${attitude##*/}
          attitude=${attitude%.Z}
     else
          $UTIL/exception $0 3 "No attitude file corresponding to $telemetry"
          attitude=none
     fi

     ################### 
     # RA and Dec
     ####################
     ra=$( $UTIL/read_parfile $JOBPAR ra)
     dec=$($UTIL/read_parfile $JOBPAR dec)

     
     ##########################
     # debugging output
     ##########################
     if [ -n "$DEBUG" ]; then
          echo ${0##*/}: unfiltered=$unfiltered
          echo ${0##*/}: index=$index
          echo ${0##*/}: mode=$mode
          echo ${0##*/}: bitrate=$bitrate
          echo ${0##*/}: teldef=$teldef
          echo ${0##*/}: telemetry=$telemetry
          echo ${0##*/}: attitude=$attitude
     fi

     $UTIL/fetch_file $caldir $teldef

     $UTIL/setup_parfile $FTOOLS/ascalin.par calfile=$teldef      \
                                             tempofile=$tempofile \
                                             attitude=$attitude   \
                                             pointing=USER        \
                                             datafile=$unfiltered \
                                             ranom=$ra            \
                                             decnom=$dec          \
                                             history=no           \
                                             verbose=no

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

exit 0