#!/usr/local/bin/bash -posix
# 
#
# ASCA processing script.
# Written by Ron Watkins, 17Apr94.
#
# Modified and maintained by Don Jennings as of 03/01/95
#
# Change History:
#
# 5.5.0--> 5.5.1 Oct 11, 1995
#---------------
#       o Numerous comments added by Ed Pier
#
# 5.4.3--> 5.5.0 Sept 10, 1995
#---------------
#       o script now designed to run under version 2.0 (and higher) of the
#	  stream deamon script
#	o took call to post-processing out of script; post processing runs
#	  as independent deamon from now on
#	o took all trend copy code segments out of script; this is done by
#	  the post-processing deamon from now on
#	o the shexit() now creates the error log file and copies the error log
#	  and job logs to the trend monitor area just before exiting.
#	o now the processing configuration file must be specified with an
#	  absolute path (second command line argument)
#	o the JOBLOGDIR, EXEDIR and AUTO variables are now specified in the
#	  processing configuration file
#
# 5.4.2--> 5.4.3 Aug 30, 1995
#---------------
#	o fixed bug in attitude log trend copyout where the $att variable
#	  was misnamed $add. This has kept attitude trend logs from being
#	  properly copied since pv5.4.0.bash.
#
# 5.4.1--> 5.4.2  Aug 17, 1995
#---------------
#       o fixed bug in report() function so that the correct time and event
#         number are written to the header page for cleaned event (evt)
#         data files
#
# 5.4.0--> 5.4.1  Aug 15, 1995
#---------------
#       o fixed a bug in the SIS selection critera for the Sn_SATFm
#         selection parameter. Was (Sn_SATFm<0||Sn_SATFm>1), now is
#         (Sn_SATFm<1||Sn_SATFm>1)
#
# 5.3.2--> 5.4.0
#---------------
#	o changed the SIS event threshold to 1000 and GIS event threshold to
#	  500 for modal event merging (change in configuration file P5.2.3
#	  and higher)
#	o now set the maxgrade parameter in faint to 6 when creating BRIGHT2
#	  mode data files (mode 12).
#     	o moved the faint.par and faintdfe.par file initialization inside the
#	  faint/faintdfe creation loop so that each new loop gets a fresh
#	  par file.
#	o changed BR_EARTH selection critera for SIS event selections to
#      	  > 20; was at > 25
#	o The SIS selection critera T_DY_NT and T_SAA are now set according
#	  to the CCDMODE (1,2,3, or 4) of the event file
#      	o if the uspinum parameter from the job.par file does not have a 
#	  value we now set USPINUM to "0"; before, USPINUM went without a
#	  value. 
#	o Added Sn_SATFm (telemetry saturation) selection critera to SIS
#	  selection equation. 
#
# 5.3.1--> 5.3.2
#---------------
#	o fixed a minor bug where some comments were not masked off with '#'
#	  characters.
# 5.3.0--> 5.3.1
#---------------
#	o changed the T_SAA and T_DY_NT selection parameters for SIS selection
#	  so that (T_DY_NT<0)||(T_DY_NT>16))&&(T_SAA<0)||(T_SAA>16) 
# 5.2.9--> 5.3.0
#---------------
#	o fixed another silly little bug -- we got to get the steam deamon
#	  fixed so that we do not have to keep incrementing the major/minor
#	  version numbers when little fixes are made!!!!!!
# 5.2.8--> 5.2.9
#---------------
#	o fixed minor bug that was inhibiting the copying of the objdist
#	  trend file out to the trend area
# 5.2.7--> 5.2.8
#---------------
#	o fixed minor bug that was writing files to tape.create without
#	  striping the path
# 5.2.6--> 5.2.7
#---------------
#	o set up to use mkfilter2 version 2.2
#	o now delete all unused gis calibration files as soon as ascalin is
#	  finished 
#	o moved copy of mkf file to trend area after DFE correction takes
#	  place
# 5.2.5--> 5.2.6
#---------------
#	o observation name is now taken from the telemetry file header instead
#	  of the object name list. If the telemetry file OBJECT keyword has
#	  no value then the job.par file is examined for the obs name.
#	o added checkatt to the processing script.
#	o added SIS event file creation to the processing script.
#	o incorporated new version of mkfilter2 (mkfilter2.2) into script
# 5.2.4 -> 5.2.5
#---------------
#	o observation combining code added that checks for the angular
#	  distance between all telemetry sets in the sequence. If the
#	  distance between any two sets is greater than MAX_SEPARATION then
#	  processing of the combined set will not occur.
# 	o moved the 'trim attitude' and 'merge attitude' functions into the
# 	  processing script; before these were done by the merge_att_ext()
#	  funtion. Elimiated the merge_att_ext() function.
#	o fixed portions of the ASCAMODE generation code so that string values
#   	  are not surrounded by single quotes -- this was making fcreate fail
#      	  when the ASCAMODE files were FITS wrapped.
#	o integrated the proc.func function suite directly into the processing
#	  script. This was done to eliminate versioning mismatches between
#         proc.func and processing scripts. The proc.func file is no longer
# 	  used by processing.
#	o added code to fits_wrap_ascii() so that any single quotes in 
#	  to-be-wrapped ascii strings are replaced by two single quotes 
#	  (ie, ' ==> '') before being processed by the fcreate ftool   
# 5.2.3 -> 5.2.4
#---------------
#   	o added a filter to the gis3bitfix portion of code so that only
# 	  mode 70 GIS files are acted upon.
#	o added the "test" option to the script. When $4 = test, the script
#	  runs in test mode. Amongst other things, post processing is not
#	  called and the processing version number is not updated.
#	o changed print command to echo in function fits_mem_sort()
# 	o modified shexit() so that it does not send out email or log errors 
#	  when processing script is in test_run mode
# 5.2.2 ->5.2.3 
#--------------
# 	o rewrote gis3bitfix portion of code so that it sends only 1 file at
#	  a time to gis3bitfix and check the DET_POS type (FLF or POW2)
#         to use the correct calibration file.
# 5.2.1 ->5.2.2
#--------------
#       o moved the gis3bitfix code segment to right after ascalin. This is
#         so gis3bitfix receives corrected input files.
#       o added giftool calls to interlace the gif output from ximage.
#       o added the EXEDIR variable to point to the machine dependent 
#         executable directory (/usr/local/bin for example)
#       o fixed the SIS ASCALIN portion of the script so that the 'tempofile'
#         parameter is set to $REFDATA/$SISPICAL. Before, 'tempofile'
#         was not being set at all for SIS files.
# 5.2.0 ->5.2.1
#--------------
#	o Changed the attpath value in the attitude.par file from "." to
#	  "./" to reflect a feature in the FTOOLS 3.3 version of attitude
#       o The ximage.bash script is now run in its own shell; before it was
#         sourced from this script
#5.1.9 -> 5.2.0
#--------------
#	o Added a 'shexit 1' when ximage.bash returns an error. This causes the
#	  processing shell to exit with an error.
#
#5.1.8 -> 5.1.9
#--------------
#	o Changed public date value to 000000 in asca{log,mode}.txt files.
#	o Copy sequence $JOB and error.log to trend staging area.
#	o Re-aligned the text spacing and indents to improve readability.
#	o Substituted $JOB for job.par.
#	o Substituted $base for $base_name in xselect script creation to help
#	  prevent line wraps in script.
#	o Replaced wastefull for loop with: cp attitude.files attitude.list.
#	o Fixed bug which copied the .attlog file from merged data rather than
#	  the .attlog file from entire data when only 1 attitude file present.
#	o Merged .mkf stage loop with mkdtime loop to reduce loop redundancy.
#	o FITS wrap the ascalog.txt and ascamode.txt files to make sure they 
#	  get
#	  archived with the data in post-processing.
#	o Added re-direct of echo output for echo commands not going to $JOB.
#
#5.1.7 -> 5.1.8
#--------------
#	o Removed quotes from some constant strings.
#	o Added COORDPRO='ERROR' keyword to files prior to ascalin runs.
#
#5.1.6 -> 5.1.7
#--------------
#	o Changed 'hostname' command to 'uname -n' to support Solaris.
#	o Added Architecture 'uname -s' to $JOB for ID purposes.
#	o Copied .dfe files to rev1 trend staging area.
#	o Copied *hdr_page.txt and *_proc_info.txt to WEB directory for Don.
#	o Added mail message to notify for post-processing errors.
#
#5.1.5 -> 5.1.6
#--------------
#	o Modified error/warning tests for SISPI. Warnings were being detected
#	  as errors. Now warnings appear as STDOUT messages.
#	o Added $SEQUENCE and $PROCEXT to teldef_ascalin.fits[1]
#	o Removed the local copy of gis[23]_cal_files_used files.
#
# 
##############################################################################
##############################################################################
##################### Define functions used in script ########################
##############################################################################
##############################################################################

# 
##############################################################################
# This function will check the filesize of $1 and if nonzero, will print out
# all subsequent files to the $JOB along with error messages.
##############################################################################

errchk ()
{
  typeset errlog="$1"
  typeset message="$2"
  typeset file

  #######################################################
  # shift the argument list two positions to the left
  # so $3 is now $1, etc.
  ######################################################
  shift 2

  if [ "$(wc -l < $errlog)" -gt 0 ]; then
    echo "$SH_VER:E2:BEGIN STDERR output from $message" >> $JOB
    echo "File: $errlog" >> $JOB
    cat $errlog >> $JOB

    for file in $*; do
      echo "File: $file" >>$JOB
      cat $file >> $JOB
    done

    echo "$SH_VER:E0:END STDERR output from $message" >> $JOB
  fi
  rm -f $errlog
}





# 
###############################################################################
#
# This function will uncompress a file from a remote to current directory and
# check the time order.
###############################################################################

zget ()
{
  typeset from="$1"
  typeset to="$2"

  echo "$SH_VER:TM:Zcat $to.Z at $SECONDS" >> $JOB

  ########################
  # uncompress
  #######################
  zcat $from > $to

  if [ $? -gt 0 ]; then
    ################################
    # error from zcat, so try again
    ################################
    echo "$SH_VER:E1:Error on $to.Z zcat" >> $JOB
    zcat $from > $to

    if [ $? -gt 0 ]; then
      ###################################
      # failed on the second try as well,
      # so abort
      ###################################
      echo "$SH_VER:E3:Zcat failed on $to.Z" >> $JOB
      echo "$SH_VER:E0:Zcat failed on $to.Z" >> $JOB

      echo "Zcat failed on $to.Z"
      echo "Run aborted"

      shexit 1
    fi

    #######################################################
    # this code reached only if second try of zcat worked
    #######################################################
    echo "$SH_VER:E1:Retry on $to.Z zcat succeeded" >> $JOB
  fi

  echo "$SH_VER:TM:Zcat $to.Z complete at $SECONDS" >> $JOB
  chmod 660 $to

  #####################################################
  # Run fverify to check the status of the input file.
  # checks for correct FITS format
  # abort if there are any problems
  #####################################################
  sed -e 's/,a,/,h,/' $FTOOLS/fverify.par > fverify.par
  $FTOOLS/pset ./fverify.par infile=$to
  $FTOOLS/pset ./fverify.par prhead=no
  $FTOOLS/pset ./fverify.par testdata=no

  $FTOOLS/fverify >> $JOB 2> log.tmp

  if [ $(wc -l < log.tmp) -ne 0 ]; then
    errchk log.tmp "FVERIFY $LINENO" fverify.par
    echo "$SH_VER:CO:Fverify failed on RAW frf input file $to"
    shexit 1
  fi

  rm -f fverify.par
}





# 
###############################################################################
#
# This function adds or updates a fits keyword in a fits file. Add occurs if
# the keyword is not found in the header.
################################################################################

add_fits_keyword ()
{
  ########################################################
  # positional param #1 is name of fits file + extention
  # positional param #2 is keyword name
  # positional param #3 is keyword contents
  # positional param #4 is the comment (optional)
  ########################################################

  sed -e 's/,a,/,h,/' $FTOOLS/fparkey.par > fparkey.par
  $FTOOLS/pset ./fparkey.par value="$3"
  $FTOOLS/pset ./fparkey.par fitsfile="$1"
  $FTOOLS/pset ./fparkey.par keyword="$2"

  if [ ! -z "$4" ]; then
    ###########################
    # optional comment
    ###########################
    $FTOOLS/pset ./fparkey.par comm="$4"
  fi

  $FTOOLS/pset ./fparkey.par add=yes


  $FTOOLS/fparkey >> $JOB 2> log.tmp
  errchk log.tmp "add_fits_keyword.FPARKEY $LINENO" fparkey.par
  rm -f fparkey.par
}





# 
###############################################################################
# This function retrieves the value of a keyword from a fits header.
# A number of variables are defined in the main ksh so that the function
# can communicate the results back to the main ksh.
###############################################################################

get_fits_keyword ()
{
  ##############################################################################
  # positional param #1 is name of fits file + extention
  # positional param #2 is keyword name
  # external variables are needed: get_fits_keyword_value,get_fits_keyword_exist
  ##############################################################################

  sed -e 's/,a,/,h,/' $FTOOLS/fkeypar.par > fkeypar.par
  $FTOOLS/pset ./fkeypar.par fitsfile="$1"
  $FTOOLS/pset ./fkeypar.par keyword="$2"

  $FTOOLS/fkeypar >> $JOB 2> log.tmp

  errchk log.tmp "get_fits_keyword.FKEYPAR $LINENO" fkeypar.par

  ####################################################
  # look in fkeypar.par to see if the keyword exists
  ####################################################
  get_fits_keyword_exist="$($FTOOLS/pget ./fkeypar.par exist)"

  if [ "$get_fits_keyword_exist" = "yes" ]; then
    #################################################
    # keyword exists, so set get_fits_keyword_value
    #################################################
    get_fits_keyword_value="$($FTOOLS/pget ./fkeypar.par value)"

  else
    #################################################################
    # keyword does not exist, so set get_fits_keyword_value to null
    #################################################################
    get_fits_keyword_value=""
  fi

  rm -f fkeypar.par
}




# 
##############################################################################
#
# This function wraps a fits file around an ascii text file. The file max 
# length is calculated with an awk program.
#
##############################################################################

fits_wrap_ascii ()
{
  ################################################################
  # positional param #1 is name of the output fits file
  # positional param #2 is name of the ascii file to be wrapped
  ################################################################

  #############################################################################
  # We have to replace any single quotes with two single quotes to make fcreate
  # happy
  #############################################################################
  sed -e "s/\'/\'\'/g" $2 > wrap1.tmp

  #########################################
  # use awk to calculate maximum line length
  ###########################################
  mll="$(awk 'BEGIN {ll=0} {if (length > ll) ll=length} END {print ll}'                 wrap1.tmp)"

  ##############################################################
  # We have to place single quotes around each line for fcreate
  ###############################################################
  perl -pe 's/(.+)/\047$1\047/||s/^/\047 \047/' wrap1.tmp > wrap2.tmp


  sed -e 's/,a,/,h,/' $FTOOLS/fcreate.par > fcreate.par
  echo "text A"${mll}" char" > fcreate.cdfile
  $FTOOLS/pset ./fcreate.par  cdfile=fcreate.cdfile
  $FTOOLS/pset ./fcreate.par  datafile="wrap2.tmp"
  $FTOOLS/pset ./fcreate.par  outfile="$1"
  $FTOOLS/pset ./fcreate.par  tbltype="ASCII"

  $FTOOLS/fcreate  >> $JOB 2> log.tmp

  errchk log.tmp "fits_wrap_ascii.FCREATE $LINENO" fcreate.par fcreate.cdfile

  rm -f fcreate.par fcreate.cdfile wrap1.tmp wrap2.tmp
}




# 
###############################################################################
#
# This function will wrap a fits file around a GIF image. It uses the primary
# array within the FITS file to contain the image. BITPIX=8, NAXIS1=1 and
# NAXIS2=length-of-gif-file.
#
###############################################################################

fits_wrap_gif ()
{
  ############################################################
  # positional param #1 is name of the output fits file
  # positional param #2 is name of the gif file to be wrapped
  ############################################################

  if [ ! -s $BIN/.gif.head -o ! -s $BIN/.gif.zero ]; then
    echo "ERROR: Missing .gif.head and/or .gif.zero" >> $JOB
    return
  fi

  cat $BIN/.gif.head > fwg.tmp
  cat $2 >> fwg.tmp
  cat $BIN/.gif.zero >> fwg.tmp

  ######################################
  # convert from 2880 byte blocks
  ######################################
  size="$(wc -c < fwg.tmp)"
  dd if=fwg.tmp of=$1 ibs=2880 count="$(expr $size / 2880)" > /dev/null 2>&1
  rm -f fwg.tmp

  #############################################
  # define NAXIS2 and FNAME keywords
  #############################################
  size="$(wc -c < $2)"
  add_fits_keyword $1[0] NAXIS2 "$size" "length of data axis   2"
  add_fits_keyword $1[0] FNAME "$2" "GIF filename"
}





# 
###############################################################################
#
# This function checks the input file to see if it is in time order
# This check is necessary to trap ongoing bugs in various programs
# Returns without any message if the file is ok, else writes to the $JOB
# and returns status of "2"
#
###############################################################################

check_time_order ()
{
  #######################################################################
  # positional parameter #1 is the FITS file and extention to be tested
  # positional parameter #2 is the FITS file column name to check
  # positional parameter #3 is whether to check for equal times (yes/no)
  #######################################################################

  sed -e 's/,a,/,h,/' $FTOOLS/cktime.par > cktime.par
  $FTOOLS/pset ./cktime.par infile="$1"
  $FTOOLS/pset ./cktime.par colname="$2"
  $FTOOLS/pset ./cktime.par ckequal=$3

  $FTOOLS/cktime > cktime.log 2> log.tmp

  errchk log.tmp "check_time_order.CKTIME $LINENO" cktime.par
  last_line="$(tail -1 cktime.log)"
  if [ $($BIN/recmp "$last_line" "ORDERED") -ne 0 ]; then
    echo "$SH_VER:E1:Out of time order FITS file detected: $1" >> $JOB
    cat cktime.log >> $JOB
    rm -f cktime.par cktime.log
    return 2
  fi

  rm -f cktime.par cktime.log
}






# 
###############################################################################
#
# Preform a FITS Memory SORT on a given column name.
#
###############################################################################

fits_mem_sort ()
{
  ###############################################
  # $1 = input FITS file + extension
  # $2 = FITS column name to sort on
  #################################################

  sed -e 's/,a,/,h,/' $FTOOLS/fmemsort.par  >./fmemsort.par
  $FTOOLS/pset ./fmemsort.par infile=$1
  $FTOOLS/pset ./fmemsort.par outfile=tmp.fits
  $FTOOLS/pset ./fmemsort.par column=$2
  $FTOOLS/pset ./fmemsort.par method=insert
  $FTOOLS/pset ./fmemsort.par ascend=yes
  $FTOOLS/pset ./fmemsort.par copyall=yes
  $FTOOLS/pset ./fmemsort.par history=yes

  $FTOOLS/fmemsort >> $JOB 2> log.tmp

  errchk log.tmp "fits_mem_sort.FMEMSORT $LINENO" fmemsort.par
  if [ -s ./tmp.fits ]
  then  
    mv tmp.fits $1
    echo "$TYPE:$PROC_VER:E1:$1 corrected using FMEMSORT" >>$JOB
  fi

  rm fmemsort.par
}




# 
###############################################################################
# global return value for function add_ontimes
###############################################################################

add_ontimes ()
{
  #######################################################################
  # positional parameter #1 is the flat file list of files to accumlate
  # ontimes for
  #######################################################################

  add_ontimes_value="0.0"

  for file in $(cat $1); do
    get_fits_keyword ${file}[0] ONTIME
    add_ontimes_value="$(echo $add_ontimes_value $get_fits_keyword_value | nawk '{print $1+$2}')"

  done

  add_ontimes_value="$(echo $add_ontimes_value | nawk '{print int($1)}')"
}




# 
###############################################################################
#
# Function to exit the script in a clean way and notify the logfiles.
#
###############################################################################

shexit ()
{
  typeset -i level="$1"
  typeset log="$AUTO/lists/processing.done.list"

  if [ "$TEST_RUN" = "true" ]
  then
    exit $level
  fi
  if [ "$level" -eq 0 ]; then
    echo "$SH_VER:M1:${SEQUENCE}_${PROCEXT} Finished @ $(date)" >> $log
  else
    echo "$SH_VER:E1:${SEQUENCE}_${PROCEXT} Error @ $(date)" \
         "error=$level" >> $log
    echo "$SH_VER:E1:${SEQUENCE}_${PROCEXT} Error @ $(date) error=$level"
    mail -s "$TYPE:Error exit" $USERNAME <<- EOF
	Sequence ${SEQUENCE}.${PROCEXT} has non-zero exit status!
	Host: $(uname -n)
	Directory: $RUN_DIR
	EOF
  fi
  rm -f $COMMAND

#  
#####################################################################
# create the error log
# the error log contains all the E2 and E3 error messages which
# were reported to the job log
######################################################################

  ERROR_LOG="${ADSE}_$PROCEXT.error.log"
  echo "ASCA Operations Job Log Error Summary" > $ERROR_LOG
  echo " " >> $ERROR_LOG
  echo $(date) >> $ERROR_LOG
  echo "Sequence: " $SEQUENCE >> $ERROR_LOG
  echo " " >> $ERROR_LOG
  echo "Start of E2, E3 errors found" >> $ERROR_LOG
  sed -n -e '/.*\:E[23]\:.*/,/.*\:E0\:.*/p' $JOB >> $ERROR_LOG
  echo "End of summary" >> $ERROR_LOG

###############################################################
# transfer the job.log and error.log files to the trend area
################################################################

  cp $JOB $JOBLOGDIR/${ADSE}_$PROCEXT.$JOB
  chmod 644 $JOBLOGDIR/${ADSE}_$PROCEXT.$JOB

  cp $ERROR_LOG $JOBLOGDIR/$ERROR_LOG
  chmod 644 $JOBLOGDIR/$ERROR_LOG

##########################################################
# exit to the parent shell with the appropriate status
##########################################################

  exit $level

}





# 
###############################################################################
#
# Compute the modal ID.
#
###############################################################################

get_modal_id ()
{
  ##########################################
  # Argument 1 is the MODAL configuration.
  ##########################################

  $BIN/lockit $MODAL_LIST
  id="$(awk '$0~/'"$1"'/{print NR}' < $MODAL_LIST)"

  if [ -z "$id" ]; then
    echo "$1" >> $MODAL_LIST
    id="$(awk '$0~/'"$1"'/{print NR}' < $MODAL_LIST)"
  fi

  $BIN/unlockit $MODAL_LIST
  echo $id
}





# 
###############################################################################
#
# Check the OBJECT keyword for existance and supply if missing.
#
###############################################################################

check_object ()
{
  #########################################
  # try to get it from the $1 FITS file header
  ############################################
  get_fits_keyword $1[1] OBJECT
  if [ "$get_fits_keyword_exist" = yes ]; then
    OBJECT="$get_fits_keyword_value"
    echo "$SH_VER:CO:Using keyword OBJECT: $OBJECT from $1 header"     >> $JOB
    $FTOOLS/pset ./job.par object="$OBJECT"
    return
  fi

  ##############################################
  # try to get it from the job.par file
  ##############################################
  OBJECT="$($FTOOLS/pget ./job.par object)"
  if [ -n "$OBJECT" ]
  then
    echo "$SH_VER:CO:Using keyword OBJECT: $OBJECT from job.par"       >> $JOB
    add_fits_keyword $1[1] OBJECT "$OBJECT" "Name of object"
    return
  fi

  #####################################################
  # try to get it from the first troublesome name list
  #####################################################
  OBJECT="$(grep $SEQUENCE $AUTO/lists/NAMELIST1 | awk -F: '{print $2}')"
  if [ ! -z "$OBJECT" ]; then
    echo "$SH_VER:CO:Using keyword OBJECT: $OBJECT from namelist file" >> $JOB
    add_fits_keyword $1[1] OBJECT "$OBJECT" "Name of object"
    $FTOOLS/pset ./job.par object="$OBJECT"
    return
  fi

  #####################################################
  # try to get it from the second troublesome name list
  #####################################################
  OBJECT="$(grep $SEQUENCE $AUTO/lists/NAMELIST2 | awk -F: '{print $2}')"
  if [ ! -z "$OBJECT" ]; then
    echo "$SH_VER:CO:Using keyword OBJECT: $OBJECT from namelist file" >> $JOB
    add_fits_keyword $1[1] OBJECT "$OBJECT" "Name of object"
    $FTOOLS/pset ./job.par object="$OBJECT"
    return
  fi

  ########################################################
  # if you've gotten this far, the object name can't be
  # found anywhere, so abort the script
  #######################################################
  echo "$SH_VER:E1:OBJECT name UNKNOWN, exit 1" >> $JOB
  echo "$SH_VER:E1:OBJECT name UNKNOWN, exit 1"
  shexit 1


}





# 
##############################################################################
##############################################################################
######################### Begin script main body #############################
##############################################################################
##############################################################################

trap - HUP

# 
########################################
######################################
##
##  initialize variables 
##
####################################
#####################################


