faint2bright (version 1.0)

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

###########################################################################
#
# 
# SYNTAX: faint2bright
#
# BRIEFLY: Convert FAINT mode files to BRIGHT and BRIGHT2 modes
#
# DESCRIPTION: This routine converts FAINT mode SIS unfiltered event files
# DESCRIPTION: to BRIGHT and BRIGHT2 modes.
# DESCRIPTION: <p>
# DESCRIPTION: An event typically registers on more than one pixel of
# DESCRIPTION: an SIS CCD. The pattern of excited pixels can suggest 
# DESCRIPTION: whether the event was an actual photon or not.
# DESCRIPTION: <p>
# DESCRIPTION: In FAINT mode, the nine pixels surrounding an event are 
# DESCRIPTION: telemetered to the ground. In BRIGHT mode only a single PHA
# DESCRIPTION: value and a grade describing the pattern of excited pixels
# DESCRIPTION: is sent. 
# DESCRIPTION: BRIGHT2 mode files are created only on the ground.
# DESCRIPTION: BRIGHT2 mode is like BRIGHT mode except that the extra
# DESCRIPTION: information from FAINT is used to correct for things
# DESCRIPTION: like dark frame error and echo effect.
# DESCRIPTION: <p>
# DESCRIPTION: The BRIGHT mode conversion is done with zerodef set to 1
# DESCRIPTION: in order to minimize the effect of RDD. 
# DESCRIPTION: (see fhelp faintdfe with ftools ver 3.5 or higher )
# DESCRIPTION: <p>
# DESCRIPTION: As a byproduct, the dark frame error information is extracted 
# DESCRIPTION: and stored in the dfe files.
#
# VERSION: 1.0
#
# HISTORY: 0.0 -> 1.0 1/19/96
# HISTORY: Fixed a bug in indexing pointed out by Koji Mukai
# HISTORY: The file index for new event files was set to a value higher than 
# HISTORY: the index of any existing files. However the search for the
# HISTORY: highest existing index was not inside a loop over instrument so
# HISTORY: the only the highest index for SIS1 files was found. Now the index
# HISTORY: for new files is the same as the index for the original file.
# HISTORY: This is possible since the combine script assigns a unique
# HISTORY: index for each unfiltered event file regardless of mode.
# HISTORY: Originally this was not true for sequences with multiple
# HISTORY: telemetry files, but that was also a bug.
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Converting FAINT mode files to BRIGHT and BRIGHT2 modes"

min_events=$($UTIL/read_parfile $PARFILE sis_events_min )

#################################
# loop through FAINT mode files
#################################
list=$($UTIL/generate_filename unfiltered s? any 01 any)
list=$(ls $list 2>/dev/null)

if [ -z "$list" ]; then
     $UTIL/log_entry "No FAINT mode data to convert to bright mode"
