Subs::UVOTReport (version 0.0)


package Subs::UVOTReport;
##############################################################################
#
# DESCRIPTION: This creates an HTML report of UVOT exposures. It is largely
# DESCRIPTION: a translation of the FITS exposure report data.
#
# HISTORY: 
# HISTORY: $Log: UVOTReport.pm,v $
# HISTORY: Revision 1.14  2005/03/07 22:43:56  apsop
# HISTORY: Changes to deal with UNDEF values in the input fits files.
# HISTORY:
# HISTORY: Revision 1.13  2004/09/05 19:06:21  apsop
# HISTORY: Make sure that exposure param values always get initialize to zero.
# HISTORY:
# HISTORY: Revision 1.12  2004/08/15 18:41:52  apsop
# HISTORY: Fix problem with info being available only in certain modes
# HISTORY:
# HISTORY: Revision 1.11  2004/08/13 14:27:06  apsop
# HISTORY: Update Report modules to record exposure information in job.par file.
# HISTORY:
# HISTORY: Revision 1.10  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.9  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


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

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

sub new {
    my $proto=shift;
    my $file=$proto->filename()->get("report", "uvot");
    my $self=$proto->SUPER::new($file, "UVOT Exposure Report");

    $self->{DESCRIPTION}="Making UVOT 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();

    my $exps;
    for(my $ifilt=0; $ifilt < @$Subs::UvotNames::filterCodes; $ifilt++){
      for(my $imode=0; $imode < @$Subs::UvotNames::modeNames; $imode++){
	$$exps[$ifilt][$imode] = 0;
      }
    }

    #######################################
    # get the exposure report file
    #######################################
    my $file = $filename->get("hk", "u", "er", "*");
    if ( ! -f $file ) {
        $log->entry("No UVOT exposure report FITS file");
    }else{

      ########################################
      # read the FITS file
      ########################################
      my $fits = Util::FITSfile->new($file,1);

      my @expid   = $fits->cols("ACT_ST")->table();
      my @act_st_sub = $fits->cols("ACT_STsub")->table();
      my @act_end   = $fits->cols("ACT_END")->table();
      my @act_end_sub = $fits->cols("ACT_ENDsub")->table();

      my @time    = $fits->cols("TIME")->table();
      my @exp_des = $fits->cols("EXP_DES")->table();

      my @mode = $fits->cols("MODEID")->table();
      my @submode = $fits->cols("SUBMODE")->table();
      my @filter = $fits->cols("FILTER")->table();

      my @im_x0 = $fits->cols("IM_X0_ACT")->table();
      my @im_x1 = $fits->cols("IM_X1_ACT")->table();
      my @im_y0 = $fits->cols("IM_Y0_ACT")->table();
      my @im_y1 = $fits->cols("IM_Y1_ACT")->table();

      my @ev_x0 = $fits->cols("EV_X0_ACT")->table();
      my @ev_x1 = $fits->cols("EV_X1_ACT")->table();
      my @ev_y0 = $fits->cols("EV_Y0_ACT")->table();
      my @ev_y1 = $fits->cols("EV_Y1_ACT")->table();

      my @bin_level = $fits->cols("BINLVL")->table();
      my @nevents   = $fits->cols("NEVENTS")->table();
      my @nframes   = $fits->cols("NFRAMES")->table();

      my @reason   = $fits->cols("REASON")->table();

      my $frame_duration = 552/50000.;

      #########################
      # write the HTML table 
      #########################
      $self->begin_table("Exp ID", 
			 "ICU Label", 
			 "Mode", "Submode", "Filter",
			 "Event Window", "Image Window", "Binning",
			 "Duration",
			 "Count Rate", 
			 "Data Loss",
			 "Completion");

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

        my $exp_des = 0;
	$exp_des = "0x".sprintf("%x", $exp_des[$i]) if $exp_des[$i];

        my $submode = 0;
	$submode = "0x".sprintf("%x", $submode[$i]) if $submode[$i];

	my ($event_window_x, $event_window_y, $image_window_x, $image_window_y) = ('n/a', 'n/a', 'n/a', 'n/a');

        if($mode[$i]==2 || $mode[$i]==4){
	  $event_window_x = $ev_x1[$i] - $ev_x0[$i] + 1;
	  $event_window_y = $ev_y1[$i] - $ev_y0[$i] + 1;
	}

        if($mode[$i]==3 || $mode[$i]==4){
	  $image_window_x = $im_x1[$i] - $im_x0[$i] + 1;
	  $image_window_y = $im_y1[$i] - $im_y0[$i] + 1;
	}

        my $count_rate = $nevents[$i]/($nframes[$i]*$frame_duration);
        $count_rate = sprintf("%.1f", $count_rate);

	my $loss = 0;
	my $exposure = 'INDEF';
	my $frames_acquired = 'INDEF';
	if( $act_end[$i] ne 'INDEF' &&
	    $act_end_sub[$i] ne 'INDEF' &&
	    $expid[$i] ne 'INDEF' &&
	    $act_st_sub[$i] ne 'INDEF' 
	  ){
	  $exposure = ($act_end[$i] + $act_end_sub[$i] * 20e-6 ) -
	              ($expid[$i] + $act_st_sub[$i] * 20e-6 );

	  $frames_acquired = int($exposure/$frame_duration + 0.5);

	  $loss = 100 * ($frames_acquired-$nframes[$i])/$frames_acquired
	    unless $frames_acquired==0;

	  $$exps[$filter[$i]][$mode[$i]] += $exposure;
	}

	my $filter_name = $$Subs::UvotNames::filterNames[$filter[$i]];
	my $reason_name = $$Subs::UvotNames::reasonNames[$reason[$i]];
	my $mode_name   = $$Subs::UvotNames::modeNames[$mode[$i]];

        $self->table_row($expid[$i], $exp_des, 
                         $mode_name ? $mode_name : 'Unknown', 
                         $submode, 
                         $filter_name ? $filter_name : 'Unknown',
                         "$event_window_x x $event_window_y",
                         "$image_window_x x $image_window_y",
                         $bin_level[$i],
                         $exposure eq 'INDEF' ? $exposure : sprintf("%.1f s",$exposure),
                         "$count_rate/s",
                         sprintf("%.1f%%",$loss),
                         $reason_name ? $reason_name : 'Unknown'
                         );

      }
      $self->end_table();
    }

    $self->event_exposures();

    for(my $ifilt=1; $ifilt<=10; $ifilt++){
      my $parm = $$Subs::UvotNames::filterParams{ $$Subs::UvotNames::filterCodes[$ifilt] };

      $jobpar->set({ 'uvot_image_'.$parm => sprintf('%.03f', $$exps[$ifilt][3]) });
      $jobpar->set({ 'uvot_unf_'.$parm => sprintf('%.03f', $$exps[$ifilt][2] + $$exps[$ifilt][4]) });
    }
    
} # end of body method

sub event_exposures {
    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);

    foreach my $filter (keys %$Subs::UvotNames::filterParams){
      my $exposure = 0;
      foreach my $rawFile ($filename->get('event', 'uvot', "$filter*", '*')) {
	my $rawFits = Util::FITSfile->new($rawFile);
	my $nhdus = $rawFits->nhdus();
	for(my $hdu=2; $hdu<$nhdus; $hdu++){
	  $rawFits->ext($hdu);
	  my $name = $rawFits->keyword('EXTNAME');
	  $exposure += $gtisum->command_line($rawFile."\[$name\]")->run()->stdout()
	    if $name=~/^GTI/;
	}
      }
      $jobpar->set({'uvot_evt_' . $$Subs::UvotNames::filterParams{$filter} => sprintf('%.03f', $exposure)});
    }
}