earthevents (version 1.0)

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: The SIS files have the flickering pixels removed,
# DESCRIPTION: but the GIS files are NOT giscleaned and are not
# DESCRIPTION: region filtered.
#
# VERSION: 1.0
#
# HISTORY: 0.0 -> 1.0 5/12/97
# HISTORY: Now also skip an event file if it was not created by xselect.
#
##############################################################################
#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 SIS FAINT  mode files
          ####################################
          if [ "$mode" = "01"  ]; then
               continue
          fi



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

          case "$inst" in 
          s?) 
               ################################
               # special things for SIS files
               ################################

               ########################################
               # clean the hot pixels with iterate=no
               ########################################
               rm -f $clean

               $UTIL/log_entry "Cleaning hot pixels from $unfiltered"
 
               $UTIL/setup_parfile $FTOOLS/cleansis.par datafile=$unfiltered \
                                                        outfile=$clean  \
                                                        cellsize=5     \
                                                        logprob=-5.24 \
                                                        bthresh=3      \
                                                        phamin=0       \
                                                        phamax=095     \
                                                        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

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

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

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

               unfiltered=$clean

               ;;
          esac




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

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


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

               #########################################
               # 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=. mkf_def_expr=ft*.mkf"            >>$command
               echo "extract events"                             >>$command
               echo "save events outfile=$event use_events=no"   >>$command
               echo "exit save_session=no"                       >>$command

               ######################
               # run xselect
               ######################
               $FTOOLS/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" ]; then
                    nevents=$($UTIL/read_fits_keyword ${event}[0] NEVENTS )
                    if [ $nevents -eq 0 ]; then
                         $UTIL/log_entry \
                           "Deleting $event since it contains $nevents events"
                         rm -f $event
                         continue
                    fi
               else
                    ####################################
                    # no event file produced, skipping
                    ####################################
                    $UTIL/log_entry "$event not created"
                    rm -f $event
                    continue
               fi

               ##########################################################
               # copy HOT_PIXELS extension if it exists in original file
               # but not in the extracted file
               ##########################################################
               rm -f z.tmp
               $UTIL/setup_parfile $FTOOLS/fstruct.par infile=$unfiltered \
                                                       outfile=z.tmp
               $FTOOLS/fstruct >stdout.log 2>stderr.log
               $UTIL/ftool_test fstruct $? $0 2 stdout.log stderr.log

               ext=$(grep HOT_PIXELS z.tmp | awk '{print $1}' )
               rm -f z.tmp
               ##########################
               # debugging outbut
               #########################
               if [ -n "$DEBUG" ]; then
                    echo ${0##*/}: ext=$ext
               fi


               if [ -n "$ext" ]; then
                    ###################################
                    # there is a HOT_PIXELS extension. 
                    # Check if there's already one in 
                    # the extracted file
                    ###################################
                    rm -f zz.tmp
                    $UTIL/setup_parfile $FTOOLS/fstruct.par infile=$event \
                                                            outfile=zz.tmp
                    $FTOOLS/fstruct >stdout.log 2>stderr.log
                    $UTIL/ftool_test fstruct $? $0 2 stdout.log stderr.log
     
                    dum=$(grep HOT_PIXELS zz.tmp )
                    ##########################
                    # debugging outbut
                    #########################
                    if [ -n "$DEBUG" ]; then
                         echo ${0##*/}: dum=$dum
                    fi

                    if [ -z "$dum" ]; then
                         ##################################
                         # no HOT_PIXELS extension on the 
                         # extracted file, so append it.
                         ##################################
                         $UTIL/log_entry \
                         "Copying HOT_PIXELS from $unfiltered[${ext}] to $event"

                         $UTIL/setup_parfile $FTOOLS/fappend.par          \
                                             infile="$unfiltered[${ext}]" \
                                             outfile="$event"             \
                                             pkeywds=no                   \
                                             history=no

                         $FTOOLS/fappend >stdout.log 2>stderr.log
                         $UTIL/ftool_test fappend $? $0 2 stdout.log stderr.log
                    fi
                    rm -f zz.tmp
               fi
  

               rm -f $clean



          done # loop over bright and dark

     done # loop over unfiltered files

done # loop over instrument



exit 0