screen (version 1.6)

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

###########################################################################
#
# 
# SYNTAX: screen
#
# BRIEFLY: Run xselect to screen out bad data.
#
# DESCRIPTION: This routine runs xselect on the unfiltered event files
# DESCRIPTION: to produce the filtered event files.
# DESCRIPTION: The following screening criteria are applied:
# DESCRIPTION: <h3>Filtering Criteria for Both Instruments:</h3>
# DESCRIPTION: <ul>
# DESCRIPTION: <li> SAA==0 (not in South Atlantic Anomally)
# DESCRIPTION: <li> ELV>elv (sufficiently far from limb of earth)
# DESCRIPTION: <li> COR>cor (Rigidity of earths magnetic field high enough
# DESCRIPTION:               to sheild the instruments from particles)
# DESCRIPTION: <li> ACS==0  (Attitude control in fine guidance mode )
# DESCRIPTION: <li> ANG_DIST>0 && ANG_DIST<ang_dist (Angular distance from
# DESCRIPTION:                                       the nominal pointing is
# DESCRIPTION:                                       below threshold) 
# DESCRIPTION: </ul>
# DESCRIPTION: <h3>Additional SIS Filtering Criteria:</h3>
# DESCRIPTION: <ul>
# DESCRIPTION: <li> FOV==0 (Telescope is pointing at the sky)
# DESCRIPTION: <li> Sn_SATFm != 1 (Telemetry is not saturated )
# DESCRIPTION: <li> T_DY_NT<0 || T_DY_NT> delayperccd per CCD (The time
# DESCRIPTION:                            since the last day-night transition 
# DESCRIPTION:                            above threshold)
# DESCRIPTION: <li> T_SAA<0 || T_SAA> delayperccd per CCD (The time
# DESCRIPTION:                            since the last exit from the 
# DESCRIPTION:                            South Atlantic Anomaly
# DESCRIPTION:                            above threshold)
# DESCRIPTION: <li> The data are cleaned with sisclean which removes hot and
# DESCRIPTION:      flickering pixels.
# DESCRIPTION: </ul>
# DESCRIPTION: <h3>Additional GIS Cleaning Criteria:</h3>
# DESCRIPTION: <ul>
# DESCRIPTION: <li> Gn_L1>0 (The L1 count rate is above zero )
# DESCRIPTION: <li> A region filter is applied to GIS data to filter
# DESCRIPTION:      out the noisy edges of the detectors and te calibration
# DESCRIPTION:      source.
# DESCRIPTION: <li> The data are cleaned with gisclean which applies a
# DESCRIPTION:      PI - RTI rejection criterion
# DESCRIPTION: </ul>
# DESCRIPTION: <p>
# DESCRIPTION: Dark earth and GIS bright earth observing periods are
# DESCRIPTION: also extracted for use as trend products.
#
# VERSION: 1.6
#
# HISTORY: 0.0 -> 1.0 8/20/96 
# HISTORY: Now skips FAST mode files
# HISTORY:
# HISTORY: 1.0 -> 1.1 9/11/96
# HISTORY: Now delete filtered event files with zero events
# HISTORY: 
# HISTORY: 1.1 -> 1.2 9/27/96
# HISTORY: left out a $UTIL when making the last change
# HISTORY: 9/30/96
# HISTORY: Also added a check to be sure the file existed before trying
# HISTORY: NEVENTS keyword, and specified primary extension to read 
# HISTORY: keyword from
# HISTORY: 
# HISTORY: 1.2 -> 1.3 11/25/96
# HISTORY: Now extract bright and dark earth event files and GIS calibration
# HISTORY: source spectra. Also moved the xselect clean commands to before the
# HISTORY: "select mkf" command. To accomodate the different filters.
# HISTORY: 
# HISTORY: 1.3 -> 1.4 12/23/96
# HISTORY: Now skips region filtering for MPC mode data
# HISTORY: 1/3/97 No longer extract SIS bright earth data
# HISTORY: 
# HISTORY: 1.4 -> 1.5 2/24/97
# HISTORY: Removed code which extracts calsource spectra since these are
# HISTORY: now generated in the calsource routine
# HISTORY: 
# HISTORY: 1.5 -> 1.6 4/16/97
# HISTORY: stopped skipping FAST mode files 
# HISTORY: took out bright and dark earth extractions and moved them to 
# HISTORY: a separate module
# HISTORY: Now use different GIS regions depending on whether the
# HISTORY: spread discriminator is ON or OFF
#
##############################################################################
#DEBUG=1

command=command.tmp
criteria=criteria.tmp

$UTIL/milestone "Cleaning and filtering the unfiltered event files"

