Subs::Coordinates (version $)


package Subs::Coordinates;
##############################################################################
#
# DESCRIPTION: This subroutine runs the XRT tools delivered in build 1
#
# HISTORY:
# HISTORY: $Log: Coordinates.pm,v $
# HISTORY: Revision 1.16  2015/05/22 13:59:33  apsop
# HISTORY: Updates for 3.16.08:
# HISTORY: * modify how xrtgrblc and xrtexpomap are run as requested by ASDC.
# HISTORY: * avoid E2 in UVOTReport.pm that occurs when trying to run mgtime without GTI.
# HISTORY: * update calibration clock file to v107.
# HISTORY:
# HISTORY: Revision 1.15  2014/02/27 09:30:29  apsop
# HISTORY: In event_transforms, only look for attitude file if
# HISTORY: not supplied in the $coordParams parameter.  Improved
# HISTORY: attfile log messages.
# HISTORY:
# HISTORY: Revision 1.14  2011/01/20 17:33:18  apsop
# HISTORY: Added code to use Attitude file if available
# HISTORY:
# 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: $Revision: 1.16 $
#
##############################################################################


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.  Calls coordinator.
#
# Note that parameters set here can be overridden by including them in the
# coordParams argument.
#
# Arguments:
#   inst:   Instrument, 'uvot' or 'xrt'
#   modes:  Ref to array of mode names, passed to $filename->get()
#   coordParams: Optional ref to hash of parameters pass to coordinator
###############################################################################
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, if not supplied in $coordParams
    ######################################################################
    my $attitude;
    my $attflag = undef;
    if ($coordParams && %$coordParams && exists($coordParams->{attfile})) {
	$attitude = $coordParams->{attfile};
        $attflag = delete($coordParams->{attflag});
    } else {
	$attitude = $filename->get('attcorr', 'u');
	if (! -e $attitude) {
	    $log->error(1, "$attitude uat attitude file does not exist, trying pat file");
	    $attitude = $filename->get('attcorr', 'p');
	    if (! -e $attitude) {
		$log->error(1, "$attitude pat attitude file does not exist, trying sat file");
		$attitude = $filename->get('attitude', 's');
		if (! -e $attitude) {
		    $log->error(1, "Unable to find attitude file, using NONE");
		    $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();

        if ($attflag) {
	   $log->entry("updating ATTFLAG in $unf");
           my $fits = Util::FITSfile->new($unf);
           my $nhdus = $fits->nhdus();
           for (my $hdu=0; $hdu<$nhdus; $hdu++) {
              $fits->ext($hdu);
              $fits->keyword('ATTFLAG', "'$attflag'", 'Attitude file: 100=sat, x10=pat, xx1=uat');
           }

        } # attflag

      } # end of loop over unfiltered files

    } # end of loop over modes

} # end of event_transforms method