sourceevents (version 1.2)

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

###########################################################################
#
# SYNTAX: sourceevents
#
# BRIEFLY: Extract event files for each source
#
# DESCRIPTION: This routine extracts unbinned light curves
# DESCRIPTION: (FITS event files readable by <TT>xronos</TT>)
# DESCRIPTION: for each source
# DESCRIPTION: from the filtered event files.
# DESCRIPTION: The unbinned light curves can be used for higher
# DESCRIPTION: resolution timing analyses than the binned lightcurves.
# DESCRIPTION: A binned light curve is extracted for each filtered event
# DESCRIPTION: file and source with more than %minsrcevents events.
# DESCRIPTION: Note that event files do not carry information about
# DESCRIPTION: previous region filters, so spectra should not be extracted
# DESCRIPTION: from the unbinned light curves.
# DESCRIPTION: The unbinned light curves are <EM>not</EM> dead-time corrected.
#
# VERSION: 1.2
#
# HISTORY: 0.0 -> 1.0
# HISTORY: Now keeps all event files separate.
# HISTORY: Only produces source-zero files if there are no detected sources.
# HISTORY: Uses detector coordinate region filters.
# HISTORY:
# HISTORY: 1.0 -> 1.1 8/6/97
# HISTORY: Added a check for whether or not the source region file exists.
# HISTORY: 
# HISTORY: 1.1 -> 1.2 2000-03-01
# HISTORY: Now explicitly specify detector coordinates for xselect
#
##############################################################################
#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

        region=$($UTIL/generate_filename sourceregion $inst $dimen     \
                                                      $sourcemode none \
                                                      $source)
        ##############################
        # Check if region file exists
        ##############################
        if [ ! -a "$region" ]; then
            $UTIL/log_entry "Skipping $srcevent since $region does not exist"
            let source=$source+1
            continue
        fi

        $UTIL/log_entry "Extracting unbinned light curve $srcevent"

        ###################################
        # 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 "set image det"                                 >>$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 command file to xselect
        ###################################
        $FTOOL/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