############################################
# names of important directories and files 
############################################

export HOST=`uname -n | awk -F. '{print $1}'`
export JOB=job.log
export add_ontimes_value="0.0"
export get_fits_keyword_value
export get_fits_keyword_exist

#############################################################################
# Read in command Line parameters:
#
# $1    Data TYPE, ao or pv
# $2    Job configuration file
# $3    Processing Directory (aka. Run directory)
# $4    test toggle switch, blank      ==> run in regular operation mode,
#                           "test_run" ==> run in test mode
#############################################################################

export TYPE="$1"
export CONFIG="$2"
export RUN_DIR="$3"
export PROV_VER="$(echo $CONFIG | awk 'BEGIN{FS="/"}{print $NF}')"

#########################################################
# determine the run type -- standard run or test run
#########################################################

if [ "$4" = "test_run" ]
then
  TEST_RUN="true"
else
  TEST_RUN="false"
fi
export TEST_RUN

########################################
# append TYPE to PROC_VER to get SH_VER
#######################################

case "$TYPE" in
ao) export SH_VER="AO:$PROC_VER" ;;
pv) export SH_VER="PV:$PROC_VER" ;;
* ) echo "E3:No data TYPE found"
    shexit 1 ;;
esac

######################################################################
# check if RUN_DIR has a non-null value and is an existing directory
######################################################################
if [ -z "$RUN_DIR" -o ! -d "$RUN_DIR" ]; then
    echo "$TYPE:RUN_DIR is illegal for this shell job"
    exit 1
fi

########################################################
# change directory to directory where processing happens 
#########################################################
cd $RUN_DIR

####################################
# log some things 
##################################
if [ "$TEST_RUN" = "true" ]
then
  echo "$SH_VER:LO:Start TEST RUN at $(date)" > $JOB
else
  echo "$SH_VER:LO:Start at $(date)" > $JOB
fi

echo "$SH_VER:LO:Hostname $(uname -n)" >> $JOB
echo "$SH_VER:LO:Architecture $(uname -s)" >> $JOB
echo "$SH_VER:LO:Processing directory $RUN_DIR" >> $JOB
echo "$SH_VER:LO:Process num = $$" >> $JOB
echo "$SH_VER:LO:Process script = ${0##*/}"  >> $JOB

#####################################
# read in the configuration file
#####################################

. $CONFIG


##################################################################
# check if job.par exists and has something in it. If not, quit
#################################################################
if [ ! -s job.par ]; then
    echo "$SH_VER:E3:NO JOB.PAR FOUND!!!!" >> $JOB
    echo "$SH_VER:E3:NO JOB.PAR FOUND!!!!"
    shexit 1
fi

#####################################################################
# read some variables from job.par and assign some related variables
######################################################################

export BIN="$AUTO/bin"
export SEQUENCE="$($FTOOLS/pget ./job.par sequence)"
export SEQPROCNUM="$($FTOOLS/pget ./job.par seqprocnum)"
export ORBIT="$($FTOOLS/pget ./job.par orbit)"
export PROCEXT="$(echo $SEQPROCNUM | nawk '{printf ("%03d", $0)}')"
export ADSE="ad$SEQUENCE"

#######################################################################
# update the proc.history file so that version numbers may be tracked
# unless this is just a test run
########################################################################
if [ "$TEST_RUN" = "false" ]
then
  echo "$SEQUENCE $PROC_VER START $(date +%y%m%d%H%M%S)" >> $PROC_HIST
fi



##############################################################################
###############################################################################
##
##  RUN APPLICATIONS PROGRAMS HERE
##
###############################################################################
###############################################################################



##############################################################################
# Create FLAT job.par.info. This file contains one entry per attitude and
# telemetry file. It's used to make it easier to get at the staging directory,
# filenames, and Euler angles for each file.
###############################################################################

