Subs::NominalPointing (version $)


package Subs::NominalPointing;

##############################################################################
#
# DESCRIPTION: Determine the mean pointing for the observation.
# DESCRIPTION: This subroutine first merges all attitude files into a 
# DESCRIPTION: single file. It then runs the aspect FTOOL to calculate the
# DESCRIPTION: mean pointing. It applies an instrument to spacecraft mean
# DESCRIPTION: alignment file if there is one.
# DESCRIPTION: The result is a set of Euler angles giving the mean spacecraft
# DESCRIPTION: orientation, and a set of RA, Dec and Roll giving the mean
# DESCRIPTION: instrument pointing as offset by the alignment file.
# DESCRIPTION: The subroutine next calculates the galactic coordinates 
# DESCRIPTION: corresponding to the mean instrument RA, Dec and Roll.
# DESCRIPTION: All of this information is logged and stored in the
# DESCRIPTION: job.par file.
#
# HISTORY: $Log: NominalPointing.pm,v $
# HISTORY: Revision 1.5  2014/02/27 06:38:17  apsop
# HISTORY: VERSION header now shows CVS Revision
# HISTORY:
# HISTORY: Revision 1.4  2012/08/03 05:18:31  apsop
# HISTORY: sub determine_pointing takes 2 new optional named arguments,
# HISTORY: ERROR_LEVEL and PROBLEM_REF; should be backward-compatible.
# HISTORY: (Change by Bob Wiegand.)
# HISTORY:
# HISTORY: Revision 1.5  2009/05/06 17:50:11  apsop
# HISTORY: stools was changed to ftcoco
# HISTORY:
# HISTORY: Revision 1.4  2006/08/24 13:34:35  apsop
# HISTORY: Add history entry for automatic CVS history update.
# HISTORY:
# HISTORY: 2.1 -> 3.0 2002-09-04
# HISTORY: Created this common subroutine from the ASCA-specific Aspecting.pm
# HISTORY: module version 2.1. This was done by stipping out the ASCA-specific
# HISTORY: "fixing" code and putting in a way to configure the file types.
#
# HISTORY: 3.0 -> 3.1 2003-08-21
# HISTORY: aspect tool has been moved to HEADAS, so call it from there now.
#
# VERSION: $Revision: 1.5 $
#
##############################################################################

use Subs::Sub;

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

use Util::CoreTags;


sub new {
    my $proto=shift;
    my $self=$proto->SUPER::new();

    $self->{DESCRIPTION}="Determine nominal pointing";

    ####################################################
    # default file types for attitude file and 
    # mean alignment file. These may be overridden
    # by subclasses. If there is no alignment file,
    # then you should set the ALIGNMENT_TYPE to "none"
    ####################################################
    $self->{ATTITUDE_TYPE}="attitude";
    $self->{ALIGNMENT_TYPE}="alignment";

    return $self;
}

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

#############################################################################
# this body method just calls determine_pointing
# subclasses may override this to do other things
#############################################################################
sub body {
    my $self=shift;

    $self->determine_pointing();

}

#############################################################################
# determine the nominal pointing after merging all attitude files
#############################################################################
sub determine_pointing {

	my ($self, $gtis, %args) = @_;

    $gtis ||= "none";

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

    $log->entry("Determining nominal pointing with GTI $gtis");

	my $problem = '';
	my $ref = $args{PROBLEM_REF};

    my $attitude  = $self->{ATTITUDE_TYPE};
    my $alignment = $self->{ALIGNMENT_TYPE};

    ########################################
    # get a list of all the attitude files
    ########################################
    my @attfiles=$filename->any($attitude);

    unless(@attfiles) {
        $log->entry("No attitude data available");
		$problem = 'No attitude data available';
        return;
    }

    #####################################################
    # merge the attitude files if there is more than one
    #####################################################
    my $tmp_file="merged_attitude.tmp";
    my $merged=Util::FITSlist->new(@attfiles)
                             ->merge($tmp_file);
 
    #################################################
    # calculate the aspect point
    #################################################
    my $alignfile;
    if($alignment ne "none") { $alignfile='CALDB'; }
    else                     { $alignfile = "none"; }

    my $aspect=Util::HEAdas->new("aspect") 
                          ->params({attfile   => $merged,
                                    alignfile => $alignfile,
                                    gtis      => $gtis,
                                    nbins     => 100,
                                    binsize   => 0.01});

    $aspect->run();
    if ($aspect->had_error) {
		my $level = $args{ERROR_LEVEL} || 2;
        $log->error([ $level, ASPECT_FAILED ],
                "unable to determine nominal pointing using GTIs $gtis");
		if ($ref) {
			$$ref = 'aspect failed';
		}
    }

    my $euler1=0.0;
    my $euler2=0.0;
    my $euler3=0.0;
    
    my $ra=0.0;
    my $dec=0.0;
    my $roll=0.0;


    #############################################
    # read the results from the aspect par file
    #############################################
    $euler1=$aspect->parfile()->read("euler1");
    $euler2=$aspect->parfile()->read("euler2");
    $euler3=$aspect->parfile()->read("euler3");

    if($euler2==180. || $euler2 == 0. && $euler1 =~ /^NaN$/i ) {
      $euler1=0.;
    }

    $ra  =$aspect->parfile()->read("ra"  );
    $dec =$aspect->parfile()->read("dec" );
    $roll=$aspect->parfile()->read("roll");

    if($dec == 90. || $dec == -90. && $ra =~ /^NaN$/i ) {
      $ra = 0.;
    }


    #############################################
    # convert to galactic coordinates
    #############################################
    my $ftcoco = Util::HEAdas->new( 'ftcoco' )->is_script(1)->params( {
                          infile   => 'NONE',
                          outfile  => '',
                          incoord  => 'R',
                          outcoord => 'G',
                          lon1     => $ra,
                          lat1     => $dec,
                          radecsys => 'FK5',
                          chatter  => 0,
                          clobber  => 'no',
                          history  => 'no'
                          } )->run( );
    #
    # Get the output Lii/Bii params if no error
    #
    my $glon = 0.0;
    my $glat = 0.0;
    if ( $ftcoco->had_error( ) ) {
        $log->error( 2, "Error in ftcoco!" );
    } else {
        $glon = $ftcoco->parfile( )->read( 'lon2', 1e-6 );
        $glat = $ftcoco->parfile( )->read( 'lat2', 1e-6 );
    }

    ####################################################
    # log the results and write them to the jobpar file
    ####################################################
    $log->entry("Nominal spacecraft Euler angles: ".
                "Phi=$euler1 Theta=$euler2 Psi=$euler3");

    $log->entry("Nominal telescope pointing: ".
                "R.A.=$ra Dec=$dec Roll Angle=$roll");

    $log->entry("Galactic Coordinates: ".
                "Lii=$glon Bii=$glat");


    $jobpar->set({euler1 => $euler1,
                  euler2 => $euler2,
                  euler3 => $euler3,
                  ra     => $ra,
                  dec    => $dec,
                  roll   => $roll,
                  glon   => $glon,
                  glat   => $glat });

    unlink $tmp_file;


} # end of body method