sourceevents (version 0.0)

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

###########################################################################
#
# 
# SYNTAX: sourceevents
#
# BRIEFLY: Extract event files for each source
#
# DESCRIPTION: Extracts events files from the filtered event files
# DESCRIPTION: for each source. These files can be used for higher
# DESCRIPTION: resolution timing analysis than the extracted lightcurves.
# DESCRIPTION: One event file is extracted for each instrument, mode 
# DESCRIPTION: (FAINT, BRIGHT, FAST, PH, MPC), and bitrate.
#
# VERSION: 0.0
#
# HISTORY: 0.0 -> 1.0
# HISTORY: Now keep all event files separate.
# HISTORY: only produce source zero files if there are no detected sources.
# HISTORY: use detector coordinate region filters.
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Extracting source event files"

############################
# some temporary file names
############################
xselcom=z.xco

#########################################
# minimum events for keeping an event file
#########################################
minsrcevents=$($UTIL/read_parfile $PARFILE minsrcevents )

list=$($UTIL/generate_filename event any any any any )
list=$(ls $list 2>/dev/null)
for event in $list; do

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




     case "$inst" in
     s?) 
          sourcemode="02"
          full=sis
          dimen=320
          ;;
     g?) 
          sourcemode=70
          full=gis
          dimen=$($UTIL/read_fits_keyword ${event}[0] RAWXBINS )
          ;;
     *) 
          $UTIL/exception $0 1 "Unknown instrument $inst"
          ;;
     esac



     ################################################
     # go to the next event file if there is no sourcecat
     ################################################

                    
     #########################
     # no sources for 
     #########################
     if [ "$mode" -ne "71" -a "$mode" -ne "03" ]; then

          ##########################
          # this is an imaging mode
          ##########################
          sourcecat=$($UTIL/generate_filename sourcecat $full $dimen \
                                                        $sourcemode $bitrate )

          if [ ! -f "$sourcecat" ]; then
               #########################################################
               # there's no source catalog, skip to the next event file
               #########################################################
               $UTIL/log_entry "No source cat $sourcecat"
               continue
          fi

          #############################################
          # get number of sources from source catalog
          #############################################
          nsources=$($UTIL/read_fits_keyword ${sourcecat}[1] NAXIS2 )
     else
          #################################
          # non-imaging mode
          #################################
          nsources=0
     fi

     ###########################################################
     # only generate source zero files if there are no sources
     ###########################################################
     if [ $nsources -gt 0 ]; then
          typeset -i source=1
     else
          typeset -i source=0
     fi
  
     ####################
     # loop over sources
     ####################
     while [ $source -le $nsources ]; do

          srcevent=$($UTIL/generate_filename srcevent $inst $index $mode   \
                                                      $bitrate $source)
          rm -f $srcevent
          $UTIL/log_entry "Extracting unbinned light curve $srcevent"


          region=$($UTIL/generate_filename sourceregion $inst $dimen \
                                                        $sourcemode none \
                                                        $source)

                       
 
          ##################################
          # generate an xselect command file
          ##################################
          rm -f $xselcom

          echo "xsel"                                           >$xselcom
          echo "set mission ASCA"                              >>$xselcom
          echo "set datadir ."                                 >>$xselcom
          echo "read events ${event}"                          >>$xselcom
          echo "filter region $region"                         >>$xselcom
          echo "extract events"                                >>$xselcom
          echo "save events outfile=$srcevent use_events=yes"  >>$xselcom
          echo "exit save_session=no"                          >>$xselcom

          ##################################
          # feed the comand file to xselect
          ##################################
          $FTOOLS/xselect @${xselcom} </dev/null >stdout.log 2>stderr.log
          $UTIL/xselect_test $? $0 stdout.log stderr.log

          rm -f $xselcom

          ###################################################
          # skip to the next source if no event file was 
          # extracted
          ##################################################
          if [ ! -a $srcevent ]; then
               let source=$source+1
               continue
          fi

          #####################################################
          # if there were less than a minimum number of 
          # events, delete the event file and skip to 
          # the next iteration
          ###################################################
          totcts=$($UTIL/read_fits_keyword ${srcevent}[1] NAXIS2 )

          if [ $totcts -lt $minsrcevents ]; then
               $UTIL/log_entry "Deleting $srcevent since it has $totcts events"

               rm -f $srcevent
               let source=$source+1
               continue
          fi


          let source=$source+1

     done #loop over sources

done #loop over event files




exit 0