rm -f job.par.info
typeset max="$($FTOOLS/pget ./job.par frfcnt)"
typeset -i cnt_i=1
while [ $cnt_i -le $max ]; do

    #######################################
    # file number string with leading zeros
    #######################################
    cnt="$(echo $cnt_i | nawk '{printf ("%03d", $0)}')"

    #############################
    # FRF (telemetry) file names
    #############################
    arc_tel="$($FTOOLS/pget ./job.par frffile_$cnt)"
    tel=${arc_tel##*/}
    tel=${tel%.Z}

    ####################################
    # spaceccraft attitude file names
    ###################################
    arc_att="$($FTOOLS/pget ./job.par attfile_$cnt)"
    att=${arc_att##*/}
    att=${att%.Z}

    ################
    # Euler angles
    ################
    e1="$($FTOOLS/pget ./job.par euler1_$cnt)"
    e2="$($FTOOLS/pget ./job.par euler2_$cnt)"
    e3="$($FTOOLS/pget ./job.par euler3_$cnt)"

    ###################################
    # make a new line in job.par.info
    ###################################
    echo "$tel $arc_tel $att $arc_att $e1 $e2 $e3" >> job.par.info

    let cnt_i+=1
done

# 
###############################################################################
##########################################################################
##
##  Do the following first for telemetry files, then for attitude files:
##  > uncompress file and transfer to working directory
##  > check for various error in files.
##
###########################################################################
############################################################################

##################################
# loop over lines in job.par.info
##################################
typeset -i tel_cnt=0
for tel in $(awk '{print $1}' job.par.info); do

    ###############################
    # uncompress telemetry file
    #############################
    arc_tel="$(grep $tel job.par.info | awk '{print $2}')"
    zget $arc_tel $tel

    ##################################################
    # check if looking at correct object in the sky
    #################################################
    echo "$SH_VER:TM:Check OBJECT keyword at $SECONDS" >> $JOB
    check_object $tel

    ######################################
    # check that telemetry is time ordered
    ######################################
    echo "$SH_VER:TM:Verify telemetry time ordered at $SECONDS" >> $JOB
    check_time_order $tel[1] TIME yes

    #########################################################
    # check that telemetry file has correct sequence number
    #########################################################
    get_fits_keyword $tel[1] SEQNUM
    if [ "$SEQUENCE" != "$get_fits_keyword_value" ]; then
        echo "$SH_VER:E3:$tel DOES NOT BELONG TO SEQ $SEQUENCE!"
        shexit 1
    fi


    ############################################################
    # check for time overlaps between consecutive files.
    ############################################################

    ################################
    # if this is the first file.....
    ################################
    let tel_cnt+=1
    if [ $tel_cnt -eq 1 ]; then

        last_fname=$tel

        ##########################################
        # read the time at the end of this file
        #########################################

        ####################
        # find the last row
        ####################
        get_fits_keyword $tel[1] NAXIS2
        last_row="$get_fits_keyword_value"


        ##########################################################
        # reformat template table parameter file (change ",a," to ",h," )
        ###########################################################
        sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par

        #######################################
        # set values in table parameter file 
        #######################################
        $FTOOLS/pset ./ftabpar.par fitsfile="$tel[1]"
        $FTOOLS/pset ./ftabpar.par column=TIME
        $FTOOLS/pset ./ftabpar.par row="$last_row"

        ##########################################
        # read last time value in telemetry file
        #########################################
        $FTOOLS/ftabpar >> $JOB 2> log.tmp
        errchk log.tmp "FTABPAR $LINENO" ftabpar.par
        last_time="$($FTOOLS/pget ./ftabpar.par value)"

        ##########################################################
        # don't need table parameter file any more, so remove it
        #########################################################
        rm ./ftabpar.par

        ###########################################################
        # skip the rest of this loop and go to next telemetry file
        ###########################################################
        continue

    fi

    ########################################################################
    # this code only reached for files after the first one
    #
    # Each 2->N file must be checked against the previous file and fselecte'd
    #########################################################################

    ##########################################################
    # read first time entry from telemetry file
    ##########################################################
    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par
    $FTOOLS/pset ./ftabpar.par fitsfile="$tel[1]"
    $FTOOLS/pset ./ftabpar.par column=TIME
    $FTOOLS/pset ./ftabpar.par row=1

    $FTOOLS/ftabpar >> $JOB 2> log.tmp
    errchk log.tmp "FTABPAR $LINENO" ftabpar.par
    first_time="$($FTOOLS/pget ./ftabpar.par value)"

    ###########################################################################
    # compare first time entry of this file to last entry of the previous file
    ###########################################################################
    time_cmp="$($STOOLS/floatcmp $last_time $first_time)"

    if [ "$time_cmp" != "lt" ]; then
        ##################################################################
        # the current file overlaps the previous file so it must be fixed
        ##################################################################
        echo "$SH_VER:E1:Time overlap between telemetry files detected" >> $JOB
        echo "$SH_VER:TM:Begin correct $tel overlap at $SECONDS" >> $JOB

        ######################################
        # 1st create the new telemetry file
        ######################################
        sed -e 's/,a,/,h,/' $FTOOLS/fselect.par > fselect.par
        $FTOOLS/pset ./fselect.par infile="$tel[1]"
        $FTOOLS/pset ./fselect.par outfile=tmp.fits
        $FTOOLS/pset ./fselect.par expr="(TIME - $last_time) .gt. 0.1"

        $FTOOLS/fselect >> $JOB 2> log.tmp
        errchk log.tmp "FSELECT $LINENO" fselect.par

        rm fselect.par

        #########################################
        # fix the keywords in the new tel file
        ########################################
        get_fits_keyword $last_fname[1] ORBIT1
        add_fits_keyword tmp.fits[1] ORBIT0 "$get_fits_keyword_value" \
                         "Corrected from $last_fname"

        get_fits_keyword $last_fname[1] MTIME1
        add_fits_keyword tmp.fits[1] MTIME0 "$get_fits_keyword_value" \
                         "Corrected from $last_fname"

        get_fits_keyword $last_fname[1] DATE1
        add_fits_keyword tmp.fits[1] DATE0 "$get_fits_keyword_value" \
                         "(dd/mm/yy) Corrected from $last_fname"

        get_fits_keyword $last_fname[1] TIME1
        add_fits_keyword tmp.fits[1] TIME0 "$get_fits_keyword_value" \
                         "Corrected from $last_fname"


        mv tmp.fits $tel

        echo "$SH_VER:TM:$tel corrected at $SECONDS" >> $JOB
        echo "$SH_VER:E1:$tel corrected." >> $JOB
    fi

    ########################
    # end of the loop stuff: 
    #########################

    last_fname=$tel

    #################################################
    # read last time entry of current telemetry file
    #################################################
    get_fits_keyword $tel[1] NAXIS2
    last_row="$get_fits_keyword_value"

    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par
    $FTOOLS/pset ./ftabpar.par fitsfile="$tel[1]"
    $FTOOLS/pset ./ftabpar.par column=TIME
    $FTOOLS/pset ./ftabpar.par row="$last_row"

    $FTOOLS/ftabpar >> $JOB 2> log.tmp
    errchk log.tmp "FTABPAR $LINENO" ftabpar.par
    last_time="$($FTOOLS/pget ./ftabpar.par value)"

done


###############################################################################
# Setup the frfread parameter set in file frfread.par
##############################################################################
sed -e 's/,a,/,h,/' ${FRFREAD%/*}/frfread.par > frfread.par

###################################
# file originated here at Goddard
##################################
$FTOOLS/pset ./frfread.par origin=GSFC

########################
# set object keyword
########################
get_fits_keyword "$tel[1]" OBJECT
if [ "$get_fits_keyword_exist" = yes ]; then
    #############################################
    # there is an object listed in telemetry file
    #############################################
    OBJECT="$(echo $get_fits_keyword_value | tr -d \')"
    $FTOOLS/pset ./frfread.par object="$OBJECT"
else
    ############################################################
    # no object name in telemetry file, so use sequence number
    ############################################################
    $FTOOLS/pset ./frfread.par object="$SEQUENCE"
fi

##################################################
# read sequence processing PI team type code number
# and set SEQPI accordingly
################################################
typeset -i seq_type="$($FTOOLS/pget ./job.par seq_type)"

case $seq_type in
1 ) SEQPI="$($FTOOLS/pget ./job.par jppiname)" ;;
2 ) SEQPI="$($FTOOLS/pget ./job.par uspiname)" ;;
3 ) SEQPI="$($FTOOLS/pget ./job.par jppiname)" ;;
4 ) SEQPI="$($FTOOLS/pget ./job.par uspiname)" ;;
6 ) SEQPI="$($FTOOLS/pget ./job.par uspiname)" ;;
7 ) SEQPI="$($FTOOLS/pget ./job.par jppiname)" ;;
8 ) SEQPI="$($FTOOLS/pget ./job.par uspiname)" ;;
9 ) SEQPI="Manager" ;;
11) SEQPI="$($FTOOLS/pget ./job.par uspiname)" ;;
* ) SEQPI="Unknown PI"
    echo "$SH_VER:E1:Unknown sequence type $seq_type" >> $JOB ;;
esac

$FTOOLS/pset ./frfread.par seqpi="$SEQPI"



###############################################################################
############################################################################
##
##  Assemble attitude file(s), check sequence ownership and time order.
##
############################################################################
###############################################################################

###############################################
# loop over all attitude files in job.par.info
###############################################
typeset -i att_cnt=0
for att in $(awk '{print $3}' job.par.info); do

    #########################
    # uncompres attitude file
    ##########################
    arc_att="$(grep $att job.par.info | awk '{print $4}')"
    zget $arc_att $att
    echo "$SH_VER:TM:Check OBJECT keyword at $SECONDS" >> $JOB

    #################################
    # check astronomical object name
    #################################
    check_object $att

    ###################################################
    # record attitude file name in file attitude.files
    ###################################################
    echo "$att" >> attitude.files

    ############################################
    # check that attitude file is time ordered
    ############################################
    echo "$SH_VER:TM:Verify attitude time ordered at $SECONDS" >> $JOB
    check_time_order $att[1] TIME yes

    #########################################################
    # check that attitude file has correct sequence number
    #########################################################
    get_fits_keyword $att[1] SEQNUM
    if [ "$SEQUENCE" != "$get_fits_keyword_value" ]; then
        echo "$SH_VER:E3:$att DOES NOT BELONG TO SEQ $SEQUENCE!"
        shexit 1
    fi


    ######################################################
    # check for time overlaps between consecutive files.
    ######################################################


    #############################
    # for the first attitude file:
    ############################
    let att_cnt+=1
    if [ $att_cnt -eq 1 ]; then

        last_fname=$att

        #################################################
        # read last time element of first attitude file
        #################################################
        get_fits_keyword $att[1] NAXIS2
        last_row="$get_fits_keyword_value"

        sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par
        $FTOOLS/pset ./ftabpar.par fitsfile="$att[1]"
        $FTOOLS/pset ./ftabpar.par column=TIME
        $FTOOLS/pset ./ftabpar.par row="$last_row"

        $FTOOLS/ftabpar >> $JOB 2> log.tmp
        errchk log.tmp "FTABPAR $LINENO" ftabpar.par
        last_time="$($FTOOLS/pget ./ftabpar.par value)"

        rm ./ftabpar.par

        ####################################################
        #skip the rest of the loop and go on to next file
        ####################################################
        continue

    fi

    ##################################################
    # this code not reached for the first attitude file
    #
    # Each 2->N file must be checked against the previous file and fselecte'd
    ###########################################################################

    #####################################################
    # read first time value for current attitude file
    ####################################################

    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par

    $FTOOLS/pset ./ftabpar.par fitsfile="$att[1]"
    $FTOOLS/pset ./ftabpar.par column=TIME
    $FTOOLS/pset ./ftabpar.par row=1

    $FTOOLS/ftabpar >> $JOB 2> log.tmp
    errchk log.tmp "FTABPAR $LINENO" ftabpar.par
    first_time="$($FTOOLS/pget ./ftabpar.par value)"

    ######################################################################
    # compare the start of this file to the end of the last attitude file
    ######################################################################
    time_cmp="$($STOOLS/floatcmp $last_time $first_time)"
    if [ "$time_cmp" != "lt" ]; then
        ###################################################################
        # the current file overlaps the previous file so it must be fixed
        ###################################################################
        echo "$SH_VER:E1:Time overlap between attitude files detected" >> $JOB
        echo "$SH_VER:TM:Begin correct $att overlap at $SECONDS" >> $JOB

        ###################################
        # 1st create the new attitude file
        ################################### 
        sed -e 's/,a,/,h,/' $FTOOLS/fselect.par > fselect.par
        $FTOOLS/pset ./fselect.par infile="$att[1]"
        $FTOOLS/pset ./fselect.par outfile=tmp.fits
        $FTOOLS/pset ./fselect.par expr="(TIME - $last_time) .gt. 0.1"

        $FTOOLS/fselect >> $JOB 2> log.tmp
        errchk log.tmp "FSELECT $LINENO" fselect.par

        rm fselect.par

        ##########################################
        # fix the keywords in the new attitude file
        #########################################
        get_fits_keyword $last_fname[1] ORBIT1
        add_fits_keyword tmp.fits[1] ORBIT0 "$get_fits_keyword_value" \
                         "Corrected from $last_fname"

        get_fits_keyword $last_fname[1] MTIME1
        add_fits_keyword tmp.fits[1] MTIME0 "$get_fits_keyword_value" \
                         "Corrected from $last_fname"

        get_fits_keyword $last_fname[1] DATE1
        add_fits_keyword tmp.fits[1] DATE0 "$get_fits_keyword_value" \
                         "(dd/mm/yy) Corrected from $last_fname"

        get_fits_keyword $last_fname[1] TIME1
        add_fits_keyword tmp.fits[1] TIME0 "$get_fits_keyword_value" \
                         "Corrected from $last_fname"

        mv tmp.fits $att

        echo "$SH_VER:TM:$att corrected at $SECONDS" >> $JOB
        echo "$SH_VER:E1:$att corrected." >> $JOB

    fi

    #####################
    # end of loop stuff
    #####################
    last_fname=$att

    ################################################
    # read last time value for this attitude file
    ###############################################
    get_fits_keyword $att[1] NAXIS2
    last_row="$get_fits_keyword_value"

    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par
    $FTOOLS/pset ./ftabpar.par fitsfile="$att[1]"
    $FTOOLS/pset ./ftabpar.par column=TIME
    $FTOOLS/pset ./ftabpar.par row="$last_row"

    $FTOOLS/ftabpar >> $JOB 2> log.tmp
    errchk log.tmp "FTABPAR $LINENO" ftabpar.par

    last_time="$($FTOOLS/pget ./ftabpar.par value)"

done

# 
###############################################################################
#############################################################################
##
##  Obtain the latest ORBIT file.
##  note: ORBIT is set from job.par file
##
#############################################################################
############################################################################

####################################################################
# check if ORBIT is defined and that it specifies a non-empty file
####################################################################
if [ -z "$ORBIT" -o ! -s "$ORBIT" ]; then

    echo "$SH_VER:E2:File $ORBIT missing" >> $JOB
    echo "$SH_VER:E2:File $ORBIT missing"

    shexit 1
fi

######################################################
# copy orbit file into working directory as frf.orbit
#######################################################
cp $ORBIT ./frf.orbit
chmod 644 ./frf.orbit
add_fits_keyword frf.orbit[1] FNAME frf.orbit

# 
###############################################################################
###############################################################################
##
##  Determine pointing information from attitude file(s)
##
###############################################################################
###############################################################################


###############################################################################
# create "trimmed" versions of all attitude files
###############################################################################


echo "$SH_VER:TM:create trimmed attitude files at $SECONDS" >> $JOB

typeset -i naxis2
typeset -i low
typeset -i high

##################################################
# loop over attitude files  
# these were relisted in the file attidue.files
##################################################
for file in $(cat attitude.files)
do

  #########################################################
  # read the number of entries in the attitude file table
  #########################################################
  get_fits_keyword $file[1] NAXIS2
  naxis2=$get_fits_keyword_value


  ###########################################################################
  # check if trimming needed for this file (are there more than 300 rows?)
  ###########################################################################

  if [ $naxis2 -gt 300 ]; then
    #################################################
    # file needs to be trimmed
    # move all but the first and last 100 rows to file_trim
    #################################################
    low=100
    high=naxis2-100
    echo "$SH_VER:LO:keep rows $low to $high in $file" >> $JOB

    sed -e 's/,a,/,h,/' $FTOOLS/fselect.par > fselect.par
    $FTOOLS/pset ./fselect.par infile="$file[1]"
    $FTOOLS/pset ./fselect.par outfile="${file}_trim"
    $FTOOLS/pset ./fselect.par expr="(#row >= $low) && (#row <= $high)"

    $FTOOLS/fselect >> $JOB 2> log.tmp
    errchk log.tmp "FSELECT $LINENO" fselect.par

  else
    ##################################################
    # less than or equal to 300 rows, no need to trim
    # copy entire file to file_trim
    ###################################################
    echo "$SH_VER:LO:$file <= 300 rows; no triming performed" >> $JOB
    cp $file ${file}_trim
  fi     

  #######################################################
  # record trimmed attitude file names in attitude.trim
  #######################################################    
  echo "${file}_trim" >> attitude.trim

done

####################################
# don't need fselect.par any more
###################################
rm -f fselect.par

###############################################################################
#
# find the mean RA,DEC for each trimmed attitude file that is
# part of this sequence. For N >= 2, make sure the seperation between any 2
# sets is < MAX_SEPARATION; if not true, then do not process this sequence.
#
#############################################################################

echo "$SH_VER:TM:calculate trimmed attitude parameters at $SECONDS" >> $JOB

typeset -i attnum=0

###############################################################################
# set up the stools objdist command
# this command will have the ra and dec from each attitude file appended to it.
# command is later run to make sure
###############################################################################
command="$STOOLS/objdist $MAX_SEPARATION"

######################################
# loop through trimmed attitude files
######################################
for att in $(cat attitude.trim)
do

    ###################################################################
    # read attitude file 
    # put processed mean spacecraft attitude info into attitude.out
    ##################################################################
    echo "$SH_VER:LO:Derive parameters for $att independently" >> $JOB

    sed -e 's/,a,/,h,/' $FTOOLS/attitude.par > attitude.par
    $FTOOLS/pset ./attitude.par attitude="$att[1]"
    $FTOOLS/pset ./attitude.par attpath=./
    $FTOOLS/pset ./attitude.par pointing=ATT

    #######################################################################
    # read attitude file, report any errors, and copy results to log file
    #######################################################################
    $FTOOLS/attitude > ./attitude.out 2> log.tmp

    errchk log.tmp "ATTITUDE $LINENO" attitude.par
    cat ./attitude.out >> $JOB

    ###################################################################
    # check the mean attitude information just read into attitude.out
    ###################################################################
    echo "$SH_VER:LO:run CHECKATT on attitude output from $att" >> $JOB

    $STOOLS/checkatt ./attitude.out 1.0 y y -1 >> $JOB 2>> $JOB
    if [ "$( $FTOOLS/pget ./attitude.par valid)" = "yes" ]
    then
      ################################################################
      # attitude information checks out
      # concatenate mean ra and dec values onto the end of command
      ################################################################
      attnum=attnum+1
      ra=$($FTOOLS/pget ./attitude.par ra_avg)
      dec=$($FTOOLS/pget ./attitude.par dec_avg)
      tmp=$command' '$ra' '$dec
      command="$tmp"
    else
      ###########################################
      # attitude information messed up, so quit
      ###########################################
      echo "$SH_VER:E2:ERROR calculating attitude parameters for $att" >> $JOB
      echo "$SH_VER:E2:job terminating at $SECONDS" >> $JOB
      shexit 1
    fi

done

############################################################################
# if there is more than one attitude file (attnum >= 2) then run the object
# distance STOOL on the RA,DEC values
#############################################################################

if [ $attnum -gt 1 ]
then

  echo "$SH_VER:LO:Running OBJDIST on RA,DEC values at $SECONDS" >> $JOB

  $command > ./objdist.$$

  ############################################################
  #  list the trimmed attitude files at the end of objdist.
  ###########################################################
  echo "ATTITUDE files used:" >> ./objdist.$$

  for att in $(cat attitude.trim)
  do
    echo "$att" >> ./objdist.$$
  done

  ####################################
  # put objdist output into log file
  ####################################
  cat ./objdist.$$ >> $JOB

  ################################################
  # check if any warnings were issued by objdist.
  # if so, quit
  ###############################################
  if [ -n "$(cat ./objdist.$$ | grep "WARNING")" ]
  then
    echo "$SH_VER:E2:MAX separation parameter exceeded; job terminating at $SECONDS" >> $JOB
    shexit 1
  fi

  ################################
  # remove objdist temporary file
  ################################
  rm -f /tmp/objdist.$$

  ##############################################################
  # tack the sequence number onto the objdist output file name
  # in order to keep this file.
  ##############################################################
  mv ./objdist.$$ ./objdist.$SEQUENCE

else
  ###########################################################################
  # there is only one telemetry file, so no need to worry if they match up
  ##########################################################################
  echo "$SH_VER:LO:Only 1 attitude/telem set found, skipping OBJDIST" >> $JOB

fi
  
##################################################################
# don't need attitude.par (which helped read attitude files)
# or attitude.out (which contained the mean attitude information)
# any more
#################################################################
rm -f attitude.par attitude.out






###############################################################################
#
# find the mean RA,DEC for each standard attitude file that is
# part of this sequence. We do this only for future reference.
#
##############################################################################


echo "$SH_VER:TM:calculate standard attitude parameters at $SECONDS" >> $JOB

########################################################################
# loop through attitude files (note these are the un-trimmed versions)
########################################################################
for att in $(cat attitude.files)
do

    ######################################
    # read attitude file into file.attlog
    ######################################
    echo "$SH_VER:LO:Derive parameters for $att independently" >> $JOB

    sed -e 's/,a,/,h,/' $FTOOLS/attitude.par > attitude.par
    $FTOOLS/pset ./attitude.par attitude="$att[1]"
    $FTOOLS/pset ./attitude.par attpath=./
    $FTOOLS/pset ./attitude.par pointing=ATT

    $FTOOLS/attitude > $att.attlog 2> log.tmp
    errchk log.tmp "ATTITUDE $LINENO" attitude.par

    ################################################
    # run checkatt and put results into log file
    ###############################################
    echo "$SH_VER:LO:run CHECKATT on attitude output from $att" >> $JOB
    $STOOLS/checkatt $att.attlog 1.0 y y -1 >> $JOB 2>> $JOB

    ###################################
    # put .attlog file into log file 
    ###################################
    cat $att.attlog >> $JOB

done




###############################################################################
#
# merge the trimmed attitude files
#
###############################################################################


echo "$SH_VER:TM:Begin merge of trimmed attitude files at $SECONDS" >> $JOB

####################################
# clear out any old fselect.par files
#####################################
rm -f fselect.par

#############################################
# tack "[1]" onto trimmed attitude file list. 
# store thses in attitude.trim.ext
#############################################
awk '{printf"%s[1]\n",$1}' attitude.trim > attitude.trim.ext

#############################
# prepare fmerge.par file
#############################
sed -e 's/,a,/,h,/' $FTOOLS/fmerge.par > fmerge.par
$FTOOLS/pset ./fmerge.par infiles="@attitude.trim.ext"
$FTOOLS/pset ./fmerge.par outfile="${ADSE}a.att"
$FTOOLS/pset ./fmerge.par copyprime=yes
##############################################################################
# note that we use the DATE-END TIME-END TSTOP values from the last attitude
# file in the merged attitude file
##############################################################################
$FTOOLS/pset ./fmerge.par lastkey="ORBIT1 MTIME1 DATE1 TIME1 MTIME1-1 DATE1-1 TIME1-1"
$FTOOLS/pset ./fmerge.par history=yes

##################################
# merge the files
##################################
$FTOOLS/fmerge >> $JOB 2> log.tmp
errchk log.tmp "FMERGE $LINENO" fmerge.par

###########################################################
# make sure merged attitude file is still time ordered
##########################################################
check_time_order ${ADSE}a.att[1] TIME yes

############################################################
# delete trimmed attitude files if this is not a test run
###########################################################
if [ "$TEST_RUN" != "true" ]
then

  for file in $(cat attitude.trim)
  do
    rm -f $file
  done

fi

############################################
# cleanup various attitude related files
############################################
rm -f attitude.trim
rm -f attitude.files
rm -f attitude.par
rm -f attitude.trim.ext
rm -f fmerge.par






###########################################################################
# Compute the mean RA and DEC from the merged attitude.
###########################################################################

echo "$SH_VER:TM:Compute mean RA/DEC from attitude at $SECONDS" >> $JOB
echo "$SH_VER:LO:This is computed from the merged attitude files" >> $JOB

###########################
# prepare attitude.par file
###########################
sed -e 's/,a,/,h,/' $FTOOLS/attitude.par > attitude.par
$FTOOLS/pset ./attitude.par attitude="${ADSE}a.att[1]"
$FTOOLS/pset ./attitude.par attpath=./
$FTOOLS/pset ./attitude.par pointing=ATT

###########################################################
# read merged attitude file
# put results into $ADSE.attlog temporarily
# and permanently into log file
###########################################################
$FTOOLS/attitude > $ADSE.attlog 2> log.tmp
errchk log.tmp "ATTITUDE $LINENO" attitude.par
cat $ADSE.attlog >> $JOB
rm -f $ADSE.attlog

############################################
# check validity of merged attitude file
###########################################

valid="$($FTOOLS/pget ./attitude.par valid)"
if [ "$valid" = "yes" ]; then
    #################################
    # merged attitude file is valid,
    # record mean RA, DEC, and ROLL
    # dump these onto log file
    #################################
    RA="$($FTOOLS/pget ./attitude.par ra_avg)"
    DEC="$($FTOOLS/pget ./attitude.par dec_avg)"
    ROLL="$($FTOOLS/pget ./attitude.par roll_avg)"
    echo "$SH_VER:CO:Using telemetry mean RA = $RA" >> $JOB
    echo "$SH_VER:CO:Using telemetry mean DEC = $DEC" >> $JOB
fi

#########################################
# check for various invalid possibilities
##########################################

if [ "$valid" != "yes" -o "$RA" = "NaN" ]; then
    ###########################################################################
    # RA is Not a Number, 
    # try to read it from merged attitude fits file header
    # with either  RA or RA_NON keyword
    # if neither exist, set RA to "0.0" by default
    ###########################################################################
    RA="0.0"
    get_fits_keyword "${ADSE}a.att[1]" RA

    if [ "$get_fits_keyword_exist" = "yes" ]; then
        ##########################
        # there is an RA keyword
        #########################
        RA="$get_fits_keyword_value"
    else
        ##############################
        # try for an RA_NOM keyword
        ##############################
        get_fits_keyword "${ADSE}a.att[1]" RA_NOM

        if [ "$get_fits_keyword_exist" = "yes" ]; then
            ##############################
            # there is an RA_NOM keyword
            ##############################
            RA="$get_fits_keyword_value"
        fi

    fi

    #########################################
    # dump the updated RA to the log file
    ##########################################
    echo "$SH_VER:CO:Using source RA = $RA" >> $JOB
fi

########
# DEC
#######

if [ "$valid" != "yes" -o "$DEC" = "NaN" ]; then
    ########################################################################
    # check for DEC or DEC_NOM keywords in merged attitude fits file header
    ########################################################################
    DEC="0.0"
    get_fits_keyword "${ADSE}a.att[1]" DEC

    if [ "$get_fits_keyword_exist" = "yes" ]; then
        DEC="$get_fits_keyword_value"
    else
        get_fits_keyword "${ADSE}a.att[1]" DEC_NOM

        if [ "$get_fits_keyword_exist" = "yes" ]; then
            DEC="$get_fits_keyword_value"
        fi

    fi

    ######################
    # dump DEC to log file
    ######################
    echo "$SH_VER:CO:Using source DEC = $DEC" >> $JOB
fi

##################################################
# if attiude read wasn't valid, set ROLL to 0.0
#################################################
if [ "$valid" != "yes" ]; then
    ROLL=0.0
fi


#################################
# set RA and DEC in frfread.par
################################
$FTOOLS/pset ./frfread.par ranom="$RA"
$FTOOLS/pset ./frfread.par decnom="$DEC"

####################################
# calculate galactic coordinates
####################################
Lii="$($STOOLS/ecl2gal $RA $DEC | awk '{print $1}')"
Bii="$($STOOLS/ecl2gal $RA $DEC | awk '{print $2}')"
echo "$SH_VER:CO:Computed Galactic Lii = $Lii" >> $JOB
echo "$SH_VER:CO:Computed Galactic Bii = $Bii" >> $JOB

#######################################
# read euler angles from attitude.par
#######################################
Euler1="$($FTOOLS/pget ./attitude.par euler1)"
Euler2="$($FTOOLS/pget ./attitude.par euler2)"
Euler3="$($FTOOLS/pget ./attitude.par euler3)"


########################################################
# remove merged attitude file unless this is a test run
########################################################
if [ $"TEST_RUN" != "true" ]
then
  rm -f ${ADSE}a.att
fi

###############################################################################
# Create .frffiles
###############################################################################

echo "$SH_VER:TM:Create FRFREAD .frffiles file at $SECONDS" >> $JOB

echo "frf_directory,s,h,\"./\",,,\"frf directory\"" > .frffiles

###########################################
# list each telemetry file into .frffiles
###########################################
for tel in $(awk '{print $1}' job.par.info); do
    echo "frf_file,s,h,\"$tel\",,,\"frf telemetry file\"" >> .frffiles
done


# 
############################################################################
##########################################################################
##
##  Run frfread
##
##  FRF stands for "First Reduction File"
##
##  This splits one big telemetry file into lots of little ones,
##  A housekeeping (HK) file is produced for each instrument.
##  An event file containing photon data is produced for each instrument
##  and with a new file created each time the spacecraft changes mode
##
##  File naming conventions:
##
##  Base FRF telemetry file: ft*.*   where the *.* varation is given by:
##                          yymmdd_hhmm.hhmm (start date, start time, end time)
##
## Modally split telemetry FRFREAD "raw" event files. The first * in each
## name stands for yymmdd_hhmm_hhmm . The second * stands for the 6 digit
## modal configuration sequence.
## 
## ft*G2*[LMH].fits         -- [LMH] bit rate GIS2 FRFREAD "raw" event files
## ft*G3*[LMH].fits         -- [LMH] bit rate GIS3 FRFREAD "raw" event files
## ft*S0*[LMH].fits         -- [LMH] bit rate SIS0 FRFREAD "raw" event files
## ft*S1*[LMH].fits         -- [LMH] bit rate SIS1 FRFREAD "raw" event files
##
##  Housekeepping and associated files. The * stands for yymmdd_hhmm_hhmm .
##
##  ft*G2HK.fits             -- GIS2 house keeping file
##  ft*G3HK.fits             -- GIS3 house keeping file
##  ft*S0HK.fits             -- SIS0 house keeping file
##  ft*S1HK.fits             -- SIS1 house keeping file
##  ft*CMHK.fits             -- common house keeping file
##
##########################################################################
##########################################################################

echo "$SH_VER:TM:Start $FRFREAD at $SECONDS" >> $JOB

$FRFREAD >> $JOB 2> log.tmp

####################
# check for errors
####################
if [ $? -ne 0 ]; then
    echo "$SH_VER:E1:FRFREAD returned non-zero exit status!" >> $JOB
    cat log.tmp >> $JOB
    echo "$SH_VER:E1:FRFREAD returned non-zero exit status!"
    shexit 1
fi

##############################################################
# check if there is more than one line with "^coordinate"
# in the output from frfread
# report results to log file
#############################################################
egrep '^coordinate' log.tmp > log1.tmp

if [ $(wc -l < log1.tmp) -gt 0 ]; then
    echo "$SH_VER:E1:STDERR output from frfreadng $LINENO" >> $JOB
    cat log1.tmp >> $JOB
    echo "$SH_VER:E0:STDERR output from frfreadng $LINENO" >> $JOB
fi

rm -f log1.tmp

#################################################
# check for lines without "^coordinate" in them
# these are errors
##################################################
egrep -v '^coordinate' log.tmp > log2.tmp
errchk log2.tmp "frfreadng $LINENO" frfread.par .frffiles
rm -f log.tmp

############################################################
# loop through files in working directory which start with
# ft, end with .fits and don't have HK in them
# these are all the files containing photon data
# remove all the files which have no events
#############################################################
for file in $(ls | egrep '^ft.*\.fits$' | grep -v HK); do
    if [ $($STOOLS/getnevents < $file) -eq 0 ]; then
        echo "$SH_VER:E1:$file dropped with 0 events" >> $JOB
        rm $file
    fi
done

####################################################################
# list the remaining files starting with ^ft and ending in .fits
# (telemetry files and housekeeping files)
# into the file RAW_FRF.list
###################################################################
ls | egrep '^ft.*\.fits$' > RAW_FRF.list

###############################################
# don't need frfread.par or .frffiles any more
##############################################
rm -f frfread.par .frffiles

# 
###############################################################################
###############################################################################
##
##  build SIS rate files from the HK (housekeeping) files
##
##  SIS rate files are called ad[sequence #]S[0/1].rate
##  after these two files are created, they are moved to directory
##  $SIS_RATE_STAGE
##
##  The steps in making the rate files are:
##    1) merge all HK files
##    2) expand them with hkexpand
##    3) set saturation flags for the CCDS
##
##  Temporary files: 
##    S[0/1]hk.fits 
##    S[0/1]hk.telem
##
##  The following code was translated into bash/ksh from a csh script written
##  by Keith Gendreau and Eric Gothelf (hkfixer)
############################################################################

#########################################
# loop over the SIS0 and SIS1 HK files
########################################
for inst in S0 S1
do

  echo "$SH_VER:TM:Begin $inst rate file create at $SECONDS" >> $JOB

  ###################################################
  # count the number of HK files for this intrument
  ###################################################
  ls *${inst}HK.fits > ./${inst}hk.list
  num_hk_files=$(echo ./${inst}hk.list | wc -l)

   #####################################################
   # if no HK files found for this SIS do not continue
   #####################################################
  if [ $num_hk_files -eq 0 ]
  then
    echo "$SH_VER:L0: no HK files found for ${inst}" >> $JOB
    echo "$SH_VER:L0: cannot make ${inst} rate file" >> $JOB
    continue
  fi

  ################################################################
  # if more than 1 HK file is found for this SIS then merge them 
  # the merged file is called [s0/s1]hk.fits 
  ################################################################
  if [ $num_hk_files -gt 1 ]
  then
    fmerge @${inst}hk.list ${inst}hk.fits - clobber=yes 
  else
    cp $(cat ${inst}hk.list) ${inst}hk.fits
  fi

  ############################################################
  # figure out which CCDs are involved in the merged HK file
  # store list of CCDs in list
  # (fdump makes an ascii dump of a fits file)
  ############################################################
  $FTOOLS/fdump ${inst}hk.fits outfile=./hk.dmp columns="NAME" rows=-                          prhead=no showunit=no showrow=no
  list=$(cat hk.dmp | grep '_SATF' | sort | uniq | sed "s/${inst}_SATF//g")
  rm -f ./hk.dmp

  ###########################################################################
  # make wordlist file for hkexpand - this tells which keywords to copy over
  ###########################################################################
  echo ${inst}_BRATE >  ./wordlist
  echo ${inst}_MODE  >> ./wordlist 
  echo ${inst}_ARENA >> ./wordlist

  ##########################################################################
  # we only add the following keywords to the wordlist if the corresponding
  # CCD is listed in the HK file
  ##########################################################################
  for i in $list
  do
    echo "$SH_VER:L0: CCD $i in use for ${inst}" >> $JOB  
    echo ${inst}'_TELM'$i >> ./wordlist
    echo ${inst}'_LRWX'$i >> ./wordlist
    echo ${inst}'_LRWY'$i >> ./wordlist
  done


  #######################################################################
  # make an expanded HK file for the columns in 'wordlist'
  # The expanded file is called [s0/s1]hk.telem
  # 
  # compressed HK format is three collumns: TIME PARAMETER_NAME VALUE
  # uncompressed format has one collumn for each parameter
  #######################################################################
  rm -f ${inst}hk.telem

  $FTOOLS/hkexpand ${inst}hk.fits ${inst}hk.telem "@wordlist" mode=h >> $JOB        2>  log.tmp

  errchk log.tmp "HKEXPAND $LINENO"

  ####################################################################
  # There is an error in the original HK file - let's fix it now.
  # 
  # The telemetry is SATURATED if the number of telemetried events
  # equals the telemetry limit:
  #
  # SIS MODE  DATA MODE     BIT RATE      TELEMETRY LIMIT
  #
  #  4 CCD     BRIGHT         LOW                32            
  #                         MEDIUM             128           BRIGHT = 1   
  #                           HIGH             1024           FAINT  = 2
  #
  #            FAINT          LOW                 8           LOW    = 1 
  #                          MEDIUM              32           MEDIUM = 2
  #                           HIGH              256           FAINT  = 4
  #
  #
  # Then Sn_SATm is set to TRUE
  # n=[0/1] for SIS0 or SIS1
  # m= CCD number
  ########################################################################

  rm -f ./out1.tmp
  rm -f ./out2.tmp

  old=./out1.tmp
  new=./out2.tmp

  cp ${inst}hk.telem ./out1.tmp

  #################################################
  # loop over all CCDs used in the merged HK file
  #################################################
  for c in $list
  do 

  ############################################################################
  # In the following statement, expr is set to:
  #
  # (S[0/1]_BRATE.eq.1 .and. S[0/1]_MODE.eq.2 .and. S[0/1]_TELM${c}.ne.32  ).or.
  # (S[0/1]_BRATE.eq.2 .and. S[0/1]_MODE.eq.2 .and. S[0/1]_TELM${c}.ne.128 ).or.
  # (S[0/1]_BRATE.eq.4 .and. S[0/1]_MODE.eq.2 .and. S[0/1]_TELM${c}.ne.1024).or.
  # (S[0/1]_BRATE.eq.1 .and. S[0/1]_MODE.eq.1 .and. S[0/1]_TELM${c}.ne.8   ).or.
  # (S[0/1]_BRATE.eq.2 .and. S[0/1]_MODE.eq.1 .and. S[0/1]_TELM${c}.ne.32  ).or.
  # (S[0/1]_BRATE.eq.4 .and. S[0/1]_MODE.eq.1 .and. S[0/1]_TELM${c}.ne.256 ) 
  #
  # fcalc evaluates this expression and sets S[0/1]_SAT[CCD number] equal to 
  # the result
  #
  #############################################################################
    expr="(${inst}_BRATE.eq.1.and.${inst}_MODE.eq.2.and.${inst}_TELM${c}.ne.32).or.(${inst}_BRATE.eq.2.and.${inst}_MODE.eq.2.and.${inst}_TELM${c}.ne.128).or.(${inst}_BRATE.eq.4.and.${inst}_MODE.eq.2.and.${inst}_TELM${c}.ne.1024).or.(${inst}_BRATE.eq.1.and.${inst}_MODE.eq.1.and.${inst}_TELM${c}.ne.8).or.(${inst}_BRATE.eq.2.and.${inst}_MODE.eq.1.and.${inst}_TELM${c}.ne.32).or.(${inst}_BRATE.eq.4.and.${inst}_MODE.eq.1.and.${inst}_TELM${c}.ne.256)" 

    fcalc $old $new ${inst}_SAT${c} "$expr" >> $JOB 2> log.tmp

    errchk log.tmp "FCALC $LINENO"

    ######################################
    # swap pointers to old and new files
    ######################################
    tmp=$old
    old=$new
    new=$tmp

    ##############################################
    #clear out the new file to recieve more input
    ##############################################
    rm -f $new

  done

  ###################################################################
  # rename the resulting file to its true product name if it exists
  ###################################################################
  if [ -r $old ]
  then
    ###################################
    # file $old exists and is readable
    # copy it to ad[SEQ #]S[0/1].rate
    ###################################
    mv $old ./'ad'$SEQUENCE${inst}'.rate'

  else
    ################################
    # error: file $old doesn't exist
    ################################
    echo "$SH_VER:E1: rate files for instrument ${inst} not created" >> $JOB

  fi


  ##########
  # cleanup
  ##########
  rm -f out1.tmp out2.tmp wordlist ${inst}hk.fits ${inst}hk.telem 
  rm -f ${inst}hk.list

done





# 
###############################################################################
###############################################################################
##
##  Run temp2gain
##  this takes the telemetry files (unsplit FRF file) 
##  and calculates instrument sensitivity as a function of time and temperature.
## 
##  the resulting "gain history files" are named after the original 
##  telemetry files but with punctuation removed and .ghf appended
##
##  the .tbl file is the gain history file in ascii format
##
###############################################################################
###############################################################################

echo "$SH_VER:TM:Begin GIS gain history create at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $ISASBIN/temp2gain.par > temp2gain.par
$FTOOLS/pset ./temp2gain.par histfile="$GAINHISTCAL"

###############################
# loop through telemetry files
###############################
for tel in $(awk '{print $1}' job.par.info); do

    #################################################################
    # ghf_fname is telemetry file name with all .'s and _'s removed
    # and .ghf appended
    ###############################################################
    ghf_fname="$(echo $tel | tr '.' '_').ghf"
    echo "$SH_VER:LO:Create $ghf_fname" >> $JOB

    $FTOOLS/pset ./temp2gain.par frffile="$tel"
    $FTOOLS/pset ./temp2gain.par outfile="$ghf_fname"
    $FTOOLS/pset ./temp2gain.par tblfile="$tel.tbl"
    $FTOOLS/pset ./temp2gain.par type=0
    $FTOOLS/pset ./temp2gain.par steptime=600

    #############################
    # run temp2gain
    #############################

    $ISASBIN/temp2gain > temp2gain.log 2> log1.tmp

    #########################
    # check for errors
    ########################
    grep "Error|Warning" log1.tmp > log.tmp
    grep -v "Error|Warning" log1.tmp >> $JOB
    rm log1.tmp
    errchk log.tmp "TEMP2GAIN $LINENO" temp2gain.par

    ################################################
    # dump any messages from temp2gain to log file
    ################################################
    if [ $(wc -l < temp2gain.log) -gt 0 ]; then
        echo "$SH_VER:L2:STDOUT output from temp2gain $LINENO" >> $JOB
        echo "$SH_VER:LO:File is $tel" >> $JOB
        cat temp2gain.log >> $JOB
        echo "$SH_VER:L0:STDOUT output from temp2gain $LINENO" >> $JOB
    fi

    rm temp2gain.log

    ###########################################
    # check if ghf file is still time ordered
    ##########################################
    check_time_order $ghf_fname[1] CAL_START yes
    check_time_order $ghf_fname[1] CAL_STOP yes

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

done

rm -f temp2gain.par




# 
###############################################################################
# Run mkfilter2
# produces filter file used by xselect
# uses HK files, frf.orbit, $RIGID=rigidity.data[version], frf.orbit, and 
# attitude file to do this.
#
# 9/8/95 rigidity.data lives in /aps/calibration
#
# output files are named after telemetry files with punctuation 
# removed and .mkf appended
###############################################################################

echo "$SH_VER:TM:Begin MKFILTER2 at $SECONDS" >> $JOB
cp $LEAPTABLE ./${LEAPTABLE##/*/}

############################
# loop over attitude files
############################
for tel in $(awk '{print $1}' job.par.info); do

    echo "$SH_VER:LO:Run mkfilter2 on $tel" >> $JOB

    ##############################################
    # get name of attitude file from job.par.info
    ###############################################
    att="$(grep $tel job.par.info | awk '{print $3}')"

    ########################################################
    # base is telemetry file name with punctuation removed
    ########################################################
    base="$(echo $tel | tr '.' '_')"

    sed -e 's/,a,/,h,/' $ISASBIN/mkfilter2.par > mkfilter2.par
    $FTOOLS/pset ./mkfilter2.par s0_hk=${base}S0HK.fits
    $FTOOLS/pset ./mkfilter2.par s1_hk=${base}S1HK.fits
    $FTOOLS/pset ./mkfilter2.par g2_hk=${base}G2HK.fits
    $FTOOLS/pset ./mkfilter2.par g3_hk=${base}G3HK.fits
    $FTOOLS/pset ./mkfilter2.par rigidity=$RIGID
    $FTOOLS/pset ./mkfilter2.par orbit=frf.orbit
    $FTOOLS/pset ./mkfilter2.par leapsec=./${LEAPTABLE##/*/}
    $FTOOLS/pset ./mkfilter2.par attitude=$att
    $FTOOLS/pset ./mkfilter2.par euler="$Euler1 $Euler2 $Euler3"
    $FTOOLS/pset ./mkfilter2.par binwidth=32
    $FTOOLS/pset ./mkfilter2.par outfile=$base.mkf
    $FTOOLS/pset ./mkfilter2.par clobber=yes

    ##################
    # run mkfilter2
    ##################
    $ISASBIN/mkfilter2 >> $JOB 2> log.tmp
    errchk log.tmp "MKFILTER2 $LINENO" mkfilter2.par

    #####################################
    # check if mkf file is time ordered
    # if not, fix it
    ####################################
    check_time_order $base.mkf[1] TIME yes
    if [ $? -eq 2 ]; then
        fits_mem_sort $base.mkf[1] TIME
    fi

done

##########################
# done with mkfilter2.par
#########################
rm -f mkfilter2.par



###############################################################################
# Run mkdtime to fill the deadtime columns in .mkf files 
###############################################################################

echo "$SH_VER:TM:Begin MKDTIME at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $FTOOLS/mkdtime.par > mkdtime.par
$FTOOLS/pset ./mkdtime.par outfile=NONE

##########################
# loop through .mkf files
##########################
for file in $(ls | egrep '\.mkf$'); do

    $FTOOLS/pset ./mkdtime.par infile="$file"

    $FTOOLS/mkdtime >> $JOB 2> log.tmp
    errchk log.tmp "mkdtime $LINENO" mkdtime.par

done

rm -f mkdtime.par


# 
#############################################################################
#############################################################################
##
##  begin ASCALIN runs on GIS files
##
##############################################################################
##############################################################################

echo "$SH_VER:TM:Start ASCALIN/GIS at $SECONDS" >> $JOB

######################################################
# create ascalin.fail.list and ascalin.ok.list files
######################################################
touch ascalin.fail.list
touch ascalin.ok.list

###############################################################
# copy $G[2/3][ON/OFF][POW/FLF]MAP files to working directory
# These are the telescope definition files (calfiles)
# 9/8/95 these reside in /aps/calibration
# and are called gas[2/3]_ano_[off/on]_[flf/pow2].fits
###############################################################
cp $G2ONPOWMAP ./${G2ONPOWMAP##/*/}
cp $G2OFFPOWMAP ./${G2OFFPOWMAP##/*/}
cp $G2ONFLFMAP ./${G2ONFLFMAP##/*/}
cp $G2OFFFLFMAP ./${G2OFFFLFMAP##/*/}
cp $G3ONPOWMAP ./${G3ONPOWMAP##/*/}
cp $G3OFFPOWMAP ./${G3OFFPOWMAP##/*/}
cp $G3ONFLFMAP ./${G3ONFLFMAP##/*/}
cp $G3OFFFLFMAP ./${G3OFFFLFMAP##/*/}

####################
# set up ascalin.par
####################
sed -e 's/,a,/,h,/' $FTOOLS/ascalin.par > ascalin.par
$FTOOLS/pset ./ascalin.par history=yes
$FTOOLS/pset ./ascalin.par ranom="$RA"
$FTOOLS/pset ./ascalin.par decnom="$DEC"
$FTOOLS/pset ./ascalin.par verbose=no

#############################################################
# loop over all event files (modaly split) which have 
# mode number "70" (GIS PH mode)
############################################################
for CURRENT_FITS in $(ls | egrep '70[HML]\.fits$' | sort); do

    add_fits_keyword "$CURRENT_FITS[0]" COORDPRO ERROR

    ######################################################
    # read POS_DET (position determination method) equals FLF or POW2
    # and ANO_TUNE 
    # from current event file
    ######################################################
    get_fits_keyword "$CURRENT_FITS[0]" POS_DET
    POS_DET="$get_fits_keyword_value"

    get_fits_keyword "$CURRENT_FITS[0]" ANO_TUNE
    ANO_TUNE="$get_fits_keyword_value"

    ###########################################################################
    # determine which Telescope Definition to use (i.e. set calfile parameter)
    #
    ###########################################################################
    case $CURRENT_FITS in
    *G2*) case $POS_DET in
          ###################
          # GIS2 files
          ###################

          *POW*) case $ANO_TUNE in
                 *ON*) $FTOOLS/pset ./ascalin.par calfile="${G2ONPOWMAP##/*/}"
                       echo "${G2ONPOWMAP##/*/}" >> gis2_cal_files_used ;;
                 *   ) $FTOOLS/pset ./ascalin.par calfile="${G2OFFPOWMAP##/*/}"
                       echo "${G2OFFPOWMAP##/*/}" >> gis2_cal_files_used ;;
                 esac ;;

          *FLF*) case $ANO_TUNE in
                 *ON*) $FTOOLS/pset ./ascalin.par calfile="${G2ONFLFMAP##/*/}"
                       echo "${G2ONFLFMAP##/*/}" >> gis2_cal_files_used ;;
                 *   ) $FTOOLS/pset ./ascalin.par calfile="${G2OFFFLFMAP##/*/}"
                       echo "${G2OFFFLFMAP##/*/}" >> gis2_cal_files_used ;;
                 esac ;;

          *    ) echo "Unknown POS_DET=$POS_DET for $CURRENT_FITS" >> $JOB
                 echo "File skipped" >> $JOB
                 echo $CURRENT_FITS >> ascalin.fail.list
                 continue ;;

          esac ;;

    *G3*) case $POS_DET in
          ###################
          # GIS2 files
          ###################

          *POW*) case $ANO_TUNE in
                 *ON*) $FTOOLS/pset ./ascalin.par calfile="${G3ONPOWMAP##/*/}"
                       echo "${G3ONPOWMAP##/*/}" >> gis3_cal_files_used ;;
                 *   ) $FTOOLS/pset ./ascalin.par calfile="${G3OFFPOWMAP##/*/}"
                       echo "${G3OFFPOWMAP##/*/}" >> gis3_cal_files_used ;;
                 esac ;;

          *FLF*) case $ANO_TUNE in
                 *ON*) $FTOOLS/pset ./ascalin.par calfile="${G3ONFLFMAP##/*/}"
                       echo "${G3ONFLFMAP##/*/}" >> gis3_cal_files_used ;;
                 *   ) $FTOOLS/pset ./ascalin.par calfile="${G3OFFFLFMAP##/*/}"
                       echo "${G3OFFFLFMAP##/*/}" >> gis3_cal_files_used ;;
              esac ;;

          *    ) echo "Unknown POS_DET=$POS_DET for $CURRENT_FITS" >> $JOB
                 echo "File skipped" >> $JOB
                 echo $CURRENT_FITS >> ascalin.fail.list
                 continue ;;

          esac ;;

    esac

    #########################################
    # read TLM_FILE keyword 
    #########################################
    get_fits_keyword "$CURRENT_FITS[0]" TLM_FILE
    export TLM_FILE="$(echo "$get_fits_keyword_value" | tr -d \')"

    ################################################
    # set tempofile = Temporal history file
    # better known as the gain history file (.ghf)
    #################################################
    $FTOOLS/pset ./ascalin.par tempofile="$(echo ${TLM_FILE} | tr '.' '_').ghf"


    ########################################################################
    # check if $TLM_FILE is in job.par.info and get
    # name of corresponding attitude file
    #
    # If possible set pointing=USER and attitude to correct attitude file
    # in order to use correct attitude file for pointing information
    # Otherwise, set pointing=EULER and attitude=euler
    # in order to use euler angles for pointing information.
    # If possible, the Euler angles are taken from job.par.info,
    # otherewise, they are just set to zero
    ##################################################################
    attitude="$(grep $TLM_FILE job.par.info | awk '{print $3}')"

    if [ -n "$attitude" -a -s "$attitude" ]; then
        #####################################################################
        # $TLM_FILE is in job.par.info
        # since attitude is defined and points to an existing non-empty file
        #####################################################################
        $FTOOLS/pset ./ascalin.par attitude="$attitude"
        $FTOOLS/pset ./ascalin.par pointing=USER

    else
        #############################################
        # use euler angles from job.par.info instead
        #############################################
        $FTOOLS/pset ./ascalin.par attitude=euler
        $FTOOLS/pset ./ascalin.par pointing=EULER

        ###################################################
        # get corresponding euler angles from job.par.info
        ###################################################
        export EULER1="$(grep $TLM_FILE job.par.info | awk '{print $5}')"
        export EULER2="$(grep $TLM_FILE job.par.info | awk '{print $6}')"
        export EULER3="$(grep $TLM_FILE job.par.info | awk '{print $7}')"

        if [ -z "$EULER1" ]; then 
            #############################################
            # can't get euler angles from job.par.info
            ############################################
            $FTOOLS/pset ./ascalin.par eulerphi=0.0
            $FTOOLS/pset ./ascalin.par eulertheta=0.0
            $FTOOLS/pset ./ascalin.par eulerpsi=0.0
        else
            ##################################
            # use euler angles in ascalin.par
            ##################################
            $FTOOLS/pset ./ascalin.par eulerphi="$EULER1"
            $FTOOLS/pset ./ascalin.par eulertheta="$EULER2"
            $FTOOLS/pset ./ascalin.par eulerpsi="$EULER3"
        fi
    fi

    ########################################################
    # set datafile to the current modally split event file
    ########################################################
    $FTOOLS/pset ./ascalin.par datafile="$CURRENT_FITS"

    ################################
    # run ascalin
    ################################
    $FTOOLS/ascalin > ascalin.log 2> log.tmp

    ######################
    # check for errors
    ######################
    cp log.tmp log1.tmp
    errchk log1.tmp "ascalin $LINENO" ascalin.par

    #######################################
    # was ascalin terminated prematurely?
    #######################################
    log_lines="$(grep "Program terminated prematurely" log.tmp | wc -l)"
    if [ "$log_lines" -gt 0 ]; then
        echo $CURRENT_FITS >> ascalin.fail.list
    else
        echo $CURRENT_FITS >> ascalin.ok.list
    fi

    ###################################################
    # report what there is to report to the log file
    ###################################################
    if [ $(sed -n -e '2,$p' < ascalin.log | wc -l) -gt 0 ]; then
        echo "$SH_VER:L2:STDOUT output from ascalin $LINENO" >> $JOB
        echo "$SH_VER:LO:File is $CURRENT_FITS" >> $JOB
        cat ascalin.log >> $JOB
        echo "$SH_VER:L0:STDOUT output from ascalin $LINENO" >> $JOB
    fi

    rm ascalin.log

