pixl_criteria (version 2.0)

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

##############################################################################
#
# SYNTAX: pixl_criteria eventfile
#
# DESCRIPTION: This script generates the screening criteria for the
# DESCRIPTION: SIS Sn_PIXLm parameter for a given event file.
# DESCRIPTION: It does this by first selecting all the records
# DESCRIPTION: in the filter file which are included in the GTIs of the
# DESCRIPTION: event file.
# DESCRIPTION: It then further selects filter file records with the following
# DESCRIPTION: screening criteria:
# DESCRIPTION: <PRE>
# DESCRIPTION: SAA==0 &&
# DESCRIPTION: COR>6 &&
# DESCRIPTION: FOV==0 &&
# DESCRIPTION: BR_EARTH>20 &&
# DESCRIPTION: ELV>10 &&
# DESCRIPTION: ACS==0 &&
# DESCRIPTION: ANG_DIST<.01 &&
# DESCRIPTION: (T_DY_NT<0 || T_DY_NT)>64 &&
# DESCRIPTION: (T_SAA<0 || T_SAA>64) &&
# DESCRIPTION: Sn_SATFm==0 &&
# DESCRIPTION: Sn_PIXLm>0
# DESCRIPTION: </PRE>
# DESCRIPTION: The script then calculates the mean and standard deviation of
# DESCRIPTION: the Sn_PIXLm values in the selected records and sets
# DESCRIPTION: the criteria to be within
# DESCRIPTION: %pixl_nsigma sigma of the mean and greater than zero.
# DESCRIPTION: This is done for all active chips.
# DESCRIPTION: For a given chip, more than %pixl_minrows rows must survive the
# DESCRIPTION: screening process otherwise no screening criterion is generated
# DESCRIPTION: for that chip except, Sn_PIXLm > 0.
#
# VERSION: 2.0
#
# HISTORY: 0.0 -> 1.0 4/24/988
# HISTORY: Now accept T_SAA<0 and T_DY_NT<0 when selecting filter file rows.
# HISTORY: 
# HISTORY: 1.0 -> 1.1 1998-08-31
# HISTORY: Added message specifying the column on which fstatistic is
# HISTORY: being run.
# HISTORY: 
# HISTORY: 1.1 -> 2.0 1999-05-07
# HISTORY: Converted from ksh to perl
#
# CALLS: $UTIL/read_parfile
# CALLS: $UTIL/read_fits_keyword
# CALLS: $UTIL/stool_test
# CALLS: $UTIL/setup_parfile
# CALLS: $UTIL/ftool_test
# CALLS: $STOOL/gtiselect
# CALLS: $FTOOL/fselect
#
##############################################################################

$event = $ARGV[0];

$UTIL = $ENV{"UTIL"};
$FTOOL = $ENV{"FTOOL"};
$STOOL = $ENV{"STOOL"};
$PARFILE= $ENV{"PARFILE"};

chomp($nsigma =   `$UTIL/read_parfile $PARFILE pixl_nsigma`);
chomp($min_rows = `$UTIL/read_parfile $PARFILE pixl_minrows`);

###########################################################
# Determine the name of the filter file from the telemetry
# file name listed in the event file header.
###########################################################
chomp($telem = `$UTIL/read_fits_keyword ${event}+0 TLM_FILE`);

($mkf = $telem) =~ s/\./_/g;
$mkf = "${mkf}.mkf";

###########################################################################
# Select the filter file rows which fall within the GTIs of the event file
###########################################################################
$selected = "selected$$.tmp";
unlink $selected;
`$STOOL/gtiselect $selected $mkf $event 2 >stdout$$.log 2>stderr$$.log`;
`$UTIL/stool_test gtiselect $? $0 2 stdout$$.log stderr$$.log`;

######################
# Get instrument name
######################
chomp($inst = `$UTIL/read_fits_keyword $event+0 INSTRUME`);
$inst =~ s/SIS(.*)$/$1/;
$inst = "S${inst}";

############################
# Get a list of active CCDs
############################
chomp($u_ccdlist = `$UTIL/read_fits_keyword $event+0 ${inst}CCDLST`);
@ccdlist=();
$last=-1;
foreach $_ (sort(split(/ */, $u_ccdlist))) {
    if($_ != $last ) {
         @ccdlist=(@ccdlist, $_);
    }
    $last=$_;

}

$last_chip = $ccdlist[$#ccdlist];


########################
# Loop over active CCDs
########################
$criteria = "criteria$$.tmp";

print "\(\n";
foreach $chip (@ccdlist) {
    $colname = "${inst}_PIXL${chip}";

    #########################
    # Generate criteria file
    #########################
    unlink $criteria;
    open(CRITERIA, ">>$criteria");
    print CRITERIA "SAA==0 &&\n";
    print CRITERIA "COR>6 &&\n";
    print CRITERIA "FOV==0 &&\n";
    print CRITERIA "BR_EARTH>20 &&\n";
    print CRITERIA "ELV>10 &&\n";
    print CRITERIA "ACS==0 &&\n";
    print CRITERIA "ANG_DIST<.01 &&\n";
    print CRITERIA "(T_DY_NT<0 ||T_DY_NT>64) &&\n";
    print CRITERIA "(T_SAA<0 || T_SAA>64) &&\n";
    print CRITERIA "$colname>0 && ${inst}_SATF${chip}==0\n";
    close(CRITERIA);


    ####################################################
    # Apply additional selection criteria for this chip
    ####################################################
    $thischip = "thischip$$.tmp";
    unlink $thischip;
    `$UTIL/setup_parfile $FTOOL/fselect.par infile=$selected   \\
                                            outfile=$thischip  \\
                                            expr="\@$criteria" \\
                                            histkw=no          \\
                                            copyall=no         \\
                                            keycopy=no         \\
                                            clobber=yes`;
    `$FTOOL/fselect >stdout.log 2>stderr.log`;
    `$UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log`;

    ################################################
    # Check if there is a sufficient number of rows
    ################################################
    chomp($naxis2 = `$UTIL/read_fits_keyword $thischip+1 NAXIS2`);

    if ( $naxis2 < $min_rows ) {
        if ( $chip eq "$last_chip" ) {
            print "($colname>0)  )";
        }
        else {
            print "($colname>0)&&";
        }

        next;
    }

    ##############################################################
    # Calculate mean and standard deviation
    ##############################################################
    `$UTIL/log_entry "Calculating statistics for $colname"`;
    `$UTIL/setup_parfile $FTOOL/fstatistic.par infile=$thischip \\
                                               colname=$colname \\
                                               rows=-           \\
                                               outfile=STDOUT`;

    `$FTOOL/fstatistic >stdout.log 2>stderr.log`;
    $code = $?;

    chomp($mean =  `$UTIL/read_parfile fstatistic.par mean`);
    chomp($sigma = `$UTIL/read_parfile fstatistic.par sigma`);

    `$UTIL/ftool_test fstatistic $code $0 2 stdout.log stderr.log`;

    $hi = int(($mean + $nsigma*$sigma)*10)/10;
    $lo = int(($mean - $nsigma*$sigma)*10)/10;
    if ( $lo < 0. ) {
        $lo = 0.0;
    }

    if ( "$chip" eq "$last_chip" ) {
        print "($colname\>$lo && $colname\<$hi )  )\n";
    }
    else {
        print "($colname\>$lo && $colname\<$hi )&&\n";
    }
}

unlink $criteria;
unlink $selected;
unlink $thischip;

exit 0;