xselect_test (version 3.1)

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

##############################################################################
#
# SYNTAX: xselect_test code caller stdout stderr
#
# DESCRIPTION: This utility handles error checking and cleanup after
# DESCRIPTION: an xselect run.
#
# VERSION: 3.1
#
# HISTORY: 0.0 -> 1.0 8/23/96
# HISTORY: Now removes flookup.par .
# HISTORY:
# HISTORY: 1.0 -> 1.1 10/17/96
# HISTORY: Now removes grppha.par, rbnpha.par, and fixrev0pha.par .
# HISTORY:
# HISTORY: 1.1 -> 1.2 12/23/96
# HISTORY: Now removes saextrct.par (needed for MPC mode).
# HISTORY: Also, doesn't print stderr if all it contains is rm -f statements.
# HISTORY: 12/26/96 Now logs and removes *_xsel.run files. These are the
# HISTORY: actual script files which are left behind by xselect when
# HISTORY: modified to quit on non-zero status. Standard xselect removes
# HISTORY: these automatically before quitting.
# HISTORY: 1/7/97 Now removes "% completed" lines from the stdout.
# HISTORY:
# HISTORY: 1.2 ->1.3 1/22/97
# HISTORY: Commented out the code which reports the xsel_run files, since these
# HISTORY: produce lines which are too long for FITS wrapping.
# HISTORY:
# HISTORY: 1.3 -> 1.4 1/26/97
# HISTORY: Put back a rm -f *_xsel.run to compensate for the last change.
# HISTORY: 2/6/97 Now ignores code 1 and code 246. Also began handling stderr
# HISTORY: through the call to ftool_test after removing "rm -f" lines.
# HISTORY:
# HISTORY: 1.4 -> 1.5 4/28/97
# HISTORY: Now removes *_timefile.asc .
# HISTORY:
# HISTORY: 1.5 -> 1.6 5/15/97
# HISTORY: Started reporting errors for exit code 246.
# HISTORY: This is because the new extractor does not die when it
# HISTORY: extracts no events.
# HISTORY: Now deletes fselect.par .
# HISTORY:
# HISTORY: 1.6 -> 1.7 6/3/97
# HISTORY: Now reports xselect warning messages as errors.
# HISTORY:
# HISTORY: 1.7 -> 1.8 6/10/97
# HISTORY: Now explicitly checks for "no GTI" in stdout if exit code is 246.
# HISTORY: This is to accomodate an apparent change in xselect behavior
# HISTORY: for FTOOLS 4.0.
# HISTORY:
# HISTORY: 1.8 -> 1.9 7/17/97
# HISTORY: Now removes fmemsort.par .
# HISTORY: 
# HISTORY: 1.9 -> 2.0 4/8/98
# HISTORY: Made adjustments to the stderr filtering to accomodate FTOOLS 4.1
# HISTORY: Also, now remove "xselect.log" and "xselect.par" instead of
# HISTORY: "xsel.log" and "xsel13.par".
# HISTORY: Also remove fort.* which seems to be generated sometimes by the 
# HISTORY: new xselect version.
# HISTORY: 
# HISTORY: 2.0 -> 3.0 1999-05-07
# HISTORY: Converted from ksh to perl
# HISTORY: 
# HISTORY: 3.0 -> 3.1 1999-10-13
# HISTORY: now check the filtered stderr text instead of the original
# HISTORY: stderr file when deciding whether to ignore exit code=1 results.
#
# CALLS: $UTIL/exception
# CALLS: $UTIL/ftool_test
# CALLS: $UTIL/file_to_log
#
##############################################################################
#$DEBUG = 1;

($code, $caller, $stdout, $stderr) = @ARGV;

$UTIL = $ENV{"UTIL"};

$warnings = "warnings_$$.tmp";

###############################
# Remove unwanted output lines
###############################
unlink "zlog$$.tmp";
open STDOUTF, "<$stdout";
@stdout_text = <STDOUTF>;
@stdout_text = grep !/% completed/, @stdout_text;

open STDERRF, "<$stderr";
@stderr_text = <STDERRF>;
@stderr_text = grep !/rm -f/, @stderr_text;

###########################
# Extract warning messages
###########################
@warning_text = grep /WARNING:/, @stdout_text;
open WARNINGS, ">$warnings";
print WARNINGS @warning_text;

###########################
# Don't worry about code 1
###########################
if ( $code == 1 ) {
    if ( ! @stderr_text ) {
        $code = 0;
    }
}

#########################################
# Handle No GTIs case with exit code 246
#########################################
if ( $code == 246 ) {
    @thing = grep /The selection criteria produced no GTI/, @stdout_text;
    if ( @thing ) {
        ############################################################
        # Change this exit code to 0 since No GTIs error is trivial
        ############################################################
        $code = 0;
    }
}

open STDOUTF, ">$stdout";
open STDERRF, ">$stderr";
print STDOUTF @stdout_text;
print STDERRF @stderr_text;

`$UTIL/ftool_test xselect $code $caller 2 $stdout $stderr`;

##################
# Report warnings
##################
if ( -s "$warnings" ) {
    `$UTIL/exception $0 1 "xselect warning messages issued"`;
    `$UTIL/file_to_log $warnings`;
}

unlink $warnings;

#########################
# Clean up various files
#########################
unlink $stderr;

#unlink xsel.log;
#unlink xsel13.par;

unlink 'xselect.log';
unlink 'xselect.par';

unlink glob("fort.*");

unlink glob("*_display.def");
unlink glob("*_files.tmp");
unlink glob("*_obscat.tmp");
unlink glob("*_obslist.def");
unlink glob("*_*.xsl");

unlink 'cleansis.par';
unlink 'extractor.par';
unlink 'fappend.par';
unlink 'fdump.par';
unlink 'ffilecat.par';
unlink 'fhelp.par';
unlink 'maketime.par';
unlink 'mgtime.par';
unlink 'flookup.par';
unlink 'grppha.par';
unlink 'rbnpha.par';
unlink 'fixrev0pha.par';
unlink 'fselect.par';

unlink 'saextrct.par';
unlink 'fmemsort.par';

unlink glob("*_xsel.run");
unlink glob("*_timefile.asc");

exit 0