done


#########################################
# remove unused gis calibration files
#########################################

if [ -z "$(grep ${G2ONPOWMAP##/*/}  gis2_cal_files_used)" ]
then
  rm -f ${G2ONPOWMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G2OFFPOWMAP##/*/} gis2_cal_files_used)" ]
then
  rm -f ${G2OFFPOWMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G2ONFLFMAP##/*/}  gis2_cal_files_used)" ]
then
  rm -f ${G2ONFLFMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G2OFFFLFMAP##/*/} gis2_cal_files_used)" ]
then
  rm -f ${G2OFFFLFMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G3ONPOWMAP##/*/}  gis3_cal_files_used)" ]
then
  rm -f ${G3ONPOWMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G3OFFPOWMAP##/*/} gis3_cal_files_used)" ]
then
  rm -f ${G3OFFPOWMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G3ONFLFMAP##/*/}  gis3_cal_files_used)" ]
then
  rm -f ${G3ONFLFMAP##/*/}
fi

################################################################

if [ -z "$(grep ${G3OFFFLFMAP##/*/} gis3_cal_files_used)" ]
then
  rm -f ${G3OFFFLFMAP##/*/}
fi

###################
# remove par files
####################

rm -f ascalin.par






###############################################################################
###############################################################################
##
##  begin ASCALIN runs on SIS files
##
###############################################################################
###############################################################################

echo "$SH_VER:TM:Start ASCALIN/SIS at $SECONDS" >> $JOB

######################################################################
# copy telescope definition file (calfile) to working directory
# these live in /aps/callibration
#######################################################################

cp $S0MAP ${S0MAP##/*/}
cp $S1MAP ${S1MAP##/*/}

######################
# prepare ascalin.par
######################
sed -e 's/,a,/,h,/' $FTOOLS/ascalin.par > ascalin.par
$FTOOLS/pset ./ascalin.par history=yes
$FTOOLS/pset ./ascalin.par ranom="$RA"
$FTOOLS/pset ./ascalin.par decnom="$DEC"
$FTOOLS/pset ./ascalin.par verbose=no

###########################
# loop through fits files
###########################
for CURRENT_FITS in $(ls | egrep '0[12][HML]\.fits$' | sort); do

    add_fits_keyword "$CURRENT_FITS[0]" COORDPRO ERROR

    #######################################################################
    # choose correct telescope definition file (calfile) for SIS0 or SIS1
    #######################################################################
    case $CURRENT_FITS in

    *S0*) $FTOOLS/pset ./ascalin.par calfile="${S0MAP##/*/}" ;;

    *S1*) $FTOOLS/pset ./ascalin.par calfile="${S1MAP##/*/}" ;;

    *   ) echo "Unknown instrument FITS file $CURRENT_FITS" >> $JOB
          echo "File skipped" >> $JOB
          continue ;;

    esac

    $FTOOLS/pset ./ascalin.par tempofile="$REFDATA/$SISPICAL"

    #########################################################################
    # get TLM_FILE keyword from fits file
    # this is parent frf telemetry file name from modally split event file
    ########################################################################
    get_fits_keyword "$CURRENT_FITS[0]" TLM_FILE
    export TLM_FILE="$(echo "$get_fits_keyword_value" | tr -d \')"

    ##############################################################
    # try to get corresponding attitude file from job.par.info
    ##############################################################
    attitude="$(grep $TLM_FILE job.par.info | awk '{print $3}')"

    if [ -n "$attitude" -a -s "$attitude" ]; then
        ##################################################
        # sucessfully got attutude file from job.par.info
        ##################################################
        $FTOOLS/pset ./ascalin.par pointing=USER
        $FTOOLS/pset ./ascalin.par attitude="$attitude"
    else
        ##############################################
        # use euler angles from job.par.info instead
        #############################################
        $FTOOLS/pset ./ascalin.par attitude=euler
        $FTOOLS/pset ./ascalin.par pointing=EULER

        export EULER1="$(grep $TLM_FILE job.par.info | awk '{print $5}')"
        export EULER2="$(grep $TLM_FILE job.par.info | awk '{print $6}')"
        export EULER3="$(grep $TLM_FILE job.par.info | awk '{print $7}')"

        if [ -z "$EULER1" ]; then 
            ##################################################
            # couldn't get euler angles, so set them to zero
            ##################################################
            $FTOOLS/pset ./ascalin.par eulerphi=0.0
            $FTOOLS/pset ./ascalin.par eulertheta=0.0
            $FTOOLS/pset ./ascalin.par eulerpsi=0.0
        else
            $FTOOLS/pset ./ascalin.par eulerphi="$EULER1"
            $FTOOLS/pset ./ascalin.par eulertheta="$EULER2"
            $FTOOLS/pset ./ascalin.par eulerpsi="$EULER3"
        fi

    fi

    ####################################################################
    # set datafile in ascalin.par to current modally split event file
    ####################################################################
    $FTOOLS/pset ./ascalin.par datafile="$CURRENT_FITS"

    ####################
    # run ascalin
    ####################
    $FTOOLS/ascalin > ascalin.log 2> log.tmp

    #####################
    # check for errors
    #####################
    cp log.tmp log1.tmp
    errchk log1.tmp "ascalin $LINENO" ascalin.par

    ###########################################
    # check if ascalin terminated prematurely
    ##########################################
    log_lines="$(grep "Program terminated prematurely" log.tmp | wc -l)"
    if [ "$log_lines" -gt 0 ]; then
        echo $CURRENT_FITS >> ascalin.fail.list
    else
        echo $CURRENT_FITS >> ascalin.ok.list
    fi

    ##############################################
    # report what there is to report to log file
    ###############################################
    if [ $(sed -n -e '2,$p' > $JOB
        echo "$SH_VER:LO:File is $CURRENT_FITS" >> $JOB
        cat ascalin.log >> $JOB
        echo "$SH_VER:L0:STDOUT output from ascalin $LINENO" >> $JOB
    fi

    ##########################
    # done with ascalin.log
    ##########################
    rm ascalin.log

done

rm -f ascalin.par

###########################################
# report ascalin failures to log file
##########################################
if [ -s ascalin.fail.list ]; then

    echo "$SH_VER:LO:BEGIN ascalin.fail.list:" >> $JOB
    cat ascalin.fail.list >> $JOB
    echo "$SH_VER:LO:END of ascalin.fail.list" >> $JOB

fi

# 
###############################################################################
###############################################################################
##
##  Run gis3bitfix on ascalin-corrected GIS files if the observation time falls
##  between Feb 10 and Apr 8 1994; this is to correct an on-board spacecraft
##  related problem.
##
##  uses calibration files, gain history file and attitude file(s)
##  in order to fix modally split event files for the GIS3 with mode
##  number ending in "70" (PH mode)
##
#############################################################################
#############################################################################


echo "$SH_VER:TM:Begin gis3bitfix at $SECONDS" >> $JOB

typeset -i filedate

#########################################################################
# loop over all event (telemetry) files listed in the job.par.info file
#########################################################################
for file in $(awk '{print $1}' < job.par.info); do
  name=${file#ft}
  filedate=${name%_*}

  ###############################################################
  # if the obs time is not in the proper interval then ignore it
  ###############################################################
  if [ $filedate -lt 940210 -o 940408 -lt $filedate ]; then
    continue
  fi



  ######################################################################
  # from here to end of loop only executed if the date is in the range 
  # were a fix needs to be done
  ######################################################################

  ############################
  # set the attitude file name
  ############################
  attfile="$(grep $file job.par.info | awk '{print $3}')"

  ####################################
  # set the gis3bitfix list name
  #################################
  evtlist=gis3bitfix.list

  ##########################################################################
  # set the base name for all the event files to be processed by gis3bitfix
  ##########################################################################
  base=${file%?????}_${file#??????????????}

  ###########################################################################
  # process all GIS3 event files of mode number 70 (GIS PH mode)
  # that are part of the appropriate
  # base name
  ###########################################################################
  for j in $(ls ${base}G3???70[HML].fits)
  do

    ls $j > gis3bitfix.list
    timfile="$base.ghf"

    ###########################################################################
    # see what method was used to determine S/C position for this event file and
    # choose the appropriate calibration file for that method
    ############################################################################
    get_fits_keyword "$j[0]" POS_DET
    if [ -n "$(echo $get_fits_keyword_value | grep 'FLF')" ]
    then
      #########################
      # POS_DET contains "FLF"
      ########################
      calfile=$REFDATA/$GIS3BFCAL_FLF
      echo "$SH_VER:LO:using FLF cal file for $j" >> $JOB
    else
      ##################################
      # POS_DET does not contain 'FLF"
      #################################
      if [ -n "$(echo $get_fits_keyword_value | grep 'POW2')" ]
      then
        calfile=$REFDATA/$GIS3BFCAL_POW2
        echo "$SH_VER:LO:using POW2 cal file for $j" >> $JOB

      else
        calfile=$REFDATA/$GIS3BFCAL_FLF
        echo "$SH_VER:LO: Warning: unknown positioning type:" >> $JOB
        echo "$SH_VER:LO:          using FLF cal file for $j" >> $JOB

      fi

    fi

    ####################################
    # run gis3bitfix on the event file
    ####################################
    echo "$SH_VER:LO:running gis3bitfix on $j" >> $JOB
    $FTOOLS/gis3bitfix.e $evtlist $calfile $timfile $attfile > gis3bitfix.log 2> log.tmp

    ######################
    # check for errors
    ######################
    errchk log.tmp "gis3bitfix $LINENO" gis3bitfix.log gis3bitfix.list

  done
done

#######################################################
# copy original frf telemetry files files to file.fix
#######################################################
for file in $(ls | egrep '\.fix$'); do
  echo "$SH_VER:LO:mv $file to ${file%.fix}" >> $JOB
  mv $file ${file%.fix}
done

#####################
# clean up files
#####################
rm -f gis3bitfix.list
rm -f gis3bitfix.log





# 
###############################################################################
#
# Convert SIS FAINT data to BRIGHT for frfread data
#
# takes modally split event files for the SIS with mode code ending in "01"
# and creates correspongoinf files with mode number changed.
# 
# when mimicing ASCA onboard conversion      mode code => "02"
# when applying dark frame error corrections mode code => "12"
#
################################################################################

echo "$SH_VER:TM:Start FAINT->BRIGHT at $SECONDS" >> $JOB

############################################
# assign SPLIT0 and SPLIT1
# defaults are 40
############################################
SPLIT0=40
SPLIT1=40

########################################################################
# loop through files ending in "02[H/M/L].fits" (split event files)
# (SIS BRIGHT mode)
########################################################################
for CURRENT_FITS in $(ls | egrep '02[HML]\.fits$'); do

    ###########################
    # read S0_SPTRO to SLITP0
    ###########################
    get_fits_keyword "$CURRENT_FITS[0]" S0_SPTR0
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT0="$get_fits_keyword_value"
        break
    fi

    ##########################
    # read S0_SPTR1 to SPLIT0
    ##########################
    get_fits_keyword "$CURRENT_FITS[0]" S0_SPTR1
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT0="$get_fits_keyword_value"
        break
    fi

    ##########################
    # read S0_SPTR2 to SPLIT0
    ##########################
    get_fits_keyword "$CURRENT_FITS[0]" S0_SPTR2
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT0="$get_fits_keyword_value"
        break
    fi

    ##########################
    # read S0_SPTR3 to SPLIT0
    ##########################
    get_fits_keyword "$CURRENT_FITS[0]" S0_SPTR3
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT0="$get_fits_keyword_value"
        break
    fi

   ###################################################################
   # set SPLIT0 to 40 and break out of loop 
   ###################################################################
    SPLIT0=40
    break

done

###############################################################################
# loop through files ending in "02[H/M/L].fits" (modally split telemetry files)
###############################################################################
for CURRENT_FITS in $(ls | egrep '02[HML]\.fits$'); do

    ############################
    # read S1_SPTR0 into SPLIT1
    ############################
    get_fits_keyword "$CURRENT_FITS[0]" S1_SPTR0
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT1="$get_fits_keyword_value"
        break
    fi

    ############################
    # read S1_SPTR1 into SPLIT1
    ############################
    get_fits_keyword "$CURRENT_FITS[0]" S1_SPTR1
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT1="$get_fits_keyword_value"
        break
    fi

    ############################
    # read S1_SPTR2 into SPLIT1
    ############################
    get_fits_keyword "$CURRENT_FITS[0]" S1_SPTR2
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT1="$get_fits_keyword_value"
        break
    fi

    ############################
    # read S1_SPTR3 into SPLIT1
    ############################
    get_fits_keyword "$CURRENT_FITS[0]" S1_SPTR3
    if [ "$get_fits_keyword_exist" = "yes" ]; then
        SPLIT1="$get_fits_keyword_value"
        break
    fi

    ##############################################################
    # set SPLIT1=40 and break out of loop
    ###############################################################
    SPLIT1=40
    break
done

########################################################################
# loop through files ending in "01[H/M/L].fits" (FAINT mode event files)
########################################################################
for infile in $(ls | egrep '01[HML]\.fits$'); do

    ####################################
    # set up faint.par and faintdfe.par
    ####################################
    sed -e 's/,a,/,h,/' $FTOOLS/faint.par    > faint.par
    sed -e 's/,a,/,h,/' $FTOOLS/faintdfe.par > faintdfe.par

    ###################
    # set outfile
    ##################
    case $infile in
    *01H*) outfile="${infile%01H.fits}02H.fits" ;;
    *01M*) outfile="${infile%01M.fits}02M.fits" ;;
    *01L*) outfile="${infile%01L.fits}02L.fits" ;;
    esac

    #########################
    # set split in faint.par
    #########################
    case $infile in
    *S0*) $FTOOLS/pset ./faint.par split="$SPLIT0" ;;
    *S1*) $FTOOLS/pset ./faint.par split="$SPLIT1" ;;
    *   ) $FTOOLS/pset ./faint.par split=40      ;;
    esac

    ######################################
    # set infile and outfile in faint.par
    ######################################
    echo "$SH_VER:LO:Create $outfile" >> $JOB
    $FTOOLS/pset ./faint.par infile="$infile[1]"
    $FTOOLS/pset ./faint.par outfile="$outfile"

    ###################################################################
    # run ftool faint
    # bright=yes means mimic ASCA onboard FAINT to BRIGHT conversion
    ##################################################################
    $FTOOLS/faint bright=yes >> $JOB 2> log.tmp

    errchk log.tmp "faint $LINENO" faint.par

    ##########################################
    # drop outfile if it contains no events
    #######################################
    if [ $($STOOLS/getnevents < $outfile) -eq 0 ]; then
        echo "$SH_VER:E1:$outfile dropped with 0 events" >> $JOB
        rm $outfile
        continue
    fi

    #################################
    # set parameters in faintdfe.par
    #################################
    $FTOOLS/pset ./faintdfe.par infile="$infile[1]"
    $FTOOLS/pset ./faintdfe.par outfile="${infile%.fits}.dfe"

    ###############################################
    # run faintdfe
    # dfe stands for dark frame error correction
    ###############################################
    $FTOOLS/faintdfe 2> log.tmp | grep -v nrecords >> $JOB
    errchk log.tmp "faintdfe $LINENO" faintdfe.par


    #####################################################################
    # prepare to run faint again, this time using dfe files 
    # and again operating on the files with mode code ending in "01"
    # this time the resulting files will have mode code ending in "12"
    # Here FAINT mode data are converted into BRIGHT2 mode data
    # which is corrected for dark frame error and echo effect.
    ####################################################################

    case $infile in
    *01H*) outfile="${infile%01H.fits}12H.fits" ;;
    *01M*) outfile="${infile%01M.fits}12M.fits" ;;
    *01L*) outfile="${infile%01L.fits}12L.fits" ;;
    esac

    echo "$SH_VER:LO:Create $outfile" >> $JOB
    $FTOOLS/pset ./faint.par dfefile="${infile%.fits}.dfe"
    $FTOOLS/pset ./faint.par outfile="$outfile"
    $FTOOLS/pset ./faint.par maxgrade=6

    ##############################################################
    # run faint again
    # this time with bright=no meaning do not mimic ASCA onboard 
    # faint to bright conversion
    ##############################################################
    $FTOOLS/faint bright=no \ >> $JOB 2> log.tmp

    errchk log.tmp "faint $LINENO" faint.par
    if [ $($STOOLS/getnevents < $outfile) -eq 0 ]; then
        echo "$SH_VER:E1:$outfile dropped with 0 events" >> $JOB
        rm $outfile
    fi

done

########################
# cleanup files
########################
rm -f faint.par faintdfe.par

################################################
# make a list of .dfe files in s0.dfe and s1.dfe
###############################################
for file in $(ls | egrep '^ft.*\.dfe$'); do
    case "$file" in
    *S0*) cat "$file" >> s0.dfe ;;
    *S1*) cat "$file" >> s1.dfe ;;
    *   ) echo "$SH_VER:E1:Unrecognized ft*.dfe file $file" >> $JOB ;;
    esac
    rm "$file"
