Subs::LocateBurst (version 0.0)


package Subs::LocateBurst;
##############################################################################
#
# DESCRIPTION: This subroutine determines the best available position for 
# DESCRIPTION: the burst.
#
# HISTORY: 
# HISTORY: $Log: LocateBurst.pm,v $
# HISTORY: Revision 1.16  2008/05/16 14:22:12  apsop
# HISTORY: Put ra, dec in names of all generic objects.
# HISTORY:
# HISTORY: Revision 1.15  2007/02/27 19:32:10  apsop
# HISTORY: Fix bug keeping burst_cat_src and burst_veto from being updated. Store trigger time in job parameter.
# HISTORY:
# HISTORY: Revision 1.14  2007/01/31 19:19:18  apsop
# HISTORY: Add ra,dec to generic Burst object name.
# HISTORY:
# HISTORY: Revision 1.13  2006/03/06 15:04:10  apsop
# HISTORY: Set new parameter  burst_pos_info.
# HISTORY:
# HISTORY: Revision 1.12  2005/06/01 19:57:07  apsop
# HISTORY: If bat does not think this is a burst rename Burst object to be Automatic Target.
# HISTORY:
# HISTORY: Revision 1.11  2004/11/29 16:33:09  apsop
# HISTORY: Add time test to use default Z values after launch.
# HISTORY:
# HISTORY: Revision 1.10  2004/09/14 18:52:34  apsop
# HISTORY: Fix bug which was introduced in the source priority
# HISTORY:
# HISTORY: Revision 1.9  2004/08/31 01:33:35  apsop
# HISTORY: Yet another bug fix for bat_z.
# HISTORY:
# HISTORY: Revision 1.8  2004/08/30 17:53:52  apsop
# HISTORY: Another fix for calculating bat_z and bat_origin_z.
# HISTORY:
# HISTORY: Revision 1.7  2004/08/30 16:40:27  apsop
# HISTORY: Test for zero when using inverse distance to calculate bat_z.
# HISTORY:
# HISTORY: Revision 1.6  2004/08/30 13:20:55  apsop
# HISTORY: Obtain values for bat_z and bat_origin_z from HK.
# HISTORY:
# HISTORY: Revision 1.5  2004/08/17 13:55:51  apsop
# HISTORY: Store info on burst in bat cataloge
# HISTORY:
# HISTORY: Revision 1.4  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.3  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}="Determining burst position";

    return $self;
}

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

sub body {
    my $self=shift;

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

    $self->get_z_values();

    my $bat_cen = $self->get_position();

    ###########################################################
    # Record if the 'burst' is in the BAT source catalogue, or
    # if it qualifies under the BAT criteria
    ###########################################################
    if($bat_cen){
      my $cen_fits = Util::FITSfile->new($bat_cen, 0);

      my $trigtime = $cen_fits->keyword('TRIGTIME');
      $jobpar->set({burst_time => $trigtime})
	          if $trigtime;

      $jobpar->set({burst_cat_src => 'yes'})
		    if $cen_fits->keyword('CATSRC') eq 'T';
      if( $cen_fits->keyword('GRBDETEC') eq 'F' ){
	$jobpar->set({burst_veto => 'yes'});
	if( $jobpar->read('object') eq 'Burst' ){
	  $jobpar->set({object => 'Automatic Target'});
	}
      }
    }

    ##################################################
    # If object is generic 'Burst', add in ra and dec
    ##################################################
    if( $jobpar->read('object') =~ /^(Burst|Non\-GRB|GI Target|TOO|Tiling|Unknown)/ ){
      $jobpar->set({'object' =>
	   sprintf('%s (%.3f, %.3f)', $1, $jobpar->read('burst_ra'), $jobpar->read('burst_dec')) });
    }

} # end of body method


