Subs::BATFinish (version 0.0)


package Subs::BATFinish;
##############################################################################
#
# DESCRIPTION: This creates an HTML report of XRT exposures. It is largely
# DESCRIPTION: a translation of the FITS head/tail HK files.
#
# HISTORY: 
# HISTORY: $Log: BATFinish.pm,v $
# HISTORY: Revision 1.4  2005/03/07 22:43:56  apsop
# HISTORY: Changes to deal with UNDEF values in the input fits files.
# HISTORY:
# HISTORY: Revision 1.3  2004/12/05 23:35:54  apsop
# HISTORY: Only select short event files for calculating the exposure.
# HISTORY:
# HISTORY: Revision 1.2  2004/11/19 21:46:48  apsop
# HISTORY: New version of xrt2fits.
# HISTORY:
# HISTORY: Revision 1.1  2004/10/29 20:34:32  apsop
# HISTORY: Rename module from BATReport, and add code to remove unneeded bat event files.
# HISTORY:
# HISTORY: Revision 1.7  2004/09/03 12:37:23  apsop
# HISTORY: Give mask tagged lc their own type and file class.  Use DURATION for html table, but EXPOSURE for parameters.
# HISTORY:
# HISTORY: Revision 1.6  2004/09/02 00:04:35  apsop
# HISTORY: Put in handling of rate files and mask tagged rate files.
# HISTORY:
# HISTORY: Revision 1.5  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.4  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::Sub;
use Subs::HTMLPage;

@ISA = ('Subs::HTMLPage');
use strict;

sub new {
    my $proto=shift;

    my $file=$proto->filename()->get('report', 'bat');
    my $self=$proto->SUPER::new($file, 'BAT Exposure Report');

    $self->{DESCRIPTION}='BAT cleanup and HTML Exposure Report';

    return $self;
}

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

##############################################################################
#
##############################################################################
sub body {
    my $self=shift;

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

    #################################################
    # Delete bat event files that we are not keeping 
    #################################################

    unlink grep /evsh(ps|sl|as)/, $filename->get('unfiltered', 'bat', 'evsh??', '*');
    unlink grep /evsh(ps|sl|as)/, $filename->get('event', 'bat', 'evsh??', '*');

    my $survey_expo=0.0;
    my $unf_expo=0.0;
    my $evt_expo=0.0;
    my $rate_expo=0.0;
    my $masktag_expo=0.0;
    
    my $gtisum=Util::Stool->new('compute_exposure')
                          ->verbose(0);

    my @rows=();
    ############################################
    # collect information about the event files
    ############################################
    foreach my $file ($filename->get('unfiltered', 'bat', 'evshsp', '*')) {

        my $fits=Util::FITSfile->new($file);
        my $tstart=$fits->keyword('TSTART');
        my $tstop =$fits->keyword('TSTOP');
        my $row={};
        $row->{START}=sprintf('%.06f',$tstart);
        $row->{DURATION}=$tstop-$tstart;
        $row->{MODE}='Event';
        
        push @rows, ($row);

        ##############################
        # calculate the exposure
        ##############################
        $unf_expo += $gtisum->command_line($file.'\[GTI\]')
                            ->run()
                            ->stdout();
    }

    ############################################
    # collect information about the dph files
    ############################################
    foreach my $file ($filename->get('rawdph', 'bat', '*', '*')) {

        my $fits=Util::FITSfile->new($file, 1);
        my $tstart=$fits->keyword('TSTART');
        my $tstop=$fits->keyword('TSTOP');
        my $row={};
        $row->{START}=sprintf('%.06f',$tstart);;
        $row->{DURATION}=$tstop-$tstart;
        $row->{MODE}='Survey';

        push @rows, ($row);

        $survey_expo += $fits->keyword('EXPOSURE');
    }

    ############################################
    # collect information about the rate files
    ############################################
    my ($tmin, $tmax) = (0, 0);
    foreach my $file ($filename->get('rawlc', 'bat', 'rt*', '*')) {
        my $fits=Util::FITSfile->new($file, 1);
        my $tstart=$fits->keyword('TSTART');
        my $tstop=$fits->keyword('TSTOP');
        my $row={};
        $row->{START}=sprintf('%.06f',$tstart);;
        $row->{DURATION}=$tstop-$tstart;
        $row->{MODE}=$fits->keyword('DATAMODE');

        push @rows, ($row);
    }

    my $rate_file = $filename->get('rawlc', 'bat', 'ms', 0);
    if( -f $rate_file ){
      my $fits=Util::FITSfile->new($rate_file, 1);
      $rate_expo = $fits->keyword('EXPOSURE');
    } 

    ###############################################
    # collect information about the mask tag files
    ###############################################
    my @mt_files = $filename->get('mtlc', 'bat', '', '*');
    my $mt_count = @mt_files;
    foreach my $file (@mt_files) {
        my $fits=Util::FITSfile->new($file, 1);
        my $tstart=$fits->keyword('TSTART');
        my $tstop =$fits->keyword('TSTOP');
        my $row={};
        $row->{START}=sprintf('%.06f',$tstart);
        $row->{DURATION}=$tstop-$tstart;
        $row->{MODE}='Mask Tagged Source ' . $fits->keyword('CATNUM');

        push @rows, ($row);

	$masktag_expo += $fits->keyword('EXPOSURE');
    }

    ###########################################
    # and now the filtered event data
    ###########################################
    foreach my $file ($filename->get('event', 'bat', '*', '*')) {

        $evt_expo += $gtisum->command_line($file.'\[GTI\]')
                            ->run()
                            ->stdout();
    }
    
    ################################################
    # record the total exposures
    ################################################
    $log->entry("Total survey exposure $survey_expo");
    $log->entry("Total unfiltered event exposure $unf_expo");
    $log->entry("Total   filtered event exposure $evt_expo");
    
    $jobpar->set({bat_survey => sprintf('%.03f', $survey_expo),
                  bat_evt    => sprintf('%.03f', $evt_expo),
                  bat_unf    => sprintf('%.03f', $unf_expo),
		  bat_rate   => sprintf('%.03f', $rate_expo),
		  bat_mtag   => sprintf('%.03f', $masktag_expo),
		  bat_pulse  => 0,
		  bat_n_mtag => $mt_count});

    ##############################
    # sort the table rows by time
    ##############################
    @rows = sort {$a->{START} <=> $b->{START} } @rows;

    #########################
    # write the HTML table 
    #########################
    $self->begin_table('Start Time',
                       'Duration (s)',
                       'Mode');
                       
    foreach my $row (@rows) {
    
        $self->table_row($row->{START}, $row->{DURATION}, $row->{MODE});
    }

    $self->end_table();


} # end of body method