done

#########################################################################
# if there is anything in the s0 list, sort it, remove duplicate and
# put it into ad[sequence number]_s).dfe
########################################################################
if [ -s s0.dfe ]; then
    sort -n s0.dfe | uniq > ${ADSE}_s0.dfe

    ##########################################
    # put a fits header on the .dfe file list 
    ##########################################
    fits_wrap_ascii ${ADSE}_s0_dfe.wrap ${ADSE}_s0.dfe
    add_fits_keyword ${ADSE}_s0_dfe.wrap[0] FNAME \
                     ${ADSE}_s0_dfe.wrap STRIP${ADSE}_s0.dfe
fi

######################################################
# do all the same things for the SIS1 .dfe file list
######################################################
if [ -s s1.dfe ]; then
    sort -n s1.dfe | uniq > ${ADSE}_s1.dfe
    fits_wrap_ascii ${ADSE}_s1_dfe.wrap ${ADSE}_s1.dfe
    add_fits_keyword ${ADSE}_s1_dfe.wrap[0] FNAME \
                     ${ADSE}_s1_dfe.wrap STRIP${ADSE}_s1.dfe
fi




#############################################################################
# Run dfe2mkf to populate mkfilter columns with dfe information
# dfe means dark frame error
# uses dfe file lists ad[sequence]_s0.dfe made above
############################################################################

echo "$SH_VER:TM:Start DFE2MKF at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $FTOOLS/dfe2mkf.par > dfe2mkf.par

#######################################
# loop through al .mkf (filter) files 
#######################################
for file in $(ls | egrep '\.mkf$'); do

    $FTOOLS/pset ./dfe2mkf.par outfile="$file"

    ###################
    # SIS0
    ###################
    if [ -s ${ADSE}_s0.dfe ]; then
        $FTOOLS/pset ./dfe2mkf.par infile="${ADSE}_s0.dfe"
        $FTOOLS/pset ./dfe2mkf.par instrument=SIS0
        $FTOOLS/dfe2mkf >> $JOB 2> log.tmp
        errchk log.tmp "dfe2mkf $LINENO" dfe2mkf.par
    fi

    #################
    # SIS1
    #################
    if [ -s ${ADSE}_s1.dfe ]; then
        $FTOOLS/pset ./dfe2mkf.par infile="${ADSE}_s1.dfe"
        $FTOOLS/pset ./dfe2mkf.par instrument=SIS1
        $FTOOLS/dfe2mkf >> $JOB 2> log.tmp
        errchk log.tmp "dfe2mkf $LINENO" dfe2mkf.par
    fi

done

rm -f dfe2mkf.par


# 
###############################################################################
# SISPI is used to fill the SIS PI column in the FRF science files.
# works on files with mode code ending in "02" or "12"
# (SIS BRIGHT and BRIGHT2 mode data)
# PI means "Pulse Invariant"
##########################################################################

echo "$SH_VER:TM:Start SIS PI column fill at $SECONDS" >> $JOB

###################################################################
# make a list of SIS files modally split event file
# with mode code ending in "02" or "12"
##################################################################
ls | egrep 'S[01].*[01]2[HML]\.fits$' > sispi.list

#############################
# prepare sispi.par
#############################
sed -e 's/,a,/,h,/' $FTOOLS/sispi.par > sispi.par
$FTOOLS/pset ./sispi.par datafile=@sispi.list
$FTOOLS/pset ./sispi.par calfile="$REFDATA/$SISPICAL"
$FTOOLS/pset ./sispi.par launch=no
$FTOOLS/pset ./sispi.par verbose=yes
$FTOOLS/pset ./sispi.par history=yes
$FTOOLS/pset ./sispi.par gainnom=-99.0

#############################
# run sispi
#############################
$FTOOLS/sispi >> /dev/null 2> log.tmp

########################
# check for errors
########################
grep -i "warning" log.tmp | sort | uniq > log1.tmp
grep -i -v "warning" log.tmp > log2.tmp
if [ $(wc -l < log1.tmp) -gt 0 ]; then
    echo "$SH_VER:L2:STDOUT output from SISPI $LINENO" >> $JOB
    cat log1.tmp >> $JOB
    echo "$SH_VER:L0:STDOUT output from SISPI $LINENO" >> $JOB
fi

rm -f log.tmp log1.tmp
errchk log2.tmp "sispi $LINENO" sispi.par sispi.list
rm -f sispi.par sispi.list






###############################################################################
# update frfread output to point to useful files
# i.e. sets a bunch of FITS keywords which indicate associated files 
###############################################################################

echo "$SH_VER:TM:Update .fits with REF keywords at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $FTOOLS/fmodhead.par > fmodhead.par
$FTOOLS/pset ./fmodhead.par tmpfil=fmodhead.txt

#########################################################################
# loop through all the event files created by frfread
#########################################################################
for file in $(ls | egrep '[HLM]\.fits$'); do

    associated_ihk="${file%??????.fits}HK.fits"
    associated_chk="${file%????????.fits}CMHK.fits"
    associated_tlm="${file%????????.fits}"
    associated_mkf="${associated_tlm}.mkf"
    associated_att="${associated_tlm%?????}.${associated_tlm#??????????????}"
    associated_att="fa${associated_att#ft}"
    associated_orb="frf.orbit"

    echo "FNAME $file / File name" > fmodhead.txt
    echo "FNIHK $associated_ihk[1] / Instrument HK file" >> fmodhead.txt
    echo "FNCHK $associated_chk[1] / Common HK  file" >> fmodhead.txt
    echo "FNMKF $associated_mkf[1] / MKFILTER file" >> fmodhead.txt
    echo "FNATT $associated_att[1] / ATTITUDE file" >> fmodhead.txt
    echo "FNORB $associated_orb[1] / ORBIT file" >> fmodhead.txt
    echo "FNSGTI $file[2] / STD Good Time Intervals" >> fmodhead.txt
    echo "FNAGTI $file[3] / ALL Good Time Intervals" >> fmodhead.txt
    echo "SEQNUM $SEQUENCE / Observation sequence number" >> fmodhead.txt
    echo "SEQPNUM $PROCEXT / Number of times sequence processed" >> fmodhead.txt
    echo "PROCVER $PROC_VER / Processing script version" >> fmodhead.txt

    $FTOOLS/fmodhead infile=$file[0] >> $JOB 2> log.tmp
    errchk log.tmp "fmodhead $LINENO" fmodhead.par fmodhead.txt

    $FTOOLS/fmodhead infile=$file[1] >> $JOB 2> log.tmp
    errchk log.tmp "fmodhead $LINENO" fmodhead.par fmodhead.txt

done

#########################################################################
# loop through all the housekeeping files created by frfread
#########################################################################
for file in $(ls | egrep '^ft.*HK\.fits$'); do

    echo "FNAME $file / File name" > fmodhead.txt
    echo "SEQNUM $SEQUENCE / Sequential number from ODB" >> fmodhead.txt
    echo "SEQPNUM $PROCEXT / Number of times sequence processed" >> fmodhead.txt
    echo "PROCVER $PROC_VER / Processing script version" >> fmodhead.txt

    $FTOOLS/fmodhead infile=$file[0] >> $JOB 2> log.tmp
    errchk log.tmp "fmodhead $LINENO" fmodhead.par fmodhead.txt

    $FTOOLS/fmodhead infile=$file[1] >> $JOB 2> log.tmp
    errchk log.tmp "fmodhead $LINENO" fmodhead.par fmodhead.txt

done

rm -f fmodhead.par fmodhead.txt






#  
#############################################################################
###########################################################################
##
##  Create modal merge list files
##
###########################################################################
#############################################################################

echo "$SH_VER:TM:Create modal list files at $SECONDS" >> $JOB

#####################################################################
# make a list of telemetry (non-housekeeping) files which got ascalin'ed 
# sucessfully
# store list in pure.tmp
#####################################################################
touch pure.tmp
for file in $(ls | egrep '^ft.*\.fits$' | grep -v HK); do
    if [ $(grep $file ascalin.fail.list | wc -l) -eq 0 ]; then
        echo $file >> pure.tmp
    fi
done

###########################################################################
# make gis2_photon_merge_list
# successfully processed (ascalin'ed) telemetry files for the GIS2
# are fed to gissortcode and gissortsplit.
#
# The output of gissortcode has two collumns. The first is a long string
# with lots of detailed information about the spacecraft mode
# the second is the filename from stdin.
#
# gis2_photon_merge_list contains three collumns.
# total_events output_filename long_mode_string
# with one line for each unique mode string derived fron all the ascalin'ed
# GIS2 event files. These are sorted in order of decreasing number of events
#
# output_filename has the format:
# G2[3digit key number][2-digit mode code (e.g.70][h/m/l].prelist
#
# Each of these files contains a list of the original files which had
# the same mode.
#########################################################################
grep G2 pure.tmp | $STOOLS/gissortcode 2>> $JOB | sort | \
     $STOOLS/gissortsplit 2>> $JOB | sort -r > gis2_photon_merge_list

typeset -i merge_index_i=1

#####################################################################
# loop through the files in gis2_photon_merge_list
# these are the 
# G2[3digit key number][2-digit mode code (e.g.70][h/m/l].prelist
# files which contain list of event filenames for a particular mode
####################################################################
for file in $(awk '{print $2}' gis2_photon_merge_list); do

    ###########################################
    # remove files ending in 71[h/m/l].prelist
    ###########################################
    case $file in
    *71[hml]\.prelist)
        echo "$SH_VER:E1:Removing $file, MPC mode" >> $JOB
        rm -f $file
        continue ;;
    esac

    ############################################################
    # if you get down to 
    # files with fewer than $GIS_EVENTS_MIN events,
    # then remove the current file and bust out of this loop
    ############################################################
    nevt="$(grep $file gis2_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $GIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $GIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi

    ###########################################################
    # set model_config, modal_id
    ###########################################################
    modal_config="$(grep $file gis2_photon_merge_list | awk -F\' '{print $2}')"
    modal_id=$(get_modal_id "$modal_config")

    ###############################################
    # format merge_index with three leading zeros
    ###############################################
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"

    ####################################################################
    # rename .prelist file to .modallist
    # file gis2_modallist has 3 collumns
    # filename number_of_events and modal_id
    # where:
    #  filename catalogs the .modallist files
    ###################################################################
    tmp=${file#?????}
    export new_events_name="g2${merge_index}${tmp%prelist}modallist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB

    echo "$new_events_name $nevt $modal_id" >> gis2_modallist

    let merge_index_i+=1

done

rm -f gis2_photon_merge_list

##################################################################
# do much the same thing, except this time using gissc and gisss
# and rename .prelist files to .giflist
# the only difference in gissc is that it only prints files with 
# bitrate M or H and it records the bitrate as "X"
# gisss exits if it detects a bitrate other than "X" and does not include
# bitrate in the output_filenames it creates.
##################################################################

grep G2 pure.tmp | $STOOLS/gissc 2>> $JOB | sort | \
     $STOOLS/gisss 2>> $JOB | sort -r > gis2_photon_merge_list


for file in $(awk '{print $2}' gis2_photon_merge_list); do

    ###################################################################
    # remove files with mode code ending in 71 and .prelist extension
    ###################################################################
    case $file in
    *71\.prelist)
        echo "$SH_VER:E1:Removing $file, MPC mode" >> $JOB
       rm -f $file
       continue ;;
    esac

    #########################################################
    # remove files with fewer than $GIS_EVENTS_MIN events
    # and bust out of loop
    #########################################################
    nevt="$(grep $file gis2_photon_merge_list | awk '{print $1}')"

    if [ $nevt -lt $GIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $GIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi

    ##############################################
    # format merge index with three leading zeros
    ###############################################
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"

    ####################################################################
    # rename .prelist file to .giflist
    # files are called:
    # G2[3digit key number][2-digit mode code (e.g.70].giflist
    ###################################################################
    tmp=${file#?????}
    export new_events_name="g2${merge_index}${tmp%prelist}giflist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt" >> gis2_giflist

    let merge_index_i+=1
done

rm -f gis2_photon_merge_list

#######################################
# same thing for the GIS 3
#######################################
grep G3 pure.tmp | $STOOLS/gissortcode 2>> $JOB | sort | \
     $STOOLS/gissortsplit 2>> $JOB | sort -r > gis3_photon_merge_list
typeset -i merge_index_i=1
for file in $(awk '{print $2}' gis3_photon_merge_list); do
    case $file in
    *71[hml]\.prelist)
        echo "$SH_VER:E1:Removing $file, MPC mode" >> $JOB
        rm -f $file
        continue ;;
    esac
    nevt="$(grep $file gis3_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $GIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $GIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi
    modal_config="$(grep $file gis3_photon_merge_list | awk -F\' '{print $2}')"
    modal_id=$(get_modal_id "$modal_config")
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"
    tmp=${file#?????}
    export new_events_name="g3${merge_index}${tmp%prelist}modallist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt $modal_id" >> gis3_modallist
    let merge_index_i+=1
done
rm -f gis3_photon_merge_list

grep G3 pure.tmp | $STOOLS/gissc 2>> $JOB | sort | \
     $STOOLS/gisss 2>> $JOB | sort -r > gis3_photon_merge_list
for file in $(awk '{print $2}' gis3_photon_merge_list); do
    case $file in
    *71\.prelist)
        echo "$SH_VER:E1:Removing $file, MPC mode" >> $JOB
        rm -f $file
        continue ;;
    esac
    nevt="$(grep $file gis3_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $GIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $GIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"
    tmp=${file#?????}
    export new_events_name="g3${merge_index}${tmp%prelist}giflist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt" >> gis3_giflist
    let merge_index_i+=1
done
rm -f gis3_photon_merge_list

##############################
# similar for the SIS0
################################
grep S0 pure.tmp | $STOOLS/sis0sortcode 2>> $JOB | sort | \
     $STOOLS/sis0sortsplit 2>> $JOB | sort -r > sis0_photon_merge_list
typeset -i merge_index_i=1
for file in $(awk '{print $2}' sis0_photon_merge_list); do
    case $file in
    *[01]2[hml]\.prelist) ;;
    *) echo "$SH_VER:E1:Removing $file, not bright mode" >> $JOB
       rm -f $file
       continue ;;
    esac
    nevt="$(grep $file sis0_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $SIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $SIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi
    modal_config="$(grep $file sis0_photon_merge_list | awk -F\' '{print $2}')"
    modal_id=$(get_modal_id "$modal_config")
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"
    tmp=${file#?????}
    export new_events_name="s0${merge_index}${tmp%prelist}modallist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt $modal_id" >> sis0_modallist
    let merge_index_i+=1
done
rm -f sis0_photon_merge_list

grep S0 pure.tmp | $STOOLS/sis0sc 2>> $JOB | sort | \
     $STOOLS/sis0ss 2>> $JOB | sort -r > sis0_photon_merge_list
for file in $(awk '{print $2}' sis0_photon_merge_list); do
    case $file in
    *[01]2\.prelist) ;;
    *) echo "$SH_VER:E1:Removing $file, not bright mode" >> $JOB
       rm -f $file
       continue ;;
    esac
    nevt="$(grep $file sis0_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $SIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $SIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"
    tmp=${file#?????}
    export new_events_name="s0${merge_index}${tmp%prelist}giflist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt" >> sis0_giflist
    let merge_index_i+=1
done
rm -f sis0_photon_merge_list

######################
# similar for the SIS1
######################
grep S1 pure.tmp | $STOOLS/sis1sortcode 2>> $JOB | sort | \
     $STOOLS/sis1sortsplit 2>> $JOB | sort -r > sis1_photon_merge_list
typeset -i merge_index_i=1
for file in $(awk '{print $2}' sis1_photon_merge_list); do
    case $file in
    *[01]2[hml]\.prelist) ;;
    *) echo "$SH_VER:E1:Removing $file, not bright mode" >> $JOB
       rm -f $file
       continue ;;
    esac
    nevt="$(grep $file sis1_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $SIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $SIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi
    modal_config="$(grep $file sis1_photon_merge_list | awk -F\' '{print $2}')"
    modal_id=$(get_modal_id "$modal_config")
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"
    tmp=${file#?????}
    export new_events_name="s1${merge_index}${tmp%prelist}modallist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt $modal_id" >> sis1_modallist
    let merge_index_i+=1
done
rm -f sis1_photon_merge_list

grep S1 pure.tmp | $STOOLS/sis1sc 2>> $JOB | sort | \
     $STOOLS/sis1ss 2>> $JOB | sort -r > sis1_photon_merge_list
for file in $(awk '{print $2}' sis1_photon_merge_list); do
    case $file in
    *[01]2\.prelist) ;;
    *) echo "$SH_VER:E1:Removing $file, not bright mode" >> $JOB
       rm -f $file
       continue ;;
    esac
    nevt="$(grep $file sis1_photon_merge_list | awk '{print $1}')"
    if [ $nevt -lt $SIS_EVENTS_MIN ]; then
        echo "$SH_VER:E1:Removing $file nevt = $nevt < $SIS_EVENTS_MIN" >> $JOB
        rm -f $file
        continue
    fi
    merge_index="$(echo $merge_index_i | nawk '{printf ("%03d", $0)}')"
    tmp=${file#?????}
    export new_events_name="s1${merge_index}${tmp%prelist}giflist"
    mv $file $new_events_name
    echo "$SH_VER:LO:Reorder $file to $new_events_name" >> $JOB
    echo "$new_events_name $nevt" >> sis1_giflist
    let merge_index_i+=1
done

rm -f sis1_photon_merge_list
rm -f pure.tmp




###############################################################################
###############################################################################
##
##  Create initial FFILECAT catalogs.
##
##  ffilecat takes a bunch of files and lists then along with their
##  selected keywords in another fits file
##
##  here a .cat fits file is produced for each .modallist and .giflist file
#############################################################################
##############################################################################

echo "$SH_VER:TM:Create initial catalogs at $SECONDS" >> $JOB

cat $GIS_KEYWORDS > gis.keywords
cat $SIS0_KEYWORDS > sis0.keywords
cat $SIS1_KEYWORDS > sis1.keywords

##############################################
# set up parameters for ffilecat.par
###############################################
sed -e 's/,a,/,h,/' $FTOOLS/ffilecat.par > ffilecat.par
$FTOOLS/pset ./ffilecat.par maxlen=1
$FTOOLS/pset ./ffilecat.par minlen=0
$FTOOLS/pset ./ffilecat.par aform=A8
$FTOOLS/pset ./ffilecat.par eform=E9.3E1
$FTOOLS/pset ./ffilecat.par iform=I6
$FTOOLS/pset ./ffilecat.par omit=no
$FTOOLS/pset ./ffilecat.par quiet=yes
$FTOOLS/pset ./ffilecat.par infile=@ffilecat.cat

##############################
# work on GIS2 first
#############################
touch gis2_cat_list
$FTOOLS/pset ./ffilecat.par keywords=@gis.keywords

##########################################
# loop through the GIS2 .modallist files
#########################################
for list in $(ls | egrep 'g2.*\.modallist$'); do

    ############################################################
    # catalog is name of output file for ffilecat
    # it is the same as the current .modallist file but with a
    # .cat extension instead
    ###########################################################
    catalog="$ADSE${list%.modallist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB

    ###########################
    # create ffilecat.cat
    ###########################
    cat /dev/null > ffilecat.cat

    ###################################
    # set modal_id from gis2_modallist
    ###################################
    typeset -i modal_id="$(grep $list gis2_modallist | awk '{printf("%d",$3)}')"

    ######################################################
    # loop over files listed in current .modallist file
    #####################################################
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
        add_fits_keyword $file[1] MODAL_ID "$modal_id" "Modal Configuration ID"
    done

    ###############################################
    # set output file to $catalog as defined above
    ###############################################
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"

    #############################
    # run ffilecat
    #############################
    $FTOOLS/ffilecat >> $JOB 2> log.tmp

    ###########################
    # check for errors
    ############################
    errchk log.tmp "ffilecat $LINENO" ffilecat.par gis.keywords ffilecat.cat

    #############################################
    # record name of .cat file in gis2_cat_list
    #############################################
    echo "$catalog" >> gis2_cat_list

    ###############################
    # set ontime and events
    ##############################
    add_ontimes $list
    ontime="$add_ontimes_value"

    events="$(grep $list gis2_modallist | awk '{printf("%d",$2)}')"

    ###############################################
    # define various FITS keywords in .cat file
    #############################################
    add_fits_keyword $catalog[0] ONTIME $ontime "Cat cumlative ONTIME"
    add_fits_keyword $catalog[0] NEVENTS $events "Cat cumlative NEVENTS"
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"

    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"

    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"

    add_fits_keyword $catalog[1] MODAL_ID "$modal_id" "Modal Configuration ID"

done

rm -f gis2_modallist

############################################################
# do something similar for the .giflist files
###########################################################

$FTOOLS/pset ./ffilecat.par keywords=@gis.keywords

##################################
# loop over .giflist files
##################################
for list in $(ls | egrep 'g2.*\.giflist$'); do

    catalog="$ADSE${list%.giflist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB

    cat /dev/null > ffilecat.cat

    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
    done

    $FTOOLS/pset ./ffilecat.par outfile="$catalog"

    ############################
    # run ffilecat
    #############################
    $FTOOLS/ffilecat >> $JOB 2> log.tmp

    errchk log.tmp "ffilecat $LINENO" ffilecat.par gis.keywords ffilecat.cat

    ###################################
    # list .cat files in gis2_cat_list
    ###################################
    echo "$catalog" >> gis2_cat_list

    ###########################################
    # fill in some FITS keywords in .cat file
    ##########################################
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"

    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"

    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"

done

rm -f gis2_giflist

################################
# do the same for the gis3
################################
touch gis3_cat_list
$FTOOLS/pset ./ffilecat.par keywords=@gis.keywords
for list in $(ls | egrep 'g3.*\.modallist$'); do
    catalog="$ADSE${list%.modallist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB
    cat /dev/null > ffilecat.cat
    typeset -i modal_id="$(grep $list gis3_modallist | awk '{printf("%d",$3)}')"
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
        add_fits_keyword $file[1] MODAL_ID "$modal_id" "Modal Configuration ID"
    done
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"
    $FTOOLS/ffilecat >> $JOB 2> log.tmp
    errchk log.tmp "ffilecat $LINENO" ffilecat.par gis.keywords ffilecat.cat
    echo "$catalog" >> gis3_cat_list
    add_ontimes $list
    ontime="$add_ontimes_value"
    events="$(grep $list gis3_modallist | awk '{printf("%d",$2)}')"
    add_fits_keyword $catalog[0] ONTIME $ontime "Cat cumlative ONTIME"
    add_fits_keyword $catalog[0] NEVENTS $events "Cat cumlative NEVENTS"
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"
    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"
    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"
    add_fits_keyword $catalog[1] MODAL_ID "$modal_id" "Modal Configuration ID"
done
rm -f gis3_modallist

$FTOOLS/pset ./ffilecat.par keywords=@gis.keywords
for list in $(ls | egrep 'g3.*\.giflist$'); do
    catalog="$ADSE${list%.giflist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB
    cat /dev/null > ffilecat.cat
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
    done
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"
    $FTOOLS/ffilecat >> $JOB 2> log.tmp
    errchk log.tmp "ffilecat $LINENO" ffilecat.par gis.keywords ffilecat.cat
    echo "$catalog" >> gis3_cat_list
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"
    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"
    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"
done
rm -f gis3_giflist

##########################
# do the same for the SIS0
###########################
touch sis0_cat_list
$FTOOLS/pset ./ffilecat.par keywords=@sis0.keywords
for list in $(ls | egrep 's0.*\.modallist$'); do
    catalog="$ADSE${list%.modallist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB
    cat /dev/null > ffilecat.cat
    typeset -i modal_id="$(grep $list sis0_modallist | awk '{printf("%d",$3)}')"
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
        add_fits_keyword $file[1] MODAL_ID "$modal_id" "Modal Configuration ID"
    done
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"
    $FTOOLS/ffilecat >> $JOB 2> log.tmp
    errchk log.tmp "ffilecat $LINENO" ffilecat.par sis0.keywords ffilecat.cat
    echo "$catalog" >> sis0_cat_list
    add_ontimes $list
    ontime="$add_ontimes_value"
    events="$(grep $list sis0_modallist | awk '{printf("%d",$2)}')"
    add_fits_keyword $catalog[0] ONTIME $ontime "Cat cumlative ONTIME"
    add_fits_keyword $catalog[0] NEVENTS $events "Cat cumlative NEVENTS"
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"
    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"
    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"
    add_fits_keyword $catalog[1] MODAL_ID "$modal_id" "Modal Configuration ID"
done
rm -f sis0_modallist

$FTOOLS/pset ./ffilecat.par keywords=@sis0.keywords
for list in $(ls | egrep 's0.*\.giflist$'); do
    catalog="$ADSE${list%.giflist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB
    cat /dev/null > ffilecat.cat
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
    done
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"
    $FTOOLS/ffilecat >> $JOB 2> log.tmp
    errchk log.tmp "ffilecat $LINENO" ffilecat.par sis0.keywords ffilecat.cat
    echo "$catalog" >> sis0_cat_list
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"
    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"
    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"
done
rm -f sis0_giflist

############################
# do the same for the sis1
#############################
touch sis1_cat_list
$FTOOLS/pset ./ffilecat.par keywords=@sis1.keywords
for list in $(ls | egrep 's1.*\.modallist$'); do
    catalog="$ADSE${list%.modallist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB
    cat /dev/null > ffilecat.cat
    typeset -i modal_id="$(grep $list sis1_modallist | awk '{printf("%d",$3)}')"
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
        add_fits_keyword $file[1] MODAL_ID "$modal_id" "Modal Configuration ID"
    done
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"
    $FTOOLS/ffilecat >> $JOB 2> log.tmp
    errchk log.tmp "ffilecat $LINENO" ffilecat.par sis0.keywords ffilecat.cat
    echo "$catalog" >> sis1_cat_list
    add_ontimes $list
    ontime="$add_ontimes_value"
    events="$(grep $list sis1_modallist | awk '{printf("%d",$2)}')"
    add_fits_keyword $catalog[0] ONTIME $ontime "Cat cumlative ONTIME"
    add_fits_keyword $catalog[0] NEVENTS $events "Cat cumlative NEVENTS"
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"
    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"
    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"
    add_fits_keyword $catalog[1] MODAL_ID "$modal_id" "Modal Configuration ID"
done
rm -f sis1_modallist

$FTOOLS/pset ./ffilecat.par keywords=@sis1.keywords
for list in $(ls | egrep 's1.*\.giflist$'); do
    catalog="$ADSE${list%.giflist}.cat"
    echo "$SH_VER:LO:Create $catalog" >> $JOB
    cat /dev/null > ffilecat.cat
    for file in $(cat $list); do
        echo "$file[0]" >> ffilecat.cat
    done
    $FTOOLS/pset ./ffilecat.par outfile="$catalog"
    $FTOOLS/ffilecat >> $JOB 2> log.tmp
    errchk log.tmp "ffilecat $LINENO" ffilecat.par sis0.keywords ffilecat.cat
    echo "$catalog" >> sis1_cat_list
    add_fits_keyword $catalog[1] DATADIR "./"
    add_fits_keyword $catalog[1] HKDIR "./"
    get_fits_keyword "$(head -1 $list)[0]" INSTRUME
    add_fits_keyword $catalog[1] INSTRUME "$get_fits_keyword_value"
    get_fits_keyword "$(head -1 $list)[0]" TELESCOP
    add_fits_keyword $catalog[1] TELESCOP "$get_fits_keyword_value"
done
rm -f sis1_giflist
rm -f ffilecat.cat ffilecat.par
rm -f gis.keywords sis0.keywords sis1.keywords




#  
###############################################################################
###############################################################################
##
##  Try to generate GIS products here.
##  
##  This is done with xselect
##
###############################################################################
###############################################################################

echo "$SH_VER:TM:Start GIS XSELECT on modal at $SECONDS" >> $JOB

xselpar="$(ls $FTOOLS/xsel*.par)"
cp $xselpar ${xselpar##*/}

####################################################################
# loop over .giflist decendant .cat files for the GIS2 and GIS3
# i.e. those listed in gis2_cat_list and gis3_cat_list
####################################################################
for file in $(cat gis2_cat_list gis3_cat_list | awk '{print $1}'); do
    
    ###################################
    # Don't process mode 71 MPC data.
    ###################################
    case $file in
    *71[hml].cat) continue ;;
    *71.cat     ) continue ;;
    esac

    ######################################
    # Determine if 256 or 64 data arrays.
    ######################################
    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par
    $FTOOLS/pset ./ftabpar.par fitsfile="$file[1]"
    $FTOOLS/pset ./ftabpar.par column=RAWXBINS
    $FTOOLS/pset ./ftabpar.par row=1

    $FTOOLS/ftabpar >> $JOB 2> log.tmp

    errchk log.tmp "FTABPAR $LINENO" ftabpar.par
    typeset -i rawxbins="$($FTOOLS/pget ./ftabpar.par value)"
    rm ./ftabpar.par

    ####################################################
    # Set local configuration for this specific file.
    # determine the region filter to be used by xselect
    # and xexpr to select good events
    ####################################################
    echo "(SAA==0)&&(ELV>10)&&(COR>6)" > xexpr
    echo "&&(ACS==0)&&(ANG_DIST>0)&&(ANG_DIST<0.01)" >> xexpr

    case $file in
    *g2*) echo "&&(G2_L1>0)" >> xexpr
          case $rawxbins in
          64) region=$GIS2_REGION64 ;;
          * ) region=$GIS2_REGION256 ;;
          esac ;;
    *g3*) echo "&&(G3_L1>0)" >> xexpr
          case $rawxbins in
          64) region=$GIS3_REGION64 ;;
          * ) region=$GIS3_REGION256 ;;
          esac ;;
    *   ) echo "$SH_VER:E1:Bad filename, unknown instrument $file" >> $JOB
          continue ;;
    esac

    ###############################
    # Show the current file status.
    ###############################
    echo "$SH_VER:LO:=============================================" >> $JOB
    echo "$SH_VER:LO:Run GIS XSELECT on $file" >> $JOB
    echo "$SH_VER:LO:RAWXBINS=$rawxbins" >> $JOB
    echo "$SH_VER:LO:Filter Region ($region):" >> $JOB
    awk '{print " ", $0}' $region >> $JOB
    echo "$SH_VER:LO:Selection Expression:" >> $JOB
    awk '{print " ", $0}' xexpr >> $JOB

    ######################################
    # Configure the XSELECT command file.
    ######################################

    base=${file%.cat}

    ###############################
    # some premininary setup stuff
    ###############################
    echo "$base" > $base.xco
    echo " " >> $base.xco
    echo "%ED%off" >> $base.xco
    echo "set dumpcat" >> $base.xco
    echo "set mission ASCA" >> $base.xco
    echo "load obscat load_str=$file" >> $base.xco
    echo "set datadir ." >> $base.xco
    echo "choose 1-**" >> $base.xco

    ####################################
    # make unfiltered event files: .unf
    ####################################
    echo "extract events" >> $base.xco
    echo "save events outfile=$base.unf use_events=yes" >> $base.xco

    ####################
    # filter and clean 
    ####################
    echo "select mkf \"@xexpr\" data_dir=. mkf_def_expr=ft*.mkf" >> $base.xco
    echo "filter region $region" >> $base.xco
    echo "gisclean" >> $base.xco

    ###########################################################
    # make filtered and cleaned event and detector image files
    ###########################################################
    echo "extract \"events image\"" >> $base.xco
    echo "save events outfile=$base.evt use_events=yes" >> $base.xco
    echo "save image outfile=$base.detimg" >> $base.xco

    #########################
    # make an actual skymap
    #########################
    echo "clear mkf" >> $base.xco
    echo "clear region all" >> $base.xco
    echo "set XYNAME X Y" >> $base.xco
    echo "extract image" >> $base.xco
    echo "save image outfile=$base.img" >> $base.xco

    ###############
    # quit xselect
    ###############
    echo "exit save_session=no" >> $base.xco




    #################################
    # some things for the log file
    #################################
    echo "$SH_VER:LO:XSELECT command file:" >> $JOB
    awk '{print " ", $0}' $base.xco >> $JOB

    ##########################
    # run xselect
    ############################
    $FTOOLS/xselect @$base.xco < /dev/null > /dev/null 2>&1

    #######################
    # check for errors
    #######################
    if [ $? -ne 0 ]; then
        echo "$SH_VER:E1:Error in Xselect detected, return code != 0" >> $JOB
        echo "$SH_VER:E1:Purging $base files" >> $JOB
        rm -f $base.evt
        rm -f $base.img
        rm -f $base.detimg
    fi

    ##################################################
    # remove .evt files which have no events in them
    ##################################################
    if [ -s "$base.evt" ]; then
        if [ $($STOOLS/getnevents < $base.evt) -eq 0 ]; then
            echo "$SH_VER:E1:No events in $base.evt" >> $JOB
            echo "$SH_VER:E1:Products deleted for $base.cat" >> $JOB
            rm -f $base.evt
            rm -f $base.img
            rm -f $base.detimg
        else
            add_fits_keyword $base.unf[0] FNAME "$base.unf"
            add_fits_keyword $base.unf[1] FNAME "$base.unf"
            add_fits_keyword $base.evt[0] FNAME "$base.evt"
            add_fits_keyword $base.evt[1] FNAME "$base.evt"
            add_fits_keyword $base.img[0] FNAME "$base.img"
            add_fits_keyword $base.detimg[0] FNAME "$base.detimg"
        fi
    else
        echo "$SH_VER:E1:No Xselect products for $base.cat" >> $JOB
        cat xsel.log >> $JOB
    fi

    #################################
    # take care of the xsel.log file
    #################################
    cat $base.xco >> xsel.log
    rm -f $base.xco
    mv xsel.log ${base}_xsel.log
    fits_wrap_ascii ${base}_xsel_log.wrap ${base}_xsel.log

    add_fits_keyword ${base}_xsel_log.wrap[0] FNAME \
                     ${base}_xsel_log.wrap STRIP${base}_xsel.log