sub get_z_values{
    my $self=shift;

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

    #################################################################
    # Get bat_z and origin_z values from bat hk.  For in-flight data
    # go with default values in the parameter file.
    #################################################################
    my $launch = Util::Date->new('2004-11-20T17:16:00');
    if( $jobpar->read('tstart') < $launch->seconds() ){
      my $hkfile = $filename->get('enhk', 'bat', '', 0);
      if( -f $hkfile ){
	my $hkfits = Util::FITSfile->new($hkfile, 0);
	my @hdus = $hkfits->list_hdus();
	my $bat_z = 0;

	unless( grep(/^hk161x001$/, @hdus) ){
	  $log->error(1, 'No 0x161 BAT HK data, using default Z values');
	}else{
	  $hkfits->ext('hk161x001');
	  if( $hkfits->nrows() > 0 ){
	    my @invdist = $hkfits->cols('BBRIIMGINVDIST')->table();
	    my @flength = $hkfits->cols('BBRIIMGFLENGTH')->table();
	    $bat_z = 100.0 * (1.0 / $invdist[-1] + $flength[-1]) + 0.35
	      unless( $invdist[-1] == 0 );

	    $jobpar->set({bat_z => $bat_z,
			  bat_origin_z => $flength[-1] * 100.0 + 0.35});
	  }else{
	    $log->error(1, 'No 0x161 BAT HK data, using default Z values');
	  }
	}
      }else{
	$log->error(1, 'No BAT HK data, using default Z values');
      }
    }

}

sub get_position{
    my $self=shift;

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

    ################################################################
    # Determine if a human assigned burst position is set.
    # If so, we assume is more reliable than any machine-determined 
    # position.
    ################################################################
    my $root = $filename->sequence_specific();

    my $ra = $jobpar->read("burst_ra");
    my $dec = $jobpar->read("burst_dec");
    my $source = $jobpar->read("burst_pos_info");

    my @bat_cen = $filename->get('tdmess', 'bat', 'ce', '*');
    if( @bat_cen > 1 ){
      @bat_cen = grep /^${root}/, @bat_cen;
      if( @bat_cen > 1 ){
       $log->entry("More than one bat centroid message.");
      }
    }

    if($ra && $dec && $source eq "human") {
        $log->entry("Using human assigned position RA=$ra Dec=$dec");
        return $bat_cen[0];
    }

    my @xrt_cen = $filename->get('tdmess', 'xrt', 'ce', '*');
    if( @xrt_cen > 1 ){
      @xrt_cen = grep /^${root}/, @xrt_cen;
      if( @xrt_cen > 1 ){
       $log->entry("More than one xrt centroid message.");
      }
    }
    if(@xrt_cen){
      my $cen_fits = Util::FITSfile->new($xrt_cen[0], 0);
      $ra = $cen_fits->keyword('XRA_OBJ');
      $dec = $cen_fits->keyword('XDEC_OBJ');

      $log->entry("Using $xrt_cen[0] for burst position, RA = $ra, DEC = $dec.");
      $jobpar->set({burst_ra => $ra,
                    burst_dec => $dec,
		    burst_pos_info => 'xrt'});
      return $bat_cen[0];
    }    

    if(@bat_cen){
      my $cen_fits = Util::FITSfile->new($bat_cen[0], 0);
      $ra = $cen_fits->keyword('BRA_OBJ');
      $dec = $cen_fits->keyword('BDEC_OBJ');

      $log->entry("Using $bat_cen[0] for burst position, RA = $ra, DEC = $dec.");
      $jobpar->set({burst_ra => $ra,
		    burst_dec => $dec,
		    burst_pos_info => 'bat'});
      return $bat_cen[0];
    }

    #################################################################
    # for the moment we will set the burst position to the nominal
    # pointing direction. This is not the right thing to do.
    # what if it isn't a burst?
    #################################################################
    if( $source eq 'unknown' ){
      $log->entry("Setting target position to nominal pointing as a kludge");
      $ra = $jobpar->read("ra");
      $dec = $jobpar->read("dec");

      $jobpar->set({burst_ra => $ra,
		    burst_dec => $dec,
		    burst_pos_info => 'pointing'});
    }

    return $bat_cen[0];
}

1;