Subs::XRTReport (version 0.0)


package Subs::XRTReport;
##############################################################################
#
# 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: XRTReport.pm,v $
# HISTORY: Revision 1.17  2006/04/26 20:45:59  apsop
# HISTORY: Allow for either INDEF or NULL values returned from FITSfile class.
# HISTORY:
# HISTORY: Revision 1.16  2006/01/04 18:40:02  apsop
# HISTORY: Remove attempt to combine exposures, as it was screwing up the image exposure values.
# HISTORY:
# HISTORY: Revision 1.15  2005/11/17 14:16:47  apsop
# HISTORY: Test for presence of on-target GTI when calculating exposure.
# HISTORY:
# HISTORY: Revision 1.14  2005/11/08 20:06:05  apsop
# HISTORY: <Previous comment bogus>Deal with undefined mode value.
# HISTORY:
# HISTORY: Revision 1.13  2005/11/08 19:22:28  apsop
# HISTORY: Populate the TIMELIST and DATALIST hashes. Used to be an SWCheckInput.
# HISTORY:
# HISTORY: Revision 1.12  2005/09/19 13:33:56  apsop
# HISTORY: Only count on-target time when calculating exposure for cleaned event lists.
# HISTORY:
# HISTORY: Revision 1.11  2005/03/09 16:44:49  apsop
# HISTORY: Fix bug in test for INDEF value.
# HISTORY:
# HISTORY: Revision 1.10  2005/03/07 22:43:56  apsop
# HISTORY: Changes to deal with UNDEF values in the input fits files.
# HISTORY:
# HISTORY: Revision 1.9  2004/10/12 16:24:02  apsop
# HISTORY: Calculate unfiltered exposures using summed GTIs
# HISTORY:
# HISTORY: Revision 1.8  2004/09/05 19:06:21  apsop
# HISTORY: Make sure that exposure param values always get initialize to zero.
# HISTORY:
# HISTORY: Revision 1.7  2004/09/01 14:34:57  apsop
# HISTORY: Split up accumulation of lr and pu exposure times.
# HISTORY:
# HISTORY: Revision 1.6  2004/08/13 14:27:06  apsop
# HISTORY: Update Report modules to record exposure information in job.par file.
# 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", "xrt");
    my $seq = $proto->jobpar()->read('sequence') .".". $proto->jobpar()->read('seqprocnum');
    my $self=$proto->SUPER::new($file, "XRT Exposure Report for $seq");

    $self->{DESCRIPTION}="Making XRT HTML Exposure Report";

    return $self;
}

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


##############################################################################
#
##############################################################################
sub mode_name {

    my $self = shift;
    my $num = shift;

    if( $num && $num !~ /(INDEF|NULL)/ ){
      if(   $num ==  1) { return "Null"; }
      elsif($num ==  2) { return "Short Image"; }
      elsif($num ==  3) { return "Long Image"; }
      elsif($num ==  4) { return "Piled Up Photodiode"; }
      elsif($num ==  5) { return "Low Rate Photodiode"; }
      elsif($num ==  6) { return "Windowed Timing"; }
      elsif($num ==  7) { return "PhotonCounting"; }
      elsif($num ==  8) { return "Raw Data"; }
      elsif($num ==  9) { return "Bias Map"; }
      elsif($num == 10) { return "Stop"; }
    }

    return "Unknown";
}