done

rm -f ${xselpar##*/} xsel.log xexpr






#  
################################################################################
# now run gqaplot on the selected data
# gqa stands for GIS quick analysis
# gqaplot makes rough plots of image, light curve and spectrum
################################################################################

echo "$SH_VER:TM:Start GQAPLOT at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $FTOOLS/gqaplot.par > gqaplot.par
$FTOOLS/pset ./gqaplot.par device=/PS
$FTOOLS/pset ./gqaplot.par phname=PI
$FTOOLS/pset ./gqaplot.par phalog=yes
$FTOOLS/pset ./gqaplot.par xname=DETX
$FTOOLS/pset ./gqaplot.par yname=DETY

#################################################
# loop through .evt files for the GIS2 and GIS3
#################################################
for file in $(ls | egrep 'g[23].*\.evt$'); do

    $FTOOLS/pset ./gqaplot.par infile="$file"
    $FTOOLS/pset ./gqaplot.par outfile="${file%evt}out"

    #########################
    # run gqaplot
    #########################
    $FTOOLS/gqaplot >> $JOB 2> log.tmp

    ##################
    # check for errors
    ###################
    errchk log.tmp "gqaplot $LINENO" gqaplot.par

    ##############################
    # shuffle output files around
    ##############################
    mv pgplot.ps ${file%evt}ps

    fits_wrap_ascii ${file%.evt}_gqa_out.wrap ${file%evt}out
    add_fits_keyword ${file%.evt}_gqa_out.wrap[0] FNAME \
                     ${file%.evt}_gqa_out.wrap "STRIP${file%evt}out"

    fits_wrap_ascii ${file%.evt}_gqa_ps.wrap ${file%evt}ps
    add_fits_keyword ${file%.evt}_gqa_ps.wrap[0] FNAME \
                     ${file%.evt}_gqa_ps.wrap "STRIP${file%evt}ps"

done

rm -f gqaplot.par



################################################################################
# while we are here, collect the science times for these files
# i.e. exposure times
##########################################################################

echo "$SH_VER:TM:Collect science time for products at $SECONDS" >> $JOB

touch ${ADSE}_science_times.gis2
touch ${ADSE}_science_times.gis3

###############################
# loop through GIS .evt files
###############################
for file in $(ls | egrep 'g.*\.evt'); do

    SELECT_ONTIME=$($STOOLS/compute_exposure $file[2]) 2>> $JOB
    INT_SELECT_ONTIME=$($STOOLS/float2int $SELECT_ONTIME)

    case $file in
    *g2*) echo "GIS2 $file $INT_SELECT_ONTIME" >> ${ADSE}_science_times.gis2 ;;
    *g3*) echo "GIS3 $file $INT_SELECT_ONTIME" >> ${ADSE}_science_times.gis3 ;;
    esac

done

##############################################################################
##############################################################################
##
##  now move on to SIS (Boo Hiss)
##
##############################################################################
##############################################################################

echo "$SH_VER:TM:Start SIS XSELECT on modal at $SECONDS" >> $JOB

xselpar="$(ls $FTOOLS/xsel*.par)"
cp $xselpar ${xselpar##*/}

########################################################
# loop through files in sis0_cat_list and sis1_cat_list
########################################################
for file in $(cat sis0_cat_list sis1_cat_list | awk '{print $1}'); do
    
    ################################################
    # Only process the BRIGHT and BRIGHT2 mode data.
    #################################################
    case $file in
    *[01]2[hml].cat) ;;
    *[01]2.cat     ) ;;
    *              ) continue ;;
    esac

    ###################################################
    # Get S?CCDLST='0 1 2 3' (or whatever is observed)
    ###################################################
    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par

    case $file in
    *s0*) $FTOOLS/pset ./ftabpar.par column=S0CCDLST ;;
    *s1*) $FTOOLS/pset ./ftabpar.par column=S1CCDLST ;;
    esac

    $FTOOLS/pset ./ftabpar.par fitsfile="$file[1]"
    $FTOOLS/pset ./ftabpar.par row=1

    $FTOOLS/ftabpar >> $JOB 2> log.tmp

    errchk log.tmp "ftabpar $LINENO" ftabpar.par
    ccdlst="$($FTOOLS/pget ./ftabpar.par value)"

    #######################################
    # get the number of active CCDs in SIS
    #######################################
    case $file in
    *s0*) $FTOOLS/pset ./ftabpar.par column=S0CCDMOD ;;
    *s1*) $FTOOLS/pset ./ftabpar.par column=S1CCDMOD ;;
    esac

    $FTOOLS/pset ./ftabpar.par fitsfile="$file[1]"
    $FTOOLS/pset ./ftabpar.par row=1

    $FTOOLS/ftabpar >> $JOB 2> log.tmp

    errchk log.tmp "ftabpar $LINENO" ftabpar.par
    ccdmode="$($FTOOLS/pget ./ftabpar.par value)"

    #################################################
    # Build expression from CCD power configuration.
    #################################################
    echo "(SAA==0)&&(BR_EARTH>20)&&(ELV>10)&&(COR>6)"              > xexpr
    echo "&&(ACS==0)&&(ANG_DIST>0)&&(ANG_DIST<0.01)&&(FOV==0)"    >> xexpr

    case $file in
    *s0*) $BIN/ccd_select.pl 0 "$ccdlst" >> xexpr ;;
    *s1*) $BIN/ccd_select.pl 1 "$ccdlst" >> xexpr ;;
    esac

    ####################################################################
    # set the Sn_SATFm selection critera for each active CCD in the SIS
    ####################################################################
    for ccd in $ccdlst
    do
      case $file in
      *s0*) echo "&&((S0_SATF${ccd}<1)||(S0_SATF${ccd}>1))"       >> xexpr 
            ;;
      *s1*) echo "&&((S1_SATF${ccd}<1)||(S1_SATF${ccd}>1))"       >> xexpr 
            ;;
      esac
    done

    ###########################################################################
    # set the T_DY_NT and T_SAA selection critera depending upon the number of
    # CCDs active in the SIS
    ###########################################################################
    case $ccdmode in   
      1) echo "&&((T_DY_NT<0)||(T_DY_NT>16))&&((T_SAA<0)||(T_SAA>16))" >> xexpr
         ;;
      2) echo "&&((T_DY_NT<0)||(T_DY_NT>32))&&((T_SAA<0)||(T_SAA>32))" >> xexpr
         ;;
      3) echo "&&((T_DY_NT<0)||(T_DY_NT>64))&&((T_SAA<0)||(T_SAA>64))" >> xexpr
         ;;
      4) echo "&&((T_DY_NT<0)||(T_DY_NT>64))&&((T_SAA<0)||(T_SAA>64))" >> xexpr
         ;;
      *) echo "$SH_VER:E1:Cannot determine CCD mode; defaulting to 4CCD mode"                                                                         >> $JOB
         echo "&&((T_DY_NT<0)||(T_DY_NT>64))&&((T_SAA<0)||(T_SAA>64))" >> xexpr
         ;;
    esac

    ####################################
    # Show the current file status.
    ####################################
    echo "$SH_VER:LO:============================================="    >> $JOB
    echo "$SH_VER:LO:Run SIS XSELECT on $file"                         >> $JOB
    echo "$SH_VER:LO:SIS CCD Chips $ccdlst"                            >> $JOB
    echo "$SH_VER:LO:Selection Expression:"                            >> $JOB
    awk '{print " ", $0}' xexpr                                        >> $JOB

    ######################################
    # Configure the XSELECT command file.
    ######################################
    base=${file%.cat}
    echo "$base" > $base.xco
    echo " " >> $base.xco
    echo "%ED%off" >> $base.xco
    echo "set dumpcat" >> $base.xco
    echo "set mission ASCA" >> $base.xco
    echo "load obscat load_str=$file" >> $base.xco
    echo "set datadir ." >> $base.xco
    echo "choose 1-**" >> $base.xco

    echo "extract events" >> $base.xco
    echo "save events outfile=$base.unf use_events=yes" >> $base.xco

    echo "select mkf \"@xexpr\" data_dir=. mkf_def_expr=ft*.mkf" >> $base.xco
    echo "extract events" >> $base.xco
    echo "save events outfile=$base.evtsel use_events=yes" >> $base.xco

    echo "sisclean saoimage=no sis_plot=no clean=2 cellsize=5 log_prob=-5.24" \
         " bkg_thr=3 clean_phalow=0 clean_phahi=4095" >> $base.xco
    echo "save clean outfile=$base.evt use_events=yes" >> $base.xco

    echo "extract image" >> $base.xco
    echo "save image outfile=$base.detimg" >> $base.xco

    echo "clear mkf" >> $base.xco
    echo "set XYNAME X Y" >> $base.xco
    echo "extract image" >> $base.xco
    echo "save image outfile=$base.img" >> $base.xco
    echo "exit save_session=no" >> $base.xco

    ###############################
    # some things for the log file
    ###############################
    echo "$SH_VER:LO:XSELECT command file:" >> $JOB
    awk '{print " ", $0}' $base.xco >> $JOB

    ######################
    # run xselect
    #######################
    $FTOOLS/xselect @$base.xco < /dev/null > /dev/null 2>&1

    ##########################
    # check for errors
    ##########################
    if [ $? -ne 0 ]; then
        echo "$SH_VER:E1:Error in Xselect detected, return code != 0" >> $JOB
        echo "$SH_VER:E1:Purging $base files" >> $JOB
        rm -f $base.evtsel
        rm -f $base.evt
        rm -f $base.img
        rm -f $base.detimg
    fi

    ###########################
    # remove empty files
    ###########################
    if [ -s "$base.evt" ]; then
        if [ $($STOOLS/getnevents < $base.evt) -eq 0 ]; then
            rm -f $base.evtsel
            rm -f $base.evt
            rm -f $base.img
            rm -f $base.detimg
            echo "$SH_VER:E1:No events in $base.evt" >> $JOB
            echo "$SH_VER:E1:Products deleted for $base.cat" >> $JOB
        else
            add_fits_keyword $base.unf[0] FNAME "$base.unf"
            add_fits_keyword $base.unf[1] FNAME "$base.unf"
            add_fits_keyword $base.evt[0] FNAME "$base.evt"
            add_fits_keyword $base.evt[1] FNAME "$base.evt"
            add_fits_keyword $base.img[0] FNAME "$base.img"
            add_fits_keyword $base.detimg[0] FNAME "$base.detimg"
        fi
    else
        echo "$SH_VER:E1:No Xselect products for $base.cat" >> $JOB
        cat xsel.log >> $JOB
    fi


    ###############################
    # take care of xselect log file
    ###############################
    cat $base.xco >> xsel.log
    rm -f $base.xco
    mv xsel.log ${base}_xsel.log

    fits_wrap_ascii ${base}_xsel_log.wrap ${base}_xsel.log

    add_fits_keyword ${base}_xsel_log.wrap[0] FNAME \
                     ${base}_xsel_log.wrap STRIP${base}_xsel.log


done

rm -f ${xselpar##*/} xsel.log
rm -f xexpr



################################################################################
# now run sqaplot on the selected data
##############################################################################

echo "$SH_VER:TM:Start SQAPLOT at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $FTOOLS/sqaplot.par > sqaplot.par
$FTOOLS/pset ./sqaplot.par device=/PS

############################
# loop through .evtsel files
##############################
for file in $(ls | egrep 's[01].*\.evtsel$'); do

    $FTOOLS/pset ./sqaplot.par infile="$file[1]"
    $FTOOLS/pset ./sqaplot.par outfile="${file%evtsel}out"
    $FTOOLS/pset ./sqaplot.par eventfile=NONE

    if [ $($BIN/recmp $file "01[hml]\.evtsel") -eq 0 ]; then
        $FTOOLS/pset ./sqaplot.par split=40
        get_fits_keyword $file[0] SPLTH0S1
        if [ "$get_fits_keyword_exist" = "yes" ]; then
            $FTOOLS/pset ./sqaplot.par split="$get_fits_keyword_value"
        fi
        get_fits_keyword $file[0] SPLTH1S1
        if [ "$get_fits_keyword_exist" = "yes" ]; then
            $FTOOLS/pset ./sqaplot.par split="$get_fits_keyword_value"
        fi
        get_fits_keyword $file[0] SPLTH2S1
        if [ "$get_fits_keyword_exist" = "yes" ]; then
            $FTOOLS/pset ./sqaplot.par split="$get_fits_keyword_value"
        fi
        get_fits_keyword $file[0] SPLTH3S1
        if [ "$get_fits_keyword_exist" = "yes" ]; then
            $FTOOLS/pset ./sqaplot.par split="$get_fits_keyword_value"
        fi
    fi

    ################################################
    # turn off telemetry saturation until it works
    ################################################
    $FTOOLS/sqaplot telemetry=no > log.tmp 2>&1

    ###############################################
    # supress # of chips present error from sqplot
    ###############################################
    if [ $(grep "#chips with events" log.tmp | wc -l) -gt 0 ]; then
        echo "$SH_VER:E1:SQAPLOT message \'#chips with events" \
             "for $file ignored" >> $JOB
        grep -v "#chips with events" log.tmp > log.tmp2
        mv log.tmp2 log.tmp
    fi

    errchk log.tmp "sqaplot $LINENO" sqaplot.par
    mv pgplot.ps ${file%evtsel}ps

    fits_wrap_ascii ${file%.evtsel}_sqa_out.wrap ${file%evtsel}out
    add_fits_keyword ${file%.evtsel}_sqa_out.wrap[0] FNAME \
                     ${file%.evtsel}_sqa_out.wrap "STRIP${file%evtsel}out"

    fits_wrap_ascii ${file%.evtsel}_sqa_ps.wrap ${file%evtsel}ps
    add_fits_keyword ${file%.evtsel}_sqa_ps.wrap[0] FNAME \
                     ${file%.evtsel}_sqa_ps.wrap "STRIP${file%evtsel}ps"

done

echo "$SH_VER:LO:*.evtsel files removed after sqaplot" >> $JOB
rm -f sqaplot.par ${ADSE}*.evtsel


################################################################################
# while we are here, collect the science times for these files
############################################################################
echo "$SH_VER:TM:Collect science time for products at $SECONDS" >> $JOB

touch ${ADSE}_science_times.sis0
touch ${ADSE}_science_times.sis1

########################
# loop through .evp files
##########################
for file in $(ls | egrep 's.*\.evt'); do

    SELECT_ONTIME=$($STOOLS/compute_exposure $file[2]) 2>> $JOB
    INT_SELECT_ONTIME=$($STOOLS/float2int $SELECT_ONTIME)

    case $file in
    *s0*) echo "SIS0 $file $INT_SELECT_ONTIME" >> ${ADSE}_science_times.sis0 ;;
    *s1*) echo "SIS1 $file $INT_SELECT_ONTIME" >> ${ADSE}_science_times.sis1 ;;
    esac

done




#  
################################################################################
###############################################################################
##
##  Run the ximage process to create the GIF files and the interlace them with
##  giftool. Terminate if the ximage.bash script returns an error
##
##############################################################################
###############################################################################

echo "$SH_VER:TM:Run XIMAGE at $SECONDS" >> $JOB

serverFile="/aps/automation/lists/Xservers.list"


###############################################
# Set the operating system dependent features
###############################################
case `uname -s` in
SunOS )
    case `uname -r` in
    4*)
        export XIMAGE="$XANADU/sun4/bin/ximage"
        export EXT="sun" ;;
    5*)
        export XIMAGE="$XANADU/sol/bin/ximage"
	export EXT="sol" ;;
    esac ;;
ULTRIX)
    export XIMAGE="$XANADU/ulx/bin/ximage"
    export EXT="ulx" ;;
esac
csh $XANADU/tools/initu.csh
xco="ximage.xco"

########################################################################
# find an X server that will allow us to connect; Ximage needs this for
# some silly reason
########################################################################

