Subs::XrtEvents (version 0.0)


package Subs::XrtEvents;
##############################################################################
#
# DESCRIPTION: This subroutine runs the XRT tools delivered in build 1
#
# HISTORY: 
# HISTORY: $Log: XrtEvents.pm,v $
# HISTORY: Revision 1.19  2005/04/06 15:46:19  apsop
# HISTORY: Change to using CALDB for cal parameters.
# HISTORY:
# HISTORY: Revision 1.18  2005/02/25 20:29:40  apsop
# HISTORY: Explicitly set method parameter in xrtpdcorr to SG.
# HISTORY:
# HISTORY: Revision 1.17  2004/11/16 15:32:09  apsop
# HISTORY: Changes to fix handling of window timing events.
# HISTORY:
# HISTORY: Revision 1.16  2004/11/09 23:54:56  apsop
# HISTORY: Rework order of tasks to make proper split between level 1 and level1a.
# HISTORY:
# HISTORY: Revision 1.15  2004/11/02 21:25:16  apsop
# HISTORY: Rearrange calling sequence and production of level 1a files.
# HISTORY:
# HISTORY: Revision 1.14  2004/08/30 13:18:37  apsop
# HISTORY: Changes for build 9.  NONE does not work, rearrangement for bias info.
# HISTORY:
# HISTORY: Revision 1.13  2004/07/23 16:04:13  apsop
# HISTORY: Fix stupid bug in _fetching_ attitude file. Should be _getting_.
# HISTORY:
# HISTORY: Revision 1.12  2004/07/19 16:04:55  apsop
# HISTORY: Fix bug in using fetch_cal() instead of fetch() to get attitude file.
# HISTORY:
# HISTORY: Revision 1.11  2004/07/11 20:43:51  apsop
# HISTORY: Turn chatter down on timetag and hkproc in order to reduce output to reasonable level.
# HISTORY:
# HISTORY: Revision 1.10  2004/06/29 14:35:33  apsop
# HISTORY: Substantial changes to support build 8.
# HISTORY:
# HISTORY: Revision 1.9  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.8  2004/05/04 16:31:47  dah
# HISTORY: Test for presence of header file before processing it.
# HISTORY:
# HISTORY: Revision 1.7  2004/04/28 13:56:43  dah
# HISTORY: Save reconstructed event lists in seperate files.  Change order in which xrtpdcorr is called.  Fix bug in calling xrthkproc.
# HISTORY:
# HISTORY: Revision 1.6  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::Sub;

@ISA = ("Subs::Sub");
use strict;

sub new {
    my $proto=shift;
    my $self=$proto->SUPER::new();

    $self->{DESCRIPTION}="Running XRT tasks for event list processing";

    return $self;
}

##################
# METHODS:
##################

