rev1trend (version 1.9)

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

###########################################################################
# 
# SYNTAX: rev1trend
#
# BRIEFLY: Generate and list rev1 trend products
#
# DESCRIPTION: This routine generates several trend products which
# DESCRIPTION: are not included in the distribution 
#
# VERSION: 1.9
#
# HISTORY: 0.0 -> 1.0 8/23/96 
# HISTORY: added scraps file to trend catalog
# HISTORY:
# HISTORY: 1.0 -> 1.1 9/05/96 
# HISTORY: Also put single quotes around STARTUTDATE ENDUTDATE,
# HISTORY: STARTUTTIME, and ENDUTTIME values in proc_info.txt   
# HISTORY: 9/11/96
# HISTORY: added totexpo and totsky     
# HISTORY:
# HISTORY: 1.1 -> 1.2 9/19/96
# HISTORY: added corimg to trend products    
# HISTORY:   
# HISTORY: 1.2 -> 1.3 9/26/96
# HISTORY: now reads list of file types to be saved as trend products from
# HISTORY: $LISTS/file_classes instead of having them hard-wired into
# HISTORY: the script
# HISTORY: 10/03/96
# HISTORY: also fixed a bug where the line variable wasn't being reset 
# HISTORY: for each entry in the ascalog and ascamode files
# HISTORY: 
# HISTORY: 1.3 -> 1.4 11/26/96
# HISTORY: Deleted the code which creates the trendcat file.
# HISTORY: It has been moved to the fileslists subroutine since 
# HISTORY: rev1trend will eventually be deleted
# HISTORY: 
# HISTORY: 1.4 -> 1.5 12/19/96
# HISTORY: now read all pointing info from the job.par
# HISTORY: 12/26/96 Corrected syntax for several ftool_test calls in rate file 
# HISTORY: creation.
# HISTORY: 
# HISTORY: 1.5 -> 1.6 2/4/97
# HISTORY: converted from float... STOOLS to equals
# HISTORY: 
# HISTORY: 1.6 -> 1.7 3/10/97
# HISTORY: Deleted code for producing ascamode, ascalog and science times files
# HISTORY: 
# HISTORY: 1.7 -> 1.8 3/23/97
# HISTORY: Made ratefile code compatible with HK files which have an ALLGTI
# HISTORY: extension attached.
# HISTORY: Also added an ftool_test after the fdump call.
# HISTORY: 
# HISTORY: 1.8 -> 1.9 3/27/97 
# HISTORY: Now read ra , dec and roll from job.par instead of recalculating them
# HISTORY: from the euler angles. Not only that, but the euler angles were 
# HISTORY: read from the old eulern_001 numbers which are now always zero.
# HISTORY: This problem was discovered by Ed Seiler.
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Generate rev1 trend products"

