Subs::Coordinates (version 0.0)


package Subs::Coordinates;
##############################################################################
#
# DESCRIPTION: This subroutine runs the XRT tools delivered in build 1
#
# HISTORY: 
# HISTORY: $Log: Coordinates.pm,v $
# HISTORY: Revision 1.13  2006/10/01 18:59:04  apsop
# HISTORY: Add in CALDB filter expression in teldef file for FILTER and WHEELPOS keywords.
# HISTORY:
# HISTORY: Revision 1.12  2005/11/23 22:35:07  apsop
# HISTORY: Explicity set interpolation param in coordinator, LINEAR by default, CONSTANT for XRT.
# HISTORY:
# HISTORY: Revision 1.11  2005/11/08 17:30:03  apsop
# HISTORY: Use caldb for obtaining the teldef file.
# HISTORY:
# HISTORY: Revision 1.10  2005/02/08 19:13:20  apsop
# HISTORY: Formatting cleanup changes.
# HISTORY:
# HISTORY: Revision 1.9  2005/02/03 20:55:40  apsop
# HISTORY: Turn off aberration correction in coordinator.
# HISTORY:
# HISTORY: Revision 1.8  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.7  2004/04/28 13:48:56  dah
# HISTORY: Fix attitude file instrument type.
# HISTORY:
# HISTORY: Revision 1.6  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}="Doing coordinate transforms";

    return $self;
}

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


###############################################################################
# fill coordinate columns in event files
###############################################################################
sub event_transforms {

    my $self=shift;
    my $inst=shift;
    my $modes=shift;
    my $coordParams=shift;

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

    ######################################################
    # make sure there is an attitude file
    ######################################################
    my $attitude = $filename->get('attitude', 's');
    unless(-f $attitude) {
        $log->entry("No attitude data available");
        $attitude="NONE";
    }

    ######################################################
    # set up the coordinator FTOOL
    ######################################################
    my $coordinator = Util::HEAdas->new('coordinator')
                                  ->params({eventext   => 'EVENTS',
					    timecol    => 'TIME',
					    skyxnull   => 0,
					    skyynull   => 0,
					    teldef     => 'CALDB',
					    attfile    => $attitude,
					    aberration => 'no',
					    follow_sun => 'yes',
					    ra         => $jobpar->read('ra'),
					    dec        => $jobpar->read('dec'),
					    randomize  => 'yes',
					    seed       => '-1956',
					    timemargin => 32,
					    interpolation => 'LINEAR'});

    $coordinator->params($coordParams) if ($coordParams && %$coordParams);

    ###############################
    # loop over modes 
    ###############################
    foreach my $mode ( @$modes ) {

      #################################################
      # loop over the event files for this instrument
      #################################################
      my $unf;
      foreach $unf ($filename->get("unfiltered", $inst, $mode, "*") ) {

	###################################################
	#  Add in CALDB expressions of FILTER and WHEELPOS
	#    if that info is needed
	###################################################
	my $coordpar = $coordinator->parfile();
	unless( $coordParams && %$coordParams &&
		$coordParams->{'teldef'} && $coordParams->{'teldef'} ne 'CALDB' ){
	  my $unf_fits = Util::FITSfile->new($unf);
	  my @bounds = qw(FILTER WHEELPOS);
	  my @exps;
	  foreach my $bound (@bounds) {
	    my $val = $unf_fits->keyword($bound);
	    push @exps, "$bound.eq.$val" if $val;
	  }
	  if(@exps){
	    $coordinator->params({teldef => 
				  'CALDB:expression=' . join('.and.', @exps) });
	  }
	}
	
	$log->entry("running coordinator on $unf");

	$coordinator->params({eventfile=>$unf})->run();

      } # end of loop over files
    } # end of loop over modes

} # end of event_transforms method