echo "$DISPLAY : value of DISPLAY just before trying x server" >> $JOB
oldDISPLAY=$DISPLAY
DISPLAY=""
for i in $(cat $serverFile | grep -v '^#')
do
  DISPLAY=$i':0'
  $EXEDIR/XserverTest
  if [ $? -eq 0 ]
  then
    echo "Using $DISPLAY as X server for Ximage run" >> $JOB
    break
  else
    DISPLAY=""
  fi
done

if [ -z "$DISPLAY" ]
then
  echo "$SH_VER:E1:X no X servers found, skip GIF images" >> $JOB
else

##########################################################################
# loop over all of the Sky images (.img files) and send them into Ximage
# for GIF processing
##########################################################################


  for file in $(ls | egrep '^ad.*[gs][0123].....\.img$'); do
    echo "read/fits $file" > $xco
    echo "smooth"         >> $xco
    echo "cpd /ppm"       >> $xco
    echo "disp/noframe"   >> $xco
    echo "grid"           >> $xco
    echo "scale"          >> $xco
    echo "exit"           >> $xco
    echo "$SH_VER:LO:===========================================" >> $JOB

    $XIMAGE @$xco >> $JOB 2> log.tmp

    errchk log.tmp "ximage $LINENO" $xco

    $PPMTOGIF < ppmfile.ppm > ${file%.img}_img.gif 2>> $JOB
    rm -f ppmfile.ppm

  done
  rm -f $xco
  DISPLAY=$oldDISPLAY
fi


############################################################################
# loop over all the gifs; run giftool on them, copy them to the $GIF_STAGE 
# directory and then FITS wrap them
#############################################################################

for file in $(ls | egrep '\.gif$'); do

    $EXEDIR/giftool -i -B $file

    fits_wrap_gif ${file%.gif}_gif.wrap $file
    add_fits_keyword ${file%.gif}_gif.wrap[0] FNAME \
                     ${file%.gif}_gif.wrap[0] "STRIP$file"

done






# 
###############################################################################
#
# Create final FFILECAT catalogs.
#
###############################################################################

echo "$SH_VER:TM:Create final catalogs at $SECONDS" >> $JOB

rm -f *.cat

cat $GIS_KEYWORDS > gis.keywords
cat $SIS0_KEYWORDS > sis0.keywords
cat $SIS1_KEYWORDS > sis1.keywords
echo TIMEDEL >> gis.keywords
echo TIMEDEL >> sis0.keywords
echo TIMEDEL >> sis1.keywords

for file in $(cat RAW_FRF.list); do
    if [ -s "$file" ]; then
        case $file in
        ft*G2*[HML].fits) echo "$file[0]" >> gis2_raw.cat ;;
        ft*G3*[HML].fits) echo "$file[0]" >> gis3_raw.cat ;;
        ft*S0*[HML].fits) echo "$file[0]" >> sis0_raw.cat ;;
        ft*S1*[HML].fits) echo "$file[0]" >> sis1_raw.cat ;;
        esac
    fi
done
rm -f RAW_FRF.list

for file in $(ls); do
    case $file in
    ft*G2*[HML].fits) echo "$file[0]" >> gis2_frf.cat ;;
    ad*g2*[hml].unf ) echo "$file[0]" >> gis2_unf.cat ;;
    ad*g2*[hml].evt ) echo "$file[0]" >> gis2_evt.cat ;;
    ft*G3*[HML].fits) echo "$file[0]" >> gis3_frf.cat ;;
    ad*g3*[hml].unf ) echo "$file[0]" >> gis3_unf.cat ;;
    ad*g3*[hml].evt ) echo "$file[0]" >> gis3_evt.cat ;;
    ft*S0*[HML].fits) echo "$file[0]" >> sis0_frf.cat ;;
    ad*s0*[hml].unf ) echo "$file[0]" >> sis0_unf.cat ;;
    ad*s0*[hml].evt ) echo "$file[0]" >> sis0_evt.cat ;;
    ft*S1*[HML].fits) echo "$file[0]" >> sis1_frf.cat ;;
    ad*s1*[hml].unf ) echo "$file[0]" >> sis1_unf.cat ;;
    ad*s1*[hml].evt ) echo "$file[0]" >> sis1_evt.cat ;;
    esac
done

sed -e 's/,a,/,h,/' $FTOOLS/ffilecat.par > ffilecat.par
$FTOOLS/pset ./ffilecat.par maxlen=1
$FTOOLS/pset ./ffilecat.par minlen=0
$FTOOLS/pset ./ffilecat.par aform=A8
$FTOOLS/pset ./ffilecat.par eform=E9.3E1
$FTOOLS/pset ./ffilecat.par iform=I6
$FTOOLS/pset ./ffilecat.par omit=no
$FTOOLS/pset ./ffilecat.par quiet=yes

for file in $(ls | egrep '...._...\.cat$'); do
    catalog="${ADSE}_$file"

    case $file in
    gis2*) $FTOOLS/pset ./ffilecat.par keywords=@gis.keywords ;;
    gis3*) $FTOOLS/pset ./ffilecat.par keywords=@gis.keywords ;;
    sis0*) $FTOOLS/pset ./ffilecat.par keywords=@sis0.keywords ;;
    sis1*) $FTOOLS/pset ./ffilecat.par keywords=@sis1.keywords ;;
    esac

    $FTOOLS/pset ./ffilecat.par infile="@$file"
    $FTOOLS/pset ./ffilecat.par outfile=$catalog

    $FTOOLS/ffilecat >> $JOB 2> log.tmp

    errchk log.tmp "ffilecat $LINENO" ffilecat.par $file
    rm -f datamode.tmp

    for name in $(cat $file); do
        get_fits_keyword ${name%???}[1] DATAMODE
        echo "$get_fits_keyword_value" | tr -d \' | tr -d ' ' >> datamode.tmp
    done

    datamode=":"

    for name in $(sort datamode.tmp | uniq); do
        datamode="$datamode::$name"
    done

    rm -f datamode.tmp
    datamode=${datamode#::}
    datamode=${datamode#:}

    add_fits_keyword $catalog[0] FNAME "$catalog" 'File name'
    add_fits_keyword $catalog[1] FNAME "$catalog" 'File name'
    add_fits_keyword $catalog[1] TELESCOP 'ASCA' 'Telescope (mission) name'

    case $file in
    gis2*) add_fits_keyword $catalog[1] INSTRUME 'GIS2' 'Instrument ID' ;;
    gis3*) add_fits_keyword $catalog[1] INSTRUME 'GIS3' 'Instrument ID' ;;
    sis0*) add_fits_keyword $catalog[1] INSTRUME 'SIS0' 'Instrument ID' ;;
    sis1*) add_fits_keyword $catalog[1] INSTRUME 'SIS1' 'Instrument ID' ;;
    esac

    add_fits_keyword $catalog[1] DATAMODE "$datamode" 'Observational datamode'

    rm -f $file

done

rm -f ffilecat.par
rm -f gis.keywords sis0.keywords sis1.keywords



################################################################################
# Get PI name/address for ASCA*.txt and header pages.
##############################################################################

USPINUM="$($FTOOLS/pget ./job.par uspinum)"

if [ -z "$USPINUM" ]
then
  USPINUM="0"
fi

USPINAME="$($FTOOLS/pget ./job.par uspiname)"
USPIADR1="$($FTOOLS/pget ./job.par uspiaddr1)"
USPIADR2="$($FTOOLS/pget ./job.par uspiaddr2)"
USPIADR3="$($FTOOLS/pget ./job.par uspiaddr3)"
USPIADR4="$($FTOOLS/pget ./job.par uspiaddr4)"
USPIADR5="$($FTOOLS/pget ./job.par uspiaddr5)"
USPIEADR="$($FTOOLS/pget ./job.par uspiemail)"
JPPINUM="$($FTOOLS/pget ./job.par jppinum)"
JPPINAME="$($FTOOLS/pget ./job.par jppiname)"
JPPIADR1="$($FTOOLS/pget ./job.par jppiaddr1)"
JPPIADR2="$($FTOOLS/pget ./job.par jppiaddr2)"
JPPIADR3="$($FTOOLS/pget ./job.par jppiaddr3)"
JPPIADR4="$($FTOOLS/pget ./job.par jppiaddr4)"
JPPIADR5="$($FTOOLS/pget ./job.par jppiaddr5)"
JPPIEADR="$($FTOOLS/pget ./job.par jppiemail)"


# 
##############################################################################
###############################################################################
##
##  Create the ascamode.txt file.
##
#############################################################################
#############################################################################


echo "$SH_VER:TM:Create ASCAMODE at $SECONDS" >> $JOB

proposal_type="$($BIN/proposal_type $SEQUENCE)"
proposal_number="$($FTOOLS/pget ./job.par proposal)"
target_number="$($FTOOLS/pget ./job.par targetnum)"

if [ "$USPINUM" -ne 0 ]; then
    piname="$USPINAME"
else
    piname="$JPPINAME"
fi

logfile=${ADSE}_ascamode.txt
rm -f $logfile
touch $logfile

#################################
# loop through unfiltered files
#################################
for base_file in $(ls | egrep '\.unf$'); do

    #################################################
    # $file is the corresponding filtered event file
    #################################################
    file=${base_file%.unf}.evt

    echo -n "$SEQUENCE|$OBJECT|$RA|$DEC|$Lii|$Bii|" >> $logfile
    tel="unknown"
    TSTART=0
    TSTOP=0
    mode="unknown"
    bit_rate=0
    modal_id=0

    get_fits_keyword $base_file[0] TLM_FILE
    if [ "$get_fits_keyword_exist" = yes ]; then
        tel="$get_fits_keyword_value"
        tmp="$(echo $tel | sed s/\'//g)"
       	tel="$tmp"
    fi

    get_fits_keyword $base_file[0] TSTART
    if [ "$get_fits_keyword_exist" = yes ]; then
        TSTART="$get_fits_keyword_value"
    fi

    get_fits_keyword $base_file[0] TSTOP
    if [ "$get_fits_keyword_exist" = yes ]; then
        TSTOP="$get_fits_keyword_value"
    fi

    get_fits_keyword $base_file[0] DATAMODE
    if [ "$get_fits_keyword_exist" = yes ]; then
        mode="$get_fits_keyword_value"
        tmp="$(echo $mode | sed s/\'//g)"
       	mode="$tmp"
    fi

    get_fits_keyword $base_file[0] BIT_RATE
    if [ "$get_fits_keyword_exist" = yes ]; then
        bit_rate="$get_fits_keyword_value"
        tmp="$(echo $bit_rate | sed s/\'//g)"
       	bit_rate="$tmp"
    fi

    get_fits_keyword $base_file[1] MODAL_ID
    if [ "$get_fits_keyword_exist" = yes ]; then
        modal_id="$get_fits_keyword_value"
    fi

    echo -n "$tel|$TSTART|$TSTOP|$mode|$bit_rate|$modal_id|" >> $logfile

    ###################################################################
    # Compute statistics from XSELECT products based on instrument ID.
    ###################################################################
    gis_ontime=0
    gis_count_rate=0
    gis_count_rate_error=0
    sis_ontime=0
    sis_count_rate=0
    sis_count_rate_error=0

    case $base_file in
    *g[23]*)
        ######################
        # GIS
        ######################
        gis_ontime=0
        gis_nevents=0
        get_fits_keyword $base_file[0] ONTIME
        if [ "$get_fits_keyword_exist" = yes ]; then
            gis_ontime="$get_fits_keyword_value"
        fi
        get_fits_keyword $base_file[0] NEVENTS
        if [ "$get_fits_keyword_exist" = yes ]; then
            gis_nevents="$get_fits_keyword_value"
        fi
        if [ "$gis_ontime" = "0" ]; then
            echo "$SH_VAR:E1:Division by zero, ontime=0 in $base_file" >> $JOB
            gis_count_rate=0
        else
            gis_count_rate="$($STOOLS/floatdiv $gis_nevents $gis_ontime)"
        fi
        gis_count_rate_error="$($STOOLS/sqrt $gis_count_rate)" ;;

    *s[01]*)
        #########################
        # SIS
        #########################
        sis_ontime=0
        sis_nevents=0

        get_fits_keyword $base_file[0] ONTIME
        if [ "$get_fits_keyword_exist" = yes ]; then
            sis_ontime="$get_fits_keyword_value"
        fi

        get_fits_keyword $base_file[0] NEVENTS
        if [ "$get_fits_keyword_exist" = yes ]; then
            sis_nevents="$get_fits_keyword_value"
        fi


        if [ "$sis_ontime" = "0" ]; then
            echo "$SH_VAR:E1:Division by zero, ontime=0 in $base_file" >> $JOB
        sis_count_rate=0

        else
            sis_count_rate="$($STOOLS/floatdiv $sis_nevents $sis_ontime)"
        fi

        sis_count_rate_error="$($STOOLS/sqrt $sis_count_rate)" ;;
    esac

    echo -n "$gis_ontime|$gis_count_rate|$gis_count_rate_error|" >> $logfile
    echo -n "$sis_ontime|$sis_count_rate|$sis_count_rate_error|" >> $logfile

    gis_ontime=0
    gis_count_rate=0
    gis_count_rate_error=0
    sis_ontime=0
    sis_count_rate=0
    sis_count_rate_error=0
    if [ -s $file ]; then
        case $file in
        *g[23]*)
            gis_ontime=0
            gis_nevents=0
            get_fits_keyword $file[0] ONTIME
            if [ "$get_fits_keyword_exist" = yes ]; then
                gis_ontime="$get_fits_keyword_value"
            fi
            get_fits_keyword $file[0] NEVENTS
            if [ "$get_fits_keyword_exist" = yes ]; then
                gis_nevents="$get_fits_keyword_value"
            fi
            if [ "$gis_ontime" = "0" ]; then
                echo "$SH_VAR:E1:Division by zero, ontime=0 in $file" >> $JOB
                gis_count_rate=0
            else
                gis_count_rate="$($STOOLS/floatdiv $gis_nevents $gis_ontime)"
            fi
            gis_count_rate_error="$($STOOLS/sqrt $gis_count_rate)" ;;
        *s[01]*)
            sis_ontime=0
            sis_nevents=0
            get_fits_keyword $file[0] ONTIME
            if [ "$get_fits_keyword_exist" = yes ]; then
                sis_ontime="$get_fits_keyword_value"
            fi
            get_fits_keyword $file[0] NEVENTS
            if [ "$get_fits_keyword_exist" = yes ]; then
                sis_nevents="$get_fits_keyword_value"
            fi
            if [ "$sis_ontime" = "0" ]; then
                echo "$SH_VAR:E1:Division by zero, ontime=0 in $file" >> $JOB
            sis_count_rate=0
            else
                sis_count_rate="$($STOOLS/floatdiv $sis_nevents $sis_ontime)"
            fi
            sis_count_rate_error="$($STOOLS/sqrt $sis_count_rate)" ;;
        esac
    fi

    echo -n "$gis_ontime|$gis_count_rate|$gis_count_rate_error|" >> $logfile
    echo -n "$sis_ontime|$sis_count_rate|$sis_count_rate_error|" >> $logfile
    echo -n "$piname|" >> $logfile
    echo -n "$proposal_type|$proposal_number|$target_number|" >> $logfile
    echo "000000" >> $logfile
done

##############################
# Check for FAINT & MPC data.
##############################
touch tel.list
for file in $(ls | egrep '[07]1[HML]\.fits$'); do
    get_fits_keyword $base_file[0] TLM_FILE
    if [ "$get_fits_keyword_exist" = yes ]; then
        echo "$get_fits_keyword_value" >> tel.list
    fi
done

TSTART=0
TSTOP=0
bit_rate=0
modal_id=0
gis_ontime=0
gis_count_rate=0
gis_count_rate_error=0
sis_ontime=0
sis_count_rate=0
sis_count_rate_error=0
for tel in $(sort tel.list | uniq); do
    echo -n "$SEQUENCE|$OBJECT|$RA|$DEC|$Lii|$Bii|" >> $logfile
    case $tel in
    *01[HML]*) mode="FAINT" ;;
    *71[HML]*) mode="MPC" ;;
    esac
    echo -n "$tel|$TSTART|$TSTOP|$mode|$bit_rate|$modal_id|" >> $logfile
    echo -n "$gis_ontime|$gis_count_rate|$gis_count_rate_error|" >> $logfile
    echo -n "$sis_ontime|$sis_count_rate|$sis_count_rate_error|" >> $logfile
    echo -n "$gis_ontime|$gis_count_rate|$gis_count_rate_error|" >> $logfile
    echo -n "$sis_ontime|$sis_count_rate|$sis_count_rate_error|" >> $logfile
    echo -n "$piname|" >> $logfile
    echo -n "$proposal_type|$proposal_number|$target_number|" >> $logfile
    echo "000000" >> $logfile
done
rm -f tel.list

fits_wrap_ascii ${logfile%.txt}_txt.wrap $logfile





#  
################################################################################
###############################################################################
##
##  Create the ascalog.txt file.
##
##############################################################################
################################################################################

echo "$SH_VER:TM:Create ASCALOG at $SECONDS" >> $JOB
logfile=${ADSE}_ascalog.txt
rm -f $logfile
touch $logfile

####################################
# loop through FRF telemetry files
####################################
for tel in $(awk '{print $1}' job.par.info); do
    echo -n "$SEQUENCE|$OBJECT|$RA|$DEC|$Lii|$Bii|" >> $logfile

    get_fits_keyword $tel[0] TSTART
    TSTART=0
    if [ "$get_fits_keyword_exist" = yes ]; then
        TSTART="$get_fits_keyword_value"
    fi

    get_fits_keyword $tel[0] TSTOP
    TSTOP=0
    if [ "$get_fits_keyword_exist" = yes ]; then
        TSTOP="$get_fits_keyword_value"
    fi

    echo -n "$tel|$TSTART|$TSTOP|" >> $logfile

    ###########################################################
    # Set the base FRF name for the associated telemetry file.
    ###########################################################
    frf_base="${tel%.????}_${tel#??????????????}"

    ##################################################################
    # Integrate the ontime and nevents over associated GIS FRF files.
    ##################################################################
    gis_ontime=0
    gis_nevents=0
    for file in $(ls | egrep "${frf_base}G[23].....[HML]\.fits$"); do
        get_fits_keyword $file[1] ONTIME 
        if [ "$get_fits_keyword_exist" = yes ]; then
            gis_ontime="$($STOOLS/floatadd $gis_ontime $get_fits_keyword_value)"
        fi
        get_fits_keyword $file[1] NEVENTS 
        if [ "$get_fits_keyword_exist" = yes ]; then
            gis_nevents="$($STOOLS/floatadd $gis_nevents $get_fits_keyword_value)"
        fi
    done

    if [ "$gis_ontime" = "0" ]; then
        echo "$SH_VAR:E1:Division by zero, GIS ontime=0 in $file" >> $JOB
        gis_count_rate=0
    else
        gis_count_rate="$($STOOLS/floatdiv $gis_nevents $gis_ontime)"
    fi

    gis_count_rate_error="$($STOOLS/sqrt $gis_count_rate)"

    ###################################################################
    # Integrate the ontime and nevents over associated SIS FRF files.
    ###################################################################
    sis_ontime=0
    sis_nevents=0
    for file in $(ls | egrep "${frf_base}S[01].....[HML]\.fits$"); do
        get_fits_keyword $file[1] ONTIME 
        if [ "$get_fits_keyword_exist" = yes ]; then
            sis_ontime="$($STOOLS/floatadd $sis_ontime $get_fits_keyword_value)"
        fi
        get_fits_keyword $file[1] NEVENTS 
        if [ "$get_fits_keyword_exist" = yes ]; then
            sis_nevents="$($STOOLS/floatadd $sis_nevents $get_fits_keyword_value)"
        fi
    done
    if [ "$sis_ontime" = "0" ]; then
        echo "$SH_VAR:E1:Division by zero, SIS ontime=0 in $file" >> $JOB
        sis_count_rate=0
    else
        sis_count_rate="$($STOOLS/floatdiv $sis_nevents $sis_ontime)"
    fi
    sis_count_rate_error="$($STOOLS/sqrt $sis_count_rate)"

    echo -n "$gis_ontime|$gis_count_rate|$gis_count_rate_error|" >> $logfile
    echo -n "$sis_ontime|$sis_count_rate|$sis_count_rate_error|" >> $logfile
    echo -n "$piname|" >> $logfile
    echo -n "$proposal_type|$proposal_number|$target_number|" >> $logfile
    echo "000000" >> $logfile
done
fits_wrap_ascii ${logfile%.txt}_txt.wrap $logfile


#  
###############################################################################
###############################################################################
##
##  Create GIS HK textual dump files.
##
################################################################################
################################################################################


echo "$SH_VER:TM:Create text dumps of GIS HK files at $SECONDS" >> $JOB
sed -e 's/,a,/,h,/' $FTOOLS/ghkdump.par > ghkdump.par
for file in $(ls | egrep '^ft.*G[23]HK\.fits$'); do
    outfile=${file%.fits}.txt
    $FTOOLS/pset ./ghkdump.par infile="$file"
    $FTOOLS/pset ./ghkdump.par outfile="$outfile"
    $FTOOLS/ghkdump >> $JOB 2> log.tmp
    errchk log.tmp "ghkdump $LINENO" ghkdump.par
    wrapfile=${outfile%.txt}_txt.wrap
    fits_wrap_ascii $wrapfile $outfile
    add_fits_keyword $wrapfile[0] FNAME $wrapfile STRIP$outfile
done
rm -f ghkdump.par





#  
###############################################################################
##############################################################################
##
##  Create the processing header page.
##
##############################################################################
###############################################################################

echo "$SH_VER:TM:Create processing summary page at $SECONDS" >> $JOB
PROC_INFO="${ADSE}_proc_info.txt"
HEAD_PAGE="${ADSE}_hdr_page.txt"