for inst in s0 s1 g2 g3; do

     ##########################
     # criteria from par file
     ##########################
     br_earth=$(   $UTIL/read_parfile $PARFILE br_earth    )
     elv=$(        $UTIL/read_parfile $PARFILE elv         )
     cor=$(        $UTIL/read_parfile $PARFILE cor         )
     ang_dist=$(   $UTIL/read_parfile $PARFILE ang_dist    )
     delayperccd=$($UTIL/read_parfile $PARFILE delayperccd )

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



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

          filtered=$($UTIL/generate_filename event \
                                             $inst $index $mode $bitrate )

          rm -f $filtered

          $UTIL/log_entry "Filtering $unfiltered into $filtered"

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

    
          ####################################
          ####################################
          ##
          ##  create an XSELECT command file
          ##
          ####################################
          ####################################


          #########################################
          # start xselect and read unfiltered file
          #########################################
          rm -f $command
          echo "screen"                  >>$command
          echo "set mission ASCA"        >>$command
          echo "set datadir ."           >>$command
          echo "read events $unfiltered" >>$command
          
          ##############################################
          # Standard screening criteria and "cleaning" 
          ##############################################
          case "$inst" in 
          s?)
               ###############
               # SIS
               ###############
               rm -f $criteria
               echo "(SAA==0)"                                >> $criteria
               echo "&&(BR_EARTH>${br_earth})"                >> $criteria
               echo "&&(ELV>${elv})"                          >> $criteria
               echo "&&(COR>${cor})"                          >> $criteria
               echo "&&(ACS==0)"                              >> $criteria
               echo "&&(ANG_DIST>0)&& (ANG_DIST<${ang_dist})" >> $criteria
               echo "&&(FOV==0)"                                >> $criteria

               ccdlst=$( $UTIL/read_fits_keyword $unfiltered[0] \
                                                 S${inst#s}CCDLST )

               ccdmode=$($UTIL/read_fits_keyword $unfiltered[0] \
                                                 S${inst#s}CCDMOD )

               $UTIL/ccd_select.pl ${inst#s} "$ccdlst"       >> $criteria
       
               for ccd in $ccdlst ; do
                    echo "&&(  (S${inst#s}_SATF${ccd}<1)"    >> $criteria
                    echo "   ||(S${inst#s}_SATF${ccd}>1))"   >> $criteria
               done

               time=$(expr $ccdmode \* $delayperccd)

               echo "&&((T_DY_NT<0)||(T_DY_NT>${time}))" >>$criteria
               echo "&&((T_SAA  <0)||(T_SAA  >${time}))" >>$criteria

               if [ "$mode" != "03" ]; then
                    ###################################
                    # don't run sisclean in fast mode
                    ###################################
                    echo "sisclean saoimage=no sis_plot=no clean=2 cellsize=5" \
                    "log_prob=-5.24 bkg_thr=3 clean_phalow=0 clean_phahi=4095" \
                         >> $command
               fi

               ######################
               # debugging output
               ######################
               if [ -n "$DEBUG" ]; then
                    echo ${0##*/}: ccdlst=$ccdlst
                    echo ${0##*/}: ccdmode=$ccdmode
                    echo ${0##*/}: time=$time
               fi

               ;;

          g?)
               #################
               # GIS
               #################
               rm -f $criteria
               echo "(SAA==0)&&"                              >> $criteria
               echo "(BR_EARTH>${br_earth})&&"                >> $criteria
               echo "(ELV>${elv})&&"                          >> $criteria
               echo "(COR>${cor})&&"                          >> $criteria
               echo "(ACS==0)&&"                              >> $criteria
               echo "(ANG_DIST>0)&& (ANG_DIST<${ang_dist})&&" >> $criteria
               echo "(G${inst#g}_L1>0)"                       >> $criteria

               echo "gisclean"              >>$command

               ###########################################
               # info needed for selecting region filters
               ###########################################
               if [ "$mode" != "71" ]; then
                    ##################################
                    # no region filtering in MPC mode 
                    ##################################
                    dir=$($UTIL/read_parfile $PARFILE regiondir )
                    rawxbins=$($UTIL/read_fits_keyword $unfiltered RAWXBINS )
                    spread=$(  $UTIL/read_fits_keyword $unfiltered[0] S_DSCR )

                    ##############################
                    # usable field of view
                    ##############################
                    region=$($UTIL/read_parfile $PARFILE \
                                                ${inst}region$rawxbins$spread )
                    $UTIL/fetch_file $dir $region

                    ###########################
                    # debugging output 
                    ###########################
                    if [ -n "$DEBUG" ]; then
                         echo ${0##*/}: dir=$dir
                         echo ${0##*/}: rawxbins=$rawxbins
                         echo ${0##*/}: spread=$spread
                         echo ${0##*/}: region=$region
                    fi

                    echo "filter region $region" >>$command
               fi

               ;;

          *)
               #####################
               # unknown instrument
               #####################
               $UTIL/exception $0 1 "Unknown instrument $inst";;
          esac

          #############################
          # extract the filtered file
          #############################
          echo "select mkf \"@${criteria}\" data_dir=. mkf_def_expr=ft*.mkf" \
                                                              >>$command
          echo "extract events"                               >>$command
          echo "save events outfile=$filtered use_events=no" >>$command


          ######################
          # exit xselect
          ######################
          echo "exit save_session=no"                         >>$command

          ######################
          # debugging output
          ######################
          if [ -n "$DEBUG" ]; then
               echo ${0##*/}: filtered=$filtered
               echo ${0##*/}: dark=$dark
               echo ${0##*/}: bright=$bright
          fi

          $UTIL/log_entry "Using the following selection criteria:"
          $UTIL/file_to_log criteria.tmp


          ######################
          # run xselect
          ######################
          $FTOOLS/xselect @${command} </dev/null  > stdout.log 2> stderr.log

          $UTIL/xselect_test $? $0 stdout.log stderr.log
          rm -f $criteria
          rm -f $command

          if [ -a "$filtered" ]; then
               nevents=$($UTIL/read_fits_keyword ${filtered}[0] NEVENTS )
               if [ $nevents -eq 0 ]; then
                    $UTIL/log_entry \
                      "Deleting $filtered since it contains $nevents events"
                    rm -f $filtered
               fi
          fi

     done

done



exit 0