############################################################################
# The following code was snatched from the rev1 processing script.
# with only minor modifications
############################################################################

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

     $UTIL/log_entry "Generating $inst rate files"

     ###################################################
     # count the number of HK files for this intrument
     ###################################################
     ls *${inst}HK.fits | awk '{print $1"[1]"}' > ${inst}hk.list
     num_hk_files=$(wc -l < ${inst}hk.list )

     #####################################################
     # if no HK files found for this SIS do not continue
     #####################################################
     if [ $num_hk_files -eq 0 ]; then
          $UTIL/log_entry "No $inst HK files, so skipping rate file"
          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
          $FTOOLS/fmerge @${inst}hk.list ${inst}hk.fits - clobber=yes \
                >stdout.log 2>stderr.log

          $UTIL/ftool_test fmerge $? $0 2 stdout.log sttderr.log
     else
          cp *${inst}HK.fits ${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[1] outfile=./hk.dmp columns="NAME" \
               rows=- prhead=no showunit=no showrow=no >stdout.log 2>stderr.log

     $UTIL/ftool_test fdump $? $0 2 stdout.log sttderr.log


     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
          $UTIL/log_entry "CCD $i in use for ${inst}"

          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 >stdout.log 2>stderr.log
  $UTIL/ftool_test hkexpand $? $0 2 stdout.log stderr.log

  ####################################################################
  # 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."
     expr="${expr}${inst}_TELM${c}.ne.32).or.(${inst}_BRATE.eq.2.and."
     expr="${expr}${inst}_MODE.eq.2.and.${inst}_TELM${c}.ne.128).or."
     expr="${expr}(${inst}_BRATE.eq.4.and.${inst}_MODE.eq.2.and."
     expr="${expr}${inst}_TELM${c}.ne.1024).or.(${inst}_BRATE.eq.1.and."
     expr="${expr}${inst}_MODE.eq.1.and.${inst}_TELM${c}.ne.8).or."
     expr="${expr}(${inst}_BRATE.eq.2.and.${inst}_MODE.eq.1.and."
     expr="${expr}${inst}_TELM${c}.ne.32).or.(${inst}_BRATE.eq.4.and."
     expr="${expr}${inst}_MODE.eq.1.and.${inst}_TELM${c}.ne.256)"

    fcalc $old $new ${inst}_SAT${c} "$expr" >stdout.log 2>stderr.log
    $UTIL/ftool_test fcalc $? $0 2 stdout.log stderr.log

    ######################################
    # 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
    ###################################
    ratefile=$($UTIL/generate_filename ratefile s${inst#S} )
    mv $old $ratefile
    $UTIL/log_entry "$ratefile sucessfully created"

  else
    ################################
    # error: file $old doesn't exist
    ################################
    $UTIL/log_entry "$inst rate file not created"

  fi


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

done



######################################################
# procinfo file
#######################################################
PROC_INFO=$($UTIL/generate_filename procinfo )
rm -f $PROC_INFO

$UTIL/log_entry "Creating $PROC_INFO"

##########################
# object and sequence
##########################
echo  "OBJECT='$($UTIL/read_parfile $JOBPAR object  )'" >> $PROC_INFO
echo "SEQUENCE=$($UTIL/read_parfile $JOBPAR sequence)"  >> $PROC_INFO

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

echo "RA=$ra"     >> $PROC_INFO
echo "DEC=$dec"   >> $PROC_INFO
echo "ROLL=$roll" >> $PROC_INFO

###############################
# processing info
###############################
ftools=${FTOOLS%/*}
ftools=${ftools##*/}

echo "FTOOLS=$ftools"                                     >> $PROC_INFO
echo "PROCVER=$(  $UTIL/read_parfile $PARFILE version  )" >> $PROC_INFO
echo "PROCLEVEL=$($UTIL/read_parfile $JOBPAR seqprocnum)" >> $PROC_INFO
echo "PROCDATE=$(date +%y%m%d)"                           >> $PROC_INFO

#########################
# PI names
#########################

if [ "$($UTIL/read_parfile $JOBPAR uspinum)" -ne 0 ]; then
     USPINAME=$($UTIL/read_parfile $JOBPAR uspiname )
     if [ "$USPINAME" != "" ]; then
         echo "USPINAME='$USPINAME'" >> $PROC_INFO
     else
         echo "USPINAME=\"No Name\"" >> $PROC_INFO
     fi
fi

if [ "$($UTIL/read_parfile $JOBPAR jppinum)" -ne 0 ]; then
    JPPINAME=$($UTIL/read_parfile $JOBPAR jppiname )
    if [ "$JPPINAME" != "" ]; then
        echo "JPPINAME='$JPPINAME'" >> $PROC_INFO
    else
        echo "JPPINAME='No Name'" >> $PROC_INFO
    fi

fi

###############################
# times
###############################
typeset -i SEQ_MISSION_TIME=0
TOTAL_MISSION_TIME="0.0E0"
STARTUTDATE=""
ENDUTDATE=""
STARTUTTIME=""
ENDUTTIME=""
for file in $(ls ft*[0-9] 2>/dev/null ); do

     MTIME0=$($UTIL/read_fits_keyword $file[1] MTIME0 )
     MTIME1=$($UTIL/read_fits_keyword $file[1] MTIME1 )
     MTIME="$($STOOLS/equals "$MTIME1 - $MTIME0" )"
     TOTAL_MISSION_TIME="$($STOOLS/equals "$TOTAL_MISSION_TIME + $MTIME")"

     DATE0=$($UTIL/read_fits_keyword $file[1] DATE0 )
     DATE1=$($UTIL/read_fits_keyword $file[1] DATE1 )

     TIME0=$($UTIL/read_fits_keyword $file[1] TIME0 )
     TIME1=$($UTIL/read_fits_keyword $file[1] TIME1 )

     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 "TOTALTIME=$TOTAL_MISSION_TIME" >> $PROC_INFO


exit 0