Subs::SwiftSub (version 0.0)


package Subs::SwiftSub;

##############################################################################
# 
# HISTORY: 
# HISTORY: $Log: SwiftSub.pm,v $
# HISTORY: Revision 1.8  2008/06/02 13:32:58  apsop
# HISTORY: Trap the case where total exposure is zero.
# HISTORY:
# HISTORY: Revision 1.7  2008/05/16 14:31:06  apsop
# HISTORY: Add good_attitude_fraction method.
# HISTORY:
# HISTORY: Revision 1.6  2007/04/01 19:13:19  apsop
# HISTORY: Calculate version numbers and store in job par.
# HISTORY:
# HISTORY: Revision 1.5  2006/04/28 18:41:46  apsop
# HISTORY: Fix bug in testing for presence of queue file.
# HISTORY:
# HISTORY: Revision 1.4  2005/11/08 19:22:28  apsop
# HISTORY: Populate the TIMELIST and DATALIST hashes. Used to be an SWCheckInput.
# HISTORY:
# HISTORY: Revision 1.3  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.2  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::Sub;
use Util::AttTool;
use Util::GTIfile;

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

sub BEGIN {
  my $procpar  = Subs::Sub->procpar();
  my $filename = Subs::Sub->filename();
  my $log      = Subs::Sub->log();
  my $jobpar   = Subs::Sub->jobpar();

  Util::AttTool->dirs($procpar->read('acs2fits'), $procpar->read('headas').'/lib');

  ###############################################
  # Put info on data already processed into hash
  ###############################################
  my @data = split(' ', $jobpar->read('datalist'));
  @{$jobpar->{DATALIST}}{@data} = (1) x @data;

  #####################################################
  # Hash about relative times, ie time since burst and
  # final processing
  #####################################################
  $jobpar->{TIMELIST} = {};
  foreach (glob('day_*.flag')){
    $jobpar->{TIMELIST}->{ (/(day_\w+)\.flag/)[0] } = 1;
  }

  if(-f 'final_for_archive.lock' || $jobpar->read('reprocess') eq 'yes'){
    $jobpar->{TIMELIST}->{final} = 1;
    $jobpar->set({finalproc => 'yes'});
  }

  ####################################
  # Get software versions
  ####################################
  my $fversion;
  open VER, $procpar->read('ftools').'/syspfiles/ftools.par';
  while (<VER>){
    $fversion = $1 if /version,s,h,+"([^",]*)"/;
  }
  close VER;

  my $sversion = Util::Tool->new($procpar->read('headas') .'/bin', 'swiftversion')
                           ->run();
  ###############################################################################
  # Sometimes for an 'official' release they put '_S' and the end of the version
  ###############################################################################
  $sversion =~ s/_S$//;
  
  my $soft_version = 'Hea_' . $fversion . '_' . $sversion->stdout();
  chomp $soft_version;
  
  $jobpar->set({softver => $soft_version});

  #####################
  # Get caldb versions
  #####################
  my $caldb = $procpar->read('caldb');
  my @cal_vers;
  foreach my $inst qw(bat uvota xrt mis){
    my $fits = Util::FITSfile->new("$caldb/data/swift/$inst/caldb.indx");
    my $ckey = $fits->keyword('CALDBVER');
    $ckey = '?' unless $ckey;
    push @cal_vers, substr($inst, 0, 1) . $ckey;
  }
  $jobpar->set({caldbver => join('_', @cal_vers)});

  #####################################################
  # Get the earliest and latest times from the queue 
  # file
  #####################################################
  my $qname = $jobpar->read('sequence') . '.queue';
  if( -f $qname && open(QUEUE, "$qname") ){
    my (@starts, @stops);
    while(<QUEUE>){
      my @fields = split(' ');
      push @starts, $fields[2];
      push @stops,  $fields[3];
    }
    close QUEUE;
    $jobpar->{TIMELIST}->{start} = (sort {$a <=> $b} @starts)[0];
    $jobpar->{TIMELIST}->{stop}  = (sort {$b <=> $a} @stops)[0];
  }else{
    $log->error(1, "Unable to open $qname, $!");
  }
}


sub good_attitude_fraction {

  my $self = shift;
  my $start = shift;
  my $stop = shift;

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

  #######################################################################
  # Make GTI file from start and stop times and get total exposure time.
  #######################################################################
  my $attitude_gti = $filename->get('gti', 's', 'at');
  my $mgtime = Util::Ftool->new('mgtime')->params({merge => 'AND'});
  my $exp_gti_file = 'exp_gti_file.tmp';
  unlink $exp_gti_file;

  my $exp_gti_fits = Util::GTIfile->new($exp_gti_file, $start, $stop);
  my $total_exp_time = $exp_gti_fits->sum();
  ################################
  # Return unity if zero exposure
  ################################
  return 1.0 if $total_exp_time == 0;

  ########################################
  # Merge exposure GTI with attitude GTI.
  ########################################
  my $exp_good_attitude_file = 'exp_goodatt_file.tmp';
  unlink $exp_good_attitude_file;
  $mgtime->params({ingtis => "$exp_gti_file, $attitude_gti",
		   outgti => $exp_good_attitude_file})
    ->run();
      
  #######################################################################
  # Get percent good attitude, and write keyword to image extension.
  #######################################################################
  my $good_attitude_time = Util::GTIfile->new($exp_good_attitude_file)->sum();
  my $good_fraction =  $good_attitude_time/$total_exp_time;

  unlink ($exp_gti_file, $exp_good_attitude_file);

  return $good_fraction;
}