echo " " > $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo "ASCA Processing Summary   " $(date)  >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo "Processing Script and Version: " ${0##*/} $PROC_VER >> $HEAD_PAGE
echo "PROCSCRIPT=$0" > $PROC_INFO
echo "PROCVER=$SH_VER" >> $PROC_INFO
echo "PROCLEVEL=$SEQPROCNUM" >> $PROC_INFO
echo "FTOOLS=${FTOOLSBASE##*/}" >> $PROC_INFO
echo "GAINHISTVER=$GAINHISTVER" >> $PROC_INFO
echo "Sequence Number = " $SEQUENCE "   Object Name = " $OBJECT >> $HEAD_PAGE
echo "RA  =" $RA "    DEC =" $DEC >> $HEAD_PAGE
echo "Lii =" $Lii"    Bii =" $Bii >> $HEAD_PAGE
echo "PROCDATE=$(date +%y%m%d)" >> $PROC_INFO
echo "PROCTIME=$(date +%H%M%S)" >> $PROC_INFO
echo "SEQUENCE=$SEQUENCE" >> $PROC_INFO
echo "OBJECT='$OBJECT'" >> $PROC_INFO
echo "RA=$RA" >> $PROC_INFO
echo "DEC=$DEC" >> $PROC_INFO
echo "Lii=$Lii" >> $PROC_INFO
echo "Bii=$Bii" >> $PROC_INFO
echo "ROLL=$ROLL" >> $PROC_INFO

if [ "$USPINUM" -ne 0 ]; then
    echo "U.S. PI INFO:" >> $HEAD_PAGE
    echo "USPINUM=$USPINUM" >> $PROC_INFO
    if [ "$USPINAME" != "" ]; then
        echo "PI: " "$USPINAME" >> $HEAD_PAGE
        echo "USPINAME='$USPINAME'" >> $PROC_INFO
    else
        echo "PI: " "NO Name" >> $HEAD_PAGE
        echo "USPINAME=\"NO Name\"" >> $PROC_INFO
    fi
    if [ "$USPIADR1" != "" ]; then
        echo "    " "$USPIADR1" >> $HEAD_PAGE
        echo "USPIADR1='$USPIADR1'" >> $PROC_INFO
    fi
    if [ "$USPIADR2" != "" ]; then
        echo "    " "$USPIADR2" >> $HEAD_PAGE
        echo "USPIADR2='$USPIADR2'" >> $PROC_INFO
    fi
    if [ "$USPIADR3" != "" ]; then
        echo "    " "$USPIADR3" >> $HEAD_PAGE
        echo "USPIADR3='$USPIADR3'" >> $PROC_INFO
    fi
    if [ "$USPIADR4" != "" ]; then
        echo "    " "$USPIADR4" >> $HEAD_PAGE
        echo "USPIADR4='$USPIADR4'" >> $PROC_INFO
    fi
    if [ "$USPIADR5" != "" ]; then
        echo "    " "$USPIADR5" >> $HEAD_PAGE
        echo "USPIADR5='$USPIADR5'" >> $PROC_INFO
    fi
    if [ "$USPIEADR" != "" ]; then
        echo "    " "$USPIEADR" >> $HEAD_PAGE
        echo "USPIEADR='$USPIEADR'" >> $PROC_INFO
    fi
fi

if [ "$JPPINUM" -ne 0 ]; then
    echo "Japanese PI INFO:" >> $HEAD_PAGE
    echo "JPPINUM=$JPPINUM" >> $PROC_INFO
    if [ "$JPPINAME" != "" ]; then
        echo "PI: " "$JPPINAME" >> $HEAD_PAGE
        echo "JPPINAME='$JPPINAME'" >> $PROC_INFO
    else
        echo "PI: " "NO Name" >> $HEAD_PAGE
        echo "JPPINAME='NO Name'" >> $PROC_INFO
    fi
    if [ "$JPPIADR1" != "" ]; then
        echo "    " "$JPPIADR1" >> $HEAD_PAGE
        echo "JPPIADR1='$JPPIADR1'" >> $PROC_INFO
    fi
    if [ "$JPPIADR2" != "" ]; then
        echo "    " "$JPPIADR2" >> $HEAD_PAGE
        echo "JPPIADR2='$JPPIADR2'" >> $PROC_INFO
    fi
    if [ "$JPPIADR3" != "" ]; then
        echo "    " "$JPPIADR3" >> $HEAD_PAGE
        echo "JPPIADR3='$JPPIADR3'" >> $PROC_INFO
    fi
    if [ "$JPPIADR4" != "" ]; then
        echo "    " "$JPPIADR4" >> $HEAD_PAGE
        echo "JPPIADR4='$JPPIADR4'" >> $PROC_INFO
    fi
    if [ "$JPPIADR5" != "" ]; then
        echo "    " "$JPPIADR5" >> $HEAD_PAGE
        echo "JPPIADR5='$JPPIADR5'" >> $PROC_INFO
    fi
    if [ "$JPPIEADR" != "" ]; then
        echo "    " "$JPPIEADR" >> $HEAD_PAGE
        echo "JPPIEADR='$JPPIEADR'" >> $PROC_INFO
    fi
fi

echo " " >> $HEAD_PAGE
echo " " >> $HEAD_PAGE

echo "Telemetry files used:" >> $HEAD_PAGE
typeset -i SEQ_MISSION_TIME=0
TOTAL_MISSION_TIME="0.0E0"
STARTUTDATE=""
ENDUTDATE=""
STARTUTTIME=""
ENDUTTIME=""
for file in $(awk '{print $1}' job.par.info); do
    FILENAME=$file
    get_fits_keyword $file[1] ORBIT0
    ORBIT0="$get_fits_keyword_value"
    get_fits_keyword $file[1] ORBIT1
    ORBIT1="$get_fits_keyword_value"
    get_fits_keyword $file[1] MTIME0
    MTIME0="$get_fits_keyword_value"
    get_fits_keyword $file[1] MTIME1
    MTIME1="$get_fits_keyword_value"
    get_fits_keyword $file[1] DATE0
    MTIME="$($STOOLS/floatdiff $MTIME1 $MTIME0)"
    TOTAL_MISSION_TIME="$($STOOLS/floatadd $TOTAL_MISSION_TIME $MTIME)"
    DATE0="$get_fits_keyword_value"
    get_fits_keyword $file[1] DATE1
    DATE1="$get_fits_keyword_value"
    get_fits_keyword $file[1] TIME0
    TIME0="$get_fits_keyword_value"
    get_fits_keyword $file[1] TIME1
    TIME1="$get_fits_keyword_value"
    get_fits_keyword $file[1] DATEF
    DATEF="$get_fits_keyword_value"
    get_fits_keyword $file[1] TIMEF
    TIMEF="$get_fits_keyword_value"
    echo $FILENAME  "  Included Mission Time =" $MTIME >> $HEAD_PAGE
    echo "     Start orbit:" $ORBIT0 "    End orbit:" $ORBIT1 >> $HEAD_PAGE
    echo "     Start mission time:" $MTIME0 "     End mission time:" $MTIME1 \
         >> $HEAD_PAGE
    echo "     Start UT date:" $DATE0 "   End UT date:" $DATE1 >> $HEAD_PAGE
    echo "     Start UT time:" $TIME0 "   End UT time:" $TIME1 >> $HEAD_PAGE
    echo "     File create UT date/time:" $DATEF $TIMEF >> $HEAD_PAGE
    if [ -z "$STARTUTDATE" ]; then
        STARTUTDATE=$DATE0
    fi
    if [ -z "$STARTUTTIME" ]; then
        STARTUTTIME=$TIME0
    fi
    ENDUTDATE=$DATE1
    ENDUTTIME=$TIME1
done
echo "STARTUTDATE=$STARTUTDATE" >> $PROC_INFO
echo "ENDUTDATE=$ENDUTDATE" >> $PROC_INFO
echo "STARTUTTIME=$STARTUTTIME" >> $PROC_INFO
echo "ENDUTTIME=$ENDUTTIME" >> $PROC_INFO
echo " " >> $HEAD_PAGE
echo "Attitude files used:" >> $HEAD_PAGE
for file in $(awk '{print $3}' job.par.info); do
    FILENAME=$file
    get_fits_keyword $file[1] ORBIT0
    ORBIT0="$get_fits_keyword_value"
    get_fits_keyword $file[1] ORBIT1
    ORBIT1="$get_fits_keyword_value"
    get_fits_keyword $file[1] MTIME0
    MTIME0="$get_fits_keyword_value"
    get_fits_keyword $file[1] MTIME1
    MTIME1="$get_fits_keyword_value"
    get_fits_keyword $file[1] DATE0
    DATE0="$get_fits_keyword_value"
    get_fits_keyword $file[1] DATE1
    DATE1="$get_fits_keyword_value"
    get_fits_keyword $file[1] TIME0
    TIME0="$get_fits_keyword_value"
    get_fits_keyword $file[1] TIME1
    TIME1="$get_fits_keyword_value"
    get_fits_keyword $file[1] DATEF
    DATEF="$get_fits_keyword_value"
    get_fits_keyword $file[1] TIMEF
    TIMEF="$get_fits_keyword_value"
    echo $FILENAME >> $HEAD_PAGE
    echo "     Start orbit:" $ORBIT0 "   End orbit:" $ORBIT1 >> $HEAD_PAGE
    echo "     Start mission time:" $MTIME0 "   End mission time:" $MTIME1 \
         >> $HEAD_PAGE
    echo "     Start UT date:" $DATE0 "   End UT date:" $DATE1 >> $HEAD_PAGE
    echo "     Start UT time:" $TIME0 "   End UT time:" $TIME1 >> $HEAD_PAGE
    echo "     File create UT date/time:" $DATEF  $TIMEF >> $HEAD_PAGE
done
echo "" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo "Processing Products Report" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo "Final catalogs and their associated products:" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo "Total Mission Time for this sequence =" \
     $($STOOLS/float2int $TOTAL_MISSION_TIME) >> $HEAD_PAGE 
echo "TOTALTIME=$TOTAL_MISSION_TIME" >> $PROC_INFO
echo " " >> $HEAD_PAGE





#########################################################################
# define a function which prints the number of events and ontime
# for an unfiltered fevent file and the corresponding filtered event
# file if there is one.
#########################################################################

report ()
{
    typeset nevents=0
    typeset ontime=0

    get_fits_keyword $1[0] NEVENTS
    nevents="$get_fits_keyword_value"

    get_fits_keyword $1[0] ONTIME
    ontime="$get_fits_keyword_value"

    echo "$1 ${ontime}sec ${nevents}evt" >> $HEAD_PAGE

    if [ -r ${1%unf}evt ]; then
        get_fits_keyword ${1%unf}evt[0] NEVENTS
        nevents="$get_fits_keyword_value"
        get_fits_keyword ${1%unf}evt[0] ONTIME
        ontime="$get_fits_keyword_value"
        echo "${1%unf}evt ${ontime}sec ${nevents}evt" >> $HEAD_PAGE
    fi

    for file in $(ls ${1%.unf}*); do
        echo "    $file" >> $HEAD_PAGE
    done
}

#################################
# more stuff for the header page
#################################

echo "GIS2" >> $HEAD_PAGE
for file in $(ls | egrep 'gis2_...\.cat$'); do
    echo "$file" >> $HEAD_PAGE
done

for file in $(ls | egrep 'g2.*\.unf$'); do
    report $file
done

echo " " >> $HEAD_PAGE

echo "GIS3" >> $HEAD_PAGE
for file in $(ls | egrep 'gis3_...\.cat$'); do
    echo "$file" >> $HEAD_PAGE
done

for file in $(ls | egrep 'g3.*\.unf$'); do
    report $file
done

echo " " >> $HEAD_PAGE

echo "SIS0" >> $HEAD_PAGE
for file in $(ls | egrep 'sis0_...\.cat$'); do
    echo "$file" >> $HEAD_PAGE
done

for file in $(ls | egrep 's0.*\.unf$'); do
    report $file
done

echo " " >> $HEAD_PAGE

echo "SIS1" >> $HEAD_PAGE
for file in $(ls | egrep 'sis1_...\.cat$'); do
    echo "$file" >> $HEAD_PAGE
done

for file in $(ls | egrep 's1.*\.unf$'); do
    report $file
done

echo " " >> $HEAD_PAGE

rm -f gis2_cat_list
rm -f gis3_cat_list
rm -f sis0_cat_list
rm -f sis1_cat_list

echo " " >> $HEAD_PAGE
echo "End of processing summary" >> $HEAD_PAGE
echo "" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo " " >> $HEAD_PAGE

echo "Configuration Summary from the job log" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
egrep ':CO:' $JOB >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo "End of configuration summary" >> $HEAD_PAGE
echo "" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo " " >> $HEAD_PAGE

echo "Error Summary from the job log" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE

echo "E2 errors are serious and should not appear. They are handled" \
     >> $HEAD_PAGE

echo "    by processing staff and if they are present should be reported" \
     >> $HEAD_PAGE

echo "    to ascahelp@legacy.gsfc.nasa.gov" >> $HEAD_PAGE
echo " " >> $HEAD_PAGE
echo " " >> $HEAD_PAGE

$BIN/report.errors $JOB >> $HEAD_PAGE

echo " " >> $HEAD_PAGE
echo "End of error summary" >> $HEAD_PAGE
echo "End of job log summary" >> $HEAD_PAGE

fits_wrap_ascii ${HEAD_PAGE%.txt}_txt.wrap $HEAD_PAGE
add_fits_keyword ${HEAD_PAGE%.txt}_txt.wrap[0] FNAME \
                 ${HEAD_PAGE%.txt}_txt.wrap STRIP$HEAD_PAGE

chmod 644 $PROC_INFO 
#  
#############################################################################
#############################################################################
##
##  Last is the tape contents file.
##
#############################################################################
#############################################################################

echo "$SH_VER:TM:Create tape contents file at $SECONDS" >> $JOB
typeset -i file_cnt=0

#############################
# define some functions
#############################
tape_print ()
{
    if [ -s "$1" ]; then
        file_kb="$(ls -s $1 | awk '{print $1}')"
    else
        file_kb=0
    fi
    if [ "$(fgrep $1 tape.create | wc -l)" -eq 0 ]; then
        let file_cnt+=1
        echo $file_cnt "'"${1##*/}"'" $file_kb "'"$2"'" >> tape.contents
        echo ${1##*/} >> tape.create
    fi
}

add_tape_science_files ()
{
    get_fits_keyword $1[1] NAXIS2 
    FILE_NUMBER="$get_fits_keyword_value"
    sed -e 's/,a,/,h,/' $FTOOLS/ftabpar.par > ftabpar.par
    $FTOOLS/pset ./ftabpar.par fitsfile="$1[1]"
    $FTOOLS/pset ./ftabpar.par column=FILENAME
    typeset -i file_index=0
    while [ $file_index -lt $FILE_NUMBER ]; do
        let file_index+=1
        $FTOOLS/pset ./ftabpar.par row="$file_index"
        $FTOOLS/ftabpar >> $JOB 2> log.tmp
        errchk log.tmp "ftabpar $LINENO" ftabpar.par
        name="$($FTOOLS/pget ./ftabpar.par value)"
        case $name in
        *G[23]*.fits) tape_print $name "FRF GIS Science file" ;;
        *S[01]*.fits) tape_print $name "FRF SIS Science file" ;;
        *G[23]*.fits) tape_print $name "FRF GIS Science file" ;;
        *S[01]*.fits) tape_print $name "FRF SIS Science file" ;;
        *G[23]*.fits) tape_print $name "FRF GIS Science file" ;;
        *S[01]*.fits) tape_print $name "FRF SIS Science file" ;;
        *g[23]*.unf ) tape_print $name "Unfiltered GIS Science file" ;;
        *s[01]*.unf ) tape_print $name "Unfiltered SIS Science file" ;;
        *g[23]*.evt ) tape_print $name "Filtered GIS Science file" ;;
        *s[01]*.evt ) tape_print $name "Filtered SIS Science file" ;;
        *           ) tape_print $name "Science file" ;;
        esac
    done
}

rm -f tape.create
touch tape.create

tape_print ${ADSE}_tape.cat  "Tape contents list FITS file"
tape_print ${ADSE}_hdr_page_txt.wrap "Processing Summary Page"

########################
# Catalogs of products.
########################
for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_raw.cat ]; then
        tape_print ${ADSE}_${inst}_raw.cat "${inst} RAW FRF products catalog"
    fi
done

for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_frf.cat ]; then
        tape_print ${ADSE}_${inst}_frf.cat "${inst} FRF products catalog"
    fi
done

for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_unf.cat ]; then
        tape_print ${ADSE}_${inst}_unf.cat "${inst} unfiltered products catalog"
    fi
done

for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_evt.cat ]; then
        tape_print ${ADSE}_${inst}_evt.cat "${inst} filtered products catalog"
    fi
done

# Catalogs of products.
for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_raw.cat ]; then
        add_tape_science_files ${ADSE}_${inst}_raw.cat
    fi
done

for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_frf.cat ]; then
        add_tape_science_files ${ADSE}_${inst}_frf.cat
    fi
done

for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_unf.cat ]; then
        add_tape_science_files ${ADSE}_${inst}_unf.cat
    fi
done

for file in $(ls | egrep '\.evt$' | egrep -v '[hml]\.evt$'); do
    case $file in
    *g[23]*.evt ) tape_print $file "Filtered GIS Science file" ;;
    *s[01]*.evt ) tape_print $file "Filtered SIS Science file" ;;
    esac
done

for inst in gis2 gis3 sis0 sis1; do
    if [ -s ${ADSE}_${inst}_evt.cat ]; then
        add_tape_science_files ${ADSE}_${inst}_evt.cat
    fi
done


########################
# Important products.
########################
for file in $(ls | egrep '^ft.*\.mkf$'); do
    tape_print $file "MKFILTER file"
done
for file in $(ls | egrep '\.img$'); do
    tape_print $file "Xselect modal sky image"
done
for file in $(ls | egrep '\.detimg$'); do
    tape_print $file "Xselect modal detector image"
done
for file in $(ls | egrep '_gqa_out\.wrap$'); do
    tape_print $file "FITS wrapped GQAPLOT ascii product"
done
for file in $(ls | egrep '_gqa_ps\.wrap$'); do
    tape_print $file "FITS wrapped GQAPLOT PS product"
done
for file in $(ls | egrep '_sqa_out\.wrap$'); do
    tape_print $file "FITS wrapped SQAPLOT ascii product"
done
for file in $(ls | egrep '_sqa_ps\.wrap$'); do
    tape_print $file "FITS wrapped SQAPLOT PS product"
done
for file in $(ls | egrep '_xsel_log\.wrap$'); do
    tape_print $file "FITS wrapped Xselect run log"
done
for file in $(ls | egrep 'gif\.wrap$'); do
    tape_print $file "FITS wrapped GIF image"
done
for file in $(ls | egrep '_txt\.wrap$'); do
    tape_print $file "FITS wrapped GIS HK dump"
done
for file in $(ls | egrep '\.ghf$'); do
    tape_print $file "GIS Gain History file"
done

for file in $(sort -u < gis2_cal_files_used); do
    tape_print ${file##/*/} "GIS2 calibration file"
done
for file in $(sort -u < gis3_cal_files_used); do
    tape_print ${file##/*/} "GIS3 calibration file"
done

tape_print ${S0MAP##/*/} "SIS0 telescope calibration file"
tape_print ${S1MAP##/*/} "SIS1 telescope calibration file"
if [ -s ${ADSE}_s0_dfe.wrap ]; then
    tape_print ${ADSE}_s0_dfe.wrap "SIS0 DFE files"
fi
if [ -s ${ADSE}_s1_dfe.wrap ]; then
    tape_print ${ADSE}_s1_dfe.wrap "SIS1 DFE files"
fi


######################
# Secondary products.
######################
for file in $(cat ascalin.fail.list); do
    tape_print $file "ASCALIN corrupted Science file"
done
for file in $(ls | egrep '^ft.*CMHK\.fits$'); do
    tape_print $file "Common Housekeeping File"
done
for file in $(ls | egrep '^ft.*G2HK\.fits$'); do
    tape_print $file "GIS2 Instrument Housekeeping File"
done
for file in $(ls | egrep '^ft.*G3HK\.fits$'); do
    tape_print $file "GIS3 Instrument Housekeeping File"
done
for file in $(ls | egrep '^ft.*S0HK\.fits$'); do
    tape_print $file "SIS0 Instrument Housekeeping File"
done
for file in $(ls | egrep '^ft.*S1HK\.fits$'); do
    tape_print $file "SIS1 Instrument Housekeeping File"
done
tape_print frf.orbit  "Orbit file"
for file in ft??????_????.????; do
    tape_print $file "Raw telemetry file"
done
for file in fa??????_????.????; do
    tape_print $file "Attitude file"
done

fits_wrap_ascii modal_config.fits $MODAL_LIST
tape_print modal_config.fits "ASCA Modal Configuration table"
fits_wrap_ascii ${ADSE}_rigid.wrap $RIGID
add_fits_keyword ${ADSE}_rigid.wrap[0] FNAME \
                 ${ADSE}_rigid.wrap STRIP${RIGID##*/}
tape_print ${ADSE}_rigid.wrap "FITS wrapped Rigidity Data table"
tape_print $LEAPTABLE "Leap Second Correction table"

# i hate this
echo "$SH_VER:LO:$JOB closed for conversion to FITS" >> $JOB
fits_wrap_ascii ${ADSE}_joblog.wrap $JOB
add_fits_keyword ${ADSE}_joblog.wrap[0] FNAME \
                 ${ADSE}_joblog.wrap STRIP${ADSE}_$JOB
tape_print ${ADSE}_joblog.wrap "Processing job log"

##############################################
# create tape.cat file
##############################################
sed -e 's/,a,/,h,/' $FTOOLS/fcreate.par > fcreate.par
$FTOOLS/pset ./fcreate.par  cdfile=fcreate.cdfile
$FTOOLS/pset ./fcreate.par  datafile=tape.contents
$FTOOLS/pset ./fcreate.par  outfile="${ADSE}_tape.cat"
$FTOOLS/pset ./fcreate.par  tbltype=ASCII

echo "filenum I4" > fcreate.cdfile
echo "filename A57" >> fcreate.cdfile
echo "filesize I7 kilobytes" >> fcreate.cdfile
echo "descrip A64" >> fcreate.cdfile

$FTOOLS/fcreate  >> $JOB 2> log.tmp

errchk log.tmp "FCREATE $LINENO" fcreate.par fcreate.cdfile tape.contents

add_fits_keyword ${ADSE}_tape.cat[0] FNAME \
                 ${ADSE}_tape.cat STRIP${ADSE}_tape.txt

rm -f fcreate.par fcreate.cdfile

#########################################################################
# Update all outputs with standard keywords
#########################################################################

echo "$SH_VER:TM:Update output fits with standard keywords at $SECONDS" >> $JOB

######################################
# assemble what you will need to know
######################################
if [ -z "$USPINAME" ]; then
    USPINAME="NOT AVAILABLE"
fi
if [ -z "$USPIADR1" ]; then
    USPIADR1=" "
fi
if [ -z "$USPIADR2" ]; then
    USPIADR2=" "
fi
if [ -z "$USPIADR3" ]; then
    USPIADR3=" "
fi
if [ -z "$USPIADR4" ]; then
    USPIADR4=" "
fi
if [ -z "$USPIADR5" ]; then
    USPIADR5=" "
fi
if [ -z "$USPIEADR" ]; then
    USPIEADR=" "
fi
if [ -z "$JPPINAME" ]; then
    JPPINAME="NOT AVAILABLE"
fi
if [ -z "$JPPIADR1" ]; then
    JPPIADR1=" "
fi
if [ -z "$JPPIADR2" ]; then
    JPPIADR2=" "
fi
if [ -z "$JPPIADR3" ]; then
    JPPIADR3=" "
fi
if [ -z "$JPPIADR4" ]; then
    JPPIADR4=" "
fi
if [ -z "$JPPIADR5" ]; then
    JPPIADR5=" "
fi
if [ -z "$JPPIEADR" ]; then
    JPPIEADR=" "
fi

sed -e 's/,a,/,h,/' $FTOOLS/fmodhead.par > fmodhead.par
$FTOOLS/pset ./fmodhead.par tmpfil=fmodhead.txt

echo "SEQNUM   $SEQUENCE / Sequential number from ODB" > fmodhead.txt
echo "SEQPNUM  $PROCEXT / Number of times sequence processed" >> fmodhead.txt
echo "PROCVER  $PROC_VER / Processing Configuration number" >> fmodhead.txt

if [ "$USPINUM" -gt 0 ]; then
    echo "USPINUM  $USPINUM" >> fmodhead.txt
    echo "USPINAME '$USPINAME'" >> fmodhead.txt
    echo "USPIADR1 '$USPIADR1'" >> fmodhead.txt
    echo "USPIADR2 '$USPIADR2'" >> fmodhead.txt
    echo "USPIADR3 '$USPIADR3'" >> fmodhead.txt
    echo "USPIADR4 '$USPIADR4'" >> fmodhead.txt
    echo "USPIADR5 '$USPIADR5'" >> fmodhead.txt
    echo "USPIEADR '$USPIEADR'" >> fmodhead.txt
fi

if [ "$JPPINUM" -gt 0 ]; then
    echo "JPPINUM $JPPINUM" >> fmodhead.txt
    echo "JPPINAME '$JPPINAME'" >> fmodhead.txt
    echo "JPPIADR1 '$JPPIADR1'" >> fmodhead.txt
    echo "JPPIADR2 '$JPPIADR2'" >> fmodhead.txt
    echo "JPPIADR3 '$JPPIADR3'" >> fmodhead.txt
    echo "JPPIADR4 '$JPPIADR4'" >> fmodhead.txt
    echo "JPPIADR5 '$JPPIADR5'" >> fmodhead.txt
    echo "JPPIEADR '$JPPIEADR'" >> fmodhead.txt
fi

for file in $(cat tape.create); do
    if [ "$file" = "${file##*/}" ]; then
        $FTOOLS/pset ./fmodhead.par infile="$file[0]"

        $FTOOLS/fmodhead >> $JOB 2> log.tmp

        errchk log.tmp "FMODHEAD $LINENO" fmodhead.par
    fi

done


echo "SEQNUM $SEQUENCE / Sequential number from ODB" > fmodhead.txt
echo "SEQPNUM  $PROCEXT / Number of times sequence processed" >> fmodhead.txt

for file in $(cat tape.create); do
    if [ $($BIN/recmp $file "\.img") -eq 0 ] ||
       [ $($BIN/recmp $file "\.detimg") -eq 0 ] ||
       [ $($BIN/recmp $file "_img_gif\.wrap") -eq 0 ] ||
       [ $($BIN/recmp $file "teldef_ascalin\.fits") -eq 0 ]; then
        continue
    fi

    if [ "$file" = "${file##*/}" ]; then
        $FTOOLS/pset ./fmodhead.par infile="$file[1]"
        $FTOOLS/fmodhead >> $JOB 2> log.tmp
        errchk log.tmp "FMODHEAD $LINENO" fmodhead.par
    fi

done

rm -f fmodhead.par fmodhead.txt



################################################################################
# Run fverify on contents of tape.create.
##############################################################################

echo "$SH_VER:TM:Begin fverify of tape.create contents at $SECONDS" >> $JOB

sed -e 's/,a,/,h,/' $FTOOLS/fverify.par > fverify.par
$FTOOLS/pset ./fverify.par prhead=no
$FTOOLS/pset ./fverify.par testdata=yes

for file in $(cat tape.create); do
    $FTOOLS/pset ./fverify.par infile="$file"
    $FTOOLS/pset ./fverify.par prhead=no
    $FTOOLS/pset ./fverify.par testdata=no

    $FTOOLS/fverify >> $JOB 2> log.tmp
    errchk log.tmp "FVERIFY $LINENO" fverify.par

done

rm -f fverify.par

################################################################################
# WRITE FINAL DATA TO $JOB
# The $JOB is closed from the point-of-view of operations
##############################################################################
echo "$SH_VER:TM:Job end at $SECONDS" $(date) >> $JOB


###############################################################################
# tell the automation we ended OK
##############################################################################
echo "$SEQUENCE $PROC_VER END $(date +%y%m%d%H%M%S)" >> $PROC_HIST


###########################################################
# exit the script, printing the error log when doing so
##########################################################
shexit 0

###############################################################################
# Thats all folks
###############################################################################
#