##############################################################################
#
##############################################################################
sub data_key {
    my $self = shift;
    my $key =  join "|", (@_);
    $key =~ s/ //g;
    return $key;
    

}


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

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

    my $gtisum=Util::Stool->new("compute_exposure")
                          ->verbose(0);

    #######################################
    # get the exposure report file
    #######################################
    my ($image_expo, $lr_expo, $pu_expo, $pc_expo, $wt_expo) = (0, 0, 0, 0, 0);
    my ($pu_evt_expo, $lr_evt_expo, $pc_evt_expo, $wt_evt_expo) = (0, 0, 0, 0);

    my $head_name = $filename->get("hk", "x", "hd", "*");
    if ( ! -f $head_name ) {
        $log->entry("No XRT exposure header FITS file $head_name");
    }else{
      ########################################
      # read the FITS file
      ########################################
      my $head = Util::FITSfile->new($head_name,1);
      if ( $head->nhdus() <= 1 ){
        $log->entry("XRT exposure header FITS file $head_name has no extensions");
      }

      my @start = $head->cols(   "TIME")->table();
      my @stop  = $head->cols("ENDTIME")->table();
      my @mode  = $head->cols("XRTMode")->table();

      #########################
      # write the HTML table 
      #########################
      $self->begin_table("Start Time", 
			 "duration", 
			 "Mode");

      my $nrows = $head->nrows();

      for(my $i=0; $i<$nrows; $i++) {

        my $old_data = $self->data_key($mode[$i]);
        my $start = $start[$i];


#        while($i<$nrows && $self->data_key($mode[$i]) eq $old_data) { $i++; }
#        $i--;

        my $duration = $stop[$i];

	if( $duration !~ /(INDEF|NULL)/  || $start !~ /(INDEF|NULL)/ ){
	  $duration = 0;
	}else{
	  $duration -= $start;
	}

        $self->table_row( $start !~ /(INDEF|NULL)/  ? $start : sprintf("%.14g", $start),
			  sprintf("%.1f s", $duration ),
			  $self->mode_name($mode[$i])
			);

	$image_expo += $duration if($mode[$i] && $mode[$i] !~ /(INDEF|NULL)/ && 
				    ($mode[$i]==2 || $mode[$i]==3));
      }

      $self->end_table();

      my $tmpgti = 'otgti.tmp';
      unlink $tmpgti;

      my $ontarget = $filename->get('gti', 's', 'ot', 0);
      my $merge = Util::Ftool->new('mgtime')
	                     ->params({merge => 'AND',
			               outgti => $tmpgti});

      foreach my $file ($filename->get("unfiltered", "xrt", "*", "*")) {
	my $mode = ($filename->parse($file, 'unfiltered'))[1];

	if( -f $ontarget ){
	  $merge->params({ingtis => "$ontarget $file\[GTI\]"})
	        ->run();
	  $gtisum->command_line("$tmpgti\[STDGTI\]");
	}else{

	  $gtisum->command_line("$file\[GTI\]");
	}

	my $expo = $gtisum->run()->stdout();
	unlink $tmpgti if -f $tmpgti;

	$pu_expo += $expo if $mode=~/^pu/;
	$lr_expo += $expo if $mode=~/^lr/;
	$pc_expo += $expo if $mode=~/^pc/;
	$wt_expo += $expo if $mode=~/^wt/;
      }


      foreach my $file ($filename->get("event", "xrt", "*", "*")) {
	my $mode = ($filename->parse($file, 'event'))[1];

	if( -f $ontarget ){
	  $merge->params({ingtis => "$ontarget $file\[GTI\]"})
	        ->run();
	  $gtisum->command_line("$tmpgti\[STDGTI\]");
	}else{

	  $gtisum->command_line("$file\[GTI\]");
	}

	my $expo = $gtisum->run()->stdout();
	unlink $tmpgti if -f $tmpgti;

	$pu_evt_expo += $expo if $mode=~/^pu/;
	$lr_evt_expo += $expo if $mode=~/^lr/;
	$pc_evt_expo += $expo if $mode=~/^pc/;
	$wt_evt_expo += $expo if $mode=~/^wt/;
      }
    }

    $jobpar->set({xrt_image => sprintf('%.03f', $image_expo),
		  xrt_unf_piledup => sprintf('%.03f', $pu_expo),
		  xrt_unf_lowrate => sprintf('%.03f', $lr_expo),
		  xrt_unf_windowed => sprintf('%.03f', $wt_expo),
		  xrt_unf_photon => sprintf('%.03f', $pc_expo),
		  xrt_evt_piledup => sprintf('%.03f', $pu_evt_expo),
		  xrt_evt_lowrate => sprintf('%.03f', $lr_evt_expo),
		  xrt_evt_windowed => sprintf('%.03f', $wt_evt_expo),
		  xrt_evt_photon => sprintf('%.03f', $pc_evt_expo) });


} # end of body method