earthevents (version 2.1)

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

###########################################################################
#
# SYNTAX: earthevents
#
# BRIEFLY: Extract bright and dark Earth event files
#
# DESCRIPTION: This routine extracts dark Earth and GIS bright Earth
# DESCRIPTION: observing periods for use as trend products.
# DESCRIPTION: No other selection criteria are applied other than
# DESCRIPTION: FOV=1 for the dark Earth and FOV=2 for the bright Earth.
# DESCRIPTION: The SIS files have had the hot pixels
# DESCRIPTION: (but not the flickering pixels) removed.
# DESCRIPTION: This is done by running <TT>cleansis</TT> with iterate=no.
# DESCRIPTION: The GIS files are not giscleaned and are not
# DESCRIPTION: region-filtered.
# DESCRIPTION: <P>
# DESCRIPTION: Note we do not extract SIS bright Earth files since they would
# DESCRIPTION: be heavily affected by optical light.
#
# VERSION: 2.1
#
# HISTORY: 0.0 -> 1.0 5/12/97
# HISTORY: Now also skips an event file if it was not created by xselect.
# HISTORY:
# HISTORY: 1.0 -> 1.1 5/17/97
# HISTORY: Added "rm -f $clean"s before a number of "continue"s.
# HISTORY: Fixed the typo phamax=095 instead of 4095 in cleansis.
# HISTORY: This was discovered by Geoff Crew.
# HISTORY: Now skips MPC mode files until extract events works.
# HISTORY:
# HISTORY: 1.1 -> 1.2 6/10/97
# HISTORY: Now skips FAST mode files to save archive space.
# HISTORY:
# HISTORY: 1.2 -> 1.3 5/14/97
# HISTORY: Changed mkf_def_expr to ASCA_mkf_def_expr in xselect. Apparently
# HISTORY: the advent of FTOOLS 4.0 made "mkf_def_expr" an error, which
# HISTORY: caused xselect to not do any GTI selection at all, which is of course
# HISTORY: disastrous.
# HISTORY:
# HISTORY: 1.3 -> 2.0 7/8/97
# HISTORY: Moved cleaning to occur after selection.
# HISTORY:
# HISTORY: 2.0 -> 2.1 4/10/98
# HISTORY: deleted the ASCA_mkf_def_expr=ft*.mkf parameter from the xselect
# HISTORY: select mkf command. This is to accomodate FTOOLS 4.1 where
# HISTORY: this parameter is no longer recognized and is now an error.
#
############################################################################
#DEBUG=1

command=z.xco
clean=clean.tmp

$UTIL/milestone "Extracting bright and dark Earth event files."

for inst in s0 s1 g2 g3; do

    ####################################
    # Loop through the unfiltered files
    ####################################
    list=$( $UTIL/generate_filename unfiltered $inst any any any any)
    list=$(ls $list 2>/dev/null)

    for unfiltered in $list; do

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

        ##########################
        # Skip GIS MPC mode files
        ##########################
        if [ "$mode" = "71" ]; then
            continue
        fi

        case "$mode" in
        71)
            ################################################################
            # Skip GIS MPC mode since you can't (yet) extract "event" files
            ################################################################
            continue
            ;;
        01)
            ###################################################
            # Skip FAINT mode files since would be really huge
            ###################################################
            continue
            ;;
        03)
            ################################################
            # Skip FAST mode files since they would be huge
            ################################################
            continue
            ;;
        esac


        $UTIL/log_entry \
                   "Extracting bright and dark Earth events from $unfiltered"

        #########################################################
        # Loop over dark (fov=1) and bright (fov=2) Earth events
        #########################################################
        for fov in 1 2; do

            ###################################
            # Bright or dark Earth event file?
            ###################################
            case "$fov" in
            1)
                #######
                # Dark
                #######
                event=$($UTIL/generate_filename darkearth \
                                                $inst $index $mode $bitrate)
                ;;
            2)
                #########
                # Bright
                #########
                case "$inst" in
                s?)
                    ##############################
                    # Skip SIS bright Earth files
                    ##############################
                    continue
                    rm -f $clean
                    ;;
                esac

                event=$($UTIL/generate_filename brightearth \
                                                $inst $index $mode $bitrate)
                    ;;
            esac

            $UTIL/log_entry "Extracting $event"
            rm -f $event
            rm -f event.tmp

            #######################
            # xselect command file
            #######################
            rm -f $command
            echo "earthevents"                                 >>$command
            echo "set mission ASCA"                            >>$command
            echo "set datadir ."                               >>$command
            echo "read events $unfiltered"                     >>$command
            echo "select mkf \"FOV==$fov\" " \
                 "data_dir=. "        >>$command
            echo "extract events"                              >>$command
            echo "save events outfile=event.tmp use_events=no" >>$command
            echo "exit save_session=no"                        >>$command

            ##############
            # Run xselect
            ##############
            $FTOOL/xselect @${command} </dev/null > stdout.log 2> stderr.log
            $UTIL/xselect_test $? $0 stdout.log stderr.log
            rm -f $command

            ######################################
            # Delete the file if nothing came out
            ######################################
            if [ -r "event.tmp" ]; then
                nevents=$($UTIL/read_fits_keyword event.tmp[0] NEVENTS)
                if [ $nevents -eq 0 ]; then
                    $UTIL/log_entry \
                      "Deleting $event since it contains $nevents events"
                    rm -f event.tmp
                    continue
                fi
            else
                ###################################
                # No event file produced, skipping
                ###################################
                $UTIL/log_entry "$event not created"
                rm -f event.tmp
                continue
            fi

            #######################
            # Clean the event file
            #######################
            rm -f $event

            case "$inst" in
            s?)
                #######################################
                # Clean the hot pixels with iterate=no
                #######################################
                $UTIL/log_entry "Cleaning hot pixels from $unfiltered"

                $UTIL/setup_parfile $FTOOL/cleansis.par \
                                                        datafile=event.tmp \
                                                        outfile=$event  \
                                                        cellsize=5      \
                                                        logprob=-5.24   \
                                                        bthresh=3       \
                                                        phamin=0        \
                                                        phamax=4095     \
                                                        zeroedge=no     \
                                                        dirtysis=no     \
                                                        rawxcol=RAWX    \
                                                        rawycol=RAWY    \
                                                        chipcol=CCDID   \
                                                        timecol=TIME    \
                                                        detxcol=DETX    \
                                                        detycol=DETY    \
                                                        skyxcol=X       \
                                                        skyycol=Y       \
                                                        phacol=PI       \
                                                        iterate=no      \
                                                        verbose=yes

                $FTOOL/cleansis >stdout.log 2>stderr.log
                $UTIL/ftool_test cleansis $? $0 2 stdout.log stderr.log

                rm -f event.tmp

                if [ ! -a $event ]; then
                    $UTIL/log_entry "No cleaned file, skipping $event"
                    continue
                fi

                nevents=$($UTIL/read_fits_keyword $event[0] NEVENTS)
                if [ $nevents -eq 0 ]; then
                    $UTIL/log_entry "Cleaning removed all pixels"
                    rm -f $event
                    continue
                fi
                ;;

            g?)
                ########################
                # Don't clean GIS files
                ########################
                mv event.tmp $event
                ;;
            esac

        done # loop over bright and dark

    done # loop over unfiltered files

done # loop over instrument


exit 0