sub body {
    my $self=shift;

    my $log     =$self->log();
    my $filename=$self->filename();
    my $procpar =$self->procpar();
    my $jobpar  =$self->jobpar();

    ####################################
    # Get lists of event types
    ####################################
    my @phot = $filename->get('unfiltered', 'x', 'pc*', '*');
    my @diod = ($filename->get('unfiltered', 'x', 'lr*', '*'), 
		$filename->get('unfiltered', 'x', 'pu*', '*'));
    my @wind = $filename->get('unfiltered', 'x', 'wt*', '*');

    my (@wind1a, @diod1a);

    #####################################
    # get bad pixel table and teldef file
    #####################################
    my $attitude = $filename->get('attitude', 's');

    #############################
    # xrthkproc
    #############################
    my $head = $filename->get('hk', 'x', 'hd', '*');

    if(-f $head){
      #############################################
      # First we have to fix up the hk header file
      #############################################
      my $xrthkproc = Util::HEAdas->new('xrthkproc');

      $log->entry("Running ".$xrthkproc->name()." on $head");
      $xrthkproc->params({hdfile   => $head,
			  outfile  => 'hkfile.tmp',
			  attfile  => $attitude,
			  srcra    => $jobpar->read('burst_ra'),
			  srcdec   => $jobpar->read('burst_dec'),
			  ranom    => $jobpar->read('ra'),
			  decnom   => $jobpar->read('dec'),
			  teldef   => 'CALDB',
			  chatter  => 3,
			  clobber  => 'yes',
			  history  => 'yes'})
	         ->run();

      unless( $xrthkproc->had_error() ){
	unlink $head;
	rename 'hkfile.tmp', $head;
      }
    }

    ##############################
    # xrtflagpix
    ##############################
    my $flagpix = Util::HEAdas->new('xrtflagpix');
    $flagpix->params({outfile => 'flagfile.tmp',
		      hdfile     => $head,
		      bpfile => 'CALDB',
		      bptable => 'CALDB',
		      srcfile => 'CALDB',
                      userbpfile  => 'NONE',
		      outbpfile   => 'NONE',
                      overstatus  => 'yes',
                      chatter     => 5,
                      clobber     => 'yes',
                      history     => 'yes'});

    my $unf;
    foreach $unf (@phot, @wind){
        $log->entry("Running ".$flagpix->name()." on $unf");

        $flagpix->params({infile     => $unf})
                ->run();

	unless( $flagpix->had_error() ){
	  unlink $unf;
	  rename 'flagfile.tmp', $unf;
	}
    }

    #############################
    # xrttimetag
    #############################
    my $timetag = Util::HEAdas->new('xrttimetag');
    $timetag->params({outfile => 'timefile.tmp',
		      hdfile     => $head,
		      usehkkey   => -f $head ? 'yes' : 'no',
		      attfile  => $attitude,
		      srcra    => $jobpar->read('burst_ra'),
		      srcdec   => $jobpar->read('burst_dec'),
		      ranom    => $jobpar->read('ra'),
		      decnom   => $jobpar->read('dec'),
		      teldef   => 'CALDB',
		      chatter    => 3,
                      clobber    => 'yes',
                      history    => 'yes'});

    foreach $unf (@wind){
        $log->entry("Running ".$timetag->name()." on $unf");

        $timetag->params({infile => $unf})
                ->run();

	unless( $timetag->had_error() ){
	  unlink $unf;
	  rename 'timefile.tmp', $unf;	

	  push @wind1a, $filename->corresponding('unfiltered', 'reconst', $unf);
	  my $fits = Util::FITSfile->new($unf, 'EVENTS');
	  $fits->copy($wind1a[-1]);
	}
    }

    foreach $unf (@diod){
        $log->entry("Running ".$timetag->name()." on $unf");

        $timetag->params({infile => $unf})
                ->run();

	unless( $timetag->had_error() ){
	  unlink $unf;
	  rename 'timefile.tmp', $unf;	

	  push @diod1a, $filename->corresponding('unfiltered', 'reconst', $unf);
	  my $fits = Util::FITSfile->new($unf, 'EVENTS', '[gtifilter()]');
	  $fits->copy($diod1a[-1]);
	}
    }

    ###############################
    # xrtpdcorr
    ###############################

    my $pdcorr = Util::HEAdas->new('xrtpdcorr');
    $pdcorr->params({outfile  => 'pdfile.tmp',
		     biasfile => 'CALDB',
		     hdfile   => $head,
		     method   => 'SG',
		     bias     => -1,
		     history  => 'yes',
		     clobber  => 'yes',
		     chatter  => 5});

    foreach $unf (@diod1a) {
        $log->entry("Running ".$pdcorr->name()." on $unf");

	my ($inst, $mode, $index) = $filename->parse($unf, 'unfiltered');

        $pdcorr->params({infile => $unf})
	      ->run();

	unless( $pdcorr->had_error() ){
	  unlink $unf;
	  rename 'pdfile.tmp', $unf;
	}
    }

    #############################
    # xrtevtrec
    #############################

    my $evtrec = Util::HEAdas->new('xrtevtrec');
    $evtrec->params({outfile => 'recfile.tmp',
		     gradefile => 'CALDB',
		     addcol => 'no',
		     delnull => 'no',
		     event => 40,
		     split => 40,
		     chatter => 5,
		     clobber => 'yes',
		     history => 'yes'});

    foreach $unf (@diod1a, @wind1a){
        $log->entry("Running ".$evtrec->name()." on $unf");
        $evtrec->params({infile => $unf})
	       ->run();

	unless( $evtrec->had_error() ){
	  unlink $unf;
	  rename 'recfile.tmp', $unf;
	}
    }

    ###############################
    # xrtpcgrade
    ###############################
    my $ph2br = Util::HEAdas->new('xrtpcgrade');
    $ph2br->params({outfile => 'gradefile.tmp',
		    split   => 40,
		    gradefile => 'CALDB',
		    ascagrade => 'no',
                    history => 'yes',
                    clobber => 'yes',
                    chatter => 5    });

    foreach $unf (@phot) {
        $log->entry("Running ".$ph2br->name()." on $unf");
      
        $ph2br->params({infile => $unf})
	      ->run();

	unless( $ph2br->had_error() ){
	  unlink $unf;
	  rename 'gradefile.tmp', $unf;
	}
    }

    ###############################
    # xrthotpix
    ###############################
    my $hotpix = Util::HEAdas->new('xrthotpix');
    $hotpix->params({outfile => 'pixfile.tmp',
		     outbpfile => 'badfile.tmp',
		     history => 'yes',
		     clobber => 'yes',
		     chatter => 5});

    foreach $unf (@phot) {
        $log->entry("Running ".$hotpix->name()." on $unf");

        $hotpix->params({infile => $unf})
	       ->run();

	unless( $hotpix->had_error() ){
	  unlink $unf;
	  unlink 'badfile.tmp';
	  rename 'pixfile.tmp', $unf;	
	}
    }

    ####################################
    # set up xrtcalcpi
    ####################################
    my $pha2pi = Util::HEAdas->new('xrtcalcpi');
    $pha2pi->params({outfile => 'pifile.tmp',
                     gainnom  => -99.0, # uses default from cal file
                     offset   => 0.0,
                     randomflag => 'yes',
                     seed     => -1457,
                     chatter  => 2,
                     clobber  => 'yes',
                     history  => 'yes' });

    foreach $unf (@phot) {
        my ($inst, $mode, $index) = $filename->parse($unf, 'unfiltered');

        $log->entry("Running ".$pha2pi->name()." on $unf");

        $pha2pi->params({infile  => $unf,
			 gainfile => 'CALDB' 
			})
               ->run();

	unless( $pha2pi->had_error() ){
	  unlink $unf;
	  rename 'pifile.tmp', $unf;	
	}
    }

    foreach $unf (@wind1a, @diod1a) {
        my ($inst, $mode, $index) = $filename->parse($unf, 'reconst');

        $log->entry("Running ".$pha2pi->name()." on $unf");

        $pha2pi->params({infile  => $unf,
			 gainfile => 'CALDB' 
			})
               ->run();

	unless( $pha2pi->had_error() ){
	  unlink $unf;
	  rename 'pifile.tmp', $unf;
	}
    }

} # end of body method