else

     #######################################################################
     # determine split thresholds
     # nowadays these are always 40, but some early PV phave observations
     # may have other values.
     # check an on-board created unfiltered event file to see what split
     # threshold was used there
     ########################################################################
     for inst in s0 s1 ; do
     
          case "$inst" in 
          s0) caps="S0";;
          s1) caps="S1";;
          *)  $UTIL/exception $0 1 "Unknown instrument $inst ";;
          esac

          default=$($UTIL/read_parfile $PARFILE ${inst}_split )
          
          list=$($UTIL/generate_filename unfiltered $inst any 02 any )
          unfiltered=$(ls $list 2>/dev/null | head -1 )
          if [ -n "$unfiltered" ]; then
               ccd=$($UTIL/read_fits_keyword $unfiltered[0] ${caps}CCDLST \
                     | awk '{print $1}' )

               ########################
               # debugging output
               ########################
               if [ -n "$DEBUG" ]; then
                    echo ${0##*/} unfiltered=$unfiltered
                    echo ${0##*/} caps=$caps
                    echo ${0##*/} ccd=$ccd
               fi
        

               split=$($UTIL/read_fits_keyword $unfiltered[0] \
                                               ${caps}_SPTR${ccd} )
               if [ $split -ne $default ]; then
                    $UTIL/log_entry \
          "Using split threshold $split for $inst to match $unfiltered CCD $ccd"
               fi
          else
               split=$default
          fi

          case $inst in 
          s0) split0=$split;;
          s1) split1=$split;;
          *) $UTIL/exception $0 1 "Unknown instrument $inst";;
          esac

     done

     $UTIL/log_entry "Split threshold for SIS0=$split0, and SIS1=$split1"

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

     fi

     ########################
     # fetch calibration file
     #########################
     caldir=$($UTIL/read_parfile $PARFILE caldir   )
     tblfile=$($UTIL/read_parfile $PARFILE faintdfe )
     $UTIL/fetch_file $caldir $tblfile

     #####################################
     # remove any old cumulated dfe files
     #####################################
     for inst in s0 s1; do
          cumulated=$($UTIL/generate_filename dfe $inst )
          rm -f $cumulated
     done


     #################
     # convert modes
     #################
          
     list=$($UTIL/generate_filename unfiltered any any 01 any )
     list=$(ls $list 2>/dev/null)
     for unfiltered in ${list} ; do

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

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

          case $inst in 
          s0) split=$split0;;
          s1) split=$split1;;
          *) $UTIL/exception $0 1 "Unknown instrument $inst";;
          esac


          cumulated=$($UTIL/generate_filename dfe $inst )

    
          ############################
          # do BRIGHT2, then BRIGHT
          ############################
          for mode in 12 02; do

               bright=$( $UTIL/generate_filename unfiltered \
                                                 $inst $index $mode $bitrate)

               case "$mode" in 
               12) 
                    ################
                    # BRIGHT2 
                    ################
                    zerodef=1
                    bright_flag=no;;
               02)
                    ################
                    # BRIGHT
                    ###############
                    zerodef=2
                    bright_flag=yes;;
               *) 
                    ################
                    # unknown mode
                    ################
                    $UTIL/exception $0 1 "Unknown mode $mode";;
               esac



               ###############################
               # calculate dark frame error 
               ###############################
               $UTIL/log_entry \
                "Calculating DFE values for $unfiltered with zerodef=$zerodef"

               $UTIL/setup_parfile $FTOOLS/faintdfe.par infile=$unfiltered \
                                                        tblfile=$tblfile   \
                                                        split=$split       \
                                                        binsec=64          \
                                                        zerodef=$zerodef   \
                                                        outfile=dfe.tmp
                                                
               rm -f dfe.tmp
               $FTOOLS/faintdfe >stdout.log 2>stderr.log
               code=$?

               grep -v 'zero-levels are set to ones expected in bright mode' \
                    stderr.log >filtered.tmp
               if [ ! -s filtered.tmp -a -s stderr.log ]; then
                    $UTIL/log_entry "Stderr output from faintdfe:"
                    $UTIL/file_to_log stderr.log
               fi
               rm -f stderr.log

               $UTIL/ftool_test faintdfe $code $0 2 stdout.log filtered.tmp

               ########################################################
               # store the DFE values used to convert to BRIGHT mode
               ########################################################
               if [ "$mode" = "02" ]; then
                    cat dfe.tmp >>$cumulated
               fi


               ##################################
               # convert to BRIGHT/BRIGHT2 mode 
               ##################################
               $UTIL/log_entry "Converting $unfiltered to $bright"
          
               rm -f $bright
               $UTIL/setup_parfile $FTOOLS/faint.par infile=$unfiltered \
                                                     outfile=$bright    \
                                                     split=$split       \
                                                     dfefile=dfe.tmp    \
                                                     maxgrade=6         \
                                                     bright=$bright_flag

               $FTOOLS/faint >stdout.log 2>stderr.log
               $UTIL/ftool_test faint $? $0 2 stdout.log stderr.log
               rm -f dfe.tmp


               ############################################################
               # remove the file if it has < the minimum number of events
               ############################################################
               nevents=$($UTIL/read_fits_keyword ${bright}[0] NEVENTS )
               if [ $nevents -lt $min_events ]; then
              
                    $UTIL/log_entry \
                          "Removing $bright since it only has $nevents events"

                    rm -f $bright
               fi


                                                

                                                 
          
          

          done

          let i=$i+1

     done

     ###############################
     # sort the cumulated dfe files
     ###############################
     for inst in s0 s1; do

          cumulated=$($UTIL/generate_filename dfe $inst )
          if [ -s $cumulated ]; then
               sort -n $cumulated |uniq >dfe.tmp
               mv dfe.tmp $cumulated
          fi

     done


fi





exit 0